/** * Copyright 2014 (c) Anna Schumaker. */ extern "C" { #include } #include static struct database album_db; static gchar *__album_key(const std::string &name, unsigned int year) { return g_strdup_printf("%u/%s", year, name.c_str()); } static struct album *__album_setup(struct album *album, const gchar *name) { gchar *lower = string_lowercase(name); dbe_init(&album->al_dbe, album); album->al_name = name; album->al_lower = lower; g_free(lower); return album; } static struct db_entry *album_alloc(const gchar *key) { struct album *album = new struct album; gchar *name; if (sscanf(key, "%u/%m[^\n]", &album->al_year, &name) == 1) name = g_strdup(""); album = __album_setup(album, name); g_free(name); return &album->al_dbe; } static void album_free(struct db_entry *dbe) { delete ALBUM(dbe); } static gchar *album_key(struct db_entry *dbe) { return __album_key(ALBUM(dbe)->al_name, ALBUM(dbe)->al_year); } static struct db_entry *album_read(struct file *file) { struct album *album = new struct album; gchar *name, *line; line = file_readl(file); if (sscanf(line, "%u %m[^\n]", &album->al_year, &name) == 1) name = g_strdup(""); album = __album_setup(album, name); g_free(line); g_free(name); return &album->al_dbe; } static void album_write(struct file *file, struct db_entry *dbe) { file_writef(file, "%u %s", ALBUM(dbe)->al_year, ALBUM(dbe)->al_name.c_str()); } static const struct db_ops album_ops = { album_alloc, album_free, album_key, 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) { gchar *key = __album_key(name, year); struct album *album = ALBUM(db_find(&album_db, key)); g_free(key); return album; } struct album *album_get(const unsigned int index) { return ALBUM(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 */