ocarina/core/tags/album.cpp

110 lines
2.1 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 gchar *__album_key(const std::string &name, unsigned int year)
{
return g_strdup_printf("%u %s", year, name.c_str());
}
static struct album *__album_from_key(const gchar *key)
{
struct album *album = new struct album;
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);
return album;
}
album :: album() : al_year(0), al_name("") {}
static struct db_entry *album_alloc(const gchar *key)
{
return __album_from_key(key);
}
static void album_free(struct db_entry *dbe)
{
delete (struct album *)dbe;
}
static gchar *album_key(struct db_entry *dbe)
{
struct album *album = (struct album *)dbe;
return __album_key(album->al_name, album->al_year);
}
static struct db_entry *album_read(struct file *file)
{
gchar *line = file_readl(file);
struct album *album = __album_from_key(line);
g_free(line);
return album;
}
static void album_write(struct file *file, struct db_entry *dbe)
{
struct album *album = (struct album *)dbe;
file_writef(file, "%u %s", album->al_year, album->al_name.c_str());
}
static const struct db_ops album_ops = {
album_alloc,
album_free,
album_key,
album_read,
NULL,
album_write,
};
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)
{
gchar *key = __album_key(name, year);
struct album *album = db_find(&album_db, key);
g_free(key);
return album;
}
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 */