20e0a85a5d
This will be called to scan the artist database when setting up playlists. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
99 lines
1.8 KiB
C
99 lines
1.8 KiB
C
/*
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/string.h>
|
|
#include <core/tags/artist.h>
|
|
|
|
|
|
static struct database artist_db;
|
|
|
|
static struct artist *__artist_alloc(gchar *name)
|
|
{
|
|
struct artist *artist = g_malloc(sizeof(struct artist));
|
|
|
|
dbe_init(&artist->ar_dbe, artist);
|
|
artist->ar_name = name;
|
|
artist->ar_lower = string_lowercase(name);
|
|
artist->ar_playlist = NULL;
|
|
|
|
return artist;
|
|
}
|
|
|
|
|
|
static struct db_entry *artist_alloc(const gchar *name)
|
|
{
|
|
return &__artist_alloc(g_strdup(name))->ar_dbe;
|
|
}
|
|
|
|
static void artist_free(struct db_entry *dbe)
|
|
{
|
|
g_free(ARTIST(dbe)->ar_lower);
|
|
g_free(ARTIST(dbe));
|
|
}
|
|
|
|
static gchar *artist_key(struct db_entry *dbe)
|
|
{
|
|
return ARTIST(dbe)->ar_name;
|
|
}
|
|
|
|
struct db_entry *artist_read(struct file *file)
|
|
{
|
|
return &__artist_alloc(file_readl(file))->ar_dbe;
|
|
}
|
|
|
|
static void artist_write(struct file *file, struct db_entry *dbe)
|
|
{
|
|
file_writef(file, "%s", ARTIST(dbe)->ar_name);
|
|
}
|
|
|
|
|
|
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_idle(&artist_db);
|
|
}
|
|
|
|
void artist_db_deinit()
|
|
{
|
|
db_deinit(&artist_db);
|
|
}
|
|
|
|
const struct database *artist_db_get()
|
|
{
|
|
return &artist_db;
|
|
}
|
|
|
|
struct artist *artist_find(const gchar *name)
|
|
{
|
|
return ARTIST(db_find(&artist_db, name));
|
|
}
|
|
|
|
struct artist *artist_lookup(const gchar *name)
|
|
{
|
|
return ARTIST(db_get(&artist_db, name));
|
|
}
|
|
|
|
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, rhs->ar_lower);
|
|
}
|
|
|
|
#ifdef CONFIG_TESTING
|
|
const struct db_ops *test_artist_ops() { return &artist_ops; }
|
|
#endif /* CONFIG_TESTING */
|