/** * Copyright 2014 (c) Anna Schumaker. */ extern "C" { #include } #include static database album_db; static const std::string __album_key(const std::string &name, unsigned int year) { gchar *g_res = g_strdup_printf("%u %s", year, name.c_str()); std :: string res = g_res; g_free(g_res); return res; } static struct album *__album_from_key(const gchar *key) { struct album *album = new struct album; gchar *name, *lower; if (sscanf(key, "%u %m[^\n]", &album->al_year, &name) == 1) name = g_strdup(""); lower = string_lowercase(name); album->al_name = name; album->al_lower = lower; g_free(name); g_free(lower); return album; } album :: album() : al_year(0), al_name("") {} static struct db_entry *album_alloc(const gchar *key) { return __album_from_key(key); } static void album_free(struct db_entry *dbe) { delete (struct album *)dbe; } const std::string album :: primary_key() const { return __album_key(al_name, al_year); } static struct db_entry *album_read(struct file *file) { gchar *line = file_readl(file); struct album *album = __album_from_key(line); g_free(line); return album; } static void album_write(struct file *file, struct db_entry *dbe) { struct album *album = (struct album *)dbe; file_writef(file, "%u %s", album->al_year, album->al_name.c_str()); } static const struct db_ops album_ops = { album_alloc, album_free, album_read, NULL, album_write, }; void album_db_init() { db_init(&album_db, "album.db", true, &album_ops); db_load(&album_db); } void album_db_deinit() { db_deinit(&album_db); } struct album *album_find(const std::string &name, unsigned int year) { return db_find(&album_db, __album_key(name, year).c_str()); } struct album *album_get(const unsigned int index) { return db_at(&album_db, index); } int album_compare(struct album *lhs, struct album *rhs) { return string_compare(lhs->al_lower.c_str(), rhs->al_lower.c_str()); } #ifdef CONFIG_TESTING const struct db_ops *test_album_ops() { return &album_ops; } #endif /* CONFIG_TESTING */