]> git.ipfire.org Git - thirdparty/git.git/blob - strmap.h
strmap: new utility functions
[thirdparty/git.git] / strmap.h
1 #ifndef STRMAP_H
2 #define STRMAP_H
3
4 #include "hashmap.h"
5
6 struct strmap {
7 struct hashmap map;
8 unsigned int strdup_strings:1;
9 };
10
11 struct strmap_entry {
12 struct hashmap_entry ent;
13 const char *key;
14 void *value;
15 };
16
17 int cmp_strmap_entry(const void *hashmap_cmp_fn_data,
18 const struct hashmap_entry *entry1,
19 const struct hashmap_entry *entry2,
20 const void *keydata);
21
22 #define STRMAP_INIT { \
23 .map = HASHMAP_INIT(cmp_strmap_entry, NULL), \
24 .strdup_strings = 1, \
25 }
26
27 /*
28 * Initialize the members of the strmap. Any keys added to the strmap will
29 * be strdup'ed with their memory managed by the strmap.
30 */
31 void strmap_init(struct strmap *map);
32
33 /*
34 * Same as strmap_init, but for those who want to control the memory management
35 * carefully instead of using the default of strdup_strings=1.
36 */
37 void strmap_init_with_options(struct strmap *map,
38 int strdup_strings);
39
40 /*
41 * Remove all entries from the map, releasing any allocated resources.
42 */
43 void strmap_clear(struct strmap *map, int free_values);
44
45 /*
46 * Insert "str" into the map, pointing to "data".
47 *
48 * If an entry for "str" already exists, its data pointer is overwritten, and
49 * the original data pointer returned. Otherwise, returns NULL.
50 */
51 void *strmap_put(struct strmap *map, const char *str, void *data);
52
53 /*
54 * Return the data pointer mapped by "str", or NULL if the entry does not
55 * exist.
56 */
57 void *strmap_get(struct strmap *map, const char *str);
58
59 /*
60 * Return non-zero iff "str" is present in the map. This differs from
61 * strmap_get() in that it can distinguish entries with a NULL data pointer.
62 */
63 int strmap_contains(struct strmap *map, const char *str);
64
65 #endif /* STRMAP_H */