diff --git a/commands.c b/commands.c index e310fca..0d0c8ae 100644 --- a/commands.c +++ b/commands.c @@ -413,11 +413,8 @@ cmd_new(int argc, char *argv[]) } /* create & setup playlist */ - p = playlist_new(); + p = playlist_new(filename, name); p->needs_saving = true; - p->filename = filename; - if ((p->name = strdup(name)) == NULL) - err(1, "cmd_new: strdup(3) failed"); /* add playlist to media library and update ui */ medialib_playlist_add(p); @@ -429,6 +426,7 @@ cmd_new(int argc, char *argv[]) paint_library(); paint_message("playlist \"%s\" added", name); + free(filename); return 0; } diff --git a/medialib.c b/medialib.c index 35cfd2e..864df53 100644 --- a/medialib.c +++ b/medialib.c @@ -38,16 +38,8 @@ medialib_load(const char *db_file, const char *playlist_dir) err(1, "failed to strdup db file and playlist dir in medialib_init"); /* setup pseudo-playlists */ - mdb.library = playlist_new(); - mdb.library->filename = NULL; - mdb.library->name = strdup("--LIBRARY--"); - - mdb.filter_results = playlist_new(); - mdb.filter_results->filename = NULL; - mdb.filter_results->name = strdup("--FILTER--"); - - if (mdb.library->name == NULL || mdb.filter_results->name == NULL) - err(1, "failed to strdup pseudo-names in medialib_load"); + mdb.library = playlist_new(NULL, "--LIBRARY--"); + mdb.filter_results = playlist_new(NULL, "--FILTER--"); /* load the actual database */ medialib_db_load(db_file); diff --git a/playlist.c b/playlist.c index ceac2fc..d5a56d3 100644 --- a/playlist.c +++ b/playlist.c @@ -37,7 +37,7 @@ playlist_increase_capacity(playlist *p) * structure must be free(2)'d using playlist_free(). */ playlist * -playlist_new(void) +playlist_new(const char *filename, const char *name) { playlist *p; @@ -55,6 +55,15 @@ playlist_new(void) p->hist_present = -1; p->needs_saving = false; + if (filename != NULL) { + if ((p->filename = strdup(filename)) == NULL) + err(1, "%s: strdup(3) failed", __FUNCTION__); + } + if (name != NULL) { + if ((p->name = strdup(name)) == NULL) + err(1, "%s: strdup(3) failed", __FUNCTION__); + } + return p; } @@ -81,19 +90,10 @@ playlist_dup(const playlist *original, const char *filename, int i; /* create new playlist and copy simple members */ - newplist = playlist_new(); + newplist = playlist_new(filename, name); newplist->nfiles = original->nfiles; newplist->capacity = original->nfiles; - if (name != NULL) { - if ((newplist->name = strdup(name)) == NULL) - err(1, "playlist_dup: strdup name failed"); - } - if (filename != NULL) { - if ((newplist->filename = strdup(filename)) == NULL) - err(1, "playlist_dup: strdup filename failed"); - } - /* copy all of the files */ newplist->files = calloc(original->nfiles, sizeof(meta_info*)); if (newplist->files == NULL) @@ -203,20 +203,19 @@ playlist * playlist_load(const char *filename, meta_info **db, int ndb) { meta_info *mi, **mit; - FILE *fin; - char *period; - char entry[PATH_MAX + 1]; + FILE *fin; + char *name, *period; + char entry[PATH_MAX + 1]; /* open file */ if ((fin = fopen(filename, "r")) == NULL) err(1, "playlist_load: failed to open playlist '%s'", filename); + if ((name = basename(filename)) == NULL) + err(1, "%s: basename(3) failed", __FUNCTION__); + /* create playlist and setup */ - playlist *p = playlist_new(); - p->filename = strdup(filename); - p->name = strdup(basename(p->filename)); - if (p->filename == NULL || p->name == NULL) - err(1, "playlist_load: failed to allocate info for playlist '%s'", filename); + playlist *p = playlist_new(filename, name); /* hack to remove '.playlist' from name */ period = strrchr(p->name, '.'); @@ -309,7 +308,7 @@ playlist_filter(const playlist *p, bool m) if (!mi_query_isset()) return NULL; - results = playlist_new(); + results = playlist_new(NULL, NULL); for (i = 0; i < p->nfiles; i++) { if (mi_match(p->files[i])) { if (m) playlist_files_append(results, &(p->files[i]), 1, false); diff --git a/playlist.h b/playlist.h index 60c06a0..979fa62 100644 --- a/playlist.h +++ b/playlist.h @@ -81,7 +81,7 @@ typedef struct { */ /* create/destroy/duplicate playlist structs */ -playlist *playlist_new(void); +playlist *playlist_new(const char *filename, const char *name); void playlist_free(playlist *p); playlist *playlist_dup(const playlist *original, const char *filename, const char* name);