Fri, 05 Oct 2012 11:52:53 +0200
map can now load values from file into pooled memory
use with care when using a decoder that also allocates memory
olaf@2 | 1 | /* |
olaf@2 | 2 | * |
olaf@2 | 3 | */ |
olaf@2 | 4 | |
olaf@2 | 5 | #ifndef MAP_H |
olaf@2 | 6 | #define MAP_H |
olaf@2 | 7 | |
olaf@20 | 8 | #include "ucx.h" |
olaf@20 | 9 | #include "string.h" |
universe@48 | 10 | #include "mempool.h" |
universe@41 | 11 | #include <stdio.h> |
olaf@20 | 12 | |
olaf@2 | 13 | #ifdef __cplusplus |
olaf@2 | 14 | extern "C" { |
olaf@2 | 15 | #endif |
olaf@2 | 16 | |
universe@41 | 17 | #define UCX_MAP_FOREACH(elm,iter) \ |
universe@42 | 18 | for(;ucx_map_iter_next(&iter,(void*)&elm)==0;) |
olaf@31 | 19 | |
olaf@31 | 20 | typedef struct UcxMap UcxMap; |
olaf@31 | 21 | typedef struct UcxKey UcxKey; |
olaf@31 | 22 | typedef struct UcxMapElement UcxMapElement; |
olaf@31 | 23 | typedef struct UcxMapIterator UcxMapIterator; |
olaf@2 | 24 | |
universe@48 | 25 | /* |
universe@48 | 26 | * param 1: element |
universe@48 | 27 | * param 2: additional data |
universe@48 | 28 | * param 3: size of encoded data will be stored here |
universe@48 | 29 | * returns encoded / decoded string or NULL on failure |
universe@48 | 30 | */ |
universe@48 | 31 | typedef void*(*ucx_map_coder)(void*,void*,size_t*); |
universe@48 | 32 | |
olaf@20 | 33 | struct UcxMap { |
universe@29 | 34 | UcxMapElement **map; |
olaf@20 | 35 | size_t size; |
olaf@45 | 36 | size_t count; |
olaf@20 | 37 | }; |
olaf@2 | 38 | |
olaf@20 | 39 | struct UcxKey { |
olaf@20 | 40 | void *data; |
olaf@20 | 41 | size_t len; |
olaf@20 | 42 | int hash; |
olaf@20 | 43 | }; |
olaf@20 | 44 | |
olaf@20 | 45 | struct UcxMapElement { |
olaf@20 | 46 | void *data; |
olaf@20 | 47 | UcxMapElement *next; |
olaf@20 | 48 | UcxKey key; |
olaf@20 | 49 | }; |
olaf@20 | 50 | |
olaf@31 | 51 | struct UcxMapIterator { |
olaf@31 | 52 | UcxMap *map; |
olaf@31 | 53 | UcxMapElement *cur; |
olaf@31 | 54 | int index; |
olaf@31 | 55 | }; |
olaf@31 | 56 | |
olaf@20 | 57 | |
olaf@20 | 58 | UcxMap *ucx_map_new(size_t size); |
universe@29 | 59 | void ucx_map_free(UcxMap *map); |
olaf@44 | 60 | UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); |
olaf@20 | 61 | |
olaf@20 | 62 | int ucx_map_put(UcxMap *map, UcxKey key, void *data); |
olaf@20 | 63 | void* ucx_map_get(UcxMap *map, UcxKey key); |
olaf@20 | 64 | |
olaf@30 | 65 | #define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d) |
universe@29 | 66 | #define ucx_map_cstr_put(m, s, d) ucx_map_put(m, ucx_key(s, 1+strlen(s)), d) |
olaf@30 | 67 | #define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, s.length)) |
universe@29 | 68 | #define ucx_map_cstr_get(m, s) ucx_map_get(m, ucx_key(s, 1+strlen(s))) |
olaf@20 | 69 | |
olaf@20 | 70 | UcxKey ucx_key(void *data, size_t len); |
olaf@20 | 71 | |
olaf@20 | 72 | int ucx_hash(char *data, size_t len); |
olaf@2 | 73 | |
olaf@31 | 74 | UcxMapIterator ucx_map_iterator(UcxMap *map); |
olaf@31 | 75 | |
olaf@31 | 76 | int ucx_map_iter_next(UcxMapIterator *i, void **elm); |
olaf@31 | 77 | |
universe@46 | 78 | /* use macros for string maps only, values are not encoded */ |
universe@48 | 79 | #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL) |
universe@46 | 80 | #define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL) |
universe@42 | 81 | |
universe@48 | 82 | int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator, |
universe@48 | 83 | ucx_map_coder decoder, void* decdata); |
universe@46 | 84 | /* encoders shall provide null terminated strings*/ |
universe@48 | 85 | int ucx_map_store_enc(UcxMap *map, FILE *f, |
universe@48 | 86 | ucx_map_coder encoder, void* encdata); |
universe@42 | 87 | |
olaf@2 | 88 | #ifdef __cplusplus |
olaf@2 | 89 | } |
olaf@2 | 90 | #endif |
olaf@2 | 91 | |
olaf@2 | 92 | #endif /* MAP_H */ |
olaf@2 | 93 |