ucx/map.c

changeset 48
621a4430c404
parent 46
48ca036d7d9c
child 51
1c78cd19fb6b
     1.1 --- a/ucx/map.c	Fri Oct 05 10:25:33 2012 +0200
     1.2 +++ b/ucx/map.c	Fri Oct 05 11:52:53 2012 +0200
     1.3 @@ -207,7 +207,8 @@
     1.4      return 1;
     1.5  }
     1.6  
     1.7 -int ucx_map_load_enc(UcxMap *map, FILE *f, copy_func decoder, void* decdata) {
     1.8 +int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
     1.9 +        ucx_map_coder decoder, void* decdata) {
    1.10  
    1.11      int c; int r, n;
    1.12  
    1.13 @@ -268,12 +269,22 @@
    1.14          value[r] = 0;
    1.15          while (value[--r] < 33) value[r] = 0;
    1.16  
    1.17 -        if (decoder == NULL) {
    1.18 -            value = realloc(value, r+2);
    1.19 -        } else {
    1.20 -            void *decoded = decoder(value, decdata);
    1.21 +        if (decoder) {
    1.22 +            size_t decodedSize;
    1.23 +            void *decoded = decoder(value, decdata, &decodedSize);
    1.24              free(value);
    1.25              value = decoded;
    1.26 +            r = decodedSize;
    1.27 +        } else {
    1.28 +            r += 2;
    1.29 +            value = realloc(value, r);
    1.30 +        }
    1.31 +
    1.32 +        if (allocator.pool) {
    1.33 +            void *pooledValue = allocator.malloc(allocator.pool, r);
    1.34 +            memcpy(pooledValue, value, r);
    1.35 +            free(value);
    1.36 +            value = pooledValue;
    1.37          }
    1.38  
    1.39          ucx_map_cstr_put(map, key, value);
    1.40 @@ -283,7 +294,8 @@
    1.41      return 0;
    1.42  }
    1.43  
    1.44 -int ucx_map_store_enc(UcxMap *map, FILE *f, copy_func encoder, void *encdata) {
    1.45 +int ucx_map_store_enc(UcxMap *map, FILE *f,
    1.46 +        ucx_map_coder encoder, void *encdata) {
    1.47      UcxMapIterator iter = ucx_map_iterator(map);
    1.48      char *k, *v;
    1.49      sstr_t key, value;
    1.50 @@ -292,10 +304,12 @@
    1.51      UCX_MAP_FOREACH(v, iter) {
    1.52          k = (char*) iter.cur->key.data;
    1.53          key = sstr(k);
    1.54 -        if (encoder == NULL) {
    1.55 +        if (encoder) {
    1.56 +            size_t encodedSize;
    1.57 +            void *encoded = encoder(v, encdata, &encodedSize);
    1.58 +            value = sstrn(encoded,encodedSize - 1);
    1.59 +        } else {
    1.60              value = sstr(v);
    1.61 -        } else {
    1.62 -            value = sstr(encoder(v, encdata));
    1.63          }
    1.64  
    1.65          written = 0;
    1.66 @@ -304,7 +318,7 @@
    1.67          written += fwrite(value.ptr, 1, value.length, f);
    1.68          written += fwrite("\n", 1, 1, f);
    1.69  
    1.70 -        if (encoder != NULL) {
    1.71 +        if (encoder) {
    1.72              free(value.ptr);
    1.73          }
    1.74  

mercurial