# HG changeset patch # User Mike Becker # Date 1349425533 -7200 # Node ID 48ca036d7d9caca1e6a1b22cb612a0ae870ac550 # Parent dd03226c1a6bcd9a55fa2e2b919dd07164bee87e implemented encoder/decoder for map store/load diff -r dd03226c1a6b -r 48ca036d7d9c test/map_tests.c --- a/test/map_tests.c Thu Oct 04 19:46:10 2012 +0200 +++ b/test/map_tests.c Fri Oct 05 10:25:33 2012 +0200 @@ -141,6 +141,17 @@ ucx_map_free(map); } +void* test_ucx_map_store_load_encdec(void *value, void *data) { + char *string = (char*) value; + size_t n = strlen(string); + char *encoded = malloc(n+1); + for (int i = 0 ; i < n ; i++) { + encoded[i] = string[n-1-i]; + } + encoded[n] = 0; + return encoded; +} + UCX_TEST_IMPLEMENT(test_ucx_map_store_load) { UcxMap *map = ucx_map_new(4); @@ -155,14 +166,14 @@ int r; fwrite(" # comment test\n", 1, 16, f); - r = ucx_map_store(map, f); + r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL); fwrite("!discard this", 1, 13, f); fclose(f); ucx_map_free(map); map = ucx_map_new(1); f = fopen("test_ucx_map_store", "r"); - r += ucx_map_load(map, f); + r += ucx_map_load_enc(map, f, test_ucx_map_store_load_encdec, NULL); UCX_TEST_BEGIN char *value; diff -r dd03226c1a6b -r 48ca036d7d9c ucx/map.c --- a/ucx/map.c Thu Oct 04 19:46:10 2012 +0200 +++ b/ucx/map.c Fri Oct 05 10:25:33 2012 +0200 @@ -207,7 +207,7 @@ return 1; } -int ucx_map_load(UcxMap *map, FILE *f) { +int ucx_map_load_enc(UcxMap *map, FILE *f, copy_func decoder, void* decdata) { int c; int r, n; @@ -267,7 +267,14 @@ } while ((c = fgetc(f)) > 0); value[r] = 0; while (value[--r] < 33) value[r] = 0; - value = realloc(value, r+2); + + if (decoder == NULL) { + value = realloc(value, r+2); + } else { + void *decoded = decoder(value, decdata); + free(value); + value = decoded; + } ucx_map_cstr_put(map, key, value); free(key); @@ -276,7 +283,7 @@ return 0; } -int ucx_map_store(UcxMap *map, FILE *f) { +int ucx_map_store_enc(UcxMap *map, FILE *f, copy_func encoder, void *encdata) { UcxMapIterator iter = ucx_map_iterator(map); char *k, *v; sstr_t key, value; @@ -284,7 +291,12 @@ UCX_MAP_FOREACH(v, iter) { k = (char*) iter.cur->key.data; - key = sstr(k); value = sstr(v); + key = sstr(k); + if (encoder == NULL) { + value = sstr(v); + } else { + value = sstr(encoder(v, encdata)); + } written = 0; written += fwrite(key.ptr, 1, key.length, f); @@ -292,6 +304,10 @@ written += fwrite(value.ptr, 1, value.length, f); written += fwrite("\n", 1, 1, f); + if (encoder != NULL) { + free(value.ptr); + } + if (written != key.length + value.length + 4) return 1; } diff -r dd03226c1a6b -r 48ca036d7d9c ucx/map.h --- a/ucx/map.h Thu Oct 04 19:46:10 2012 +0200 +++ b/ucx/map.h Fri Oct 05 10:25:33 2012 +0200 @@ -66,14 +66,13 @@ int ucx_map_iter_next(UcxMapIterator *i, void **elm); -/* use for string maps only, values are not encoded */ -int ucx_map_load(UcxMap *map, FILE *f); -int ucx_map_store(UcxMap *map, FILE *f); +/* use macros for string maps only, values are not encoded */ +#define ucx_map_load(map, f) ucx_map_load_enc(map, f, NULL, NULL) +#define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL) -/* TODO: int ucx_map_load_enc(UcxMap *map, FILE *f, copy_func decoder, void* decdata); +/* encoders shall provide null terminated strings*/ int ucx_map_store_enc(UcxMap *map, FILE *f, copy_func encoder, void* encdata); - */ #ifdef __cplusplus }