Fri, 05 Oct 2012 10:25:33 +0200
implemented encoder/decoder for map store/load
1 /*
2 *
3 */
5 #ifndef MAP_H
6 #define MAP_H
8 #include "ucx.h"
9 #include "string.h"
10 #include <stdio.h>
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
16 #define UCX_MAP_FOREACH(elm,iter) \
17 for(;ucx_map_iter_next(&iter,(void*)&elm)==0;)
19 typedef struct UcxMap UcxMap;
20 typedef struct UcxKey UcxKey;
21 typedef struct UcxMapElement UcxMapElement;
22 typedef struct UcxMapIterator UcxMapIterator;
24 struct UcxMap {
25 UcxMapElement **map;
26 size_t size;
27 size_t count;
28 };
30 struct UcxKey {
31 void *data;
32 size_t len;
33 int hash;
34 };
36 struct UcxMapElement {
37 void *data;
38 UcxMapElement *next;
39 UcxKey key;
40 };
42 struct UcxMapIterator {
43 UcxMap *map;
44 UcxMapElement *cur;
45 int index;
46 };
49 UcxMap *ucx_map_new(size_t size);
50 void ucx_map_free(UcxMap *map);
51 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
53 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
54 void* ucx_map_get(UcxMap *map, UcxKey key);
56 #define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d)
57 #define ucx_map_cstr_put(m, s, d) ucx_map_put(m, ucx_key(s, 1+strlen(s)), d)
58 #define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, s.length))
59 #define ucx_map_cstr_get(m, s) ucx_map_get(m, ucx_key(s, 1+strlen(s)))
61 UcxKey ucx_key(void *data, size_t len);
63 int ucx_hash(char *data, size_t len);
65 UcxMapIterator ucx_map_iterator(UcxMap *map);
67 int ucx_map_iter_next(UcxMapIterator *i, void **elm);
69 /* use macros for string maps only, values are not encoded */
70 #define ucx_map_load(map, f) ucx_map_load_enc(map, f, NULL, NULL)
71 #define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL)
73 int ucx_map_load_enc(UcxMap *map, FILE *f, copy_func decoder, void* decdata);
74 /* encoders shall provide null terminated strings*/
75 int ucx_map_store_enc(UcxMap *map, FILE *f, copy_func encoder, void* encdata);
77 #ifdef __cplusplus
78 }
79 #endif
81 #endif /* MAP_H */