-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.c
61 lines (51 loc) · 1.07 KB
/
map.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "map.h"
struct map_t * map_create() {
return calloc(1, sizeof(struct map_t));
}
void map_free(struct map_t *map) {
struct map_t *m, *tmp;
m = map;
while (m) {
if (m->key)
free(m->key);
if (m->value)
free(m->value);
tmp = m;
m = m->next;
free(tmp);
}
}
char * map_get(struct map_t *map, const char *key) {
struct map_t *m;
for (m = map; m != NULL; m = m->next) {
if (m->key && !strcasecmp(key, m->key))
return m->value;
}
return NULL;
}
void map_set(struct map_t *map, const char *key, const char *val) {
struct map_t *m;
m = map;
// Map not empty?
if (m->key) {
for ( ; ; m = m->next) {
// Key exists, freeing strings for reuse.
if (!strcasecmp(key, m->key)) {
free(m->key);
free(m->value);
break;
}
// Last element, adding a new one.
else if (!m->next) {
m->next = calloc(1, sizeof(struct map_t));
m = m->next;
break;
}
}
}
// Add key and value.
m->key = calloc(strlen(key) + 1, sizeof(char));
strcpy(m->key, key);
m->value = calloc(strlen(val) + 1, sizeof(char));
strcpy(m->value, val);
}