ocarina/core/tags/album.cpp

98 lines
1.8 KiB
C++
Raw Normal View History

/**
* Copyright 2014 (c) Anna Schumaker.
*/
extern "C" {
#include <core/string.h>
}
#include <core/tags/album.h>
static database<album> album_db;
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());
std :: string res = g_res;
g_free(g_res);
return res;
}
static void __album_from_key(struct album *album, const gchar *key)
{
gchar *name, *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("") {}
static struct db_entry *album_alloc(const gchar *key)
{
struct album *album = new struct album;
__album_from_key(album, key);
return album;
}
const std::string album :: primary_key() const
{
return __album_key(al_name, al_year);
}
void album :: read(file &file)
{
gchar *line = file_readl(&file);
__album_from_key(this, line);
g_free(line);
}
void album :: write(file &file)
{
file_writef(&file, "%u %s", al_year, al_name.c_str());
}
static const struct db_ops album_ops = {
album_alloc,
};
void album_db_init()
{
db_init(&album_db, "album.db", true, &album_ops);
db_load(&album_db);
}
void album_db_deinit()
{
db_deinit(&album_db);
}
struct album *album_find(const std::string &name, unsigned int year)
{
return db_find(&album_db, __album_key(name, year).c_str());
}
struct album *album_get(const unsigned int index)
{
return db_at(&album_db, index);
}
int album_compare(struct album *lhs, struct album *rhs)
{
return string_compare(lhs->al_lower.c_str(), rhs->al_lower.c_str());
}
#ifdef CONFIG_TESTING
const struct db_ops *test_album_ops() { return &album_ops; }
#endif /* CONFIG_TESTING */