55a688cdcc
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
95 lines
2.0 KiB
C
95 lines
2.0 KiB
C
/*
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/index.h>
|
|
|
|
|
|
static struct index_entry *__index_alloc(gchar *key)
|
|
{
|
|
struct index_entry *ent = g_malloc(sizeof(struct index_entry));
|
|
dbe_init(&ent->ie_dbe, ent);
|
|
set_init(&ent->ie_set);
|
|
ent->ie_key = key;
|
|
return ent;
|
|
}
|
|
|
|
static struct db_entry *index_alloc(const gchar *key)
|
|
{
|
|
return &__index_alloc(g_strdup(key))->ie_dbe;
|
|
}
|
|
|
|
static void index_free(struct db_entry *dbe)
|
|
{
|
|
struct index_entry *ent = INDEX_ENTRY(dbe);
|
|
set_deinit(&ent->ie_set);
|
|
g_free(ent);
|
|
}
|
|
|
|
static gchar *index_key(struct db_entry *dbe)
|
|
{
|
|
return INDEX_ENTRY(dbe)->ie_key;
|
|
}
|
|
|
|
static void index_write(struct file *file, struct db_entry *dbe)
|
|
{
|
|
file_writef(file, "%s\n" , INDEX_ENTRY(dbe)->ie_key);
|
|
set_write(file, &INDEX_ENTRY(dbe)->ie_set);
|
|
}
|
|
|
|
static struct db_entry *index_read(struct file *file)
|
|
{
|
|
gchar *key = file_readl(file);
|
|
struct index_entry *ent;
|
|
|
|
ent = __index_alloc(key);
|
|
set_read(file, &ent->ie_set);
|
|
return &ent->ie_dbe;
|
|
}
|
|
|
|
|
|
static const struct db_ops index_ops = {
|
|
.dbe_alloc = index_alloc,
|
|
.dbe_free = index_free,
|
|
.dbe_key = index_key,
|
|
.dbe_read = index_read,
|
|
.dbe_write = index_write,
|
|
};
|
|
|
|
|
|
void index_init(struct database *index, const gchar *filepath, bool autosave)
|
|
{
|
|
db_init(index, filepath, autosave, &index_ops);
|
|
}
|
|
|
|
struct 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 */
|