PlaylistFile: use std::list instead of GPtrArray

This commit is contained in:
Max Kellermann 2012-09-27 23:48:29 +02:00
parent 7298b6c846
commit 609f6ce66d
6 changed files with 114 additions and 159 deletions

View File

@ -57,17 +57,13 @@ extern "C" {
#include <string.h> #include <string.h>
static void 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) { for (const auto &i : list) {
struct stored_playlist_info *playlist = client_printf(client, "playlist: %s\n", i.name.c_str());
(struct stored_playlist_info *)
g_ptr_array_index(list, i);
client_printf(client, "playlist: %s\n", playlist->name); if (i.mtime > 0)
time_print(client, "Last-Modified", i.mtime);
if (playlist->mtime > 0)
time_print(client, "Last-Modified", playlist->mtime);
} }
} }
@ -147,11 +143,8 @@ handle_lsinfo(struct client *client, int argc, char *argv[])
return result; return result;
if (isRootDirectory(uri)) { if (isRootDirectory(uri)) {
GPtrArray *list = spl_list(NULL); const auto &list = ListPlaylistFiles(NULL);
if (list != NULL) {
print_spl_list(client, list); print_spl_list(client, list);
spl_list_free(list);
}
} }
return COMMAND_RETURN_OK; return COMMAND_RETURN_OK;

View File

@ -40,17 +40,13 @@ extern "C" {
#include <stdlib.h> #include <stdlib.h>
static void 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) { for (const auto &i : list) {
struct stored_playlist_info *playlist = client_printf(client, "playlist: %s\n", i.name.c_str());
(struct stored_playlist_info *)
g_ptr_array_index(list, i);
client_printf(client, "playlist: %s\n", playlist->name); if (i.mtime > 0)
time_print(client, "Last-Modified", i.mtime);
if (playlist->mtime > 0)
time_print(client, "Last-Modified", playlist->mtime);
} }
} }
@ -218,11 +214,10 @@ handle_listplaylists(struct client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[]) G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{ {
GError *error = NULL; GError *error = NULL;
GPtrArray *list = spl_list(&error); const auto list = ListPlaylistFiles(&error);
if (list == NULL) if (list.empty() && error != NULL)
return print_error(client, error); return print_error(client, error);
print_spl_list(client, list); print_spl_list(client, list);
spl_list_free(list);
return COMMAND_RETURN_OK; return COMMAND_RETURN_OK;
} }

View File

@ -138,83 +138,68 @@ playlist_errno(GError **error_r)
} }
} }
static struct stored_playlist_info * static bool
load_playlist_info(const char *parent_path_fs, const char *name_fs) LoadPlaylistFileInfo(PlaylistFileInfo &info,
const char *parent_path_fs, const char *name_fs)
{ {
size_t name_length = strlen(name_fs); size_t name_length = strlen(name_fs);
if (name_length < sizeof(PLAYLIST_FILE_SUFFIX) || if (name_length < sizeof(PLAYLIST_FILE_SUFFIX) ||
memchr(name_fs, '\n', name_length) != NULL) memchr(name_fs, '\n', name_length) != NULL)
return NULL; return false;
if (!g_str_has_suffix(name_fs, PLAYLIST_FILE_SUFFIX)) 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); char *path_fs = g_build_filename(parent_path_fs, name_fs, NULL);
struct stat st; struct stat st;
int ret = stat(path_fs, &st); int ret = stat(path_fs, &st);
g_free(path_fs); g_free(path_fs);
if (ret < 0 || !S_ISREG(st.st_mode)) if (ret < 0 || !S_ISREG(st.st_mode))
return NULL; return false;
char *name = g_strndup(name_fs, char *name = g_strndup(name_fs,
name_length + 1 - sizeof(PLAYLIST_FILE_SUFFIX)); name_length + 1 - sizeof(PLAYLIST_FILE_SUFFIX));
char *name_utf8 = fs_charset_to_utf8(name); char *name_utf8 = fs_charset_to_utf8(name);
g_free(name); g_free(name);
if (name_utf8 == NULL) if (name_utf8 == NULL)
return NULL; return false;
struct stored_playlist_info *playlist = info.name = name_utf8;
g_new(struct stored_playlist_info, 1); info.mtime = st.st_mtime;
playlist->name = name_utf8; return true;
playlist->mtime = st.st_mtime;
return playlist;
} }
GPtrArray * PlaylistFileList
spl_list(GError **error_r) ListPlaylistFiles(GError **error_r)
{ {
PlaylistFileList list;
const char *parent_path_fs = spl_map(error_r); const char *parent_path_fs = spl_map(error_r);
if (parent_path_fs == NULL) if (parent_path_fs == NULL)
return NULL; return list;
DIR *dir = opendir(parent_path_fs); DIR *dir = opendir(parent_path_fs);
if (dir == NULL) { if (dir == NULL) {
g_set_error_literal(error_r, g_file_error_quark(), errno, g_set_error_literal(error_r, g_file_error_quark(), errno,
g_strerror(errno)); g_strerror(errno));
return NULL; return list;
} }
GPtrArray *list = g_ptr_array_new(); PlaylistFileInfo info;
struct dirent *ent; struct dirent *ent;
while ((ent = readdir(dir)) != NULL) { while ((ent = readdir(dir)) != NULL) {
struct stored_playlist_info *playlist = if (LoadPlaylistFileInfo(info, parent_path_fs, ent->d_name))
load_playlist_info(parent_path_fs, ent->d_name); list.push_back(std::move(info));
if (playlist != NULL)
g_ptr_array_add(list, playlist);
} }
closedir(dir); closedir(dir);
return list; 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 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); assert(utf8path != NULL);
@ -232,34 +217,32 @@ spl_save(GPtrArray *list, const char *utf8path, GError **error_r)
return false; return false;
} }
for (unsigned i = 0; i < list->len; ++i) { for (const auto &uri_utf8 : contents)
const char *uri = (const char *)g_ptr_array_index(list, i); playlist_print_uri(file, uri_utf8.c_str());
playlist_print_uri(file, uri);
}
fclose(file); fclose(file);
return true; return true;
} }
GPtrArray * PlaylistFileContents
spl_load(const char *utf8path, GError **error_r) LoadPlaylistFile(const char *utf8path, GError **error_r)
{ {
PlaylistFileContents contents;
if (spl_map(error_r) == NULL) if (spl_map(error_r) == NULL)
return NULL; return contents;
char *path_fs = spl_map_to_fs(utf8path, error_r); char *path_fs = spl_map_to_fs(utf8path, error_r);
if (path_fs == NULL) if (path_fs == NULL)
return NULL; return contents;
FILE *file = fopen(path_fs, "r"); FILE *file = fopen(path_fs, "r");
g_free(path_fs); g_free(path_fs);
if (file == NULL) { if (file == NULL) {
playlist_errno(error_r); playlist_errno(error_r);
return NULL; return contents;
} }
GPtrArray *list = g_ptr_array_new();
GString *buffer = g_string_sized_new(1024); GString *buffer = g_string_sized_new(1024);
char *s; char *s;
while ((s = read_text_line(file, buffer)) != NULL) { while ((s = read_text_line(file, buffer)) != NULL) {
@ -277,47 +260,13 @@ spl_load(const char *utf8path, GError **error_r)
} else } else
s = g_strdup(s); s = g_strdup(s);
g_ptr_array_add(list, s); contents.emplace_back(s);
if (contents.size() >= playlist_max_length)
if (list->len >= playlist_max_length)
break; break;
} }
fclose(file); fclose(file);
return list; return contents;
}
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;
} }
bool bool
@ -329,24 +278,28 @@ spl_move_index(const char *utf8path, unsigned src, unsigned dest,
what the hell.. */ what the hell.. */
return true; return true;
GPtrArray *list = spl_load(utf8path, error_r); GError *error = nullptr;
if (list == NULL) auto contents = LoadPlaylistFile(utf8path, &error);
if (contents.empty() && error != nullptr) {
g_propagate_error(error_r, error);
return false; return false;
}
if (src >= list->len || dest >= list->len) { if (src >= contents.size() || dest >= contents.size()) {
spl_free(list);
g_set_error_literal(error_r, playlist_quark(), g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_BAD_RANGE, PLAYLIST_RESULT_BAD_RANGE,
"Bad range"); "Bad range");
return false; return false;
} }
char *uri = spl_remove_index_internal(list, src); const auto src_i = std::next(contents.begin(), src);
spl_insert_index_internal(list, dest, uri); 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); idle_add(IDLE_STORED_PLAYLIST);
return result; return result;
@ -398,23 +351,23 @@ spl_delete(const char *name_utf8, GError **error_r)
bool bool
spl_remove_index(const char *utf8path, unsigned pos, GError **error_r) spl_remove_index(const char *utf8path, unsigned pos, GError **error_r)
{ {
GPtrArray *list = spl_load(utf8path, error_r); GError *error = nullptr;
if (list == NULL) auto contents = LoadPlaylistFile(utf8path, &error);
if (contents.empty() && error != nullptr) {
g_propagate_error(error_r, error);
return false; return false;
}
if (pos >= list->len) { if (pos >= contents.size()) {
spl_free(list);
g_set_error_literal(error_r, playlist_quark(), g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_BAD_RANGE, PLAYLIST_RESULT_BAD_RANGE,
"Bad range"); "Bad range");
return false; return false;
} }
char *uri = spl_remove_index_internal(list, pos); contents.erase(std::next(contents.begin(), pos));
g_free(uri);
bool result = spl_save(list, utf8path, error_r);
spl_free(list); bool result = SavePlaylistFile(contents, utf8path, error_r);
idle_add(IDLE_STORED_PLAYLIST); idle_add(IDLE_STORED_PLAYLIST);
return result; return result;

View File

@ -20,18 +20,32 @@
#ifndef MPD_STORED_PLAYLIST_H #ifndef MPD_STORED_PLAYLIST_H
#define MPD_STORED_PLAYLIST_H #define MPD_STORED_PLAYLIST_H
#ifdef __cplusplus
#include <list>
#include <vector>
#include <string>
#endif
#include <glib.h> #include <glib.h>
#include <stdbool.h> #include <stdbool.h>
#include <time.h> #include <time.h>
struct song; struct song;
struct stored_playlist_info { #ifdef __cplusplus
char *name;
struct PlaylistFileInfo {
std::string name;
time_t mtime; time_t mtime;
}; };
typedef std::list<PlaylistFileInfo> PlaylistFileList;
typedef std::vector<std::string> PlaylistFileContents;
#endif
extern bool playlist_saveAbsolutePaths; extern bool playlist_saveAbsolutePaths;
G_BEGIN_DECLS G_BEGIN_DECLS
@ -42,6 +56,10 @@ G_BEGIN_DECLS
void void
spl_global_init(void); spl_global_init(void);
G_END_DECLS
#ifdef __cplusplus
/** /**
* Determines whether the specified string is a valid name for a * Determines whether the specified string is a valid name for a
* stored playlist. * stored playlist.
@ -53,17 +71,11 @@ spl_valid_name(const char *name_utf8);
* Returns a list of stored_playlist_info struct pointers. Returns * Returns a list of stored_playlist_info struct pointers. Returns
* NULL if an error occurred. * NULL if an error occurred.
*/ */
GPtrArray * PlaylistFileList
spl_list(GError **error_r); ListPlaylistFiles(GError **error_r);
void PlaylistFileContents
spl_list_free(GPtrArray *list); LoadPlaylistFile(const char *utf8path, GError **error_r);
GPtrArray *
spl_load(const char *utf8path, GError **error_r);
void
spl_free(GPtrArray *list);
bool bool
spl_move_index(const char *utf8path, unsigned src, unsigned dest, 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 bool
spl_rename(const char *utf8from, const char *utf8to, GError **error_r); spl_rename(const char *utf8from, const char *utf8to, GError **error_r);
G_END_DECLS #endif
#endif #endif

View File

@ -116,18 +116,18 @@ bool
spl_print(struct client *client, const char *name_utf8, bool detail, spl_print(struct client *client, const char *name_utf8, bool detail,
GError **error_r) GError **error_r)
{ {
GPtrArray *list; GError *error = NULL;
PlaylistFileContents contents = LoadPlaylistFile(name_utf8, &error);
list = spl_load(name_utf8, error_r); if (contents.empty() && error != nullptr) {
if (list == NULL) g_propagate_error(error_r, error);
return false; return false;
}
for (unsigned i = 0; i < list->len; ++i) { for (const auto &uri_utf8 : contents) {
const char *temp = (const char *)g_ptr_array_index(list, i);
bool wrote = false; bool wrote = false;
if (detail) { if (detail) {
struct song *song = db_get_song(temp); struct song *song = db_get_song(uri_utf8.c_str());
if (song) { if (song) {
song_print_info(client, song); song_print_info(client, song);
db_return_song(song); db_return_song(song);
@ -136,11 +136,11 @@ spl_print(struct client *client, const char *name_utf8, bool detail,
} }
if (!wrote) { 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; return true;
} }

View File

@ -119,33 +119,35 @@ playlist_load_spl(struct playlist *playlist, struct player_control *pc,
unsigned start_index, unsigned end_index, unsigned start_index, unsigned end_index,
GError **error_r) GError **error_r)
{ {
GPtrArray *list; GError *error = NULL;
PlaylistFileContents contents = LoadPlaylistFile(name_utf8, &error);
list = spl_load(name_utf8, error_r); if (contents.empty() && error != nullptr) {
if (list == NULL) g_propagate_error(error_r, error);
return false; return false;
}
if (list->len < end_index) if (end_index > contents.size())
end_index = list->len; end_index = contents.size();
for (unsigned i = start_index; i < end_index; ++i) { for (unsigned i = start_index; i < end_index; ++i) {
const char *temp = (const char *)g_ptr_array_index(list, i); const auto &uri_utf8 = contents[i];
if ((playlist_append_uri(playlist, pc, temp, NULL)) != PLAYLIST_RESULT_SUCCESS) {
if ((playlist_append_uri(playlist, pc, uri_utf8.c_str(),
nullptr)) != PLAYLIST_RESULT_SUCCESS) {
/* for windows compatibility, convert slashes */ /* for windows compatibility, convert slashes */
char *temp2 = g_strdup(temp); char *temp2 = g_strdup(uri_utf8.c_str());
char *p = temp2; char *p = temp2;
while (*p) { while (*p) {
if (*p == '\\') if (*p == '\\')
*p = '/'; *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_warning("can't add file \"%s\"", temp2);
} }
g_free(temp2); g_free(temp2);
} }
} }
spl_free(list);
return true; return true;
} }