From 5e4be9e495677a6728e3161e39a05a081a5bff9a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 8 Oct 2008 11:05:25 +0200 Subject: song: replaced all song constructors Provide separate constructors for creating a remote song, a local song, and one for loading data from a song file. This way, we can add more assertions. --- src/playlist.c | 4 ++-- src/song.c | 53 ++++++++++++++++++++++++++++++++--------------------- src/song.h | 14 ++++++++++++-- src/song_save.c | 4 ++-- src/update.c | 2 +- 5 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/playlist.c b/src/playlist.c index 624636c0c..823720aef 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -561,7 +561,7 @@ enum playlist_result addToPlaylist(const char *url, int *added_id) if ((song = getSongFromDB(url))) { } else if (!(isValidRemoteUtf8Url(url) && - (song = newSong(url, NULL)))) { + (song = song_remote_new(url)))) { return PLAYLIST_RESULT_NO_SUCH_SONG; } @@ -581,7 +581,7 @@ int addToStoredPlaylist(const char *url, const char *utf8file) if (!isValidRemoteUtf8Url(url)) return ACK_ERROR_NO_EXIST; - song = newSong(url, NULL); + song = song_remote_new(url); if (song) { int ret = appendSongToStoredPlaylistByPath(utf8file, song); freeJustSong(song); diff --git a/src/song.c b/src/song.c index 3219e7c48..f96f90c8a 100644 --- a/src/song.c +++ b/src/song.c @@ -26,7 +26,7 @@ #include "decoder_list.h" #include "decoder_api.h" -struct song * +static struct song * song_alloc(const char *url, struct directory *parent) { size_t urllen; @@ -45,34 +45,45 @@ song_alloc(const char *url, struct directory *parent) } struct song * -newSong(const char *url, struct directory *parentDir) +song_remote_new(const char *url) +{ + return song_alloc(url, NULL); +} + +struct song * +song_file_new(const char *path, struct directory *parent) +{ + assert(parent != NULL); + + return song_alloc(path, parent); +} + +struct song * +song_file_load(const char *path, struct directory *parent) { struct song *song; - assert(*url); + struct decoder_plugin *plugin; + unsigned int next = 0; + char path_max_tmp[MPD_PATH_MAX], *abs_path; + + assert(parent != NULL); - if (strchr(url, '\n')) { - DEBUG("newSong: '%s' is not a valid uri\n", url); + if (strchr(path, '\n')) { + DEBUG("newSong: '%s' is not a valid uri\n", path); return NULL; } - song = song_alloc(url, parentDir); + song = song_file_new(path, parent); - if (song_is_file(song)) { - struct decoder_plugin *plugin; - unsigned int next = 0; - char path_max_tmp[MPD_PATH_MAX]; - char *abs_path = rmp2amp_r(path_max_tmp, - get_song_url(path_max_tmp, song)); + abs_path = rmp2amp_r(path_max_tmp, get_song_url(path_max_tmp, song)); + while (song->tag == NULL && + (plugin = isMusic(abs_path, &(song->mtime), next++))) { + song->tag = plugin->tag_dup(abs_path); + } - while (!song->tag && (plugin = isMusic(abs_path, - &(song->mtime), - next++))) { - song->tag = plugin->tag_dup(abs_path); - } - if (!song->tag || song->tag->time < 0) { - freeJustSong(song); - song = NULL; - } + if (song->tag == NULL || song->tag->time < 0) { + freeJustSong(song); + return NULL; } return song; diff --git a/src/song.h b/src/song.h index 2510f3770..c3b79f966 100644 --- a/src/song.h +++ b/src/song.h @@ -36,11 +36,21 @@ struct song { char url[sizeof(int)]; }; +/** allocate a new song with a remote URL */ struct song * -song_alloc(const char *url, struct directory *parent); +song_remote_new(const char *url); +/** allocate a new song with a local file name */ struct song * -newSong(const char *url, struct directory *parentDir); +song_file_new(const char *path, struct directory *parent); + +/** + * allocate a new song structure with a local file name and attempt to + * load its metadata. If all decoder plugin fail to read its meta + * data, NULL is returned. + */ +struct song * +song_file_load(const char *path, struct directory *parent); void freeJustSong(struct song *); diff --git a/src/song_save.c b/src/song_save.c index 00e4a8580..976a97afd 100644 --- a/src/song_save.c +++ b/src/song_save.c @@ -113,8 +113,8 @@ void readSongInfoIntoList(FILE *fp, struct songvec *sv, if (song) insertSongIntoList(sv, song); - song = song_alloc(buffer + strlen(SONG_KEY), - parentDir); + song = song_file_new(buffer + strlen(SONG_KEY), + parentDir); } else if (*buffer == 0) { /* ignore empty lines (starting with '\0') */ } else if (song == NULL) { diff --git a/src/update.c b/src/update.c index 33253065f..e41a23b91 100644 --- a/src/update.c +++ b/src/update.c @@ -201,7 +201,7 @@ addToDirectory(struct directory *directory, const char *name) struct song *song; const char *shortname = mpd_basename(name); - if (!(song = newSong(shortname, directory))) + if (!(song = song_file_load(shortname, directory))) return -1; songvec_add(&directory->songs, song); LOG("added %s\n", name); -- cgit v1.2.3