Permalink
Browse files

Add iterator support to lt_variant_db_t

  • Loading branch information...
1 parent eaeafdf commit d9ec445eb81fa8ad64bb5a3eea694949301db582 @tagoh committed Oct 29, 2012
Showing with 73 additions and 9 deletions.
  1. +50 −7 liblangtag/lt-variant-db.c
  2. +23 −2 tests/variant-db.c
@@ -18,6 +18,7 @@
#include <string.h>
#include <libxml/xpath.h>
#include "lt-error.h"
+#include "lt-iter-private.h"
#include "lt-variant.h"
#include "lt-variant-private.h"
#include "lt-list.h"
@@ -38,10 +39,14 @@
* registered with IANA.
*/
struct _lt_variant_db_t {
- lt_mem_t parent;
- lt_xml_t *xml;
- lt_trie_t *variant_entries;
+ lt_iter_tmpl_t parent;
+ lt_xml_t *xml;
+ lt_trie_t *variant_entries;
};
+typedef struct _lt_variant_db_iter_t {
+ lt_iter_t parent;
+ lt_iter_t *iter;
+} lt_variant_db_iter_t;
/*< private >*/
static lt_bool_t
@@ -178,6 +183,42 @@ lt_variant_db_parse(lt_variant_db_t *variantdb,
return retval;
}
+static lt_iter_t *
+_lt_variant_db_iter_init(lt_iter_tmpl_t *tmpl)
+{
+ lt_variant_db_iter_t *retval;
+ lt_variant_db_t *db = (lt_variant_db_t *)tmpl;
+
+ retval = malloc(sizeof (lt_variant_db_iter_t));
+ if (retval) {
+ retval->iter = lt_iter_init((lt_iter_tmpl_t *)db->variant_entries);
+ if (!retval->iter) {
+ free(retval);
+ retval = NULL;
+ }
+ }
+
+ return &retval->parent;
+}
+
+static void
+_lt_variant_db_iter_fini(lt_iter_t *iter)
+{
+ lt_variant_db_iter_t *db_iter = (lt_variant_db_iter_t *)iter;
+
+ lt_iter_finish(db_iter->iter);
+}
+
+static lt_bool_t
+_lt_variant_db_iter_next(lt_iter_t *iter,
+ lt_pointer_t *key,
+ lt_pointer_t *val)
+{
+ lt_variant_db_iter_t *db_iter = (lt_variant_db_iter_t *)iter;
+
+ return lt_iter_next(db_iter->iter, key, val);
+}
+
/*< public >*/
/**
* lt_variant_db_new:
@@ -195,8 +236,10 @@ lt_variant_db_new(void)
lt_error_t *err = NULL;
lt_variant_t *le;
+ LT_ITER_TMPL_INIT (&retval->parent, _lt_variant_db);
+
retval->variant_entries = lt_trie_new();
- lt_mem_add_ref(&retval->parent, retval->variant_entries,
+ lt_mem_add_ref((lt_mem_t *)retval, retval->variant_entries,
(lt_destroy_func_t)lt_trie_unref);
le = lt_variant_create();
@@ -220,7 +263,7 @@ lt_variant_db_new(void)
retval = NULL;
goto bail;
}
- lt_mem_add_ref(&retval->parent, retval->xml,
+ lt_mem_add_ref((lt_mem_t *)retval, retval->xml,
(lt_destroy_func_t)lt_xml_unref);
lt_variant_db_parse(retval, &err);
@@ -249,7 +292,7 @@ lt_variant_db_ref(lt_variant_db_t *variantdb)
{
lt_return_val_if_fail (variantdb != NULL, NULL);
- return lt_mem_ref(&variantdb->parent);
+ return lt_mem_ref((lt_mem_t *)variantdb);
}
/**
@@ -263,7 +306,7 @@ void
lt_variant_db_unref(lt_variant_db_t *variantdb)
{
if (variantdb)
- lt_mem_unref(&variantdb->parent);
+ lt_mem_unref((lt_mem_t *)variantdb);
}
/**
View
@@ -24,15 +24,36 @@ main(int argc,
char **argv)
{
lt_variant_db_t *variantdb;
+ lt_variant_t *variant;
setlocale(LC_ALL, "");
lt_db_set_datadir(TEST_DATADIR);
variantdb = lt_variant_db_new();
- if (lt_strcmp0(argv[1], "list") == 0) {
+ if (lt_strcmp0(argv[1], "list_keys") == 0) {
+ const char *key;
+ lt_iter_t *iter;
+
+ iter = lt_iter_init((lt_iter_tmpl_t *)variantdb);
+ while (lt_iter_next(iter,
+ (lt_pointer_t *)&key,
+ NULL)) {
+ printf("%s\n", key);
+ }
+ lt_iter_finish(iter);
+ } else if (lt_strcmp0(argv[1], "list_values") == 0) {
+ lt_iter_t *iter;
+
+ iter = lt_iter_init((lt_iter_tmpl_t *)variantdb);
+ while (lt_iter_next(iter,
+ NULL,
+ (lt_pointer_t *)&variant)) {
+ lt_variant_dump(variant);
+ }
+ lt_iter_finish(iter);
} else if (lt_strcmp0(argv[1], "lookup") == 0) {
- lt_variant_t *variant = lt_variant_db_lookup(variantdb, argv[2]);
+ variant = lt_variant_db_lookup(variantdb, argv[2]);
const lt_list_t *prefix, *l;
if (variant) {

0 comments on commit d9ec445

Please sign in to comment.