diff --git a/core/filter.cpp b/core/filter.cpp index 834567e0..11aedb33 100644 --- a/core/filter.cpp +++ b/core/filter.cpp @@ -3,9 +3,9 @@ */ #include -#include extern "C" { +#include #include } diff --git a/core/index.cpp b/core/index.c similarity index 83% rename from core/index.cpp rename to core/index.c index 4fbb9a8b..43909afc 100644 --- a/core/index.cpp +++ b/core/index.c @@ -1,4 +1,4 @@ -/** +/* * Copyright 2014 (c) Anna Schumaker. */ #include @@ -6,9 +6,9 @@ static struct index_entry *__index_alloc(gchar *key) { - struct index_entry *ent = new struct index_entry; + struct index_entry *ent = g_malloc(sizeof(struct index_entry)); dbe_init(&ent->ie_dbe, ent); - ent->ie_set = SET_INIT(); + set_init(&ent->ie_set); ent->ie_key = key; return ent; } @@ -22,7 +22,7 @@ static void index_free(struct db_entry *dbe) { struct index_entry *ent = INDEX_ENTRY(dbe); set_deinit(&ent->ie_set); - delete ent; + g_free(ent); } static gchar *index_key(struct db_entry *dbe) @@ -48,12 +48,11 @@ static struct db_entry *index_read(struct file *file) static const struct db_ops index_ops = { - index_alloc, - index_free, - index_key, - index_read, - NULL, - index_write, + .dbe_alloc = index_alloc, + .dbe_free = index_free, + .dbe_key = index_key, + .dbe_read = index_read, + .dbe_write = index_write, }; @@ -62,8 +61,8 @@ 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 *index_insert(struct database *index, const gchar *key, + unsigned int value) { struct index_entry *it = INDEX_ENTRY(db_find(index, key)); diff --git a/gui/manager.cpp b/gui/manager.cpp index 801a50f0..3ecfce81 100644 --- a/gui/manager.cpp +++ b/gui/manager.cpp @@ -1,6 +1,9 @@ /* * Copyright 2014 (c) Anna Schumaker. */ +extern "C" { +#include +} #include #include #include diff --git a/include/core/index.h b/include/core/index.h index 8ab48b9a..d9622472 100644 --- a/include/core/index.h +++ b/include/core/index.h @@ -1,21 +1,16 @@ -/** +/* * Copyright 2014 (c) Anna Schumaker. + * + * The struct index_entry is used to associate a database key + * with a set of integers, creating an inverted index. */ #ifndef OCARINA_CORE_INDEX_H #define OCARINA_CORE_INDEX_H -extern "C" { #include #include -} - -#include -/** - * The index_entry struct is used to associate a specific key with a set of - * integer identifiers. This lets us use a Database as an inverted index. - */ struct index_entry { gchar *ie_key; struct set ie_set; @@ -29,7 +24,7 @@ struct index_entry { void index_init(struct database *, const gchar *, bool); /* Add a value to an index item with the specified key. */ -index_entry *index_insert(struct database *, const gchar *, unsigned int); +struct index_entry *index_insert(struct database *, const gchar *, unsigned int); /* Remove a value from an index item with the specified key. */ void index_remove(struct database *, const gchar *, unsigned int); @@ -40,4 +35,4 @@ bool index_has(struct database *, const gchar *, unsigned int); #ifdef CONFIG_TESTING const struct db_ops *test_index_ops(); #endif /* CONFIG_TESTING */ -#endif /* OCARINA_CORE_DATABASE_H */ +#endif /* OCARINA_CORE_INDEX_H */ diff --git a/include/core/playlist.h b/include/core/playlist.h index 1082f79f..06fc3fdc 100644 --- a/include/core/playlist.h +++ b/include/core/playlist.h @@ -4,7 +4,9 @@ #ifndef OCARINA_CORE_PLAYLIST_H #define OCARINA_CORE_PLAYLIST_H +extern "C" { #include +} #include #include diff --git a/include/core/set.h b/include/core/set.h index e4a497d5..3a1afa2f 100644 --- a/include/core/set.h +++ b/include/core/set.h @@ -24,6 +24,10 @@ struct set_iter { .s_set = g_hash_table_new(g_direct_hash, g_direct_equal), \ } +static inline void set_init(struct set *set) +{ + set->s_set = g_hash_table_new(g_direct_hash, g_direct_equal); +} static inline void set_deinit(struct set *set) { diff --git a/tests/core/Sconscript b/tests/core/Sconscript index 46b70ada..119ea909 100644 --- a/tests/core/Sconscript +++ b/tests/core/Sconscript @@ -20,7 +20,7 @@ res += [ CoreTest("file", "file.c") ] res += [ CoreTest("date", "date.c") ] res += [ CoreTest("set", "set.c") ] res += [ CoreTest("database", "database.c") ] -res += [ CoreTest("index", "index.cpp") ] +res += [ CoreTest("index", "index.c") ] res += [ CoreTest("filter", "filter.cpp") ] res += [ CoreTest("idle", "idle.cpp") ] diff --git a/tests/core/index.cpp b/tests/core/index.c similarity index 83% rename from tests/core/index.cpp rename to tests/core/index.c index b576ee75..3602f1c4 100644 --- a/tests/core/index.cpp +++ b/tests/core/index.c @@ -3,14 +3,14 @@ * Test a Database */ #include -#include "test.h" +#include static void test_entry() { const struct db_ops *index_ops = test_index_ops(); + struct index_entry *ie; struct set_iter it; - index_entry *ie; unsigned int i; struct file f; @@ -57,11 +57,11 @@ static void test_entry() index_ops->dbe_free(&ie->ie_dbe); } -static void test_stress(unsigned int N) +static void __test_stress(unsigned int N) { const struct db_ops *index_ops = test_index_ops(); + struct index_entry *ie, *ie2; struct database index; - index_entry *ie, *ie2; unsigned int i; gchar key[2] = ""; char c; @@ -72,11 +72,11 @@ static void test_stress(unsigned int N) for (c = 'a'; c <= 'z'; c++) { g_strlcpy(key, &c, 2); ie = index_insert(&index, key, 0); - test_loop_not_equal(ie, NULL, c - 'a'); + test_loop_not_equal((void *)ie, NULL, c - 'a'); test_loop_equal(index_ops->dbe_key(&ie->ie_dbe), key, c - 'a'); for (i = 0; i < N; i++) ie2 = index_insert(&index, key, i); - test_loop_equal(ie, ie2, c - 'a'); + test_loop_equal((void *)ie, (void *)ie2, c - 'a'); test_loop_equal(set_size(&ie->ie_set), N, c - 'a'); test_loop_equal(index.db_size, c - 'a' + 1, c - 'a'); } test_loop_passed(); @@ -86,8 +86,8 @@ static void test_stress(unsigned int N) for (c = 'a'; c <= 'z'; c += 4) { g_strlcpy(key, &c, 2); for (i = 0; i < N; i++) - test_loop_equal(index_has(&index, key, i), true, i); - test_loop_equal(index_has(&index, key, N), false, c - 'a'); + test_loop_equal(index_has(&index, key, i), (bool)true, i); + test_loop_equal(index_has(&index, key, N), (bool)false, c - 'a'); } test_loop_passed(); /* index_remove() */ @@ -96,7 +96,7 @@ static void test_stress(unsigned int N) for (i = 0; i < N; i++) index_remove(&index, key, i); ie = INDEX_ENTRY(db_find(&index, key)); - test_loop_not_equal(ie, NULL, c - 'a'); + test_loop_not_equal((void *)ie, NULL, c - 'a'); test_loop_equal(set_size(&ie->ie_set), 0, c - 'a'); } test_loop_passed(); index_remove(&index, "ZZ", 42); @@ -105,8 +105,8 @@ static void test_stress(unsigned int N) db_deinit(&index); } -static void test_basics() { test_stress(10); } -static void test_stress() { test_stress(100000); } +static void test_basics() { __test_stress(10); } +static void test_stress() { __test_stress(100000); } DECLARE_UNIT_TESTS( UNIT_TEST("Index Entry", test_entry),