forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyvals.c
316 lines (258 loc) · 5.92 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* Common key-value list processing
*
* Used as a small general purpose store for
* tags, segment lists etc
*
*/
#define USE_TREE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string.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;
}
struct keyval *firstItem(struct keyval *head)
{
if (head == NULL || head == head->next)
return NULL;
return head->next;
}
struct keyval *nextItem(struct keyval *head, struct keyval *item)
{
if (item->next == head)
return NULL;
return item->next;
}
/* 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 1
// 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);
}
void cloneList( struct keyval *target, struct keyval *source )
{
struct keyval *ptr;
for( ptr = source->next; ptr != source; ptr=ptr->next )
addItem( target, ptr->key, ptr->value, 0 );
}
/* create an escaped version of the string for hstore table insert */
/* make shure dst is 2*strlen(src) */
static void escape4hstore(char *dst, char *src) {
size_t i,j;
j=0;
for (i=0;i<strlen(src);i++) {
switch(src[i]) {
case '\\':
dst[j]='\\';j++;dst[j]='\\';j++;dst[j]='\\';j++;dst[j]='\\'; break;
case '"':
dst[j]='\\';j++;dst[j]='\\';j++;dst[j]='"'; break;
case '\t':
dst[j]='\\';j++;dst[j]='\t'; break;
case '\r':
dst[j]='\\';j++;dst[j]='\r'; break;
case '\n':
dst[j]='\\';j++;dst[j]='\n'; break;
default:
dst[j]=src[i];
}
j++;
}
dst[j]='\0';
}
/* print struct keyval in syntax for pgsql hstore import
\ and " need to be escaped
*/
void keyval2hstore(char *hstring, struct keyval *tags)
{
keyval2hstore_manual(hstring, tags->key, tags->value);
}
void keyval2hstore_manual(char *hstring, char *key, char *value)
{
static char* str=NULL;
static size_t stlen=0;
size_t len;
len=strlen(value);
if (len>stlen) {
stlen=len;
str=realloc(str,1+stlen*2);
}
len=strlen(key);
if (len>stlen) {
stlen=len;
str=realloc(str,1+stlen*2);
}
escape4hstore(str,key);
hstring+=sprintf(hstring,"\"%s\"=>",str);
escape4hstore(str,value);
sprintf(hstring,"\"%s\"",str);
}