From 928c215494fb1f2220b6418a823507c8b7381b2f Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 22 Oct 2015 10:14:13 -0400 Subject: [PATCH] core/tags/album: Clean up constructors Remove an unused constructor and share code between everything left. Signed-off-by: Anna Schumaker --- core/tags/album.cpp | 53 ++++++++++++++++----------------------- include/core/tags/album.h | 4 --- tests/core/tags/album.cpp | 10 ++++---- 3 files changed, 27 insertions(+), 40 deletions(-) diff --git a/core/tags/album.cpp b/core/tags/album.cpp index 7e1c480a..863ba521 100644 --- a/core/tags/album.cpp +++ b/core/tags/album.cpp @@ -9,57 +9,48 @@ extern "C" { static database 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) diff --git a/include/core/tags/album.h b/include/core/tags/album.h index 3f2e3f42..76a8192e 100644 --- a/include/core/tags/album.h +++ b/include/core/tags/album.h @@ -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 &); /** diff --git a/tests/core/tags/album.cpp b/tests/core/tags/album.cpp index 9a1da6ee..a94749ef 100644 --- a/tests/core/tags/album.cpp +++ b/tests/core/tags/album.cpp @@ -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);