1c22ecfda4
We now have an interface for controlling everything through struct db_entry, so let's just have the database store a db_entry directly. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
98 lines
2.0 KiB
C++
98 lines
2.0 KiB
C++
/**
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/index.h>
|
|
|
|
|
|
index_entry :: index_entry()
|
|
{
|
|
ie_set = SET_INIT();
|
|
}
|
|
|
|
static struct db_entry *index_alloc(const gchar *key)
|
|
{
|
|
struct index_entry *ent = new struct index_entry;
|
|
ent->ie_key = key;
|
|
return ent;
|
|
}
|
|
|
|
static void index_free(struct db_entry *dbe)
|
|
{
|
|
struct index_entry *ent = (struct index_entry *)dbe;
|
|
set_deinit(&ent->ie_set);
|
|
delete (struct index_entry *)dbe;
|
|
}
|
|
|
|
static gchar *index_key(struct db_entry *dbe)
|
|
{
|
|
struct index_entry *ent = (struct index_entry *)dbe;
|
|
return g_strdup(ent->ie_key.c_str());
|
|
}
|
|
|
|
static void index_write(struct file *file, struct db_entry *dbe)
|
|
{
|
|
struct index_entry *ent = (struct index_entry *)dbe;
|
|
file_writef(file, "%s\n" , ent->ie_key.c_str());
|
|
set_write(file, &ent->ie_set);
|
|
}
|
|
|
|
static struct db_entry *index_read(struct file *file)
|
|
{
|
|
struct index_entry *ent = new index_entry;
|
|
gchar *key = file_readl(file);
|
|
|
|
ent->ie_key = key;
|
|
g_free(key);
|
|
|
|
set_read(file, &ent->ie_set);
|
|
return ent;
|
|
}
|
|
|
|
|
|
static const struct db_ops index_ops = {
|
|
index_alloc,
|
|
index_free,
|
|
index_key,
|
|
index_read,
|
|
NULL,
|
|
index_write,
|
|
};
|
|
|
|
|
|
void index_init(struct database *index, const gchar *filepath, bool autosave)
|
|
{
|
|
db_init(index, filepath, autosave, &index_ops);
|
|
}
|
|
|
|
index_entry *index_insert(struct database *index, const gchar *key,
|
|
unsigned int value)
|
|
{
|
|
struct index_entry *it = INDEX_ENTRY(db_find(index, key));
|
|
|
|
set_insert(&it->ie_set, value);
|
|
db_autosave(index);
|
|
return it;
|
|
}
|
|
|
|
void index_remove(struct database *index, const gchar *key, unsigned int value)
|
|
{
|
|
struct index_entry *it = INDEX_ENTRY(db_get(index, key));
|
|
if (it) {
|
|
set_remove(&it->ie_set, value);
|
|
db_autosave(index);
|
|
}
|
|
}
|
|
|
|
bool index_has(struct database *index, const gchar *key, unsigned int value)
|
|
{
|
|
struct index_entry *it = INDEX_ENTRY(db_get(index, key));
|
|
|
|
if (!it)
|
|
return false;
|
|
return set_has(&it->ie_set, value);
|
|
}
|
|
|
|
#ifdef CONFIG_TESTING
|
|
const struct db_ops *test_index_ops() { return &index_ops; }
|
|
#endif /* CONFIG_TESTING */
|