diff options
Diffstat (limited to '')
-rw-r--r-- | src/song_save.c | 54 |
1 files changed, 33 insertions, 21 deletions
diff --git a/src/song_save.c b/src/song_save.c index 8f4e1614d..b18057201 100644 --- a/src/song_save.c +++ b/src/song_save.c @@ -34,15 +34,20 @@ #define SONG_KEY "key: " #define SONG_MTIME "mtime: " +static GQuark +song_save_quark(void) +{ + return g_quark_from_static_string("song_save"); +} + static void -song_save_url(FILE *fp, struct song *song) +song_save_uri(FILE *fp, struct song *song) { if (song->parent != NULL && song->parent->path != NULL) fprintf(fp, SONG_FILE "%s/%s\n", - directory_get_path(song->parent), song->url); + directory_get_path(song->parent), song->uri); else - fprintf(fp, SONG_FILE "%s\n", - song->url); + fprintf(fp, SONG_FILE "%s\n", song->uri); } static int @@ -50,9 +55,9 @@ song_save(struct song *song, void *data) { FILE *fp = data; - fprintf(fp, SONG_KEY "%s\n", song->url); + fprintf(fp, SONG_KEY "%s\n", song->uri); - song_save_url(fp, song); + song_save_uri(fp, song); if (song->tag != NULL) tag_save(fp, song->tag); @@ -70,9 +75,9 @@ void songvec_save(FILE *fp, struct songvec *sv) } static void -insertSongIntoList(struct songvec *sv, struct song *newsong) +commit_song(struct songvec *sv, struct song *newsong) { - struct song *existing = songvec_find(sv, newsong->url); + struct song *existing = songvec_find(sv, newsong->uri); if (!existing) { songvec_add(sv, newsong); @@ -93,7 +98,7 @@ insertSongIntoList(struct songvec *sv, struct song *newsong) } static char * -matchesAnMpdTagItemKey(char *buffer, enum tag_type *itemType) +parse_tag_value(char *buffer, enum tag_type *type_r) { int i; @@ -102,7 +107,7 @@ matchesAnMpdTagItemKey(char *buffer, enum tag_type *itemType) if (0 == strncmp(tag_item_names[i], buffer, len) && buffer[len] == ':') { - *itemType = i; + *type_r = i; return g_strchug(buffer + len + 1); } } @@ -110,12 +115,13 @@ matchesAnMpdTagItemKey(char *buffer, enum tag_type *itemType) return NULL; } -void readSongInfoIntoList(FILE *fp, struct songvec *sv, - struct directory *parent) +bool +songvec_load(FILE *fp, struct songvec *sv, struct directory *parent, + GError **error_r) { char buffer[MPD_PATH_MAX + 1024]; struct song *song = NULL; - enum tag_type itemType; + enum tag_type type; const char *value; while (fgets(buffer, sizeof(buffer), fp) && @@ -124,24 +130,26 @@ void readSongInfoIntoList(FILE *fp, struct songvec *sv, if (0 == strncmp(SONG_KEY, buffer, strlen(SONG_KEY))) { if (song) - insertSongIntoList(sv, song); + commit_song(sv, song); song = song_file_new(buffer + strlen(SONG_KEY), parent); } else if (*buffer == 0) { /* ignore empty lines (starting with '\0') */ } else if (song == NULL) { - g_error("Problems reading song info"); + g_set_error(error_r, song_save_quark(), 0, + "Problems reading song info"); + return false; } else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) { /* we don't need this info anymore */ - } else if ((value = matchesAnMpdTagItemKey(buffer, - &itemType)) != NULL) { + } else if ((value = parse_tag_value(buffer, + &type)) != NULL) { if (!song->tag) { song->tag = tag_new(); tag_begin_add(song->tag); } - tag_add_item(song->tag, itemType, value); + tag_add_item(song->tag, type, value); } else if (0 == strncmp(SONG_TIME, buffer, strlen(SONG_TIME))) { if (!song->tag) { song->tag = tag_new(); @@ -151,11 +159,15 @@ void readSongInfoIntoList(FILE *fp, struct songvec *sv, song->tag->time = atoi(&(buffer[strlen(SONG_TIME)])); } else if (0 == strncmp(SONG_MTIME, buffer, strlen(SONG_MTIME))) { song->mtime = atoi(&(buffer[strlen(SONG_MTIME)])); + } else { + g_set_error(error_r, song_save_quark(), 0, + "unknown line in db: %s", buffer); + return false; } - else - g_error("unknown line in db: %s", buffer); } if (song) - insertSongIntoList(sv, song); + commit_song(sv, song); + + return true; } |