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

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-11-11 16:53:56 -05:00
parent 2755e8a09b
commit 5cc4efb6ac
5 changed files with 31 additions and 33 deletions

View File

@ -6,30 +6,25 @@
static struct database library_db;
library :: library()
: li_size(0), li_enabled(false)
{
}
static struct db_entry *library_alloc(const gchar *path)
{
struct library *library = new struct library;
dbe_init(&library->li_dbe, library);
library->li_size = 0;
library->li_enabled = true;
library->li_path = path;
return library;
return &library->li_dbe;
}
static void library_free(struct db_entry *dbe)
{
delete (struct library *)dbe;
delete LIBRARY(dbe);
}
static gchar *library_key(struct db_entry *dbe)
{
struct library *library = (struct library *)dbe;
return g_strdup_printf(library->li_path.c_str());
return g_strdup_printf(LIBRARY(dbe)->li_path.c_str());
}
static struct db_entry *library_read(struct file *file)
@ -38,6 +33,9 @@ static struct db_entry *library_read(struct file *file)
gchar *path;
int enabled;
dbe_init(&library->li_dbe, library);
library->li_size = 0;
file_readf(file, "%d\n", &enabled);
library->li_enabled = enabled;
@ -45,13 +43,13 @@ static struct db_entry *library_read(struct file *file)
library->li_path = path;
g_free(path);
return library;
return &library->li_dbe;
}
static void library_write(struct file *file, struct db_entry *dbe)
{
struct library *library = (struct library *)dbe;
file_writef(file, "%d %s", library->li_enabled, library->li_path.c_str());
file_writef(file, "%d %s", LIBRARY(dbe)->li_enabled,
LIBRARY(dbe)->li_path.c_str());
}
@ -94,7 +92,7 @@ struct library *library_get(const unsigned int index)
void library_remove(struct library *library)
{
if (library)
db_remove(&library_db, library);
db_remove(&library_db, &library->li_dbe);
}
void library_set_enabled(struct library *library, bool enabled)

View File

@ -17,7 +17,7 @@ static gchar *__track_key(struct library *library, const std::string &path)
{
if (!library)
return g_strdup("");
return g_strdup_printf("%u/%s", library->dbe_index, path.c_str());
return g_strdup_printf("%u/%s", library->li_dbe.dbe_index, path.c_str());
}
track :: track()
@ -128,7 +128,7 @@ static struct db_entry *track_read(struct file *file)
static void track_write(struct file *file, struct db_entry *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->li_dbe.dbe_index,
track->tr_artist->ar_dbe.dbe_index,
track->tr_album->al_dbe.dbe_index,
track->tr_genre->ge_dbe.dbe_index,

View File

@ -38,7 +38,7 @@ static void list_path(struct library *lib)
if (lib) {
row = *(c_list->append());
row[c_cols.c_id] = lib->dbe_index;
row[c_cols.c_id] = lib->li_dbe.dbe_index;
row[c_cols.c_enabled] = lib->li_enabled;
row[c_cols.c_size] = lib->li_size;
row[c_cols.c_path] = lib->li_path;

View File

@ -20,15 +20,14 @@ extern "C" {
* ... << enabled2 << path2
* ... << enabled3 << path3
*/
struct library : public db_entry {
struct library {
unsigned int li_size; /* This library's track count. */
bool li_enabled;/* True if this library is enabled. */
std::string li_path; /* This library's root path. */
library(); /**< Library tag constructor. */
struct db_entry li_dbe;
};
#define LIBRARY(dbe) ((struct library *)dbe)
#define LIBRARY(dbe) ((struct library *)DBE_DATA(dbe))
/* Called to initialize the library database. */

View File

@ -9,7 +9,7 @@ static void test_verify_zelda(struct library *library)
const struct db_ops *library_ops = test_library_ops();
test_equal(library->li_enabled, true);
test_equal(library->li_size, 0);
test_str_equal(library_ops->dbe_key(library), "/home/Zelda/Music");
test_str_equal(library_ops->dbe_key(&library->li_dbe), "/home/Zelda/Music");
}
static void test_verify_link(struct library *library)
@ -17,7 +17,7 @@ static void test_verify_link(struct library *library)
const struct db_ops *library_ops = test_library_ops();
test_equal(library->li_enabled, false);
test_equal(library->li_size, 0);
test_str_equal(library_ops->dbe_key(library), "/home/Link/Music");
test_str_equal(library_ops->dbe_key(&library->li_dbe), "/home/Link/Music");
}
static void test_library()
@ -26,8 +26,8 @@ static void test_library()
struct library *link, *zelda, *library;
file f;
link = (struct library *)library_ops->dbe_alloc("/home/Link/Music");
zelda = (struct library *)library_ops->dbe_alloc("/home/Zelda/Music");
link = LIBRARY(library_ops->dbe_alloc("/home/Link/Music"));
zelda = LIBRARY(library_ops->dbe_alloc("/home/Zelda/Music"));
library_set_enabled(link, false);
test_verify_link(link);
@ -38,25 +38,25 @@ static void test_library()
file_init(&f, "library_tag", 0);
file_open(&f, OPEN_WRITE);
library_ops->dbe_write(&f, link);
library_ops->dbe_write(&f, &link->li_dbe);
file_writef(&f, "\n");
library_ops->dbe_write(&f, zelda);
library_ops->dbe_write(&f, &zelda->li_dbe);
file_close(&f);
file_open(&f, OPEN_READ);
library = (struct library *)library_ops->dbe_read(&f);
library = LIBRARY(library_ops->dbe_read(&f));
test_verify_link(library);
test_equal(library_file(library, "navi.mp3"), "/home/Link/Music/navi.mp3");
library_ops->dbe_free(library);
library_ops->dbe_free(&library->li_dbe);
library = (struct library *)library_ops->dbe_read(&f);
library = LIBRARY(library_ops->dbe_read(&f));
file_close(&f);
test_verify_zelda(library);
test_equal(library_file(library, "impa.ogg"), "/home/Zelda/Music/impa.ogg");
library_ops->dbe_free(link);
library_ops->dbe_free(zelda);
library_ops->dbe_free(library);
library_ops->dbe_free(&link->li_dbe);
library_ops->dbe_free(&zelda->li_dbe);
library_ops->dbe_free(&library->li_dbe);
}
static void test_library_db()
@ -95,6 +95,7 @@ static void test_library_db()
library_db.db_entries = g_ptr_array_new();
db_load(&library_db);
test_equal(library_db.db_size, 0);
g_ptr_array_free(library_db.db_entries, true);
}
DECLARE_UNIT_TESTS(