core/tags/album: Clean up constructors

Remove an unused constructor and share code between everything left.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-22 10:14:13 -04:00
parent e5b0047812
commit 928c215494
3 changed files with 27 additions and 40 deletions

View File

@ -9,57 +9,48 @@ extern "C" {
static database<album> album_db;
static const std::string make_key(const std::string &name, unsigned int year)
static const std::string __album_key(const std::string &name, unsigned int year)
{
gchar *g_res = g_strdup_printf("%u/%s", year, name.c_str());
gchar *g_res = g_strdup_printf("%u %s", year, name.c_str());
std :: string res = g_res;
g_free(g_res);
return res;
}
album :: album() : al_year(0), al_name("") {}
album :: album(const std::string &name, unsigned int year)
: al_year(year), al_name(name)
{
gchar *lower = string_lowercase(al_name.c_str());
al_lower = lower;
g_free(lower);
}
album :: album(const std::string &key)
static void __album_from_key(struct album *album, const gchar *key)
{
gchar *name, *lower;
sscanf(key.c_str(), "%u/%m[^\n]\n", &al_year, &name);
lower = string_lowercase(name);
al_name = name;
al_lower = lower;
if (sscanf(key, "%u %m[^\n]", &album->al_year, &name) == 1)
name = g_strdup("");
lower = string_lowercase(name);
album->al_name = name;
album->al_lower = lower;
g_free(name);
g_free(lower);
}
album :: album() : al_year(0), al_name("") {}
album :: album(const std::string &key)
{
__album_from_key(this, key.c_str());
}
const std::string album :: primary_key() const
{
return make_key(al_name, al_year);
return __album_key(al_name, al_year);
}
void album :: read(file &file)
{
gchar *name, *lower;
file_readf(&file, "%u", &al_year);
name = file_readl(&file);
lower = string_lowercase(name);
al_name = name;
al_lower = lower;
g_free(name);
g_free(lower);
gchar *line = file_readl(&file);
__album_from_key(this, line);
g_free(line);
}
void album :: write(file &file)
@ -81,7 +72,7 @@ void album_db_deinit()
struct album *album_find(const std::string &name, unsigned int year)
{
return db_find(&album_db, make_key(name, year).c_str());
return db_find(&album_db, __album_key(name, year).c_str());
}
struct album *album_get(const unsigned int index)

View File

@ -26,11 +26,7 @@ struct album : public DatabaseEntry {
/**
* Album tag constructor
*
* @param name Album name
* @param year Album year
*/
album(const std::string &, unsigned int);
album(const std::string &);
/**

View File

@ -9,7 +9,7 @@ static void test_verify_empty(struct album *album)
test_equal(album->al_name, "");
test_equal(album->al_lower, "");
test_equal(album->al_year, 0);
test_equal(album->primary_key(), "0/");
test_equal(album->primary_key(), "0 ");
}
static void test_verify_hyrule(struct album *album)
@ -17,12 +17,12 @@ static void test_verify_hyrule(struct album *album)
test_equal(album->al_name, "Hyrule Symphony");
test_equal(album->al_lower, "hyrule symphony");
test_equal(album->al_year, 1998);
test_equal(album->primary_key(), "1998/Hyrule Symphony");
test_equal(album->primary_key(), "1998 Hyrule Symphony");
}
static void test_album()
{
struct album *album = new struct album("Hyrule Symphony", 1998);
struct album *album = new struct album("1998 Hyrule Symphony");
file f;
test_verify_hyrule(album);
@ -49,8 +49,8 @@ static void test_album()
static void test_album_compare()
{
struct album *twilight = new struct album("Twilight Princess", 2006);
struct album *skyward = new struct album("skyward sword", 2011);
struct album *twilight = new struct album("2006 Twilight Princess");
struct album *skyward = new struct album("2011 skyward sword");
test_equal(album_compare(twilight, twilight), 0);
test_equal(album_compare(twilight, skyward), 1);