-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyvals.c
236 lines (191 loc) · 4.26 KB
/
keyvals.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Copied from osm2pgsql by Martijn van Oosterhout <[email protected]> 2008 */
/* Licence: GPL */
/* Common key-value list processing
*
* Used as a small general purpose store for
* tags, segment lists etc
*
*/
#define _GNU_SOURCE
#undef USE_TREE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "keyvals.h"
#ifdef USE_TREE
#include "text-tree.h"
#endif
void initList(struct keyval *head)
{
assert(head);
head->next = head;
head->prev = head;
head->key = NULL;
head->value = NULL;
}
void freeItem(struct keyval *p)
{
if (!p)
return;
#ifdef USE_TREE
text_release(tree_ctx, p->key);
text_release(tree_ctx, p->value);
#else
free(p->key);
free(p->value);
#endif
free(p);
}
unsigned int countList(struct keyval *head)
{
struct keyval *p;
unsigned int count = 0;
if (!head)
return 0;
p = head->next;
while(p != head) {
count++;
p = p->next;
}
return count;
}
int listHasData(struct keyval *head)
{
if (!head)
return 0;
return (head->next != head);
}
char *getItem(struct keyval *head, const char *name)
{
struct keyval *p;
if (!head)
return NULL;
p = head->next;
while(p != head) {
if (!strcmp(p->key, name))
return p->value;
p = p->next;
}
return NULL;
}
/* Pulls all items from list which match this prefix
* note: they are removed from the original list an returned in a new one
*/
struct keyval *getMatches(struct keyval *head, const char *name)
{
struct keyval *out = NULL;
struct keyval *p;
if (!head)
return NULL;
out = malloc(sizeof(struct keyval));
if (!out)
return NULL;
initList(out);
p = head->next;
while(p != head) {
struct keyval *next = p->next;
if (!strncmp(p->key, name, strlen(name))) {
//printf("match: %s=%s\n", p->key, p->value);
p->next->prev = p->prev;
p->prev->next = p->next;
pushItem(out, p);
}
p = next;
}
if (listHasData(out))
return out;
free(out);
return NULL;
}
void updateItem(struct keyval *head, const char *name, const char *value)
{
struct keyval *item;
if (!head)
return;
item = head->next;
while(item != head) {
if (!strcmp(item->key, name)) {
#ifdef USE_TREE
text_release(tree_ctx, item->value);
item->value = (char *)text_get(tree_ctx,value);
#else
free(item->value);
item->value = strdup(value);
#endif
return;
}
item = item->next;
}
addItem(head, name, value, 0);
}
struct keyval *popItem(struct keyval *head)
{
struct keyval *p;
if (!head)
return NULL;
p = head->next;
if (p == head)
return NULL;
head->next = p->next;
p->next->prev = head;
p->next = NULL;
p->prev = NULL;
return p;
}
void pushItem(struct keyval *head, struct keyval *item)
{
assert(head);
assert(item);
item->next = head;
item->prev = head->prev;
head->prev->next = item;
head->prev = item;
}
int addItem(struct keyval *head, const char *name, const char *value, int noDupe)
{
struct keyval *item;
assert(head);
assert(name);
assert(value);
if (noDupe) {
item = head->next;
while (item != head) {
if (!strcmp(item->value, value) && !strcmp(item->key, name))
return 1;
item = item->next;
}
}
item = malloc(sizeof(struct keyval));
if (!item) {
fprintf(stderr, "Error allocating keyval\n");
return 2;
}
#ifdef USE_TREE
item->key = (char *)text_get(tree_ctx,name);
item->value = (char *)text_get(tree_ctx,value);
#else
item->key = strdup(name);
item->value = strdup(value);
#endif
#if 0
// Add to head
item->next = head->next;
item->prev = head;
head->next->prev = item;
head->next = item;
#else
// Add to tail
item->prev = head->prev;
item->next = head;
head->prev->next = item;
head->prev = item;
#endif
return 0;
}
void resetList(struct keyval *head)
{
struct keyval *item;
while((item = popItem(head)))
freeItem(item);
}