aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/OtherCommands.cxx21
-rw-r--r--src/PlaylistCommands.cxx19
-rw-r--r--src/PlaylistFile.cxx153
-rw-r--r--src/PlaylistFile.h38
-rw-r--r--src/PlaylistPrint.cxx18
-rw-r--r--src/PlaylistSave.cxx24
6 files changed, 114 insertions, 159 deletions
diff --git a/src/OtherCommands.cxx b/src/OtherCommands.cxx
index ab7321953..2c714507e 100644
--- a/src/OtherCommands.cxx
+++ b/src/OtherCommands.cxx
@@ -57,17 +57,13 @@ extern "C" {
#include <string.h>
static void
-print_spl_list(struct client *client, GPtrArray *list)
+print_spl_list(struct client *client, const PlaylistFileList &list)
{
- for (unsigned i = 0; i < list->len; ++i) {
- struct stored_playlist_info *playlist =
- (struct stored_playlist_info *)
- g_ptr_array_index(list, i);
+ for (const auto &i : list) {
+ client_printf(client, "playlist: %s\n", i.name.c_str());
- client_printf(client, "playlist: %s\n", playlist->name);
-
- if (playlist->mtime > 0)
- time_print(client, "Last-Modified", playlist->mtime);
+ if (i.mtime > 0)
+ time_print(client, "Last-Modified", i.mtime);
}
}
@@ -147,11 +143,8 @@ handle_lsinfo(struct client *client, int argc, char *argv[])
return result;
if (isRootDirectory(uri)) {
- GPtrArray *list = spl_list(NULL);
- if (list != NULL) {
- print_spl_list(client, list);
- spl_list_free(list);
- }
+ const auto &list = ListPlaylistFiles(NULL);
+ print_spl_list(client, list);
}
return COMMAND_RETURN_OK;
diff --git a/src/PlaylistCommands.cxx b/src/PlaylistCommands.cxx
index 108318c2f..f6f43f652 100644
--- a/src/PlaylistCommands.cxx
+++ b/src/PlaylistCommands.cxx
@@ -40,17 +40,13 @@ extern "C" {
#include <stdlib.h>
static void
-print_spl_list(struct client *client, GPtrArray *list)
+print_spl_list(struct client *client, const PlaylistFileList &list)
{
- for (unsigned i = 0; i < list->len; ++i) {
- struct stored_playlist_info *playlist =
- (struct stored_playlist_info *)
- g_ptr_array_index(list, i);
+ for (const auto &i : list) {
+ client_printf(client, "playlist: %s\n", i.name.c_str());
- client_printf(client, "playlist: %s\n", playlist->name);
-
- if (playlist->mtime > 0)
- time_print(client, "Last-Modified", playlist->mtime);
+ if (i.mtime > 0)
+ time_print(client, "Last-Modified", i.mtime);
}
}
@@ -218,11 +214,10 @@ handle_listplaylists(struct client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
GError *error = NULL;
- GPtrArray *list = spl_list(&error);
- if (list == NULL)
+ const auto list = ListPlaylistFiles(&error);
+ if (list.empty() && error != NULL)
return print_error(client, error);
print_spl_list(client, list);
- spl_list_free(list);
return COMMAND_RETURN_OK;
}
diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx
index 9beae76ad..462060f0e 100644
--- a/src/PlaylistFile.cxx
+++ b/src/PlaylistFile.cxx
@@ -138,83 +138,68 @@ playlist_errno(GError **error_r)
}
}
-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);
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;
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;
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;
- struct stored_playlist_info *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)
{
+ PlaylistFileList list;
+
const char *parent_path_fs = spl_map(error_r);
if (parent_path_fs == NULL)
- return NULL;
+ return list;
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;
+ return list;
}
- GPtrArray *list = g_ptr_array_new();
-
+ PlaylistFileInfo info;
struct dirent *ent;
while ((ent = readdir(dir)) != NULL) {
- struct stored_playlist_info *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 =
- (struct stored_playlist_info *)
- 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)
{
assert(utf8path != NULL);
@@ -232,34 +217,32 @@ spl_save(GPtrArray *list, const char *utf8path, GError **error_r)
return false;
}
- for (unsigned i = 0; i < list->len; ++i) {
- const char *uri = (const char *)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)
{
+ PlaylistFileContents contents;
+
if (spl_map(error_r) == NULL)
- return NULL;
+ return contents;
char *path_fs = spl_map_to_fs(utf8path, error_r);
if (path_fs == NULL)
- return NULL;
+ return contents;
FILE *file = fopen(path_fs, "r");
g_free(path_fs);
if (file == NULL) {
playlist_errno(error_r);
- return NULL;
+ return contents;
}
- GPtrArray *list = g_ptr_array_new();
-
GString *buffer = g_string_sized_new(1024);
char *s;
while ((s = read_text_line(file, buffer)) != NULL) {
@@ -277,47 +260,13 @@ 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 = (char *)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)
-{
- assert(idx < list->len);
-
- char *uri = (char *)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
@@ -329,24 +278,28 @@ spl_move_index(const char *utf8path, unsigned src, unsigned dest,
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;
}
- char *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;
@@ -398,23 +351,23 @@ spl_delete(const char *name_utf8, GError **error_r)
bool
spl_remove_index(const char *utf8path, unsigned pos, GError **error_r)
{
- 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;
}
- char *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;
diff --git a/src/PlaylistFile.h b/src/PlaylistFile.h
index 35eda6d7b..ddd2a47ec 100644
--- a/src/PlaylistFile.h
+++ b/src/PlaylistFile.h
@@ -20,18 +20,32 @@
#ifndef MPD_STORED_PLAYLIST_H
#define MPD_STORED_PLAYLIST_H
+#ifdef __cplusplus
+#include <list>
+#include <vector>
+#include <string>
+#endif
+
#include <glib.h>
#include <stdbool.h>
#include <time.h>
struct song;
-struct stored_playlist_info {
- char *name;
+#ifdef __cplusplus
+
+struct PlaylistFileInfo {
+ std::string name;
time_t mtime;
};
+typedef std::list<PlaylistFileInfo> PlaylistFileList;
+
+typedef std::vector<std::string> PlaylistFileContents;
+
+#endif
+
extern bool playlist_saveAbsolutePaths;
G_BEGIN_DECLS
@@ -42,6 +56,10 @@ G_BEGIN_DECLS
void
spl_global_init(void);
+G_END_DECLS
+
+#ifdef __cplusplus
+
/**
* Determines whether the specified string is a valid name for a
* stored playlist.
@@ -53,17 +71,11 @@ spl_valid_name(const char *name_utf8);
* Returns a list of stored_playlist_info struct pointers. Returns
* NULL if an error occurred.
*/
-GPtrArray *
-spl_list(GError **error_r);
-
-void
-spl_list_free(GPtrArray *list);
+PlaylistFileList
+ListPlaylistFiles(GError **error_r);
-GPtrArray *
-spl_load(const char *utf8path, GError **error_r);
-
-void
-spl_free(GPtrArray *list);
+PlaylistFileContents
+LoadPlaylistFile(const char *utf8path, GError **error_r);
bool
spl_move_index(const char *utf8path, unsigned src, unsigned dest,
@@ -87,6 +99,6 @@ spl_append_uri(const char *file, const char *utf8file, GError **error_r);
bool
spl_rename(const char *utf8from, const char *utf8to, GError **error_r);
-G_END_DECLS
+#endif
#endif
diff --git a/src/PlaylistPrint.cxx b/src/PlaylistPrint.cxx
index 832f49cfd..4f50825be 100644
--- a/src/PlaylistPrint.cxx
+++ b/src/PlaylistPrint.cxx
@@ -116,18 +116,18 @@ bool
spl_print(struct client *client, const char *name_utf8, bool detail,
GError **error_r)
{
- GPtrArray *list;
-
- list = spl_load(name_utf8, error_r);
- if (list == NULL)
+ GError *error = NULL;
+ PlaylistFileContents contents = LoadPlaylistFile(name_utf8, &error);
+ if (contents.empty() && error != nullptr) {
+ g_propagate_error(error_r, error);
return false;
+ }
- for (unsigned i = 0; i < list->len; ++i) {
- const char *temp = (const char *)g_ptr_array_index(list, i);
+ for (const auto &uri_utf8 : contents) {
bool wrote = false;
if (detail) {
- struct song *song = db_get_song(temp);
+ struct song *song = db_get_song(uri_utf8.c_str());
if (song) {
song_print_info(client, song);
db_return_song(song);
@@ -136,11 +136,11 @@ spl_print(struct client *client, const char *name_utf8, bool detail,
}
if (!wrote) {
- client_printf(client, SONG_FILE "%s\n", temp);
+ client_printf(client, SONG_FILE "%s\n",
+ uri_utf8.c_str());
}
}
- spl_free(list);
return true;
}
diff --git a/src/PlaylistSave.cxx b/src/PlaylistSave.cxx
index 00de539d9..4c8af627b 100644
--- a/src/PlaylistSave.cxx
+++ b/src/PlaylistSave.cxx
@@ -119,33 +119,35 @@ playlist_load_spl(struct playlist *playlist, struct player_control *pc,
unsigned start_index, unsigned end_index,
GError **error_r)
{
- GPtrArray *list;
-
- list = spl_load(name_utf8, error_r);
- if (list == NULL)
+ GError *error = NULL;
+ PlaylistFileContents contents = LoadPlaylistFile(name_utf8, &error);
+ if (contents.empty() && error != nullptr) {
+ g_propagate_error(error_r, error);
return false;
+ }
- if (list->len < end_index)
- end_index = list->len;
+ if (end_index > contents.size())
+ end_index = contents.size();
for (unsigned i = start_index; i < end_index; ++i) {
- const char *temp = (const char *)g_ptr_array_index(list, i);
- if ((playlist_append_uri(playlist, pc, temp, NULL)) != PLAYLIST_RESULT_SUCCESS) {
+ const auto &uri_utf8 = contents[i];
+
+ if ((playlist_append_uri(playlist, pc, uri_utf8.c_str(),
+ nullptr)) != PLAYLIST_RESULT_SUCCESS) {
/* for windows compatibility, convert slashes */
- char *temp2 = g_strdup(temp);
+ char *temp2 = g_strdup(uri_utf8.c_str());
char *p = temp2;
while (*p) {
if (*p == '\\')
*p = '/';
p++;
}
- if ((playlist_append_uri(playlist, pc, temp, NULL)) != PLAYLIST_RESULT_SUCCESS) {
+ if ((playlist_append_uri(playlist, pc, temp2, NULL)) != PLAYLIST_RESULT_SUCCESS) {
g_warning("can't add file \"%s\"", temp2);
}
g_free(temp2);
}
}
- spl_free(list);
return true;
}