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
|
|
|
|
|
|
|
static const std::string make_key(const std::string &name, unsigned int year)
|
|
|
|
{
|
2015-09-01 08:58:34 -04:00
|
|
|
gchar *g_res = g_strdup_printf("%u/%s", year, name.c_str());
|
|
|
|
std :: string res = g_res;
|
|
|
|
|
|
|
|
g_free(g_res);
|
|
|
|
return res;
|
2014-11-16 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-22 09:58:12 -04:00
|
|
|
album :: album() : al_year(0), al_name("") {}
|
2014-11-08 20:53:08 -05:00
|
|
|
|
2015-10-22 08:16:53 -04:00
|
|
|
album :: album(const std::string &name, unsigned int year)
|
2015-10-22 09:58:12 -04:00
|
|
|
: al_year(year), al_name(name)
|
2014-11-08 20:53:08 -05:00
|
|
|
{
|
2015-10-22 09:58:12 -04:00
|
|
|
gchar *lower = string_lowercase(al_name.c_str());
|
|
|
|
al_lower = lower;
|
|
|
|
g_free(lower);
|
2014-11-08 20:53:08 -05:00
|
|
|
}
|
|
|
|
|
2015-10-22 08:16:53 -04:00
|
|
|
album :: album(const std::string &key)
|
2015-10-20 10:10:26 -04:00
|
|
|
{
|
|
|
|
gchar *name, *lower;
|
2015-10-22 09:58:12 -04:00
|
|
|
sscanf(key.c_str(), "%u/%m[^\n]\n", &al_year, &name);
|
2015-10-20 10:10:26 -04:00
|
|
|
|
2015-10-22 09:58:12 -04:00
|
|
|
lower = string_lowercase(name);
|
|
|
|
al_name = name;
|
|
|
|
al_lower = lower;
|
2015-10-20 10:10:26 -04:00
|
|
|
|
|
|
|
g_free(name);
|
|
|
|
g_free(lower);
|
|
|
|
}
|
|
|
|
|
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 09:58:12 -04:00
|
|
|
return make_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 09:58:12 -04:00
|
|
|
gchar *name, *lower;
|
|
|
|
|
2015-10-22 08:16:53 -04:00
|
|
|
file_readf(&file, "%u", &al_year);
|
2015-10-22 09:58:12 -04:00
|
|
|
|
|
|
|
name = file_readl(&file);
|
|
|
|
lower = string_lowercase(name);
|
|
|
|
al_name = name;
|
|
|
|
al_lower = lower;
|
|
|
|
|
|
|
|
g_free(name);
|
|
|
|
g_free(lower);
|
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-09-24 11:18:48 -04:00
|
|
|
void album_db_init()
|
2014-11-16 14:47:51 -05:00
|
|
|
{
|
2015-09-21 09:02:13 -04:00
|
|
|
db_init(&album_db, "album.db", true);
|
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-20 10:10:26 -04:00
|
|
|
return db_find(&album_db, make_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());
|
|
|
|
}
|