ocarina/core/tags/library.cpp

99 lines
1.7 KiB
C++

/**
* 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;
}
const std::string library :: primary_key() const
{
return li_path;
}
void library :: read(file &file)
{
int enabled;
gchar *path;
file_readf(&file, "%d\n", &enabled);
li_enabled = enabled;
path = file_readl(&file);
li_path = path;
g_free(path);
}
void library :: write(file &file)
{
file_writef(&file, "%d %s", li_enabled, li_path.c_str());
}
static const struct db_ops library_ops = {
library_alloc,
};
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 */