2014-11-08 20:53:08 -05:00
|
|
|
/**
|
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2015-10-20 10:10:26 -04:00
|
|
|
extern "C" {
|
2015-01-27 14:02:47 -05:00
|
|
|
#include <core/string.h>
|
2015-10-20 10:10:26 -04:00
|
|
|
}
|
2014-11-08 20:53:08 -05:00
|
|
|
#include <core/tags/album.h>
|
|
|
|
|
2014-11-16 14:47:51 -05:00
|
|
|
|
2015-10-22 08:16:53 -04:00
|
|
|
static database<album> album_db;
|
2014-11-16 14:47:51 -05:00
|
|
|
|
2015-10-22 10:14:13 -04:00
|
|
|
static const std::string __album_key(const std::string &name, unsigned int year)
|
2014-11-16 14:47:51 -05:00
|
|
|
{
|
2015-10-22 10:14:13 -04:00
|
|
|
gchar *g_res = g_strdup_printf("%u %s", year, name.c_str());
|
2015-09-01 08:58:34 -04:00
|
|
|
std :: string res = g_res;
|
|
|
|
|
|
|
|
g_free(g_res);
|
|
|
|
return res;
|
2014-11-16 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2015-10-22 10:14:13 -04:00
|
|
|
static void __album_from_key(struct album *album, const gchar *key)
|
|
|
|
{
|
|
|
|
gchar *name, *lower;
|
2014-11-16 14:47:51 -05:00
|
|
|
|
2015-10-22 10:14:13 -04:00
|
|
|
if (sscanf(key, "%u %m[^\n]", &album->al_year, &name) == 1)
|
|
|
|
name = g_strdup("");
|
2014-11-08 20:53:08 -05:00
|
|
|
|
2015-10-22 10:14:13 -04:00
|
|
|
lower = string_lowercase(name);
|
|
|
|
album->al_name = name;
|
|
|
|
album->al_lower = lower;
|
|
|
|
|
|
|
|
g_free(name);
|
2015-10-22 09:58:12 -04:00
|
|
|
g_free(lower);
|
2014-11-08 20:53:08 -05:00
|
|
|
}
|
|
|
|
|
2015-10-20 10:10:26 -04:00
|
|
|
|
2015-10-22 10:14:13 -04:00
|
|
|
album :: album() : al_year(0), al_name("") {}
|
2015-10-20 10:10:26 -04:00
|
|
|
|
2015-11-02 16:59:55 -05:00
|
|
|
static struct db_entry *album_alloc(const gchar *key)
|
2015-10-22 10:14:13 -04:00
|
|
|
{
|
2015-11-02 16:59:55 -05:00
|
|
|
struct album *album = new struct album;
|
|
|
|
__album_from_key(album, key);
|
|
|
|
return album;
|
2015-10-20 10:10:26 -04:00
|
|
|
}
|
|
|
|
|
2015-10-22 08:16:53 -04:00
|
|
|
const std::string album :: primary_key() const
|
2014-11-08 20:53:08 -05:00
|
|
|
{
|
2015-10-22 10:14:13 -04:00
|
|
|
return __album_key(al_name, al_year);
|
2014-11-08 20:53:08 -05:00
|
|
|
}
|
|
|
|
|
2015-10-22 08:16:53 -04:00
|
|
|
void album :: read(file &file)
|
2014-11-08 20:53:08 -05:00
|
|
|
{
|
2015-10-22 10:14:13 -04:00
|
|
|
gchar *line = file_readl(&file);
|
|
|
|
__album_from_key(this, line);
|
|
|
|
g_free(line);
|
2014-11-08 20:53:08 -05:00
|
|
|
}
|
|
|
|
|
2015-10-22 08:16:53 -04:00
|
|
|
void album :: write(file &file)
|
2014-11-08 20:53:08 -05:00
|
|
|
{
|
2015-10-22 09:58:12 -04:00
|
|
|
file_writef(&file, "%u %s", al_year, al_name.c_str());
|
2014-11-08 20:53:08 -05:00
|
|
|
}
|
|
|
|
|
2014-11-16 14:47:51 -05:00
|
|
|
|
2015-11-03 10:39:06 -05:00
|
|
|
static const struct db_ops album_ops = {
|
2015-11-02 16:59:55 -05:00
|
|
|
album_alloc,
|
2015-11-03 10:39:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-09-24 11:18:48 -04:00
|
|
|
void album_db_init()
|
2014-11-16 14:47:51 -05:00
|
|
|
{
|
2015-11-03 10:39:06 -05:00
|
|
|
db_init(&album_db, "album.db", true, &album_ops);
|
2015-09-17 08:12:25 -04:00
|
|
|
db_load(&album_db);
|
2014-11-16 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2015-09-24 11:18:48 -04:00
|
|
|
void album_db_deinit()
|
|
|
|
{
|
|
|
|
db_deinit(&album_db);
|
|
|
|
}
|
|
|
|
|
2015-09-24 11:13:12 -04:00
|
|
|
struct album *album_find(const std::string &name, unsigned int year)
|
2014-11-16 14:47:51 -05:00
|
|
|
{
|
2015-10-22 10:14:13 -04:00
|
|
|
return db_find(&album_db, __album_key(name, year).c_str());
|
2014-11-16 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2015-09-24 11:09:59 -04:00
|
|
|
struct album *album_get(const unsigned int index)
|
2014-11-16 14:47:51 -05:00
|
|
|
{
|
2015-10-20 08:19:47 -04:00
|
|
|
return db_at(&album_db, index);
|
2014-11-16 14:47:51 -05:00
|
|
|
}
|
2015-10-22 09:58:12 -04:00
|
|
|
|
|
|
|
int album_compare(struct album *lhs, struct album *rhs)
|
|
|
|
{
|
|
|
|
return string_compare(lhs->al_lower.c_str(), rhs->al_lower.c_str());
|
|
|
|
}
|
2015-11-03 10:39:06 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_TESTING
|
|
|
|
const struct db_ops *test_album_ops() { return &album_ops; }
|
|
|
|
#endif /* CONFIG_TESTING */
|