Exemple #1
0
/* Load PLS file into plist. Return the number of items read. */
static int plist_load_pls (struct plist *plist, const char *fname,
		const char *cwd)
{
	FILE *file;
	char *line;
	long i, nitems, added = 0;
	char *e;

	if (!(file = fopen(fname, "r"))) {
		error ("Can't open playlist file: %s",
				strerror(errno));
		return 0;
	}

	line = read_ini_value (file, "playlist", "NumberOfEntries");
	if (!line) {

		/* Assume that it is a pls file version 1 - plist_load_m3u()
		 * should handle it like an m3u file without the m3u extensions. */
		fclose (file);
		return plist_load_m3u (plist, fname, cwd, 0);
	}

	nitems = strtol (line, &e, 10);
	if (*e) {
		error ("Broken PLS file");
		free (line);
		return 0;
	}
	free (line);

	for (i = 1; i <= nitems; i++) {
		char *pls_file, *pls_title, *pls_length;
		char key[16];
		int time;
		int last_added;
		char path[2*PATH_MAX];

		sprintf (key, "File%ld", i);
		if (!(pls_file = read_ini_value(file, "playlist", key))) {
			error ("Broken PLS file");
			break;
		}

		sprintf (key, "Title%ld", i);
		pls_title = read_ini_value(file, "playlist", key);

		sprintf (key, "Length%ld", i);
		pls_length = read_ini_value(file, "playlist", key);

		if (pls_length) {
			time = strtol (pls_length, &e, 10);
			if (*e)
				time = -1;
		}
		else
			time = -1;

		if (strlen(pls_file) <= PATH_MAX) {
			make_path (path, sizeof(path), cwd, pls_file);
			if (plist_find_fname(plist, path) == -1) {
				last_added = plist_add (plist, path);

				if (pls_title && pls_title[0])
					plist_set_title_tags (plist, last_added,
							pls_title);

				if (time > 0) {
					plist->items[last_added].tags
						= tags_new ();
					plist->items[last_added].tags->time
						= time;
					plist->items[last_added].tags->filled
						|= TAGS_TIME;
				}
			}
		}

		free (pls_file);
		if (pls_title)
			free (pls_title);
		if (pls_length)
			free (pls_length);
		added++;
	}

	fclose (file);

	return added;
}
Exemple #2
0
/* Load M3U file into plist.  Return the number of items read. */
static int plist_load_m3u (struct plist *plist, const char *fname,
                           const char *cwd, const int load_serial)
{
    FILE *file;
    char *line = NULL;
    int last_added = -1;
    int after_extinf = 0;
    int added = 0;

    file = fopen (fname, "r");
    if (!file) {
        error_errno ("Can't open playlist file", errno);
        return 0;
    }

    /* Lock gets released by fclose(). */
    if (lockf (fileno (file), F_LOCK, 0) == -1)
        log_errno ("Can't lock the playlist file", errno);

    while ((line = read_line (file))) {
        if (!strncmp (line, "#EXTINF:", sizeof("#EXTINF:") - 1)) {
            char *comma, *num_err;
            char time_text[10] = "";
            int time_sec;

            if (after_extinf) {
                error ("Broken M3U file: double #EXTINF!");
                plist_delete (plist, last_added);
                goto err;
            }

            /* Find the comma */
            comma = strchr (line + (sizeof("#EXTINF:") - 1), ',');
            if (!comma) {
                error ("Broken M3U file: no comma in #EXTINF!");
                goto err;
            }

            /* Get the time string */
            time_text[sizeof(time_text) - 1] = 0;
            strncpy (time_text, line + sizeof("#EXTINF:") - 1,
                     MIN(comma - line - (sizeof("#EXTINF:") - 1),
                         sizeof(time_text)));
            if (time_text[sizeof(time_text) - 1]) {
                error ("Broken M3U file: wrong time!");
                goto err;
            }

            /* Extract the time. */
            time_sec = strtol (time_text, &num_err, 10);
            if (*num_err) {
                error ("Broken M3U file: time is not a number!");
                goto err;
            }

            after_extinf = 1;
            last_added = plist_add (plist, NULL);
            plist_set_title_tags (plist, last_added, comma + 1);

            if (*time_text)
                plist_set_item_time (plist, last_added, time_sec);
        }
        else if (line[0] != '#') {
            char path[2 * PATH_MAX];

            strip_string (line);
            if (strlen (line) <= PATH_MAX) {
                make_path (path, sizeof(path), cwd, line);

                if (plist_find_fname (plist, path) == -1) {
                    if (after_extinf)
                        plist_set_file (plist, last_added, path);
                    else
                        plist_add (plist, path);
                    added += 1;
                }
                else if (after_extinf)
                    plist_delete (plist, last_added);
            }
            else if (after_extinf)
                plist_delete (plist, last_added);

            after_extinf = 0;
        }
        else if (load_serial &&
                 !strncmp (line, "#MOCSERIAL: ", sizeof("#MOCSERIAL: ") - 1)) {
            char *serial_str = line + sizeof("#MOCSERIAL: ") - 1;

            if (serial_str[0]) {
                char *err;
                long serial;

                serial = strtol (serial_str, &err, 0);
                if (!*err) {
                    plist_set_serial (plist, serial);
                    logit ("Got MOCSERIAL tag with serial %ld", serial);
                }
            }
        }
        free (line);
    }

err:
    free (line);
    fclose (file);
    return added;
}