2015-11-11 10:49:23 -05:00
|
|
|
/*
|
2014-11-08 13:43:23 -05:00
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2015-10-22 13:19:09 -04:00
|
|
|
#include <core/string.h>
|
2014-11-08 13:43:23 -05:00
|
|
|
#include <core/tags/artist.h>
|
|
|
|
|
2014-11-16 10:08:12 -05:00
|
|
|
|
2015-11-04 12:23:00 -05:00
|
|
|
static struct database artist_db;
|
2014-11-16 10:08:12 -05:00
|
|
|
|
2015-11-11 10:42:17 -05:00
|
|
|
static struct artist *__artist_alloc(gchar *name)
|
2014-11-08 13:43:23 -05:00
|
|
|
{
|
2015-11-11 10:49:23 -05:00
|
|
|
struct artist *artist = g_malloc(sizeof(struct artist));
|
2015-10-22 13:19:09 -04:00
|
|
|
|
2015-11-11 10:04:03 -05:00
|
|
|
dbe_init(&artist->ar_dbe, artist);
|
2016-05-15 09:40:28 -04:00
|
|
|
artist->ar_name = name;
|
2016-05-19 11:12:13 -04:00
|
|
|
artist->ar_tokens = g_str_tokenize_and_fold(name, NULL, &artist->ar_alts);
|
2016-05-15 09:40:28 -04:00
|
|
|
artist->ar_playlist = NULL;
|
2015-11-11 10:42:17 -05:00
|
|
|
|
|
|
|
return artist;
|
|
|
|
}
|
2015-10-22 13:19:09 -04:00
|
|
|
|
2015-11-11 10:42:17 -05:00
|
|
|
|
2015-11-11 10:49:23 -05:00
|
|
|
static struct db_entry *artist_alloc(const gchar *name)
|
2015-11-11 10:42:17 -05:00
|
|
|
{
|
|
|
|
return &__artist_alloc(g_strdup(name))->ar_dbe;
|
2015-10-22 13:19:09 -04:00
|
|
|
}
|
|
|
|
|
2015-09-30 09:15:15 -04:00
|
|
|
static void artist_free(struct db_entry *dbe)
|
|
|
|
{
|
2016-05-19 11:12:13 -04:00
|
|
|
g_strfreev(ARTIST(dbe)->ar_tokens);
|
|
|
|
g_strfreev(ARTIST(dbe)->ar_alts);
|
2015-11-11 10:49:23 -05:00
|
|
|
g_free(ARTIST(dbe));
|
2015-09-30 09:15:15 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 09:33:55 -04:00
|
|
|
static gchar *artist_key(struct db_entry *dbe)
|
2015-10-22 13:19:09 -04:00
|
|
|
{
|
2015-11-11 10:42:17 -05:00
|
|
|
return ARTIST(dbe)->ar_name;
|
2015-10-22 13:19:09 -04:00
|
|
|
}
|
|
|
|
|
2015-09-22 09:24:19 -04:00
|
|
|
struct db_entry *artist_read(struct file *file)
|
2015-10-22 13:19:09 -04:00
|
|
|
{
|
2015-11-11 10:42:17 -05:00
|
|
|
return &__artist_alloc(file_readl(file))->ar_dbe;
|
2015-10-22 13:19:09 -04:00
|
|
|
}
|
|
|
|
|
2015-09-22 15:54:21 -04:00
|
|
|
static void artist_write(struct file *file, struct db_entry *dbe)
|
2015-10-22 13:19:09 -04:00
|
|
|
{
|
2015-11-11 10:42:17 -05:00
|
|
|
file_writef(file, "%s", ARTIST(dbe)->ar_name);
|
2014-11-08 13:43:23 -05:00
|
|
|
}
|
2014-11-16 10:08:12 -05:00
|
|
|
|
|
|
|
|
2015-11-03 10:39:06 -05:00
|
|
|
static const struct db_ops artist_ops = {
|
2015-11-02 16:59:55 -05:00
|
|
|
artist_alloc,
|
2015-09-30 09:15:15 -04:00
|
|
|
artist_free,
|
2015-09-23 09:33:55 -04:00
|
|
|
artist_key,
|
2015-09-22 09:24:19 -04:00
|
|
|
artist_read,
|
2015-10-05 20:12:32 -04:00
|
|
|
NULL,
|
2015-09-22 15:54:21 -04:00
|
|
|
artist_write,
|
2015-11-03 10:39:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-10-22 13:06:12 -04:00
|
|
|
void artist_db_init()
|
2014-11-16 10:08:12 -05:00
|
|
|
{
|
2015-11-03 10:39:06 -05:00
|
|
|
db_init(&artist_db, "artist.db", true, &artist_ops);
|
2016-02-26 09:39:33 -05:00
|
|
|
db_load_idle(&artist_db);
|
2014-11-16 10:08:12 -05:00
|
|
|
}
|
|
|
|
|
2015-10-22 13:06:12 -04:00
|
|
|
void artist_db_deinit()
|
|
|
|
{
|
|
|
|
db_deinit(&artist_db);
|
|
|
|
}
|
|
|
|
|
2016-05-15 10:17:30 -04:00
|
|
|
const struct database *artist_db_get()
|
|
|
|
{
|
|
|
|
return &artist_db;
|
|
|
|
}
|
|
|
|
|
2015-11-11 10:42:17 -05:00
|
|
|
struct artist *artist_find(const gchar *name)
|
2014-11-16 10:08:12 -05:00
|
|
|
{
|
2015-11-11 10:42:17 -05:00
|
|
|
return ARTIST(db_find(&artist_db, name));
|
2014-11-16 10:08:12 -05:00
|
|
|
}
|
|
|
|
|
2016-05-15 09:47:05 -04:00
|
|
|
struct artist *artist_lookup(const gchar *name)
|
|
|
|
{
|
|
|
|
return ARTIST(db_get(&artist_db, name));
|
|
|
|
}
|
|
|
|
|
2015-10-22 12:01:25 -04:00
|
|
|
struct artist *artist_get(const unsigned int index)
|
2014-11-16 10:08:12 -05:00
|
|
|
{
|
2015-11-04 12:23:00 -05:00
|
|
|
return ARTIST(db_at(&artist_db, index));
|
2014-11-16 10:08:12 -05:00
|
|
|
}
|
2015-10-22 13:19:09 -04:00
|
|
|
|
|
|
|
int artist_compare(struct artist *lhs, struct artist *rhs)
|
|
|
|
{
|
2016-05-19 11:12:13 -04:00
|
|
|
return string_compare_tokens(lhs->ar_tokens, rhs->ar_tokens);
|
2015-10-22 13:19:09 -04:00
|
|
|
}
|
2015-11-03 10:39:06 -05:00
|
|
|
|
2016-05-19 14:31:50 -04:00
|
|
|
bool artist_match_token(struct artist *artist, const gchar *string)
|
|
|
|
{
|
|
|
|
return string_match_token(string, artist->ar_tokens) ||
|
|
|
|
string_match_token(string, artist->ar_alts);
|
|
|
|
}
|
2015-11-03 10:39:06 -05:00
|
|
|
#ifdef CONFIG_TESTING
|
|
|
|
const struct db_ops *test_artist_ops() { return &artist_ops; }
|
|
|
|
#endif /* CONFIG_TESTING */
|