ocarina/core/index.cpp

96 lines
1.8 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;
}
const std::string index_entry :: primary_key() const
{
return ie_key;
}
void index_entry :: write(file &file)
{
file_writef(&file, "%s\n" , ie_key.c_str());
set_write(&file, &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_read,
NULL,
};
void index_init(database<index_entry> *index, const gchar *filepath,
bool autosave)
{
db_init(index, filepath, autosave, &index_ops);
}
index_entry *index_insert(database<index_entry> *index, const gchar *key,
unsigned int value)
{
index_entry *it = db_find(index, key);
set_insert(&it->ie_set, value);
db_autosave(index);
return it;
}
void index_remove(database<index_entry> *index, const gchar *key,
unsigned int value)
{
index_entry *it = db_get(index, key);
if (it) {
set_remove(&it->ie_set, value);
db_autosave(index);
}
}
bool index_has(database<index_entry> *index, const gchar *key, unsigned int value)
{
index_entry *it = 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 */