Mon, 15 Jul 2013 15:43:18 +0200
removed map load/store
test/main.c | file | annotate | diff | comparison | revisions | |
test/map_tests.c | file | annotate | diff | comparison | revisions | |
test/map_tests.h | 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/main.c Mon Jul 15 14:25:50 2013 +0200 +++ b/test/main.c Mon Jul 15 15:43:18 2013 +0200 @@ -165,8 +165,6 @@ ucx_test_register(suite, test_ucx_map_remove); ucx_test_register(suite, test_ucx_map_iterator); ucx_test_register(suite, test_ucx_map_iterator_chain); - ucx_test_register(suite, test_ucx_map_store_load); - ucx_test_register(suite, test_ucx_map_store_load_with_mempool); ucx_test_register(suite, test_ucx_map_clone); ucx_test_register(suite, test_ucx_map_rehash);
--- a/test/map_tests.c Mon Jul 15 14:25:50 2013 +0200 +++ b/test/map_tests.c Mon Jul 15 15:43:18 2013 +0200 @@ -204,121 +204,6 @@ ucx_map_free(map); } -void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) { - const char *string = (const char*) value; - size_t n = strlen(string); - char *encoded = (char*) malloc(n+1); - for (size_t i = 0 ; i < n ; i++) { - encoded[i] = string[n-1-i]; - } - encoded[n] = 0; - *size = n+1; - return encoded; -} - -UCX_TEST_IMPLEMENT(test_ucx_map_store_load) { - UcxMap *map = ucx_map_new(4); - - ucx_map_cstr_put(map, "test", (void*)"test"); - ucx_map_cstr_put(map, "key", (void*)"value"); - ucx_map_cstr_put(map, "other.very.long.key", (void*)"value"); - ucx_map_cstr_put(map, "testkey", (void*)"testvalue"); - ucx_map_cstr_put(map, "simple", (void*)"not a key but an extremely long " - "value to test if the buffer extension works as designed"); - - UCX_TEST_BEGIN - FILE *f = tmpfile(); - UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted") - int r; - - fwrite(" # comment test\n", 1, 16, f); - r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL); - fwrite("!discard this", 1, 13, f); - fflush(f); - - ucx_map_free(map); - map = ucx_map_new(1); - fseek(f, 0, SEEK_SET); - UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT; - r += ucx_map_load_enc(map, f, allocator, - test_ucx_map_store_load_encdec, NULL); - fclose(f); - - const char *value; - UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); - - value = (const char *) ucx_map_cstr_get(map, "test"); - UCX_TEST_ASSERT(value != NULL, "value not found for key: test"); - UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test"); - - value = (const char *) ucx_map_cstr_get(map, "key"); - UCX_TEST_ASSERT(value != NULL, "value not found for key: key"); - UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key"); - - value = (const char *) ucx_map_cstr_get(map, "other.very.long.key"); - UCX_TEST_ASSERT(value != NULL, - "value not found for key: other.very.long.key"); - UCX_TEST_ASSERT(strcmp(value, "value") == 0, - "value error for key: other.very.long.key"); - - value = (const char *) ucx_map_cstr_get(map, "testkey"); - UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey"); - UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0, - "value error for key: testkey"); - - value = (const char *) ucx_map_cstr_get(map, "simple"); - UCX_TEST_ASSERT(value != NULL, "value not found for key: simple"); - UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value " - "to test if the buffer extension works as designed") == 0, - "value error for key: simple"); - - void *d; - UcxMapIterator iter = ucx_map_iterator(map); - UCX_MAP_FOREACH(key, d, iter) { - free(d); - } - ucx_map_free(map); - UCX_TEST_END -} - -UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) { - UcxMap *map = ucx_map_new(4); - - ucx_map_cstr_put(map, "test", (void*)"test"); - ucx_map_cstr_put(map, "key", (void*)"value"); - ucx_map_cstr_put(map, "testkey", (void*)"testvalue"); - ucx_map_cstr_put(map, "simple", (void*)"a simple value"); - - UCX_TEST_BEGIN - FILE *f = tmpfile(); - UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted"); - int r; - r = ucx_map_store_enc(map, f, NULL, NULL); - ucx_map_free(map); - fflush(f); - - UcxMempool *pool = ucx_mempool_new(4); - map = ucx_map_new(4); - fseek(f, 0, SEEK_SET); - UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool); - r += ucx_map_load_enc(map, f, allocator, - test_ucx_map_store_load_encdec, NULL); - fclose(f); - - 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(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"); - } - - ucx_mempool_free(pool); - ucx_map_free(map); - UCX_TEST_END -} - UCX_TEST_IMPLEMENT(test_ucx_map_clone) { UcxMap *map = ucx_map_new(4);
--- a/test/map_tests.h Mon Jul 15 14:25:50 2013 +0200 +++ b/test/map_tests.h Mon Jul 15 15:43:18 2013 +0200 @@ -43,8 +43,6 @@ UCX_TEST_DECLARE(test_ucx_map_remove); UCX_TEST_DECLARE(test_ucx_map_iterator); UCX_TEST_DECLARE(test_ucx_map_iterator_chain); -UCX_TEST_DECLARE(test_ucx_map_store_load); -UCX_TEST_DECLARE(test_ucx_map_store_load_with_mempool); UCX_TEST_DECLARE(test_ucx_map_clone); UCX_TEST_DECLARE(test_ucx_map_rehash);
--- a/ucx/map.c Mon Jul 15 14:25:50 2013 +0200 +++ b/ucx/map.c Mon Jul 15 15:43:18 2013 +0200 @@ -71,7 +71,7 @@ do { UcxMapElement *next = elem->next; map->allocator->free(map->allocator->pool, elem->key.data); - free(elem); + map->allocator->free(map->allocator->pool, elem); elem = next; } while (elem != NULL); } @@ -313,122 +313,3 @@ return 1; } -int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator, - ucx_map_coder decoder, void* decdata) { - - int c; int r, n; - - char *key, *value; - - while ((c = fgetc(f)) > 0) { - /* Discard leading spaces and comments */ - if (c < 33) continue; - if (c == '#' || c == '!') { - while ((c = (char) fgetc(f)) > 0) { - if (c == '\n') break; - } - continue; - } - - /* read into key buffer */ - n = 16; - key = (char*) malloc(n); - r = 0; - do { - if (c == '=') break; - if (r > n - 2) { - n *= 2; - key = (char*) realloc(key, n); - } - key[r] = c; - r++; - } while ((c = fgetc(f)) > 0); - if (c <= 0) { - free(key); - return 1; - } - key[r] = 0; - while (key[--r] == ' ') key[r] = 0; - - /* skip whitespaces */ - while ((c = fgetc(f)) > 0) { - if (c > 32) break; - } - if (c <= 0) { - free(key); - return 1; - } - - /* read into value buffer */ - n = 64; - value = (char*) malloc(n); - r = 0; - do { - if (c == '\n') break; - if (r > n - 2) { - n *= 2; - value = (char*) realloc(value, n); - } - value[r] = c; - r++; - } while ((c = fgetc(f)) > 0); - value[r] = 0; - while (value[--r] < 33) value[r] = 0; - - if (decoder) { - size_t decodedSize; - void *decoded = decoder(value, decdata, &decodedSize); - free(value); - value = (char*) decoded; - r = decodedSize; - } else { - r += 2; - value = (char*) realloc(value, r); - } - - if (allocator.pool) { - void *pooledValue = allocator.malloc(allocator.pool, r); - memcpy(pooledValue, value, r); - free(value); - value = (char*) pooledValue; - } - - ucx_map_cstr_put(map, key, value); - free(key); - } - - return 0; -} - -int ucx_map_store_enc(UcxMap *map, FILE *f, - ucx_map_coder encoder, void *encdata) { - UcxMapIterator iter = ucx_map_iterator(map); - char *v; - sstr_t key, value; - size_t written; - - UCX_MAP_FOREACH(k, v, iter) { - key = sstrn(k.data, k.len); - if (encoder) { - size_t encodedSize; - void *encoded = encoder(v, encdata, &encodedSize); - value = sstrn((char*) encoded,encodedSize - 1); - } else { - value = sstr(v); - } - - written = 0; - written += fwrite(key.ptr, 1, key.length, f); - written += fwrite(" = ", 1, 3, f); - written += fwrite(value.ptr, 1, value.length, f); - written += fwrite("\n", 1, 1, f); - - if (encoder) { - free(value.ptr); - } - - if (written != key.length + value.length + 4) return 1; - } - - return 0; -}
--- a/ucx/map.h Mon Jul 15 14:25:50 2013 +0200 +++ b/ucx/map.h Mon Jul 15 15:43:18 2013 +0200 @@ -122,15 +122,6 @@ 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) -#define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL) - -int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator, - ucx_map_coder decoder, void* decdata); -/* encoders shall provide null terminated strings*/ -int ucx_map_store_enc(UcxMap *map, FILE *f, - ucx_map_coder encoder, void* encdata); #ifdef __cplusplus }
--- a/ucx/properties.c Mon Jul 15 14:25:50 2013 +0200 +++ b/ucx/properties.c Mon Jul 15 15:43:18 2013 +0200 @@ -245,21 +245,19 @@ int ucx_properties_store(UcxMap *map, FILE *file) { UcxMapIterator iter = ucx_map_iterator(map); char *v; - sstr_t key, value; + sstr_t value; size_t written; UCX_MAP_FOREACH(k, v, iter) { - //k = (char*) iter.cur->key.data; - key = sstrn(k.data, k.len); value = sstr(v); written = 0; - written += fwrite(key.ptr, 1, key.length, file); + written += fwrite(k.data, 1, k.len, file); written += fwrite(" = ", 1, 3, file); written += fwrite(value.ptr, 1, value.length, file); written += fwrite("\n", 1, 1, file); - if (written != key.length + value.length + 4) return 1; + if (written != k.len + value.length + 4) return 1; } return 0;