~ubuntu-branches/ubuntu/utopic/mpd/utopic-proposed

« back to all changes in this revision

Viewing changes to src/playlist/ExtM3uPlaylistPlugin.cxx

  • Committer: Package Import Robot
  • Author(s): Steve Kowalik
  • Date: 2013-11-12 18:17:40 UTC
  • mfrom: (2.2.36 sid)
  • Revision ID: package-import@ubuntu.com-20131112181740-72aa4zihehoobedp
Tags: 0.18.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Add libmp3lame-dev to Build-Depends, and enable LAME.
  - Read the user for the daemon from the config file in the init script.
  - Move avahi-daemon from Suggests to Recommends.
  - Added apport hook to include user configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
 
3
 * http://www.musicpd.org
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License along
 
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
#include "ExtM3uPlaylistPlugin.hxx"
 
22
#include "PlaylistPlugin.hxx"
 
23
#include "SongEnumerator.hxx"
 
24
#include "Song.hxx"
 
25
#include "tag/Tag.hxx"
 
26
#include "util/StringUtil.hxx"
 
27
#include "TextInputStream.hxx"
 
28
 
 
29
#include <glib.h>
 
30
 
 
31
#include <string.h>
 
32
#include <stdlib.h>
 
33
 
 
34
class ExtM3uPlaylist final : public SongEnumerator {
 
35
        TextInputStream tis;
 
36
 
 
37
public:
 
38
        ExtM3uPlaylist(InputStream &is)
 
39
                :tis(is) {
 
40
        }
 
41
 
 
42
        bool CheckFirstLine() {
 
43
                std::string line;
 
44
                return tis.ReadLine(line) &&
 
45
                        strcmp(line.c_str(), "#EXTM3U") == 0;
 
46
        }
 
47
 
 
48
        virtual Song *NextSong() override;
 
49
};
 
50
 
 
51
static SongEnumerator *
 
52
extm3u_open_stream(InputStream &is)
 
53
{
 
54
        ExtM3uPlaylist *playlist = new ExtM3uPlaylist(is);
 
55
 
 
56
        if (!playlist->CheckFirstLine()) {
 
57
                /* no EXTM3U header: fall back to the plain m3u
 
58
                   plugin */
 
59
                delete playlist;
 
60
                return NULL;
 
61
        }
 
62
 
 
63
        return playlist;
 
64
}
 
65
 
 
66
/**
 
67
 * Parse a EXTINF line.
 
68
 *
 
69
 * @param line the rest of the input line after the colon
 
70
 */
 
71
static Tag *
 
72
extm3u_parse_tag(const char *line)
 
73
{
 
74
        long duration;
 
75
        char *endptr;
 
76
        const char *name;
 
77
        Tag *tag;
 
78
 
 
79
        duration = strtol(line, &endptr, 10);
 
80
        if (endptr[0] != ',')
 
81
                /* malformed line */
 
82
                return NULL;
 
83
 
 
84
        if (duration < 0)
 
85
                /* 0 means unknown duration */
 
86
                duration = 0;
 
87
 
 
88
        name = strchug_fast(endptr + 1);
 
89
        if (*name == 0 && duration == 0)
 
90
                /* no information available; don't allocate a tag
 
91
                   object */
 
92
                return NULL;
 
93
 
 
94
        tag = new Tag();
 
95
        tag->time = duration;
 
96
 
 
97
        /* unfortunately, there is no real specification for the
 
98
           EXTM3U format, so we must assume that the string after the
 
99
           comma is opaque, and is just the song name*/
 
100
        if (*name != 0)
 
101
                tag->AddItem(TAG_NAME, name);
 
102
 
 
103
        return tag;
 
104
}
 
105
 
 
106
Song *
 
107
ExtM3uPlaylist::NextSong()
 
108
{
 
109
        Tag *tag = NULL;
 
110
        std::string line;
 
111
        const char *line_s;
 
112
        Song *song;
 
113
 
 
114
        do {
 
115
                if (!tis.ReadLine(line)) {
 
116
                        delete tag;
 
117
                        return NULL;
 
118
                }
 
119
                
 
120
                line_s = line.c_str();
 
121
 
 
122
                if (g_str_has_prefix(line_s, "#EXTINF:")) {
 
123
                        delete tag;
 
124
                        tag = extm3u_parse_tag(line_s + 8);
 
125
                        continue;
 
126
                }
 
127
 
 
128
                line_s = strchug_fast(line_s);
 
129
        } while (line_s[0] == '#' || *line_s == 0);
 
130
 
 
131
        song = Song::NewRemote(line_s);
 
132
        song->tag = tag;
 
133
        return song;
 
134
}
 
135
 
 
136
static const char *const extm3u_suffixes[] = {
 
137
        "m3u",
 
138
        NULL
 
139
};
 
140
 
 
141
static const char *const extm3u_mime_types[] = {
 
142
        "audio/x-mpegurl",
 
143
        NULL
 
144
};
 
145
 
 
146
const struct playlist_plugin extm3u_playlist_plugin = {
 
147
        "extm3u",
 
148
 
 
149
        nullptr,
 
150
        nullptr,
 
151
        nullptr,
 
152
        extm3u_open_stream,
 
153
 
 
154
        nullptr,
 
155
        extm3u_suffixes,
 
156
        extm3u_mime_types,
 
157
};