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.
This commit is contained in:
Max Kellermann 2008-10-08 11:05:25 +02:00
parent 4a510a2674
commit 5e4be9e495
5 changed files with 49 additions and 28 deletions

View File

@ -561,7 +561,7 @@ enum playlist_result addToPlaylist(const char *url, int *added_id)
if ((song = getSongFromDB(url))) { if ((song = getSongFromDB(url))) {
} else if (!(isValidRemoteUtf8Url(url) && } else if (!(isValidRemoteUtf8Url(url) &&
(song = newSong(url, NULL)))) { (song = song_remote_new(url)))) {
return PLAYLIST_RESULT_NO_SUCH_SONG; return PLAYLIST_RESULT_NO_SUCH_SONG;
} }
@ -581,7 +581,7 @@ int addToStoredPlaylist(const char *url, const char *utf8file)
if (!isValidRemoteUtf8Url(url)) if (!isValidRemoteUtf8Url(url))
return ACK_ERROR_NO_EXIST; return ACK_ERROR_NO_EXIST;
song = newSong(url, NULL); song = song_remote_new(url);
if (song) { if (song) {
int ret = appendSongToStoredPlaylistByPath(utf8file, song); int ret = appendSongToStoredPlaylistByPath(utf8file, song);
freeJustSong(song); freeJustSong(song);

View File

@ -26,7 +26,7 @@
#include "decoder_list.h" #include "decoder_list.h"
#include "decoder_api.h" #include "decoder_api.h"
struct song * static struct song *
song_alloc(const char *url, struct directory *parent) song_alloc(const char *url, struct directory *parent)
{ {
size_t urllen; size_t urllen;
@ -45,34 +45,45 @@ song_alloc(const char *url, struct directory *parent)
} }
struct song * 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; struct song *song;
assert(*url); struct decoder_plugin *plugin;
unsigned int next = 0;
char path_max_tmp[MPD_PATH_MAX], *abs_path;
if (strchr(url, '\n')) { assert(parent != NULL);
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; return NULL;
} }
song = song_alloc(url, parentDir); song = song_file_new(path, parent);
if (song_is_file(song)) { abs_path = rmp2amp_r(path_max_tmp, get_song_url(path_max_tmp, song));
struct decoder_plugin *plugin; while (song->tag == NULL &&
unsigned int next = 0; (plugin = isMusic(abs_path, &(song->mtime), next++))) {
char path_max_tmp[MPD_PATH_MAX]; song->tag = plugin->tag_dup(abs_path);
char *abs_path = rmp2amp_r(path_max_tmp, }
get_song_url(path_max_tmp, song));
while (!song->tag && (plugin = isMusic(abs_path, if (song->tag == NULL || song->tag->time < 0) {
&(song->mtime), freeJustSong(song);
next++))) { return NULL;
song->tag = plugin->tag_dup(abs_path);
}
if (!song->tag || song->tag->time < 0) {
freeJustSong(song);
song = NULL;
}
} }
return song; return song;

View File

@ -36,11 +36,21 @@ struct song {
char url[sizeof(int)]; char url[sizeof(int)];
}; };
/** allocate a new song with a remote URL */
struct song * 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 * 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 void
freeJustSong(struct song *); freeJustSong(struct song *);

View File

@ -113,8 +113,8 @@ void readSongInfoIntoList(FILE *fp, struct songvec *sv,
if (song) if (song)
insertSongIntoList(sv, song); insertSongIntoList(sv, song);
song = song_alloc(buffer + strlen(SONG_KEY), song = song_file_new(buffer + strlen(SONG_KEY),
parentDir); parentDir);
} else if (*buffer == 0) { } else if (*buffer == 0) {
/* ignore empty lines (starting with '\0') */ /* ignore empty lines (starting with '\0') */
} else if (song == NULL) { } else if (song == NULL) {

View File

@ -201,7 +201,7 @@ addToDirectory(struct directory *directory, const char *name)
struct song *song; struct song *song;
const char *shortname = mpd_basename(name); const char *shortname = mpd_basename(name);
if (!(song = newSong(shortname, directory))) if (!(song = song_file_load(shortname, directory)))
return -1; return -1;
songvec_add(&directory->songs, song); songvec_add(&directory->songs, song);
LOG("added %s\n", name); LOG("added %s\n", name);