ucx/map.c

changeset 46
48ca036d7d9c
parent 45
dd03226c1a6b
child 48
621a4430c404
     1.1 --- a/ucx/map.c	Thu Oct 04 19:46:10 2012 +0200
     1.2 +++ b/ucx/map.c	Fri Oct 05 10:25:33 2012 +0200
     1.3 @@ -207,7 +207,7 @@
     1.4      return 1;
     1.5  }
     1.6  
     1.7 -int ucx_map_load(UcxMap *map, FILE *f) {
     1.8 +int ucx_map_load_enc(UcxMap *map, FILE *f, copy_func decoder, void* decdata) {
     1.9  
    1.10      int c; int r, n;
    1.11  
    1.12 @@ -267,7 +267,14 @@
    1.13          } while ((c = fgetc(f)) > 0);
    1.14          value[r] = 0;
    1.15          while (value[--r] < 33) value[r] = 0;
    1.16 -        value = realloc(value, r+2);
    1.17 +
    1.18 +        if (decoder == NULL) {
    1.19 +            value = realloc(value, r+2);
    1.20 +        } else {
    1.21 +            void *decoded = decoder(value, decdata);
    1.22 +            free(value);
    1.23 +            value = decoded;
    1.24 +        }
    1.25  
    1.26          ucx_map_cstr_put(map, key, value);
    1.27          free(key);
    1.28 @@ -276,7 +283,7 @@
    1.29      return 0;
    1.30  }
    1.31  
    1.32 -int ucx_map_store(UcxMap *map, FILE *f) {
    1.33 +int ucx_map_store_enc(UcxMap *map, FILE *f, copy_func encoder, void *encdata) {
    1.34      UcxMapIterator iter = ucx_map_iterator(map);
    1.35      char *k, *v;
    1.36      sstr_t key, value;
    1.37 @@ -284,7 +291,12 @@
    1.38  
    1.39      UCX_MAP_FOREACH(v, iter) {
    1.40          k = (char*) iter.cur->key.data;
    1.41 -        key = sstr(k); value = sstr(v);
    1.42 +        key = sstr(k);
    1.43 +        if (encoder == NULL) {
    1.44 +            value = sstr(v);
    1.45 +        } else {
    1.46 +            value = sstr(encoder(v, encdata));
    1.47 +        }
    1.48  
    1.49          written = 0;
    1.50          written += fwrite(key.ptr, 1, key.length, f);
    1.51 @@ -292,6 +304,10 @@
    1.52          written += fwrite(value.ptr, 1, value.length, f);
    1.53          written += fwrite("\n", 1, 1, f);
    1.54  
    1.55 +        if (encoder != NULL) {
    1.56 +            free(value.ptr);
    1.57 +        }
    1.58 +
    1.59          if (written != key.length + value.length + 4) return 1;
    1.60      }
    1.61  

mercurial