ocarina/core/tags/library.cpp

114 lines
2.1 KiB
C++
Raw Normal View History

/**
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/tags/library.h>
static database<struct library> 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;
library->li_size = 0;
library->li_enabled = true;
library->li_path = path;
return library;
}
static void library_free(struct db_entry *dbe)
{
delete (struct 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());
}
static struct db_entry *library_read(struct file *file)
{
struct library *library = new struct library;
gchar *path;
int enabled;
file_readf(file, "%d\n", &enabled);
library->li_enabled = enabled;
path = file_readl(file);
library->li_path = path;
g_free(path);
return library;
}
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());
}
static const struct db_ops library_ops = {
library_alloc,
library_free,
library_key,
library_read,
NULL,
library_write,
};
void library_db_init()
{
db_init(&library_db, "library.db", true, &library_ops);
db_load(&library_db);
}
void library_db_deinit()
{
db_deinit(&library_db);
}
const database<struct library> *library_db_get()
{
return &library_db;
}
struct library *library_find(const std::string &path)
{
return db_find(&library_db, path.c_str());
}
struct library *library_get(const unsigned int index)
{
return db_at(&library_db, index);
}
void library_remove(struct library *library)
{
if (library)
db_remove(&library_db, library);
}
void library_set_enabled(struct library *library, bool enabled)
{
library->li_enabled = enabled;
db_save(&library_db);
}
std::string library_file(struct library *library, const std::string &path)
{
return library->li_path + "/" + path;
}
#ifdef CONFIG_TESTING
const struct db_ops *test_library_ops() { return &library_ops; }
#endif /* CONFIG_TESTING */