diff options
Diffstat (limited to '')
-rw-r--r-- | src/PlaylistFile.cxx (renamed from src/stored_playlist.c) | 227 |
1 files changed, 82 insertions, 145 deletions
diff --git a/src/stored_playlist.c b/src/PlaylistFile.cxx index 39ba2bac1..3223edee3 100644 --- a/src/stored_playlist.c +++ b/src/PlaylistFile.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project + * Copyright (C) 2003-2012 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -18,16 +18,21 @@ */ #include "config.h" -#include "stored_playlist.h" -#include "playlist_save.h" -#include "text_file.h" +#include "PlaylistFile.hxx" +#include "PlaylistSave.hxx" #include "song.h" +#include "io_error.h" + +extern "C" { +#include "text_file.h" #include "mapper.h" #include "path.h" #include "uri.h" #include "database.h" #include "idle.h" #include "conf.h" +} + #include "glib_compat.h" #include <assert.h> @@ -128,96 +133,73 @@ playlist_errno(GError **error_r) break; default: - g_set_error_literal(error_r, g_file_error_quark(), errno, - g_strerror(errno)); + set_error_errno(error_r); break; } } -static struct stored_playlist_info * -load_playlist_info(const char *parent_path_fs, const char *name_fs) +static bool +LoadPlaylistFileInfo(PlaylistFileInfo &info, + const char *parent_path_fs, const char *name_fs) { size_t name_length = strlen(name_fs); - char *path_fs, *name, *name_utf8; - int ret; - struct stat st; - struct stored_playlist_info *playlist; if (name_length < sizeof(PLAYLIST_FILE_SUFFIX) || memchr(name_fs, '\n', name_length) != NULL) - return NULL; + return false; if (!g_str_has_suffix(name_fs, PLAYLIST_FILE_SUFFIX)) - return NULL; + return false; - path_fs = g_build_filename(parent_path_fs, name_fs, NULL); - ret = stat(path_fs, &st); + char *path_fs = g_build_filename(parent_path_fs, name_fs, NULL); + struct stat st; + int ret = stat(path_fs, &st); g_free(path_fs); if (ret < 0 || !S_ISREG(st.st_mode)) - return NULL; + return false; - name = g_strndup(name_fs, - name_length + 1 - sizeof(PLAYLIST_FILE_SUFFIX)); - name_utf8 = fs_charset_to_utf8(name); + char *name = g_strndup(name_fs, + name_length + 1 - sizeof(PLAYLIST_FILE_SUFFIX)); + char *name_utf8 = fs_charset_to_utf8(name); g_free(name); if (name_utf8 == NULL) - return NULL; + return false; - playlist = g_new(struct stored_playlist_info, 1); - playlist->name = name_utf8; - playlist->mtime = st.st_mtime; - return playlist; + info.name = name_utf8; + info.mtime = st.st_mtime; + return true; } -GPtrArray * -spl_list(GError **error_r) +PlaylistFileList +ListPlaylistFiles(GError **error_r) { - const char *parent_path_fs = spl_map(error_r); - DIR *dir; - struct dirent *ent; - GPtrArray *list; - struct stored_playlist_info *playlist; + PlaylistFileList list; + const char *parent_path_fs = spl_map(error_r); if (parent_path_fs == NULL) - return NULL; + return list; - dir = opendir(parent_path_fs); + DIR *dir = opendir(parent_path_fs); if (dir == NULL) { - g_set_error_literal(error_r, g_file_error_quark(), errno, - g_strerror(errno)); - return NULL; + set_error_errno(error_r); + return list; } - list = g_ptr_array_new(); - + PlaylistFileInfo info; + struct dirent *ent; while ((ent = readdir(dir)) != NULL) { - playlist = load_playlist_info(parent_path_fs, ent->d_name); - if (playlist != NULL) - g_ptr_array_add(list, playlist); + if (LoadPlaylistFileInfo(info, parent_path_fs, ent->d_name)) + list.push_back(std::move(info)); } closedir(dir); return list; } -void -spl_list_free(GPtrArray *list) -{ - for (unsigned i = 0; i < list->len; ++i) { - struct stored_playlist_info *playlist = - g_ptr_array_index(list, i); - g_free(playlist->name); - g_free(playlist); - } - - g_ptr_array_free(list, true); -} - static bool -spl_save(GPtrArray *list, const char *utf8path, GError **error_r) +SavePlaylistFile(const PlaylistFileContents &contents, const char *utf8path, + GError **error_r) { - FILE *file; - assert(utf8path != NULL); if (spl_map(error_r) == NULL) @@ -227,45 +209,39 @@ spl_save(GPtrArray *list, const char *utf8path, GError **error_r) if (path_fs == NULL) return false; - file = fopen(path_fs, "w"); + FILE *file = fopen(path_fs, "w"); g_free(path_fs); if (file == NULL) { playlist_errno(error_r); return false; } - for (unsigned i = 0; i < list->len; ++i) { - const char *uri = g_ptr_array_index(list, i); - playlist_print_uri(file, uri); - } + for (const auto &uri_utf8 : contents) + playlist_print_uri(file, uri_utf8.c_str()); fclose(file); return true; } -GPtrArray * -spl_load(const char *utf8path, GError **error_r) +PlaylistFileContents +LoadPlaylistFile(const char *utf8path, GError **error_r) { - FILE *file; - GPtrArray *list; - char *path_fs; + PlaylistFileContents contents; if (spl_map(error_r) == NULL) - return NULL; + return contents; - path_fs = spl_map_to_fs(utf8path, error_r); + char *path_fs = spl_map_to_fs(utf8path, error_r); if (path_fs == NULL) - return NULL; + return contents; - file = fopen(path_fs, "r"); + FILE *file = fopen(path_fs, "r"); g_free(path_fs); if (file == NULL) { playlist_errno(error_r); - return NULL; + return contents; } - list = g_ptr_array_new(); - GString *buffer = g_string_sized_new(1024); char *s; while ((s = read_text_line(file, buffer)) != NULL) { @@ -283,80 +259,46 @@ spl_load(const char *utf8path, GError **error_r) } else s = g_strdup(s); - g_ptr_array_add(list, s); - - if (list->len >= playlist_max_length) + contents.emplace_back(s); + if (contents.size() >= playlist_max_length) break; } fclose(file); - return list; -} - -void -spl_free(GPtrArray *list) -{ - for (unsigned i = 0; i < list->len; ++i) { - char *uri = g_ptr_array_index(list, i); - g_free(uri); - } - - g_ptr_array_free(list, true); -} - -static char * -spl_remove_index_internal(GPtrArray *list, unsigned idx) -{ - char *uri; - - assert(idx < list->len); - - uri = g_ptr_array_remove_index(list, idx); - assert(uri != NULL); - return uri; -} - -static void -spl_insert_index_internal(GPtrArray *list, unsigned idx, char *uri) -{ - assert(idx <= list->len); - - g_ptr_array_add(list, uri); - - memmove(list->pdata + idx + 1, list->pdata + idx, - (list->len - idx - 1) * sizeof(list->pdata[0])); - g_ptr_array_index(list, idx) = uri; + return contents; } bool spl_move_index(const char *utf8path, unsigned src, unsigned dest, GError **error_r) { - char *uri; - if (src == dest) /* this doesn't check whether the playlist exists, but what the hell.. */ return true; - GPtrArray *list = spl_load(utf8path, error_r); - if (list == NULL) + GError *error = nullptr; + auto contents = LoadPlaylistFile(utf8path, &error); + if (contents.empty() && error != nullptr) { + g_propagate_error(error_r, error); return false; + } - if (src >= list->len || dest >= list->len) { - spl_free(list); + if (src >= contents.size() || dest >= contents.size()) { g_set_error_literal(error_r, playlist_quark(), PLAYLIST_RESULT_BAD_RANGE, "Bad range"); return false; } - uri = spl_remove_index_internal(list, src); - spl_insert_index_internal(list, dest, uri); + const auto src_i = std::next(contents.begin(), src); + auto value = std::move(*src_i); + contents.erase(src_i); - bool result = spl_save(list, utf8path, error_r); + const auto dest_i = std::next(contents.begin(), dest); + contents.insert(dest_i, std::move(value)); - spl_free(list); + bool result = SavePlaylistFile(contents, utf8path, error_r); idle_add(IDLE_STORED_PLAYLIST); return result; @@ -390,14 +332,11 @@ spl_clear(const char *utf8path, GError **error_r) bool spl_delete(const char *name_utf8, GError **error_r) { - char *path_fs; - int ret; - - path_fs = spl_map_to_fs(name_utf8, error_r); + char *path_fs = spl_map_to_fs(name_utf8, error_r); if (path_fs == NULL) return false; - ret = unlink(path_fs); + int ret = unlink(path_fs); g_free(path_fs); if (ret < 0) { playlist_errno(error_r); @@ -411,25 +350,23 @@ spl_delete(const char *name_utf8, GError **error_r) bool spl_remove_index(const char *utf8path, unsigned pos, GError **error_r) { - char *uri; - - GPtrArray *list = spl_load(utf8path, error_r); - if (list == NULL) + GError *error = nullptr; + auto contents = LoadPlaylistFile(utf8path, &error); + if (contents.empty() && error != nullptr) { + g_propagate_error(error_r, error); return false; + } - if (pos >= list->len) { - spl_free(list); + if (pos >= contents.size()) { g_set_error_literal(error_r, playlist_quark(), PLAYLIST_RESULT_BAD_RANGE, "Bad range"); return false; } - uri = spl_remove_index_internal(list, pos); - g_free(uri); - bool result = spl_save(list, utf8path, error_r); + contents.erase(std::next(contents.begin(), pos)); - spl_free(list); + bool result = SavePlaylistFile(contents, utf8path, error_r); idle_add(IDLE_STORED_PLAYLIST); return result; @@ -439,7 +376,6 @@ bool spl_append_song(const char *utf8path, struct song *song, GError **error_r) { FILE *file; - struct stat st; if (spl_map(error_r) == NULL) return false; @@ -455,6 +391,7 @@ spl_append_song(const char *utf8path, struct song *song, GError **error_r) return false; } + struct stat st; if (fstat(fileno(file), &st) < 0) { playlist_errno(error_r); fclose(file); @@ -480,15 +417,13 @@ spl_append_song(const char *utf8path, struct song *song, GError **error_r) bool spl_append_uri(const char *url, const char *utf8file, GError **error_r) { - struct song *song; - if (uri_has_scheme(url)) { - song = song_remote_new(url); + struct song *song = song_remote_new(url); bool success = spl_append_song(utf8file, song, error_r); song_free(song); return success; } else { - song = db_get_song(url); + struct song *song = db_get_song(url); if (song == NULL) { g_set_error_literal(error_r, playlist_quark(), PLAYLIST_RESULT_NO_SUCH_SONG, @@ -496,7 +431,9 @@ spl_append_uri(const char *url, const char *utf8file, GError **error_r) return false; } - return spl_append_song(utf8file, song, error_r); + bool success = spl_append_song(utf8file, song, error_r); + db_return_song(song); + return success; } } |