Example #1
0
GPtrArray *
spl_load(const char *utf8path, GError **error_r)
{
	FILE *file;
	GPtrArray *list;
	char *path_fs;

	if (spl_map(error_r) == NULL)
		return NULL;

	path_fs = spl_map_to_fs(utf8path, error_r);
	if (path_fs == NULL)
		return NULL;

	file = fopen(path_fs, "r");
	g_free(path_fs);
	if (file == NULL) {
		playlist_errno(error_r);
		return NULL;
	}

	list = g_ptr_array_new();

	GString *buffer = g_string_sized_new(1024);
	char *s;
	while ((s = read_text_line(file, buffer)) != NULL) {
		if (*s == 0 || *s == PLAYLIST_COMMENT)
			continue;

		if (g_path_is_absolute(s)) {
			char *t = fs_charset_to_utf8(s);
			if (t == NULL)
				continue;

			s = g_strconcat("file://", t, NULL);
			g_free(t);
		} else if (!uri_has_scheme(s)) {
			char *path_utf8;

			path_utf8 = map_fs_to_utf8(s);
			if (path_utf8 == NULL)
				continue;

			s = path_utf8;
		} else {
			s = fs_charset_to_utf8(s);
			if (s == NULL)
				continue;
		}

		g_ptr_array_add(list, s);

		if (list->len >= playlist_max_length)
			break;
	}

	fclose(file);
	return list;
}
Example #2
0
GPtrArray *
spl_load(const char *utf8path)
{
	FILE *file;
	GPtrArray *list;
	char buffer[MPD_PATH_MAX];
	char *path_fs;

	if (!spl_valid_name(utf8path))
		return NULL;

	path_fs = map_spl_utf8_to_fs(utf8path);
	if (path_fs == NULL)
		return NULL;

	while (!(file = fopen(path_fs, "r")) && errno == EINTR);
	g_free(path_fs);
	if (file == NULL)
		return NULL;

	list = g_ptr_array_new();

	while (fgets(buffer, sizeof(buffer), file)) {
		char *s = buffer;

		if (*s == PLAYLIST_COMMENT)
			continue;

		g_strchomp(buffer);

		if (!uri_has_scheme(s)) {
			char *path_utf8;
			struct song *song;

			path_utf8 = map_fs_to_utf8(s);
			if (path_utf8 == NULL)
				continue;

			song = db_get_song(path_utf8);
			g_free(path_utf8);
			if (song == NULL)
				continue;

			s = song_get_uri(song);
		} else
			s = g_strdup(s);

		g_ptr_array_add(list, s);

		if (list->len >= playlist_max_length)
			break;
	}

	while (fclose(file) && errno == EINTR);
	return list;
}