-
Notifications
You must be signed in to change notification settings - Fork 3
/
mkconfig.c
344 lines (279 loc) · 8.51 KB
/
mkconfig.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
#define MAX_SHAPEFILES 32
#define MAX_COLUMNS 32
#define MAX_KEYS 32
#define BUFSIZE 1024
regex_t r_file;
regex_t r_column;
regex_t r_key;
regex_t r_where;
#define bool int
#define true 1
#define false 0
struct shape_column {
int type;
char *name;
int nkeys;
char *keys[MAX_KEYS];
};
struct shape_file {
int id;
int format;
char* name;
int ncolumns;
struct shape_column *columns[MAX_COLUMNS];
int nwheres;
char *wheres[MAX_KEYS];
};
int file_count;
struct shape_file *all[MAX_SHAPEFILES];
struct shape_file *current_shape_file;
char *SHP_TYPES[]={ "SHPT_POINT", "SHPT_ARC", "SHPT_POLYGON" };
char *COL_TYPES[]={ "FTInteger", "FTString", "FTDouble" };
char *COL_SIZES[]={ "11", "48", "11, 5" };
char *OSM_TYPE[] ={ "node", "way", "polygon" };
void setup_regex() {
int ret;
if((ret=regcomp(&r_file, "^\\s*file\\s+([a-z]+)\\s+\"([^\"]*)\"", REG_EXTENDED))) {
fprintf(stderr, "Error compiling regular expression: %d\n", ret);
exit(1);
}
if((ret=regcomp(&r_column, "^\\s*column\\s+([a-z]+)\\s+\"([^\"]*)\"", REG_EXTENDED))) {
fprintf(stderr, "Error compiling regular expression: %d\n", ret);
exit(1);
}
if((ret=regcomp(&r_where, "^\\s*where", REG_EXTENDED))) {
fprintf(stderr, "Error compiling regular expression: %d\n", ret);
exit(1);
}
if((ret=regcomp(&r_key, "^\\s*key\\s+\"([^\"]*)\"", REG_EXTENDED))) {
fprintf(stderr, "Error compiling regular expression: %d\n", ret);
exit(1);
}
}
void write_files_setup() {
int i, col_i;
for(i=0; i<file_count; i++) {
current_shape_file=all[i];
printf(" shapefile_new(%d, %s, \"%s\", %i", current_shape_file->id, SHP_TYPES[current_shape_file->format], current_shape_file->name, current_shape_file->ncolumns+1);
printf(",\n \"osm_id\", FTInteger, 11");
for(col_i=0; col_i<current_shape_file->ncolumns; col_i++) {
struct shape_column *column=current_shape_file->columns[col_i];
printf(",\n \"%s\", %s, %s", column->name, COL_TYPES[column->type], COL_SIZES[column->type]);
}
printf(");\n\n");
}
}
void write_files_parse(int format) {
int i, col_i, where_i;
for(i=0; i<file_count; i++) {
current_shape_file=all[i];
if(current_shape_file->format!=format)
continue;
printf(" {\n");
for(where_i=0; where_i<current_shape_file->nwheres; where_i++) {
printf(" const char *where%d = g_hash_table_lookup(current_tags, \"%s\");\n", where_i, current_shape_file->wheres[where_i]);
}
for(col_i=0; col_i<current_shape_file->ncolumns; col_i++) {
struct shape_column *column=current_shape_file->columns[col_i];
if(column->nkeys==0) {
printf(" const char *col%d = g_hash_table_lookup(current_tags, \"%s\");\n", col_i, column->name);
}
else {
int key_i;
printf(" const char *col%d = g_hash_table_lookup(current_tags, \"%s\");\n", col_i, column->keys[0]);
for(key_i=1; key_i<column->nkeys; key_i++) {
printf(" if (!col%d) col%d = g_hash_table_lookup(current_tags, \"%s\");\n", col_i, col_i, column->keys[key_i]);
}
}
}
/* if(current_shape_file->nwheres==0) {
int col_i;
printf("\tif (");
if(current_shape_file->ncolumns>1)
printf("(col0)");
for(col_i=1; col_i<current_shape_file->ncolumns; col_i++) {
printf(" || (col%d)", col_i);
}
printf(") {\n");
} */
if(current_shape_file->nwheres>0) {
int where_i;
printf(" if (");
printf("(where%d)", 0);
for(where_i=1; where_i<current_shape_file->nwheres; where_i++) {
printf(" || (where%d)", where_i);
}
printf(") {\n");
}
else {
printf(" {\n");
}
printf(" shapefile_add_%s(%i, current_id", OSM_TYPE[current_shape_file->format], current_shape_file->id);
for(col_i=0; col_i<current_shape_file->ncolumns; col_i++) {
struct shape_column *column=current_shape_file->columns[col_i];
switch(column->type) {
case 0:
printf(", col%d ? atoi(col%d) : 0", col_i, col_i);
break;
case 1:
printf(", col%d", col_i);
break;
case 2:
printf(", col%d ? atof(col%d) : 0.0", col_i, col_i);
break;
}
}
printf(");\n }\n");
printf(" }\n\n");
}
}
bool parse_line_file(char *row) {
regmatch_t matches[10];
char tmp[BUFSIZE];
int tmpi;
if(!(regexec(&r_file, row, 3, matches, 0))) {
current_shape_file=malloc(sizeof(struct shape_file));
all[file_count]=current_shape_file;
current_shape_file->id=file_count++;
current_shape_file->ncolumns=0;
current_shape_file->nwheres=0;
strncpy(tmp, row+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so);
tmp[matches[1].rm_eo-matches[1].rm_so]='\0';
tmpi=
(!strcmp(tmp, "point")?0:
!strcmp(tmp, "line")?1:
!strcmp(tmp, "polygon")?2:-1);
if(tmpi==-1) {
fprintf(stderr, "Error parsing type of shapefile, must be point, line or polygon: %s\n", tmp);
exit(1);
}
current_shape_file->format=tmpi;
strncpy(tmp, row+matches[2].rm_so, matches[2].rm_eo-matches[2].rm_so);
tmp[matches[2].rm_eo-matches[2].rm_so]='\0';
current_shape_file->name=malloc(strlen(tmp)+1);
strcpy(current_shape_file->name, tmp);
return true;
}
return false;
}
bool parse_line_column(char *row) {
regmatch_t matches[10];
char tmp[BUFSIZE];
int tmpi;
if(!(regexec(&r_column, row, 3, matches, 0))) {
struct shape_column *column=malloc(sizeof(struct shape_file));
current_shape_file->columns[current_shape_file->ncolumns++]=column;
strncpy(tmp, row+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so);
tmp[matches[1].rm_eo-matches[1].rm_so]='\0';
tmpi=
(!strcmp(tmp, "int")?0:
!strcmp(tmp, "string")?1:
!strcmp(tmp, "float")?2:-1);
if(tmpi==-1) {
fprintf(stderr, "Error parsing type of column, must be int, string or float: %s\n", tmp);
exit(1);
}
column->type=tmpi;
strncpy(tmp, row+matches[2].rm_so, matches[2].rm_eo-matches[2].rm_so);
tmp[matches[2].rm_eo-matches[2].rm_so]='\0';
column->name=malloc(strlen(tmp)+1);
strcpy(column->name, tmp);
row=row+matches[0].rm_eo+1;
column->nkeys=0;
while(strlen(row)) {
if(regexec(&r_key, row, 2, matches, 0))
return false;
strncpy(tmp, row+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so);
tmp[matches[1].rm_eo-matches[1].rm_so]='\0';
column->keys[column->nkeys]=malloc(strlen(tmp)+1);
strcpy(column->keys[column->nkeys], tmp);
column->nkeys++;
row=row+matches[0].rm_eo+1;
}
return true;
}
return false;
}
bool parse_line_where(char *row) {
regmatch_t matches[10];
char tmp[BUFSIZE];
int tmpi;
if(!(regexec(&r_where, row, 1, matches, 0))) {
row=row+matches[0].rm_eo+1;
while(strlen(row)) {
if(regexec(&r_key, row, 2, matches, 0))
return false;
strncpy(tmp, row+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so);
tmp[matches[1].rm_eo-matches[1].rm_so]='\0';
current_shape_file->wheres[current_shape_file->nwheres]=malloc(strlen(tmp)+1);
strcpy(current_shape_file->wheres[current_shape_file->nwheres], tmp);
current_shape_file->nwheres++;
row=row+matches[0].rm_eo+1;
}
return true;
}
return false;
}
bool parse_line(char *row) {
if(row[0]=='\0')
return true;
if(row[0]=='#')
return true;
return parse_line_file(row) ||
parse_line_column(row) ||
parse_line_where(row);
}
void rtrim(char *str) {
char c;
int p=strlen(str);
bool changed=true;
while( p-- && changed ) {
changed=false;
c=str[p];
if((c=='\n')||(c=='\r')||(c==' ')||(c=='\t')) {
str[p]='\0';
changed=true;
}
}
}
void main() {
setup_regex();
file_count=0;
FILE *f;
char row[BUFSIZE];
int line=0;
f=fopen("osm2shp.cfg", "r");
while(fgets(row, BUFSIZE, f)) {
line++;
rtrim(row);
if(!parse_line(row)) {
fprintf(stderr, "Error parsing line %d: '%s'\n", line, row);
exit(1);
}
}
regex_t r_type;
regcomp(&r_type, "%TYPE_([0-9])%", REG_EXTENDED);
FILE *template;
char tmp[BUFSIZE];
regmatch_t matches[10];
int i;
template=fopen("config.template", "r");
while(fgets(row, BUFSIZE, template)) {
if(!strncmp("%SETUP%", row, 7)) {
write_files_setup();
}
else if(!regexec(&r_type, row, 2, matches, 0)) {
strncpy(tmp, row+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so);
tmp[matches[1].rm_eo-matches[1].rm_so]='\0';
write_files_parse(atoi(tmp));
}
else {
printf("%s", row);
}
}
}