From d24e17ebdd5eeb16c87b897a9ecb52a5704a52b3 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 | 52 ++++++++++++++++++++++++++++++---------------------- src/song.h | 15 +++++++++++++-- src/update.c | 2 +- 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/playlist.c b/src/playlist.c index 02800c2ba..22df8be5d 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -577,7 +577,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; } @@ -596,7 +596,7 @@ int addToStoredPlaylist(const char *url, const char *utf8file) if (!isValidRemoteUtf8Url(url)) return ACK_ERROR_NO_EXIST; - if ((song = newSong(url, NULL))) { + if ((song = song_remote_new(url))) { int ret = appendSongToStoredPlaylistByPath(utf8file, song); freeJustSong(song); return ret; diff --git a/src/song.c b/src/song.c index be84bfa4b..cf9994086 100644 --- a/src/song.c +++ b/src/song.c @@ -46,34 +46,41 @@ static struct mpd_song * song_alloc(const char *url, struct directory *parent) return song; } -struct mpd_song *newSong(const char *url, struct directory *parentDir) +struct mpd_song * song_remote_new(const char *url) +{ + return song_alloc(url, NULL); +} + +struct mpd_song * song_file_new(const char *path, struct directory *parent) +{ + assert(parent != NULL); + + return song_alloc(path, parent); +} + +struct mpd_song * song_file_load(const char *path, struct directory *parent) { struct mpd_song *song; - assert(*url); + unsigned int next = 0; + InputPlugin *plugin; + char path_max_tmp[MPD_PATH_MAX]; + char *abs_path; - if (strchr(url, '\n')) { - DEBUG("newSong: '%s' is not a valid uri\n", url); + if (strchr(path, '\n')) { + DEBUG("song_file_load: '%s' is not a valid uri\n", path); return NULL; } - song = song_alloc(url, parentDir); - - if (song_is_file(song)) { - InputPlugin *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)); + song = song_file_new(path, parent); + abs_path = rmp2amp_r(path_max_tmp, get_song_url(path_max_tmp, song)); - while (!song->tag && (plugin = isMusic(abs_path, - &(song->mtime), - next++))) { - song->tag = plugin->tagDupFunc(abs_path); - } - if (!song->tag || song->tag->time < 0) { - freeJustSong(song); - song = NULL; - } + while (!song->tag && + (plugin = isMusic(abs_path, &song->mtime, next++))) { + song->tag = plugin->tagDupFunc(abs_path); + } + if (!song->tag || song->tag->time < 0) { + freeJustSong(song); + return NULL; } return song; @@ -172,7 +179,8 @@ void readSongInfoIntoList(FILE * fp, struct directory * parentDir) if (!prefixcmp(buffer, SONG_KEY)) { 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/song.h b/src/song.h index 88adde0f0..0d2f976ac 100644 --- a/src/song.h +++ b/src/song.h @@ -38,10 +38,21 @@ struct mpd_song { char url[sizeof(size_t)]; }; -struct mpd_song *newSong(const char *url, struct directory *parentDir); - void freeJustSong(struct mpd_song *); +/** allocate a new song with a remote URL */ +struct mpd_song * song_remote_new(const char *url); + +/** allocate a new song with a local file name */ +struct mpd_song * 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 mpd_song * song_file_load(const char *path, struct directory *parent); + ssize_t song_print_info(struct mpd_song * song, int fd); /* like song_print_info, but casts data into an fd first */ diff --git a/src/update.c b/src/update.c index 674ad0519..c40f25694 100644 --- a/src/update.c +++ b/src/update.c @@ -195,7 +195,7 @@ addToDirectory(struct directory *directory, const char *name) struct mpd_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