core/tags/album: Replace constructor with a backwards pointer

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-11-11 07:59:21 -05:00
parent ab837b1a18
commit 7c3bba11b5
4 changed files with 43 additions and 38 deletions

View File

@ -11,57 +11,63 @@ static struct database album_db;
static gchar *__album_key(const std::string &name, unsigned int year) static gchar *__album_key(const std::string &name, unsigned int year)
{ {
return g_strdup_printf("%u %s", year, name.c_str()); return g_strdup_printf("%u/%s", year, name.c_str());
} }
static struct album *__album_from_key(const gchar *key) static struct album *__album_setup(struct album *album, const gchar *name)
{ {
struct album *album = new struct album; gchar *lower = string_lowercase(name);
gchar *name, *lower;
if (sscanf(key, "%u %m[^\n]", &album->al_year, &name) == 1) dbe_init(&album->al_dbe, album);
name = g_strdup("");
lower = string_lowercase(name);
album->al_name = name; album->al_name = name;
album->al_lower = lower; album->al_lower = lower;
g_free(name);
g_free(lower); g_free(lower);
return album; return album;
} }
album :: album() : al_year(0), al_name("") {}
static struct db_entry *album_alloc(const gchar *key) static struct db_entry *album_alloc(const gchar *key)
{ {
return __album_from_key(key); struct album *album = new struct album;
gchar *name;
if (sscanf(key, "%u/%m[^\n]", &album->al_year, &name) == 1)
name = g_strdup("");
album = __album_setup(album, name);
g_free(name);
return &album->al_dbe;
} }
static void album_free(struct db_entry *dbe) static void album_free(struct db_entry *dbe)
{ {
delete (struct album *)dbe; delete ALBUM(dbe);
} }
static gchar *album_key(struct db_entry *dbe) static gchar *album_key(struct db_entry *dbe)
{ {
struct album *album = (struct album *)dbe; return __album_key(ALBUM(dbe)->al_name, ALBUM(dbe)->al_year);
return __album_key(album->al_name, album->al_year);
} }
static struct db_entry *album_read(struct file *file) static struct db_entry *album_read(struct file *file)
{ {
gchar *line = file_readl(file); struct album *album = new struct album;
struct album *album = __album_from_key(line); gchar *name, *line;
line = file_readl(file);
if (sscanf(line, "%u %m[^\n]", &album->al_year, &name) == 1)
name = g_strdup("");
album = __album_setup(album, name);
g_free(line); g_free(line);
return album; g_free(name);
return &album->al_dbe;
} }
static void album_write(struct file *file, struct db_entry *dbe) static void album_write(struct file *file, struct db_entry *dbe)
{ {
struct album *album = (struct album *)dbe; file_writef(file, "%u %s", ALBUM(dbe)->al_year, ALBUM(dbe)->al_name.c_str());
file_writef(file, "%u %s", album->al_year, album->al_name.c_str());
} }

View File

@ -130,7 +130,7 @@ static void track_write(struct file *file, struct db_entry *dbe)
struct track *track = (struct track *)dbe; struct track *track = (struct track *)dbe;
file_writef(file, "%u %u %u %u %u ", track->tr_library->dbe_index, file_writef(file, "%u %u %u %u %u ", track->tr_library->dbe_index,
track->tr_artist->dbe_index, track->tr_artist->dbe_index,
track->tr_album->dbe_index, track->tr_album->al_dbe.dbe_index,
track->tr_genre->dbe_index, track->tr_genre->dbe_index,
track->tr_track); track->tr_track);
date_write(file, &track->tr_date); date_write(file, &track->tr_date);

View File

@ -20,15 +20,14 @@ extern "C" {
* ... << year2 << GenericTag::write() * ... << year2 << GenericTag::write()
* ... << year3 << GenericTag::write() * ... << year3 << GenericTag::write()
*/ */
struct album : public db_entry { struct album {
unsigned int al_year; /* This album's year. */ unsigned int al_year; /* This album's year. */
std::string al_name; /* This album's name. */ std::string al_name; /* This album's name. */
std::string al_lower; /* This album's name (lowercased). */ std::string al_lower; /* This album's name (lowercased). */
struct db_entry al_dbe;
album(); /**< Album tag constructor */
}; };
#define ALBUM(dbe) ((struct album *)dbe); #define ALBUM(dbe) ((struct album *)DBE_DATA(dbe))
/* Called to initialize the album database. */ /* Called to initialize the album database. */

View File

@ -10,7 +10,7 @@ static void test_verify_empty(struct album *album)
test_equal(album->al_name, ""); test_equal(album->al_name, "");
test_equal(album->al_lower, ""); test_equal(album->al_lower, "");
test_equal(album->al_year, 0); test_equal(album->al_year, 0);
test_str_equal(album_ops->dbe_key(album), "0 "); test_str_equal(album_ops->dbe_key(&album->al_dbe), "0/");
} }
static void test_verify_hyrule(struct album *album) static void test_verify_hyrule(struct album *album)
@ -19,7 +19,7 @@ static void test_verify_hyrule(struct album *album)
test_equal(album->al_name, "Hyrule Symphony"); test_equal(album->al_name, "Hyrule Symphony");
test_equal(album->al_lower, "hyrule symphony"); test_equal(album->al_lower, "hyrule symphony");
test_equal(album->al_year, 1998); test_equal(album->al_year, 1998);
test_str_equal(album_ops->dbe_key(album), "1998 Hyrule Symphony"); test_str_equal(album_ops->dbe_key(&album->al_dbe), "1998/Hyrule Symphony");
} }
static void test_album() static void test_album()
@ -28,25 +28,25 @@ static void test_album()
struct album *album; struct album *album;
file f; file f;
album = (struct album *)album_ops->dbe_alloc("1998 Hyrule Symphony"); album = ALBUM(album_ops->dbe_alloc("1998/Hyrule Symphony"));
test_verify_hyrule(album); test_verify_hyrule(album);
file_init(&f, "album_tag", 0); file_init(&f, "album_tag", 0);
file_open(&f, OPEN_WRITE); file_open(&f, OPEN_WRITE);
file_writef(&f, "0 \n"); file_writef(&f, "0 \n");
album_ops->dbe_write(&f, album); album_ops->dbe_write(&f, &album->al_dbe);
file_close(&f); file_close(&f);
album_ops->dbe_free(album); album_ops->dbe_free(&album->al_dbe);
file_open(&f, OPEN_READ); file_open(&f, OPEN_READ);
album = (struct album *)album_ops->dbe_read(&f); album = ALBUM(album_ops->dbe_read(&f));
test_verify_empty(album); test_verify_empty(album);
album_ops->dbe_free(album); album_ops->dbe_free(&album->al_dbe);
album = (struct album *)album_ops->dbe_read(&f); album = ALBUM(album_ops->dbe_read(&f));
file_close(&f); file_close(&f);
test_verify_hyrule(album); test_verify_hyrule(album);
album_ops->dbe_free(album); album_ops->dbe_free(&album->al_dbe);
} }
static void test_album_compare() static void test_album_compare()
@ -54,15 +54,15 @@ static void test_album_compare()
const struct db_ops *album_ops = test_album_ops(); const struct db_ops *album_ops = test_album_ops();
struct album *twilight, *skyward; struct album *twilight, *skyward;
twilight = (struct album *)album_ops->dbe_alloc("2006 Twilight Princess"); twilight = ALBUM(album_ops->dbe_alloc("2006/Twilight Princess"));
skyward = (struct album *)album_ops->dbe_alloc("2011 skyward sword"); skyward = ALBUM(album_ops->dbe_alloc("2011/skyward sword"));
test_equal(album_compare(twilight, twilight), 0); test_equal(album_compare(twilight, twilight), 0);
test_equal(album_compare(twilight, skyward), 1); test_equal(album_compare(twilight, skyward), 1);
test_equal(album_compare(skyward, twilight), -1); test_equal(album_compare(skyward, twilight), -1);
album_ops->dbe_free(twilight); album_ops->dbe_free(&twilight->al_dbe);
album_ops->dbe_free(skyward); album_ops->dbe_free(&skyward->al_dbe);
} }
static void test_album_db() static void test_album_db()