implemented encoder/decoder for map store/load

Fri, 05 Oct 2012 10:25:33 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 05 Oct 2012 10:25:33 +0200
changeset 46
48ca036d7d9c
parent 45
dd03226c1a6b
child 47
22fb97e9f58a
child 48
621a4430c404

implemented encoder/decoder for map store/load

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
     1.1 --- a/test/map_tests.c	Thu Oct 04 19:46:10 2012 +0200
     1.2 +++ b/test/map_tests.c	Fri Oct 05 10:25:33 2012 +0200
     1.3 @@ -141,6 +141,17 @@
     1.4      ucx_map_free(map);
     1.5  }
     1.6  
     1.7 +void* test_ucx_map_store_load_encdec(void *value, void *data) {
     1.8 +    char *string = (char*) value;
     1.9 +    size_t n = strlen(string);
    1.10 +    char *encoded = malloc(n+1);
    1.11 +    for (int i = 0 ; i < n ; i++) {
    1.12 +        encoded[i] = string[n-1-i];
    1.13 +    }
    1.14 +    encoded[n] = 0;
    1.15 +    return encoded;
    1.16 +}
    1.17 +
    1.18  UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
    1.19      UcxMap *map = ucx_map_new(4);
    1.20  
    1.21 @@ -155,14 +166,14 @@
    1.22      int r;
    1.23  
    1.24      fwrite(" # comment test\n", 1, 16, f);
    1.25 -    r = ucx_map_store(map, f);
    1.26 +    r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL);
    1.27      fwrite("!discard this", 1, 13, f);
    1.28  
    1.29      fclose(f);
    1.30      ucx_map_free(map);
    1.31      map = ucx_map_new(1);
    1.32      f = fopen("test_ucx_map_store", "r");
    1.33 -    r += ucx_map_load(map, f);
    1.34 +    r += ucx_map_load_enc(map, f, test_ucx_map_store_load_encdec, NULL);
    1.35  
    1.36      UCX_TEST_BEGIN
    1.37      char *value;
     2.1 --- a/ucx/map.c	Thu Oct 04 19:46:10 2012 +0200
     2.2 +++ b/ucx/map.c	Fri Oct 05 10:25:33 2012 +0200
     2.3 @@ -207,7 +207,7 @@
     2.4      return 1;
     2.5  }
     2.6  
     2.7 -int ucx_map_load(UcxMap *map, FILE *f) {
     2.8 +int ucx_map_load_enc(UcxMap *map, FILE *f, copy_func decoder, void* decdata) {
     2.9  
    2.10      int c; int r, n;
    2.11  
    2.12 @@ -267,7 +267,14 @@
    2.13          } while ((c = fgetc(f)) > 0);
    2.14          value[r] = 0;
    2.15          while (value[--r] < 33) value[r] = 0;
    2.16 -        value = realloc(value, r+2);
    2.17 +
    2.18 +        if (decoder == NULL) {
    2.19 +            value = realloc(value, r+2);
    2.20 +        } else {
    2.21 +            void *decoded = decoder(value, decdata);
    2.22 +            free(value);
    2.23 +            value = decoded;
    2.24 +        }
    2.25  
    2.26          ucx_map_cstr_put(map, key, value);
    2.27          free(key);
    2.28 @@ -276,7 +283,7 @@
    2.29      return 0;
    2.30  }
    2.31  
    2.32 -int ucx_map_store(UcxMap *map, FILE *f) {
    2.33 +int ucx_map_store_enc(UcxMap *map, FILE *f, copy_func encoder, void *encdata) {
    2.34      UcxMapIterator iter = ucx_map_iterator(map);
    2.35      char *k, *v;
    2.36      sstr_t key, value;
    2.37 @@ -284,7 +291,12 @@
    2.38  
    2.39      UCX_MAP_FOREACH(v, iter) {
    2.40          k = (char*) iter.cur->key.data;
    2.41 -        key = sstr(k); value = sstr(v);
    2.42 +        key = sstr(k);
    2.43 +        if (encoder == NULL) {
    2.44 +            value = sstr(v);
    2.45 +        } else {
    2.46 +            value = sstr(encoder(v, encdata));
    2.47 +        }
    2.48  
    2.49          written = 0;
    2.50          written += fwrite(key.ptr, 1, key.length, f);
    2.51 @@ -292,6 +304,10 @@
    2.52          written += fwrite(value.ptr, 1, value.length, f);
    2.53          written += fwrite("\n", 1, 1, f);
    2.54  
    2.55 +        if (encoder != NULL) {
    2.56 +            free(value.ptr);
    2.57 +        }
    2.58 +
    2.59          if (written != key.length + value.length + 4) return 1;
    2.60      }
    2.61  
     3.1 --- a/ucx/map.h	Thu Oct 04 19:46:10 2012 +0200
     3.2 +++ b/ucx/map.h	Fri Oct 05 10:25:33 2012 +0200
     3.3 @@ -66,14 +66,13 @@
     3.4  
     3.5  int ucx_map_iter_next(UcxMapIterator *i, void **elm);
     3.6  
     3.7 -/* use for string maps only, values are not encoded */
     3.8 -int ucx_map_load(UcxMap *map, FILE *f);
     3.9 -int ucx_map_store(UcxMap *map, FILE *f);
    3.10 +/* use macros for string maps only, values are not encoded */
    3.11 +#define ucx_map_load(map, f) ucx_map_load_enc(map, f, NULL, NULL)
    3.12 +#define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL)
    3.13  
    3.14 -/* TODO:
    3.15  int ucx_map_load_enc(UcxMap *map, FILE *f, copy_func decoder, void* decdata);
    3.16 +/* encoders shall provide null terminated strings*/
    3.17  int ucx_map_store_enc(UcxMap *map, FILE *f, copy_func encoder, void* encdata);
    3.18 - */
    3.19  
    3.20  #ifdef	__cplusplus
    3.21  }

mercurial