new map foreach macro

Mon, 15 Jul 2013 14:25:50 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 15 Jul 2013 14:25:50 +0200
changeset 111
c8c59d7f4536
parent 110
1cf71e56f01e
child 112
6384016df2a3

new map foreach macro

test/map_tests.c file | annotate | diff | comparison | revisions
ucx/map.c file | annotate | diff | comparison | revisions
ucx/map.h file | annotate | diff | comparison | revisions
ucx/properties.c file | annotate | diff | comparison | revisions
--- a/test/map_tests.c	Mon Jul 15 13:53:51 2013 +0200
+++ b/test/map_tests.c	Mon Jul 15 14:25:50 2013 +0200
@@ -179,7 +179,7 @@
     int hit = 0;
 
     int* v;
-    UCX_MAP_FOREACH(v, i) {
+    UCX_MAP_FOREACH(key, v, i) {
         check += *v;
         hit++;
     }
@@ -274,7 +274,7 @@
 
     void *d;
     UcxMapIterator iter = ucx_map_iterator(map);
-    UCX_MAP_FOREACH(d, iter) {
+    UCX_MAP_FOREACH(key, d, iter) {
         free(d);
     }
     ucx_map_free(map);
@@ -308,7 +308,7 @@
     UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
     UcxMapIterator iter = ucx_map_iterator(map);
     const char *value; size_t n;
-    UCX_MAP_FOREACH(value, iter) {
+    UCX_MAP_FOREACH(key, value, iter) {
         n = strlen(value);
         UCX_TEST_ASSERT(strncmp((const char*) pool->data[iter.index], value, n),
                 "values of map does not match pooled values");
--- a/ucx/map.c	Mon Jul 15 13:53:51 2013 +0200
+++ b/ucx/map.c	Mon Jul 15 14:25:50 2013 +0200
@@ -88,7 +88,7 @@
         copy_func fnc, void *data) {
     UcxMapIterator i = ucx_map_iterator(from);
     void *value;
-    UCX_MAP_FOREACH(value, i) {
+    UCX_MAP_FOREACH(key, value, i) {
         int ret = ucx_map_put(to, i.cur->key, fnc ? fnc(value, data) : value);
         if(ret != 0) {
             return 1;
@@ -282,7 +282,7 @@
     return i;
 }
 
-int ucx_map_iter_next(UcxMapIterator *i, void **elm) {
+int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm) {
     UcxMapElement *e = i->cur;
     
     if(e == NULL) {
@@ -296,6 +296,7 @@
             if(e->data != NULL) {
                 i->cur = e;
                 *elm = e->data;
+                *key = e->key;
                 return 0;
             }
 
@@ -402,13 +403,12 @@
 int ucx_map_store_enc(UcxMap *map, FILE *f,
         ucx_map_coder encoder, void *encdata) {
     UcxMapIterator iter = ucx_map_iterator(map);
-    char *k, *v;
+    char *v;
     sstr_t key, value;
     size_t written;
 
-    UCX_MAP_FOREACH(v, iter) {
-        k = (char*) iter.cur->key.data;
-        key = sstrn(k, iter.cur->key.len);
+    UCX_MAP_FOREACH(k, v, iter) {
+        key = sstrn(k.data, k.len);
         if (encoder) {
             size_t encodedSize;
             void *encoded = encoder(v, encdata, &encodedSize);
--- a/ucx/map.h	Mon Jul 15 13:53:51 2013 +0200
+++ b/ucx/map.h	Mon Jul 15 14:25:50 2013 +0200
@@ -38,8 +38,8 @@
 extern "C" {
 #endif
 
-#define UCX_MAP_FOREACH(elm,iter) \
-        for(;ucx_map_iter_next(&iter,(void**)&elm)==0;)
+#define UCX_MAP_FOREACH(key,elm,iter) \
+        for(UcxKey key;ucx_map_iter_next(&iter,&key, (void**)&elm)==0;)
 
 typedef struct UcxMap          UcxMap;
 typedef struct UcxKey          UcxKey;
@@ -120,7 +120,7 @@
 
 UcxMapIterator ucx_map_iterator(UcxMap *map);
 
-int ucx_map_iter_next(UcxMapIterator *i, void **elm);
+int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm);
 
 /* use macros for string maps only, values are not encoded */
 #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL)
--- a/ucx/properties.c	Mon Jul 15 13:53:51 2013 +0200
+++ b/ucx/properties.c	Mon Jul 15 14:25:50 2013 +0200
@@ -97,7 +97,7 @@
             parser->tmp = NULL;
             parser->tmpcap = 0;
             parser->tmplen = 0;
-            // run parse with the tmp buffer as main buffer
+            // run ucx_properties_next with the tmp buffer as main buffer
             int ret = ucx_properties_next(parser, name, value);
             
             // restore original buffer
@@ -244,13 +244,13 @@
 
 int ucx_properties_store(UcxMap *map, FILE *file) {
     UcxMapIterator iter = ucx_map_iterator(map);
-    char *k, *v;
+    char *v;
     sstr_t key, value;
     size_t written;
 
-    UCX_MAP_FOREACH(v, iter) {
-        k = (char*) iter.cur->key.data;
-        key = sstrn(k, iter.cur->key.len);
+    UCX_MAP_FOREACH(k, v, iter) {
+        //k = (char*) iter.cur->key.data;
+        key = sstrn(k.data, k.len);
         value = sstr(v);
 
         written = 0;

mercurial