ocarina/core/tags/artist.cpp

92 lines
1.7 KiB
C++
Raw Normal View History

/**
* Copyright 2014 (c) Anna Schumaker.
*/
extern "C" {
#include <core/string.h>
}
#include <core/tags/artist.h>
static struct database artist_db;
artist :: artist() {}
static db_entry *artist_alloc(const gchar *name)
{
struct artist *artist = new struct artist;
gchar *lower = string_lowercase(name);
artist->ar_name = name;
artist->ar_lower = lower;
g_free(lower);
return artist;
}
static void artist_free(struct db_entry *dbe)
{
delete (struct artist *)dbe;
}
static gchar *artist_key(struct db_entry *dbe)
{
struct artist *artist = (struct artist *)dbe;
return g_strdup(artist->ar_name.c_str());
}
struct db_entry *artist_read(struct file *file)
{
gchar *name = file_readl(file);
struct artist *artist = (struct artist *)artist_alloc(name);
g_free(name);
return artist;
}
static void artist_write(struct file *file, struct db_entry *dbe)
{
struct artist *artist = (struct artist *)dbe;
file_writef(file, "%s", artist->ar_name.c_str());
}
static const struct db_ops artist_ops = {
artist_alloc,
artist_free,
artist_key,
artist_read,
NULL,
artist_write,
};
void artist_db_init()
{
db_init(&artist_db, "artist.db", true, &artist_ops);
db_load(&artist_db);
}
void artist_db_deinit()
{
db_deinit(&artist_db);
}
struct artist *artist_find(const std::string &name)
{
return ARTIST(db_find(&artist_db, name.c_str()));
}
struct artist *artist_get(const unsigned int index)
{
return ARTIST(db_at(&artist_db, index));
}
int artist_compare(struct artist *lhs, struct artist *rhs)
{
return string_compare(lhs->ar_lower.c_str(), rhs->ar_lower.c_str());
}
#ifdef CONFIG_TESTING
const struct db_ops *test_artist_ops() { return &artist_ops; }
#endif /* CONFIG_TESTING */