2 * Copyright (C) 2007 by INdT
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * @author Gustavo Sverzut Barbieri <gustavo.barbieri@openbossa.org>
24 * pls playlist parser.
26 * This parser doesn't actually parse the whole file, instead it just checks
27 * for the header [playlist], then search the beginning and ending of the file
28 * (at most PLS_MAX_N_ENTRIES_BYTES_LOOKUP bytes) in order to find out
29 * NumberOfEntries=XXX line. If there are too many bogus (ie: empty) lines or
30 * this line is inside the data declaration, then it will fail the parse.
31 * In theory this should not happen, so let's wait for bug reports.
38 #define _XOPEN_SOURCE 600
39 #include <lightmediascanner_plugin.h>
40 #include <lightmediascanner_db.h>
41 #include <sys/types.h>
50 #define PLS_MAX_N_ENTRIES_BYTES_LOOKUP 64
53 _pls_find_header(int fd)
55 const char header[] = "[playlist]";
56 char buf[sizeof(header) - 1];
59 /* skip out white spaces */
66 fprintf(stderr, "ERROR: premature end of file.\n");
74 if (buf[0] != header[0])
77 /* try to read rest (from the second on) of the header */
78 r = read(fd, buf + 1, sizeof(buf) - 1);
82 } else if (r != sizeof(buf) - 1) {
83 fprintf(stderr, "ERROR: premature end of file: read %d of %d bytes.\n",
88 if (memcmp(buf + 1, header + 1, sizeof(buf) - 1) != 0) {
89 fprintf(stderr, "ERROR: invalid pls header '%.*s'\n",
90 sizeof(buf) - 1, buf);
111 _pls_find_n_entries_start(int fd, struct lms_playlist_info *info)
113 char buf[PLS_MAX_N_ENTRIES_BYTES_LOOKUP];
114 const char n_entries[] = "NumberOfEntries=";
119 off = lseek(fd, 0, SEEK_CUR);
125 r = read(fd, buf, sizeof(buf));
132 for (i = 0; i < r; i++) {
136 if (c == n_entries[0]) {
139 if (memcmp(buf + i, n_entries + 1, sizeof(n_entries) - 2) != 0) {
140 off += i + sizeof(n_entries) - 2;
144 i += sizeof(n_entries) - 2;
151 fprintf(stderr, "WARNING: missing end of line\n");
155 info->n_entries = atoi(p);
157 } else if (c == 'V') {
158 /* skip possible 'Version=XX' */
159 for (i++; i < r; i++)
162 } else if (isspace(c))
171 /* not at the file beginning, reset offset */
172 if (lseek(fd, off, SEEK_SET) < 0) {
181 _pls_parse_entries_line(int fd, struct lms_playlist_info *info, char *buf, int len)
183 const char n_entries[] = "NumberOfEntries=";
186 for (i = 0; i < len; i++, buf++)
194 if (memcmp(buf, n_entries, sizeof(n_entries) - 1) != 0)
197 buf += sizeof(n_entries) - 1;
198 len -= sizeof(n_entries) - 1;
201 info->n_entries = atoi(buf);
206 _pls_find_n_entries_end(int fd, const struct lms_file_info *finfo, struct lms_playlist_info *info)
208 char buf[PLS_MAX_N_ENTRIES_BYTES_LOOKUP];
212 if (finfo->size > sizeof(buf))
213 if (lseek(fd, finfo->size - sizeof(buf), SEEK_SET) < 0) {
218 r = read(fd, buf, sizeof(buf));
226 for (i = r - 1; i >= 0; i--) {
227 if (buf[i] == '\n') {
231 len = last_nl - i - 1;
235 ret = _pls_parse_entries_line(fd, info, buf + i + 1, len);
248 _pls_parse(int fd, const struct lms_file_info *finfo, struct lms_playlist_info *info)
252 r = _pls_find_header(fd);
254 fprintf(stderr, "ERROR: could not find pls header. code=%d\n", r);
258 r = _pls_find_n_entries_start(fd, info);
262 r = _pls_find_n_entries_end(fd, finfo, info);
264 fprintf(stderr, "ERROR: could not find pls NumberOfEntries=\n");
269 static const char _name[] = "pls";
270 static const struct lms_string_size _exts[] = {
271 LMS_STATIC_STRING_SIZE(".pls")
275 struct lms_plugin plugin;
276 lms_db_playlist_t *playlist_db;
280 _match(struct plugin *p, const char *path, int len, int base)
284 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
288 return (void*)(i + 1);
292 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
294 struct lms_playlist_info info = {0};
297 fd = open(finfo->path, O_RDONLY);
303 if (_pls_parse(fd, finfo, &info) != 0) {
305 "WARNING: could not parse playlist '%s'.\n", finfo->path);
309 ext_idx = ((int)match) - 1;
310 info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
311 info.title.str = malloc((info.title.len + 1) * sizeof(char));
312 memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
313 info.title.str[info.title.len] = '\0';
314 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
317 r = lms_db_playlist_add(plugin->playlist_db, &info);
320 free(info.title.str);
321 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
328 _setup(struct plugin *plugin, struct lms_context *ctxt)
330 plugin->playlist_db = lms_db_playlist_new(ctxt->db);
331 if (!plugin->playlist_db)
338 _start(struct plugin *plugin, struct lms_context *ctxt)
340 return lms_db_playlist_start(plugin->playlist_db);
344 _finish(struct plugin *plugin, struct lms_context *ctxt)
346 if (plugin->playlist_db)
347 return lms_db_playlist_free(plugin->playlist_db);
354 _close(struct plugin *plugin)
360 API struct lms_plugin *
361 lms_plugin_open(void)
363 struct plugin *plugin;
365 plugin = malloc(sizeof(*plugin));
366 plugin->plugin.name = _name;
367 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
368 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
369 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
370 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
371 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
372 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
374 return (struct lms_plugin *)plugin;