removed map load/store

Mon, 15 Jul 2013 15:43:18 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 15 Jul 2013 15:43:18 +0200
changeset 112
6384016df2a3
parent 111
c8c59d7f4536
child 113
8693d7874773

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
     1.1 --- a/test/main.c	Mon Jul 15 14:25:50 2013 +0200
     1.2 +++ b/test/main.c	Mon Jul 15 15:43:18 2013 +0200
     1.3 @@ -165,8 +165,6 @@
     1.4          ucx_test_register(suite, test_ucx_map_remove);
     1.5          ucx_test_register(suite, test_ucx_map_iterator);
     1.6          ucx_test_register(suite, test_ucx_map_iterator_chain);
     1.7 -        ucx_test_register(suite, test_ucx_map_store_load);
     1.8 -        ucx_test_register(suite, test_ucx_map_store_load_with_mempool);
     1.9          ucx_test_register(suite, test_ucx_map_clone);
    1.10          ucx_test_register(suite, test_ucx_map_rehash);
    1.11          
     2.1 --- a/test/map_tests.c	Mon Jul 15 14:25:50 2013 +0200
     2.2 +++ b/test/map_tests.c	Mon Jul 15 15:43:18 2013 +0200
     2.3 @@ -204,121 +204,6 @@
     2.4      ucx_map_free(map);
     2.5  }
     2.6  
     2.7 -void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) {
     2.8 -    const char *string = (const char*) value;
     2.9 -    size_t n = strlen(string);
    2.10 -    char *encoded = (char*) malloc(n+1);
    2.11 -    for (size_t i = 0 ; i < n ; i++) {
    2.12 -        encoded[i] = string[n-1-i];
    2.13 -    }
    2.14 -    encoded[n] = 0;
    2.15 -    *size = n+1;
    2.16 -    return encoded;
    2.17 -}
    2.18 -
    2.19 -UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
    2.20 -    UcxMap *map = ucx_map_new(4);
    2.21 -
    2.22 -    ucx_map_cstr_put(map, "test", (void*)"test");
    2.23 -    ucx_map_cstr_put(map, "key", (void*)"value");
    2.24 -    ucx_map_cstr_put(map, "other.very.long.key", (void*)"value");
    2.25 -    ucx_map_cstr_put(map, "testkey", (void*)"testvalue");
    2.26 -    ucx_map_cstr_put(map, "simple", (void*)"not a key but an extremely long "
    2.27 -            "value to test if the buffer extension works as designed");
    2.28 -
    2.29 -    UCX_TEST_BEGIN
    2.30 -    FILE *f = tmpfile();
    2.31 -    UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted")
    2.32 -    int r;
    2.33 -
    2.34 -    fwrite(" # comment test\n", 1, 16, f);
    2.35 -    r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL);
    2.36 -    fwrite("!discard this", 1, 13, f);
    2.37 -    fflush(f);
    2.38 -
    2.39 -    ucx_map_free(map);
    2.40 -    map = ucx_map_new(1);
    2.41 -    fseek(f, 0, SEEK_SET);
    2.42 -    UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT;
    2.43 -    r += ucx_map_load_enc(map, f, allocator,
    2.44 -            test_ucx_map_store_load_encdec, NULL);
    2.45 -    fclose(f);
    2.46 -
    2.47 -    const char *value;
    2.48 -    UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
    2.49 -
    2.50 -    value = (const char *) ucx_map_cstr_get(map, "test");
    2.51 -    UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
    2.52 -    UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
    2.53 -
    2.54 -    value = (const char *) ucx_map_cstr_get(map, "key");
    2.55 -    UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
    2.56 -    UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
    2.57 -
    2.58 -    value = (const char *) ucx_map_cstr_get(map, "other.very.long.key");
    2.59 -    UCX_TEST_ASSERT(value != NULL,
    2.60 -            "value not found for key: other.very.long.key");
    2.61 -    UCX_TEST_ASSERT(strcmp(value, "value") == 0,
    2.62 -            "value error for key: other.very.long.key");
    2.63 -
    2.64 -    value = (const char *) ucx_map_cstr_get(map, "testkey");
    2.65 -    UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
    2.66 -    UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
    2.67 -            "value error for key: testkey");
    2.68 -
    2.69 -    value = (const char *) ucx_map_cstr_get(map, "simple");
    2.70 -    UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
    2.71 -    UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
    2.72 -            "to test if the buffer extension works as designed") == 0,
    2.73 -            "value error for key: simple");
    2.74 -
    2.75 -    void *d;
    2.76 -    UcxMapIterator iter = ucx_map_iterator(map);
    2.77 -    UCX_MAP_FOREACH(key, d, iter) {
    2.78 -        free(d);
    2.79 -    }
    2.80 -    ucx_map_free(map);
    2.81 -    UCX_TEST_END
    2.82 -}
    2.83 -
    2.84 -UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) {
    2.85 -    UcxMap *map = ucx_map_new(4);
    2.86 -
    2.87 -    ucx_map_cstr_put(map, "test", (void*)"test");
    2.88 -    ucx_map_cstr_put(map, "key", (void*)"value");
    2.89 -    ucx_map_cstr_put(map, "testkey", (void*)"testvalue");
    2.90 -    ucx_map_cstr_put(map, "simple", (void*)"a simple value");
    2.91 -
    2.92 -    UCX_TEST_BEGIN
    2.93 -    FILE *f = tmpfile();
    2.94 -    UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted");
    2.95 -    int r;
    2.96 -    r = ucx_map_store_enc(map, f, NULL, NULL);
    2.97 -    ucx_map_free(map);
    2.98 -    fflush(f);
    2.99 -
   2.100 -    UcxMempool *pool = ucx_mempool_new(4);
   2.101 -    map = ucx_map_new(4);
   2.102 -    fseek(f, 0, SEEK_SET);
   2.103 -    UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool);
   2.104 -    r += ucx_map_load_enc(map, f, allocator,
   2.105 -            test_ucx_map_store_load_encdec, NULL);
   2.106 -    fclose(f);
   2.107 -
   2.108 -    UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
   2.109 -    UcxMapIterator iter = ucx_map_iterator(map);
   2.110 -    const char *value; size_t n;
   2.111 -    UCX_MAP_FOREACH(key, value, iter) {
   2.112 -        n = strlen(value);
   2.113 -        UCX_TEST_ASSERT(strncmp((const char*) pool->data[iter.index], value, n),
   2.114 -                "values of map does not match pooled values");
   2.115 -    }
   2.116 -
   2.117 -    ucx_mempool_free(pool);
   2.118 -    ucx_map_free(map);
   2.119 -    UCX_TEST_END
   2.120 -}
   2.121 -
   2.122  UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
   2.123      UcxMap *map = ucx_map_new(4);
   2.124      
     3.1 --- a/test/map_tests.h	Mon Jul 15 14:25:50 2013 +0200
     3.2 +++ b/test/map_tests.h	Mon Jul 15 15:43:18 2013 +0200
     3.3 @@ -43,8 +43,6 @@
     3.4  UCX_TEST_DECLARE(test_ucx_map_remove);
     3.5  UCX_TEST_DECLARE(test_ucx_map_iterator);
     3.6  UCX_TEST_DECLARE(test_ucx_map_iterator_chain);
     3.7 -UCX_TEST_DECLARE(test_ucx_map_store_load);
     3.8 -UCX_TEST_DECLARE(test_ucx_map_store_load_with_mempool);
     3.9  UCX_TEST_DECLARE(test_ucx_map_clone);
    3.10  UCX_TEST_DECLARE(test_ucx_map_rehash);
    3.11  
     4.1 --- a/ucx/map.c	Mon Jul 15 14:25:50 2013 +0200
     4.2 +++ b/ucx/map.c	Mon Jul 15 15:43:18 2013 +0200
     4.3 @@ -71,7 +71,7 @@
     4.4              do {
     4.5                  UcxMapElement *next = elem->next;
     4.6                  map->allocator->free(map->allocator->pool, elem->key.data);
     4.7 -                free(elem);
     4.8 +                map->allocator->free(map->allocator->pool, elem);
     4.9                  elem = next;
    4.10              } while (elem != NULL);
    4.11          }
    4.12 @@ -313,122 +313,3 @@
    4.13      return 1;
    4.14  }
    4.15  
    4.16 -int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
    4.17 -        ucx_map_coder decoder, void* decdata) {
    4.18 -
    4.19 -    int c; int r, n;
    4.20 -
    4.21 -    char *key, *value;
    4.22 -
    4.23 -    while ((c = fgetc(f)) > 0) {
    4.24 -        /* Discard leading spaces and comments */
    4.25 -        if (c < 33) continue;
    4.26 -        if (c == '#' || c == '!') {
    4.27 -            while ((c = (char) fgetc(f)) > 0) {
    4.28 -                if (c == '\n') break;
    4.29 -            }
    4.30 -            continue;
    4.31 -        }
    4.32 -
    4.33 -        /* read into key buffer */
    4.34 -        n = 16;
    4.35 -        key = (char*) malloc(n);
    4.36 -        r = 0;
    4.37 -        do {
    4.38 -            if (c == '=') break;
    4.39 -            if (r > n - 2) {
    4.40 -                n *= 2;
    4.41 -                key = (char*) realloc(key, n);
    4.42 -            }
    4.43 -            key[r] = c;
    4.44 -            r++;
    4.45 -        } while ((c = fgetc(f)) > 0);
    4.46 -        if (c <= 0) {
    4.47 -            free(key);
    4.48 -            return 1;
    4.49 -        }
    4.50 -        key[r] = 0;
    4.51 -        while (key[--r] == ' ') key[r] = 0;
    4.52 -
    4.53 -        /* skip whitespaces */
    4.54 -        while ((c = fgetc(f)) > 0) {
    4.55 -            if (c > 32) break;
    4.56 -        }
    4.57 -        if (c <= 0) {
    4.58 -            free(key);
    4.59 -            return 1;
    4.60 -        }
    4.61 -
    4.62 -        /* read into value buffer */
    4.63 -        n = 64;
    4.64 -        value = (char*) malloc(n);
    4.65 -        r = 0;
    4.66 -        do {
    4.67 -            if (c == '\n') break;
    4.68 -            if (r > n - 2) {
    4.69 -                n *= 2;
    4.70 -                value = (char*) realloc(value, n);
    4.71 -            }
    4.72 -            value[r] = c;
    4.73 -            r++;
    4.74 -        } while ((c = fgetc(f)) > 0);
    4.75 -        value[r] = 0;
    4.76 -        while (value[--r] < 33) value[r] = 0;
    4.77 -
    4.78 -        if (decoder) {
    4.79 -            size_t decodedSize;
    4.80 -            void *decoded = decoder(value, decdata, &decodedSize);
    4.81 -            free(value);
    4.82 -            value = (char*) decoded;
    4.83 -            r = decodedSize;
    4.84 -        } else {
    4.85 -            r += 2;
    4.86 -            value = (char*) realloc(value, r);
    4.87 -        }
    4.88 -
    4.89 -        if (allocator.pool) {
    4.90 -            void *pooledValue = allocator.malloc(allocator.pool, r);
    4.91 -            memcpy(pooledValue, value, r);
    4.92 -            free(value);
    4.93 -            value = (char*) pooledValue;
    4.94 -        }
    4.95 -
    4.96 -        ucx_map_cstr_put(map, key, value);
    4.97 -        free(key);
    4.98 -    }
    4.99 -
   4.100 -    return 0;
   4.101 -}
   4.102 -
   4.103 -int ucx_map_store_enc(UcxMap *map, FILE *f,
   4.104 -        ucx_map_coder encoder, void *encdata) {
   4.105 -    UcxMapIterator iter = ucx_map_iterator(map);
   4.106 -    char *v;
   4.107 -    sstr_t key, value;
   4.108 -    size_t written;
   4.109 -
   4.110 -    UCX_MAP_FOREACH(k, v, iter) {
   4.111 -        key = sstrn(k.data, k.len);
   4.112 -        if (encoder) {
   4.113 -            size_t encodedSize;
   4.114 -            void *encoded = encoder(v, encdata, &encodedSize);
   4.115 -            value = sstrn((char*) encoded,encodedSize - 1);
   4.116 -        } else {
   4.117 -            value = sstr(v);
   4.118 -        }
   4.119 -
   4.120 -        written = 0;
   4.121 -        written += fwrite(key.ptr, 1, key.length, f);
   4.122 -        written += fwrite(" = ", 1, 3, f);
   4.123 -        written += fwrite(value.ptr, 1, value.length, f);
   4.124 -        written += fwrite("\n", 1, 1, f);
   4.125 -
   4.126 -        if (encoder) {
   4.127 -            free(value.ptr);
   4.128 -        }
   4.129 -
   4.130 -        if (written != key.length + value.length + 4) return 1;
   4.131 -    }
   4.132 -
   4.133 -    return 0;
   4.134 -}
     5.1 --- a/ucx/map.h	Mon Jul 15 14:25:50 2013 +0200
     5.2 +++ b/ucx/map.h	Mon Jul 15 15:43:18 2013 +0200
     5.3 @@ -122,15 +122,6 @@
     5.4  
     5.5  int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm);
     5.6  
     5.7 -/* use macros for string maps only, values are not encoded */
     5.8 -#define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL)
     5.9 -#define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL)
    5.10 -
    5.11 -int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
    5.12 -        ucx_map_coder decoder, void* decdata);
    5.13 -/* encoders shall provide null terminated strings*/
    5.14 -int ucx_map_store_enc(UcxMap *map, FILE *f,
    5.15 -        ucx_map_coder encoder, void* encdata);
    5.16  
    5.17  #ifdef	__cplusplus
    5.18  }
     6.1 --- a/ucx/properties.c	Mon Jul 15 14:25:50 2013 +0200
     6.2 +++ b/ucx/properties.c	Mon Jul 15 15:43:18 2013 +0200
     6.3 @@ -245,21 +245,19 @@
     6.4  int ucx_properties_store(UcxMap *map, FILE *file) {
     6.5      UcxMapIterator iter = ucx_map_iterator(map);
     6.6      char *v;
     6.7 -    sstr_t key, value;
     6.8 +    sstr_t value;
     6.9      size_t written;
    6.10  
    6.11      UCX_MAP_FOREACH(k, v, iter) {
    6.12 -        //k = (char*) iter.cur->key.data;
    6.13 -        key = sstrn(k.data, k.len);
    6.14          value = sstr(v);
    6.15  
    6.16          written = 0;
    6.17 -        written += fwrite(key.ptr, 1, key.length, file);
    6.18 +        written += fwrite(k.data, 1, k.len, file);
    6.19          written += fwrite(" = ", 1, 3, file);
    6.20          written += fwrite(value.ptr, 1, value.length, file);
    6.21          written += fwrite("\n", 1, 1, file);
    6.22  
    6.23 -        if (written != key.length + value.length + 4) return 1;
    6.24 +        if (written != k.len + value.length + 4) return 1;
    6.25      }
    6.26  
    6.27      return 0;

mercurial