|
|
tmap.c - ics2txt - convert icalendar .ics file to plain text |
|
|
 |
git clone git://bitreich.org/ics2txt git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ics2txt (git://bitreich.org) |
|
|
 |
Log |
|
|
 |
Files |
|
|
 |
Refs |
|
|
 |
Tags |
|
|
 |
README |
|
|
|
--- |
|
|
|
tmap.c (1701B) |
|
|
|
--- |
|
|
|
1 #include "map.h" |
|
|
|
2 |
|
|
|
3 #include <stdlib.h> |
|
|
|
4 #include <string.h> |
|
|
|
5 |
|
|
|
6 #include "util.h" |
|
|
|
7 |
|
|
|
8 static int |
|
|
|
9 map_cmp(void const *v1, void const *v2) |
|
|
|
10 { |
|
|
|
11 struct map_entry const *e1 = v1, *e2 = v2; |
|
|
|
12 |
|
|
|
13 return strcmp(e1->key, e2->key); |
|
|
|
14 } |
|
|
|
15 |
|
|
|
16 void * |
|
|
|
17 map_get(struct map *map, char *key) |
|
|
|
18 { |
|
|
|
19 struct map_entry *entry, k = { .key = key }; |
|
|
|
20 size_t sz; |
|
|
|
21 |
|
|
|
22 sz = sizeof(*map->entry); |
|
|
|
23 if ((entry = bsearch(&k, map->entry, map->len, sz, map_cmp)) == NULL) |
|
|
|
24 return NULL; |
|
|
|
25 return entry->value; |
|
|
|
26 } |
|
|
|
27 |
|
|
|
28 int |
|
|
|
29 map_set(struct map *map, char *key, void *value) |
|
|
|
30 { |
|
|
|
31 struct map_entry *insert, *e; |
|
|
|
32 size_t i, sz; |
|
|
|
33 void *v; |
|
|
|
34 |
|
|
|
35 for (i = 0; i < map->len; i++) { |
|
|
|
36 int cmp = strcmp(key, map->entry[i].key); |
|
|
|
37 |
|
|
|
38 if (cmp == 0) { |
|
|
|
39 map->entry[i].value = value; |
|
|
|
40 return 0; |
|
|
|
41 } |
|
|
|
42 if (cmp < 0) |
|
|
|
43 break; |
|
|
|
44 } |
|
|
|
45 |
|
|
|
46 sz = sizeof(*map->entry); |
|
|
|
47 if ((v = reallocarray(map->entry, map->len + 1, sz)) == NULL) |
|
|
|
48 return -1; |
|
|
|
49 map->entry = v; |
|
|
|
50 map->len++; |
|
|
|
51 |
|
|
|
52 insert = map->entry + i; |
|
|
|
53 for (e = map->entry + map->len - 2; e >= insert; e--) |
|
|
|
54 e[1] = e[0]; |
|
|
|
55 |
|
|
|
56 insert->key = key; |
|
|
|
57 insert->value = value; |
|
|
|
58 |
|
|
|
59 return 0; |
|
|
|
60 } |
|
|
|
61 |
|
|
|
62 int |
|
|
|
63 map_del(struct map *map, char *key) |
|
|
|
64 { |
|
|
|
65 size_t i; |
|
|
|
66 |
|
|
|
67 for (i = 0; i < map->len; i++) { |
|
|
|
68 int cmp = strcmp(key, map->entry[i].key); |
|
|
|
69 |
|
|
|
70 if (cmp == 0) |
|
|
|
71 break; |
|
|
|
72 if (cmp < 0) |
|
|
|
73 return -1; |
|
|
|
74 } |
|
|
|
75 if (i == map->len) |
|
|
|
76 return -1; |
|
|
|
77 |
|
|
|
78 map->len--; |
|
|
|
79 for (; i < map->len; i++) |
|
|
|
80 map->entry[i] = map->entry[i + 1]; |
|
|
|
81 return 0; |
|
|
|
82 } |
|
|
|
83 |
|
|
|
84 void |
|
|
|
85 map_init(struct map *map) |
|
|
|
86 { |
|
|
|
87 memset(map, 0, sizeof(*map)); |
|
|
|
88 } |
|
|
|
89 |
|
|
|
90 void |
|
|
|
91 map_free_keys(struct map *map) |
|
|
|
92 { |
|
|
|
93 for (size_t i = 0; i < map->len; i++) |
|
|
|
94 free(map->entry[i].key); |
|
|
|
95 } |
|
|
|
96 |
|
|
|
97 void |
|
|
|
98 map_free(struct map *map, void (*fn)(void *)) |
|
|
|
99 { |
|
|
|
100 if (fn != NULL) { |
|
|
|
101 for (size_t i = 0; i < map->len; i++) |
|
|
|
102 fn(map->entry[i].value); |
|
|
|
103 } |
|
|
|
104 free(map->entry); |
|
|
|
105 map->len = 0; |
|
|
|
106 } |
|