/[zanavi_public1]/navit/navit/maptool/itembin.c
ZANavi

Diff of /navit/navit/maptool/itembin.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 36 Revision 37
1/**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
1/** 20/**
2 * Navit, a modular navigation system. 21 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2011 Navit Team 22 * Copyright (C) 2005-2011 Navit Team
4 * 23 *
5 * This program is free software; you can redistribute it and/or 24 * This program is free software; you can redistribute it and/or
25#include "maptool.h" 44#include "maptool.h"
26#include "linguistics.h" 45#include "linguistics.h"
27#include "file.h" 46#include "file.h"
28#include "debug.h" 47#include "debug.h"
29 48
30
31
32int
33item_bin_read(struct item_bin *ib, FILE *in) 49int item_bin_read(struct item_bin *ib, FILE *in)
34{ 50{
35 if (fread(ib, 4, 1, in) == 0) 51 if (fread(ib, 4, 1, in) == 0)
36 return 0; 52 return 0;
37 if (!ib->len) 53 if (!ib->len)
38 return 1; 54 return 1;
39 if (fread((unsigned char *)ib+4, ib->len*4, 1, in)) 55 if (fread((unsigned char *) ib + 4, ib->len * 4, 1, in))
40 return 2; 56 return 2;
41 return 0; 57 return 0;
42} 58}
43 59
44void
45item_bin_set_type(struct item_bin *ib, enum item_type type) 60void item_bin_set_type(struct item_bin *ib, enum item_type type)
46{ 61{
47 ib->type=type; 62 ib->type = type;
48} 63}
49 64
50void
51item_bin_init(struct item_bin *ib, enum item_type type) 65void item_bin_init(struct item_bin *ib, enum item_type type)
52{ 66{
53 ib->clen=0; 67 ib->clen = 0;
54 ib->len=2; 68 ib->len = 2;
55 item_bin_set_type(ib, type); 69 item_bin_set_type(ib, type);
56} 70}
57 71
58
59void
60item_bin_add_coord(struct item_bin *ib, struct coord *c, int count) 72void item_bin_add_coord(struct item_bin *ib, struct coord *c, int count)
61{ 73{
62 struct coord *c2=(struct coord *)(ib+1); 74 struct coord *c2 = (struct coord *) (ib + 1);
63 c2+=ib->clen/2; 75 c2 += ib->clen / 2;
64 memcpy(c2, c, count*sizeof(struct coord)); 76 memcpy(c2, c, count * sizeof(struct coord));
65 ib->clen+=count*2; 77 ib->clen += count * 2;
66 ib->len+=count*2; 78 ib->len += count * 2;
67} 79}
68 80
69void
70item_bin_add_coord_reverse(struct item_bin *ib, struct coord *c, int count) 81void item_bin_add_coord_reverse(struct item_bin *ib, struct coord *c, int count)
71{ 82{
72 int i; 83 int i;
73 for (i = count-1 ; i >= 0 ; i--) 84 for (i = count - 1; i >= 0; i--)
85 {
74 item_bin_add_coord(ib, &c[i], 1); 86 item_bin_add_coord(ib, &c[i], 1);
87 }
75} 88}
76 89
77void
78item_bin_bbox(struct item_bin *ib, struct rect *r) 90void item_bin_bbox(struct item_bin *ib, struct rect *r)
79{ 91{
80 struct coord c; 92 struct coord c;
81 item_bin_add_coord(ib, &r->l, 1); 93 item_bin_add_coord(ib, &r->l, 1);
82 c.x=r->h.x; 94 c.x = r->h.x;
83 c.y=r->l.y; 95 c.y = r->l.y;
84 item_bin_add_coord(ib, &c, 1); 96 item_bin_add_coord(ib, &c, 1);
85 item_bin_add_coord(ib, &r->h, 1); 97 item_bin_add_coord(ib, &r->h, 1);
86 c.x=r->l.x; 98 c.x = r->l.x;
87 c.y=r->h.y; 99 c.y = r->h.y;
88 item_bin_add_coord(ib, &c, 1); 100 item_bin_add_coord(ib, &c, 1);
89 item_bin_add_coord(ib, &r->l, 1); 101 item_bin_add_coord(ib, &r->l, 1);
90} 102}
91 103
92void
93item_bin_copy_coord(struct item_bin *ib, struct item_bin *from, int dir) 104void item_bin_copy_coord(struct item_bin *ib, struct item_bin *from, int dir)
94{ 105{
95 struct coord *c=(struct coord *)(from+1); 106 struct coord *c = (struct coord *) (from + 1);
96 int i,count=from->clen/2; 107 int i, count = from->clen / 2;
97 if (dir >= 0) { 108 if (dir >= 0)
109 {
98 item_bin_add_coord(ib, c, count); 110 item_bin_add_coord(ib, c, count);
99 return; 111 return;
100 } 112 }
101 for (i = 1 ; i <= count ; i++) 113 for (i = 1; i <= count; i++)
114 {
102 item_bin_add_coord(ib, &c[count-i], 1); 115 item_bin_add_coord(ib, &c[count - i], 1);
116 }
103} 117}
104 118
105void
106item_bin_add_coord_rect(struct item_bin *ib, struct rect *r) 119void item_bin_add_coord_rect(struct item_bin *ib, struct rect *r)
107{ 120{
108 item_bin_add_coord(ib, &r->l, 1); 121 item_bin_add_coord(ib, &r->l, 1);
109 item_bin_add_coord(ib, &r->h, 1); 122 item_bin_add_coord(ib, &r->h, 1);
110} 123}
111 124
112int
113attr_bin_write_data(struct attr_bin *ab, enum attr_type type, void *data, int size) 125int attr_bin_write_data(struct attr_bin *ab, enum attr_type type, void *data, int size)
114{ 126{
115 int pad=(4-(size%4))%4; 127 int pad = (4 - (size % 4)) % 4;
116 ab->type=type; 128 ab->type = type;
117 memcpy(ab+1, data, size); 129 memcpy(ab + 1, data, size);
118 memset((unsigned char *)(ab+1)+size, 0, pad); 130 memset((unsigned char *) (ab + 1) + size, 0, pad);
119 ab->len=(size+pad)/4+1; 131 ab->len = (size + pad) / 4 + 1;
120 return ab->len+1; 132 return ab->len + 1;
121} 133}
122 134
123int
124attr_bin_write_attr(struct attr_bin *ab, struct attr *attr) 135int attr_bin_write_attr(struct attr_bin *ab, struct attr *attr)
125{ 136{
126 return attr_bin_write_data(ab, attr->type, attr_data_get(attr), attr_data_size(attr)); 137 return attr_bin_write_data(ab, attr->type, attr_data_get(attr), attr_data_size(attr));
127} 138}
128 139
129void
130item_bin_add_attr_data(struct item_bin *ib, enum attr_type type, void *data, int size) 140void item_bin_add_attr_data(struct item_bin *ib, enum attr_type type, void *data, int size)
131{ 141{
132 struct attr_bin *ab=(struct attr_bin *)((int *)ib+ib->len+1); 142 struct attr_bin *ab = (struct attr_bin *) ((int *) ib + ib->len + 1);
133 ib->len+=attr_bin_write_data(ab, type, data, size); 143 ib->len += attr_bin_write_data(ab, type, data, size);
134} 144}
135 145
136void
137item_bin_add_attr(struct item_bin *ib, struct attr *attr) 146void item_bin_add_attr(struct item_bin *ib, struct attr *attr)
138{ 147{
139 struct attr_bin *ab=(struct attr_bin *)((int *)ib+ib->len+1); 148 struct attr_bin *ab = (struct attr_bin *) ((int *) ib + ib->len + 1);
140 if (ATTR_IS_GROUP(attr->type)) { 149 if (ATTR_IS_GROUP(attr->type))
150 {
141 int i=0; 151 int i = 0;
142 int *abptr; 152 int *abptr;
143 ab->type=attr->type; 153 ab->type = attr->type;
144 ab->len=1; 154 ab->len = 1;
145 abptr=(int *)(ab+1); 155 abptr = (int *) (ab + 1);
146 while (attr->u.attrs[i].type) { 156 while (attr->u.attrs[i].type)
157 {
147 int size=attr_bin_write_attr((struct attr_bin *)abptr, &attr->u.attrs[i]); 158 int size = attr_bin_write_attr((struct attr_bin *) abptr, &attr->u.attrs[i]);
148 ab->len+=size; 159 ab->len += size;
149 abptr+=size; 160 abptr += size;
150 i++; 161 i++;
151 } 162 }
152 ib->len+=ab->len+1; 163 ib->len += ab->len + 1;
153 164
165 }
154 } else 166 else
167 {
155 ib->len+=attr_bin_write_attr(ab, attr); 168 ib->len += attr_bin_write_attr(ab, attr);
156 169 }
157} 170}
158 171
159void 172static char *coord_to_str(struct coord *c);
173
160item_bin_remove_attr(struct item_bin *ib, void *ptr) 174void item_bin_remove_attr(struct item_bin *ib, void *ptr)
161{ 175{
162 unsigned char *s=(unsigned char *)ib; 176 unsigned char *s = (unsigned char *) ib;
163 unsigned char *e=s+(ib->len+1)*4; 177 unsigned char *e = s + (ib->len + 1) * 4;
164 s+=sizeof(struct item_bin)+ib->clen*4; 178 s += sizeof(struct item_bin) + ib->clen * 4;
165 while (s < e) { 179 while (s < e)
180 {
166 struct attr_bin *ab=(struct attr_bin *)s; 181 struct attr_bin *ab = (struct attr_bin *) s;
167 s+=(ab->len+1)*4; 182 s += (ab->len + 1) * 4;
168 if ((void *)(ab+1) == ptr) { 183 if ((void *) (ab + 1) == ptr)
169 ib->len-=ab->len+1; 184 {
170 memmove(ab,s,e-s); 185 ib->len -= ab->len + 1;
171 return; 186 memmove(ab, s, e - s);
172 } 187 return;
173 } 188 }
189 }
174} 190}
175 191
176void
177item_bin_add_attr_int(struct item_bin *ib, enum attr_type type, int val) 192void item_bin_add_attr_int(struct item_bin *ib, enum attr_type type, int val)
178{ 193{
179 struct attr attr; 194 struct attr attr;
180 attr.type=type; 195 attr.type = type;
181 attr.u.num=val; 196 attr.u.num = val;
182 item_bin_add_attr(ib, &attr); 197 item_bin_add_attr(ib, &attr);
183} 198}
184 199
185void * 200void *
186item_bin_get_attr(struct item_bin *ib, enum attr_type type, void *last) 201item_bin_get_attr(struct item_bin *ib, enum attr_type type, void *last)
187{ 202{
188 unsigned char *s=(unsigned char *)ib; 203 unsigned char *s = (unsigned char *) ib;
189 unsigned char *e=s+(ib->len+1)*4; 204 unsigned char *e = s + (ib->len + 1) * 4;
190 s+=sizeof(struct item_bin)+ib->clen*4; 205 s += sizeof(struct item_bin) + ib->clen * 4;
191 while (s < e) { 206 while (s < e)
207 {
192 struct attr_bin *ab=(struct attr_bin *)s; 208 struct attr_bin *ab = (struct attr_bin *) s;
193 s+=(ab->len+1)*4; 209 s += (ab->len + 1) * 4;
194 if (ab->type == type && (void *)(ab+1) > last) { 210 if (ab->type == type && (void *) (ab + 1) > last)
211 {
195 return (ab+1); 212 return (ab + 1);
196 } 213 }
197 } 214 }
198 return NULL; 215 return NULL;
199} 216}
200 217
201struct attr_bin * 218struct attr_bin *
202item_bin_get_attr_bin_last(struct item_bin *ib) 219item_bin_get_attr_bin_last(struct item_bin *ib)
203{ 220{
204 struct attr_bin *ab=NULL; 221 struct attr_bin *ab = NULL;
205 unsigned char *s=(unsigned char *)ib; 222 unsigned char *s = (unsigned char *) ib;
206 unsigned char *e=s+(ib->len+1)*4; 223 unsigned char *e = s + (ib->len + 1) * 4;
207 s+=sizeof(struct item_bin)+ib->clen*4; 224 s += sizeof(struct item_bin) + ib->clen * 4;
208 while (s < e) { 225 while (s < e)
226 {
209 ab=(struct attr_bin *)s; 227 ab = (struct attr_bin *) s;
210 s+=(ab->len+1)*4; 228 s += (ab->len + 1) * 4;
211 } 229 }
212 return ab; 230 return ab;
213} 231}
214 232
215void
216item_bin_add_attr_longlong(struct item_bin *ib, enum attr_type type, long long val) 233void item_bin_add_attr_longlong(struct item_bin *ib, enum attr_type type, long long val)
217{ 234{
218 struct attr attr; 235 struct attr attr;
219 attr.type=type; 236 attr.type = type;
220 attr.u.num64=&val; 237 attr.u.num64 = &val;
221 item_bin_add_attr(ib, &attr); 238 item_bin_add_attr(ib, &attr);
222} 239}
223 240
224void
225item_bin_add_attr_string(struct item_bin *ib, enum attr_type type, char *str) 241void item_bin_add_attr_string(struct item_bin *ib, enum attr_type type, char *str)
226{ 242{
227 struct attr attr; 243 struct attr attr;
228 if (! str) 244 if (!str)
229 return; 245 return;
230 attr.type=type; 246 attr.type = type;
231 attr.u.str=str; 247 attr.u.str = str;
232 item_bin_add_attr(ib, &attr); 248 item_bin_add_attr(ib, &attr);
233} 249}
234 250
235void
236item_bin_add_attr_range(struct item_bin *ib, enum attr_type type, short min, short max) 251void item_bin_add_attr_range(struct item_bin *ib, enum attr_type type, short min, short max)
237{ 252{
238 struct attr attr; 253 struct attr attr;
239 attr.type=type; 254 attr.type = type;
240 attr.u.range.min=min; 255 attr.u.range.min = min;
241 attr.u.range.max=max; 256 attr.u.range.max = max;
242 item_bin_add_attr(ib, &attr); 257 item_bin_add_attr(ib, &attr);
243} 258}
244 259
245void 260void item_bin_write_xml(struct item_bin *ib, char *filename)
246item_bin_write(struct item_bin *ib, FILE *out)
247{ 261{
248 if (debug_itembin(ib)) 262 FILE *out = tempfile("", filename, 2);
249 {
250 fprintf(stderr,"== item_bin_write == FILE: %p ==\n",out);
251 dump_itembin(ib);
252 fprintf(stderr,"== item_bin_write == END ==\n");
253 }
254 263
255 // long long t=(ib->len+1)*4;
256 // fprintf(stderr,"item_bin_write:fwrite "LONGLONG_FMT"\n", t);
257 fwrite(ib, (ib->len+1)*4, 1, out);
258}
259
260struct item_bin *
261item_bin_dup(struct item_bin *ib)
262{
263 int len=(ib->len+1)*4;
264 struct item_bin *ret=g_malloc(len);
265 memcpy(ret, ib, len);
266
267 return ret;
268}
269
270void
271item_bin_write_range(struct item_bin *ib, FILE *out, int min, int max)
272{
273 struct range r;
274
275 r.min=min;
276 r.max=max;
277 fwrite(&r, sizeof(r), 1, out);
278 item_bin_write(ib, out);
279}
280
281
282void
283item_bin_write_clipped(struct item_bin *ib, struct tile_parameter *param, struct item_bin_sink *out)
284{
285 struct tile_data tile_data;
286 int i;
287 bbox((struct coord *)(ib+1), ib->clen/2, &tile_data.item_bbox);
288 tile_data.buffer[0]='\0';
289 tile_data.tile_depth=tile(&tile_data.item_bbox, NULL, tile_data.buffer, param->max, param->overlap, &tile_data.tile_bbox);
290 if (tile_data.tile_depth == param->max || tile_data.tile_depth >= param->min) {
291 item_bin_write_to_sink(ib, out, &tile_data);
292 return;
293 }
294 for (i = 0 ; i < 4 ; i++) {
295 struct rect clip_rect;
296 tile_data.buffer[tile_data.tile_depth]='a'+i;
297 tile_data.buffer[tile_data.tile_depth+1]='\0';
298 tile_bbox(tile_data.buffer, &clip_rect, param->overlap);
299 if (ib->type < type_area)
300 clip_line(ib, &clip_rect, param, out);
301 else
302 clip_polygon(ib, &clip_rect, param, out);
303 }
304}
305
306static char *
307coord_to_str(struct coord *c)
308{
309 int x=c->x;
310 int y=c->y;
311 char *sx="";
312 char *sy="";
313 if (x < 0) {
314 sx="-";
315 x=-x;
316 }
317 if (y < 0) {
318 sy="-";
319 y=-y;
320 }
321 return g_strdup_printf("%s0x%x %s0x%x",sx,x,sy,y);
322}
323
324static void
325dump_coord(struct coord *c, FILE *out)
326{
327 char *str=coord_to_str(c);
328 fprintf(out,"%s",str);
329 g_free(str);
330}
331
332
333void
334item_bin_dump(struct item_bin *ib, FILE *out)
335{
336 struct coord *c; 264 struct coord *c;
337 struct attr_bin *a; 265 struct attr_bin *a;
338 struct attr attr; 266 struct attr attr;
339 int *attr_start; 267 int *attr_start;
340 int *attr_end; 268 int *attr_end;
341 int i; 269 int i;
342 char *str; 270 char *str;
343 271
272 c = (struct coord *) (ib + 1);
273
274 // LOTS of output ---------
275 if (ib->type < type_line)
276 {
277 //dump_coord(c, out);
278 // fprintf(out, " ");
279 }
280 // LOTS of output ---------
281
282
283 attr_start = (int *) (ib + 1) + ib->clen;
284 attr_end = (int *) ib + ib->len + 1;
285
286
287 // fprintf(out, "type=%s", item_to_name(ib->type));
288 // char *item_type = item_to_name(ib->type);
289
290 fprintf(out, "<way visible=\"true\" version=\"1\"");
291 // fprintf(out, "boundary=administrative,admin_level=2");
292
293// "w boundary=administrative,admin_level=2 border_country\n"
294// "w boundary=territorial,admin_level=2 border_country\n"
295// "w boundary=maritime,admin_level=2 border_country\n"
296// "w administrative=boundary,admin_level=2 border_country\n"
297// "w boundary=administrative,maritime=yes,admin_level=2 border_country\n"
298// "w boundary=administrative,maritime=yes,admin_level=4 border_country\n"
299// "w boundary=administrative,border_type=state,admin_level=4 border_country\n" };
300
301
302 while (attr_start < attr_end)
303 {
304 a = (struct attr_bin *) (attr_start);
305 attr_start += a->len + 1;
306 attr.type = a->type;
307 attr_data_set(&attr, (a + 1));
308 str = attr_to_text(&attr, NULL, 1);
309
310 if (!strcmp("osm_wayid", attr_to_name(a->type)))
311 {
312 fprintf(out, " id=\"%s\"", str);
313 }
314 else
315 {
316 fprintf(out, " %s=\"%s\"", attr_to_name(a->type), str);
317 }
318 g_free(str);
319 }
320 // fprintf(out, " debug=\"length=%d\"", ib->len);
321 fprintf(out, ">\n");
322
323 // LOTS of output -----------
324 if (ib->type >= type_line)
325 {
326 // long x_;
327 long y_;
328 for (i = 0; i < ib->clen / 2; i++)
329 {
330 // x_ = (c + i)->x;
331 y_ = (c + i)->y;
332 fprintf(out, "<nd ref=\"%lu\"/>\n", y_);
333 }
334 }
335 // LOTS of output -----------
336
337 fprintf(out, "<tag k=\"admin_level\" v=\"2\"/>\n");
338 fprintf(out, "<tag k=\"boundary\" v=\"administrative\"/>\n");
339 fprintf(out, "</way>\n");
340
341 //if (debug_itembin(ib))
342 //{
343 // fprintf(stderr,"\n== item_bin_dump == END ==\n");
344 //}
345
346 fclose(out);
347}
348
349void item_bin_write(struct item_bin *ib, FILE *out)
350{
351 if (debug_itembin(ib))
352 {
353 fprintf(stderr, "== item_bin_write == FILE: %p ==\n", out);
354 dump_itembin(ib);
355 fprintf(stderr, "== item_bin_write == END ==\n");
356 }
357
358 // long long t=(ib->len+1)*4;
359 // fprintf(stderr,"item_bin_write:fwrite "LONGLONG_FMT"\n", t);
360 fwrite(ib, (ib->len + 1) * 4, 1, out);
361}
362
363struct item_bin *
364item_bin_dup(struct item_bin *ib)
365{
366 int len = (ib->len + 1) * 4;
367 struct item_bin *ret = g_malloc(len);
368 memcpy(ret, ib, len);
369
370 return ret;
371}
372
373void item_bin_write_range(struct item_bin *ib, FILE *out, int min, int max)
374{
375 struct range r;
376
377 r.min = min;
378 r.max = max;
379 fwrite(&r, sizeof(r), 1, out);
380 item_bin_write(ib, out);
381}
382
383void item_bin_write_clipped(struct item_bin *ib, struct tile_parameter *param, struct item_bin_sink *out)
384{
385 struct tile_data tile_data;
386 int i;
387 bbox((struct coord *) (ib + 1), ib->clen / 2, &tile_data.item_bbox);
388 tile_data.buffer[0] = '\0';
389 tile_data.tile_depth = tile(&tile_data.item_bbox, NULL, tile_data.buffer, param->max, param->overlap, &tile_data.tile_bbox);
390 if (tile_data.tile_depth == param->max || tile_data.tile_depth >= param->min)
391 {
392 item_bin_write_to_sink(ib, out, &tile_data);
393 return;
394 }
395 for (i = 0; i < 4; i++)
396 {
397 struct rect clip_rect;
398 tile_data.buffer[tile_data.tile_depth] = 'a' + i;
399 tile_data.buffer[tile_data.tile_depth + 1] = '\0';
400 tile_bbox(tile_data.buffer, &clip_rect, param->overlap);
401 if (ib->type < type_area)
402 clip_line(ib, &clip_rect, param, out);
403 else
404 clip_polygon(ib, &clip_rect, param, out);
405 }
406}
407
408static char *
409coord_to_str(struct coord *c)
410{
411 int x = c->x;
412 int y = c->y;
413 char *sx = "";
414 char *sy = "";
415
416 if (x < 0)
417 {
418 sx = "-";
419 x = -x;
420 }
421
422 if (y < 0)
423 {
424 sy = "-";
425 y = -y;
426 }
427
428 return g_strdup_printf("%s0x%x %s0x%x", sx, x, sy, y);
429}
430
431static void dump_coord(struct coord *c, FILE *out)
432{
433 char *str = coord_to_str(c);
434 fprintf(out, "%s", str);
435 g_free(str);
436}
437
438void item_bin_dump(struct item_bin *ib, FILE *out)
439{
440 struct coord *c;
441 struct attr_bin *a;
442 struct attr attr;
443 int *attr_start;
444 int *attr_end;
445 int i;
446 char *str;
447
344 //if (debug_itembin(ib)) 448 //if (debug_itembin(ib))
345 //{ 449 //{
346 // fprintf(stderr,"\n== item_bin_dump == START ==\n"); 450 // fprintf(stderr,"\n== item_bin_dump == START ==\n");
347 //} 451 //}
348 452
349 c=(struct coord *)(ib+1); 453 c = (struct coord *) (ib + 1);
454
455 // LOTS of output ---------
350 //if (ib->type < type_line) { 456 if (ib->type < type_line)
457 {
351 // dump_coord(c,out); 458 dump_coord(c, out);
352 // fprintf(out, " "); 459 fprintf(out, " ");
353 //} 460 }
461 // LOTS of output ---------
462
463
354 attr_start=(int *)(ib+1)+ib->clen; 464 attr_start = (int *) (ib + 1) + ib->clen;
355 attr_end=(int *)ib+ib->len+1; 465 attr_end = (int *) ib + ib->len + 1;
356 fprintf(out,"type=%s", item_to_name(ib->type)); 466 fprintf(out, "type=%s", item_to_name(ib->type));
357 while (attr_start < attr_end) { 467 while (attr_start < attr_end)
468 {
358 a=(struct attr_bin *)(attr_start); 469 a = (struct attr_bin *) (attr_start);
359 attr_start+=a->len+1; 470 attr_start += a->len + 1;
360 attr.type=a->type; 471 attr.type = a->type;
361 attr_data_set(&attr, (a+1)); 472 attr_data_set(&attr, (a + 1));
362 str=attr_to_text(&attr, NULL, 1); 473 str = attr_to_text(&attr, NULL, 1);
363 fprintf(out," %s=\"%s\"", attr_to_name(a->type), str); 474 fprintf(out, " %s=\"%s\"", attr_to_name(a->type), str);
364 g_free(str); 475 g_free(str);
365 } 476 }
366 fprintf(out," debug=\"length=%d\"", ib->len); 477 fprintf(out, " debug=\"length=%d\"", ib->len);
367 fprintf(out,"\n"); 478 fprintf(out, "\n");
479
480 // LOTS of output -----------
368 //if (ib->type >= type_line) { 481 if (ib->type >= type_line)
482 {
369 // for (i = 0 ; i < ib->clen/2 ; i++) { 483 for (i = 0; i < ib->clen / 2; i++)
484 {
370 // dump_coord(c+i,out); 485 dump_coord(c + i, out);
371 // fprintf(out,"\n"); 486 fprintf(out, "\n");
372 // } 487 }
373 //} 488 }
489 // LOTS of output -----------
374 490
375 //if (debug_itembin(ib)) 491 //if (debug_itembin(ib))
376 //{ 492 //{
377 // fprintf(stderr,"\n== item_bin_dump == END ==\n"); 493 // fprintf(stderr,"\n== item_bin_dump == END ==\n");
378 //} 494 //}
379} 495}
380 496
381void
382dump_itembin(struct item_bin *ib) 497void dump_itembin(struct item_bin *ib)
383{ 498{
384 // item_bin_dump(ib, stdout); 499 // item_bin_dump(ib, stdout);
385 item_bin_dump(ib, stderr); 500 item_bin_dump(ib, stderr);
386} 501}
387 502
388struct population_table { 503struct population_table
504{
389 enum item_type type; 505 enum item_type type;
390 int population; 506 int population;
391}; 507};
392 508
393static struct population_table town_population[] = { 509static struct population_table town_population[] =
510{
394 {type_town_label_0e0,0}, 511{ type_town_label_0e0, 0 },
395 {type_town_label_1e0,1}, 512{ type_town_label_1e0, 1 },
396 {type_town_label_2e0,2}, 513{ type_town_label_2e0, 2 },
397 {type_town_label_5e0,5}, 514{ type_town_label_5e0, 5 },
398 {type_town_label_1e1,10}, 515{ type_town_label_1e1, 10 },
399 {type_town_label_2e1,20}, 516{ type_town_label_2e1, 20 },
400 {type_town_label_5e1,50}, 517{ type_town_label_5e1, 50 },
401 {type_town_label_1e2,100}, 518{ type_town_label_1e2, 100 },
402 {type_town_label_2e2,200}, 519{ type_town_label_2e2, 200 },
403 {type_town_label_5e2,500}, 520{ type_town_label_5e2, 500 },
404 {type_town_label_1e3,1000}, 521{ type_town_label_1e3, 1000 },
405 {type_town_label_2e3,2000}, 522{ type_town_label_2e3, 2000 },
406 {type_town_label_5e3,5000}, 523{ type_town_label_5e3, 5000 },
407 {type_town_label_1e4,10000}, 524{ type_town_label_1e4, 10000 },
408 {type_town_label_2e4,20000}, 525{ type_town_label_2e4, 20000 },
409 {type_town_label_5e4,50000}, 526{ type_town_label_5e4, 50000 },
410 {type_town_label_1e5,100000}, 527{ type_town_label_1e5, 100000 },
411 {type_town_label_2e5,200000}, 528{ type_town_label_2e5, 200000 },
412 {type_town_label_5e5,500000}, 529{ type_town_label_5e5, 500000 },
413 {type_town_label_1e6,1000000}, 530{ type_town_label_1e6, 1000000 },
414 {type_town_label_2e6,2000000}, 531{ type_town_label_2e6, 2000000 },
415 {type_town_label_5e6,5000000}, 532{ type_town_label_5e6, 5000000 },
416 {type_town_label_1e7,10000000}, 533{ type_town_label_1e7, 10000000 }, };
417};
418 534
419static struct population_table district_population[] = { 535static struct population_table district_population[] =
536{
420 {type_district_label_0e0,0}, 537{ type_district_label_0e0, 0 },
421 {type_district_label_1e0,1}, 538{ type_district_label_1e0, 1 },
422 {type_district_label_2e0,2}, 539{ type_district_label_2e0, 2 },
423 {type_district_label_5e0,5}, 540{ type_district_label_5e0, 5 },
424 {type_district_label_1e1,10}, 541{ type_district_label_1e1, 10 },
425 {type_district_label_2e1,20}, 542{ type_district_label_2e1, 20 },
426 {type_district_label_5e1,50}, 543{ type_district_label_5e1, 50 },
427 {type_district_label_1e2,100}, 544{ type_district_label_1e2, 100 },
428 {type_district_label_2e2,200}, 545{ type_district_label_2e2, 200 },
429 {type_district_label_5e2,500}, 546{ type_district_label_5e2, 500 },
430 {type_district_label_1e3,1000}, 547{ type_district_label_1e3, 1000 },
431 {type_district_label_2e3,2000}, 548{ type_district_label_2e3, 2000 },
432 {type_district_label_5e3,5000}, 549{ type_district_label_5e3, 5000 },
433 {type_district_label_1e4,10000}, 550{ type_district_label_1e4, 10000 },
434 {type_district_label_2e4,20000}, 551{ type_district_label_2e4, 20000 },
435 {type_district_label_5e4,50000}, 552{ type_district_label_5e4, 50000 },
436 {type_district_label_1e5,100000}, 553{ type_district_label_1e5, 100000 },
437 {type_district_label_2e5,200000}, 554{ type_district_label_2e5, 200000 },
438 {type_district_label_5e5,500000}, 555{ type_district_label_5e5, 500000 },
439 {type_district_label_1e6,1000000}, 556{ type_district_label_1e6, 1000000 },
440 {type_district_label_2e6,2000000}, 557{ type_district_label_2e6, 2000000 },
441 {type_district_label_5e6,5000000}, 558{ type_district_label_5e6, 5000000 },
442 {type_district_label_1e7,10000000}, 559{ type_district_label_1e7, 10000000 }, };
443};
444 560
445void
446item_bin_set_type_by_population(struct item_bin *ib, int population) 561void item_bin_set_type_by_population(struct item_bin *ib, int population)
447{ 562{
448 struct population_table *table; 563 struct population_table *table;
449 int i,count; 564 int i, count;
450 565
451 if (population < 0) 566 if (population < 0)
567 {
452 population=0; 568 population = 0;
569 }
570
453 if (item_is_district(*ib)) { 571 if (item_is_district(*ib))
572 {
454 table=district_population; 573 table = district_population;
455 count=sizeof(district_population)/sizeof(district_population[0]); 574 count = sizeof(district_population) / sizeof(district_population[0]);
456 } else { 575 }
576 else
577 {
457 table=town_population; 578 table = town_population;
458 count=sizeof(town_population)/sizeof(town_population[0]); 579 count = sizeof(town_population) / sizeof(town_population[0]);
459 } 580 }
581
460 for (i = 0 ; i < count ; i++) { 582 for (i = 0; i < count; i++)
583 {
461 if (population < table[i].population) 584 if (population < table[i].population)
585 {
462 break; 586 break;
463 } 587 }
588 }
589
464 item_bin_set_type(ib, table[i-1].type); 590 item_bin_set_type(ib, table[i - 1].type);
465} 591}
466 592
467void
468item_bin_town_write_match(struct item_bin *ib, enum attr_type type, enum attr_type match, FILE *out) 593void item_bin_town_write_match(struct item_bin *ib, enum attr_type type, enum attr_type match, FILE *out)
469{ 594{
470 char *word_orig=item_bin_get_attr(ib, type, NULL); 595 char *word_orig = item_bin_get_attr(ib, type, NULL);
471 int i; 596 int i;
472 // ?? int len=ib->len; 597 // ?? int len=ib->len;
473 598
474 if (!word_orig) 599 if (!word_orig)
475 { 600 {
483 608
484 // xx ++ item_bin_add_attr_string(ib, match, str); 609 // xx ++ item_bin_add_attr_string(ib, match, str);
485 610
486 //for (i = 1 ; i < 2 ; i++) 611 //for (i = 1 ; i < 2 ; i++)
487 //{ 612 //{
488 i=1; 613 i = 1;
489 char *str=linguistics_expand_special(word_orig, i); 614 char *str = linguistics_expand_special(word_orig, i);
490 if (str) 615 if (str)
491 { 616 {
492 char *str2; 617 char *str2;
493 str2=linguistics_casefold(str); 618 str2 = linguistics_casefold(str);
494 if (str2) 619 if (str2)
495 { 620 {
496 // ?? ib->len=len-(len-strlen(str2)); 621 // ?? ib->len=len-(len-strlen(str2));
497 //fprintf(stderr,"i=%d match1=%s\n",i,str); 622 //fprintf(stderr,"i=%d match1=%s\n",i,str);
498 //fprintf(stderr,"i=%d match2=%s\n",i,str2); 623 //fprintf(stderr,"i=%d match2=%s\n",i,str2);
499 item_bin_add_attr_string(ib, match, str2); 624 item_bin_add_attr_string(ib, match, str2);
500 g_free(str2); 625 g_free(str2);
501 } 626 }
502 g_free(str); 627 g_free(str);
503 } 628 }
504 //} 629 //}
505 630
506 item_bin_write(ib, out); 631 item_bin_write(ib, out);
507 632
508 //if (word_orig!=NULL) 633 //if (word_orig!=NULL)
509 //{ 634 //{
510 // g_free(word_orig); 635 // g_free(word_orig);
511 //} 636 //}
512} 637}
513 638
514
515void
516item_bin_write_match(struct item_bin *ib, enum attr_type type, enum attr_type match, FILE *out) 639void item_bin_write_match(struct item_bin *ib, enum attr_type type, enum attr_type match, FILE *out)
517{ 640{
518 char *word=item_bin_get_attr(ib, type, NULL); 641 char *word = item_bin_get_attr(ib, type, NULL);
519 int i; 642 int i;
520 //int words=0; 643 //int words=0;
521 int len=ib->len; 644 int len = ib->len;
522 //int first=1; 645 //int first=1;
523 if (!word) 646 if (!word)
524 { 647 {
525 return; 648 return;
526 } 649 }
527 650
528 if (debug_itembin(ib)) 651 if (debug_itembin(ib))
529 { 652 {
530 fprintf(stderr,"###################################\n"); 653 fprintf(stderr, "###################################\n");
531 fprintf(stderr,"###################################\n"); 654 fprintf(stderr, "###################################\n");
532 fprintf(stderr,"name=%s\n",word); 655 fprintf(stderr, "name=%s\n", word);
533 } 656 }
534 657
535 word=linguistics_next_word(word); 658 word = linguistics_next_word(word);
536 659
537 while (word) 660 while (word)
538 { 661 {
539 if (debug_itembin(ib)) 662 if (debug_itembin(ib))
540 { 663 {
541 fprintf(stderr,"word=%s\n",word); 664 fprintf(stderr, "word=%s\n", word);
542 } 665 }
543 for (i = 0 ; i < 3 ; i++) 666 for (i = 0; i < 3; i++)
544 { 667 {
545 char *str=linguistics_expand_special(word, i); 668 char *str = linguistics_expand_special(word, i);
546 if (str) 669 if (str)
547 { 670 {
548 ib->len=len; 671 ib->len = len;
549 if (debug_itembin(ib)) 672 if (debug_itembin(ib))
550 { 673 {
551 fprintf(stderr,"i=%d match=%s\n",i,str); 674 fprintf(stderr, "i=%d match=%s\n", i, str);
552 } 675 }
553 item_bin_add_attr_string(ib, match, str); 676 item_bin_add_attr_string(ib, match, str);
554 item_bin_write(ib, out); 677 item_bin_write(ib, out);
555 g_free(str); 678 g_free(str);
556 } 679 }
557 } 680 }
558 word=linguistics_next_word(NULL); 681 word = linguistics_next_word(NULL);
559 } 682 }
560 683
561 if (word!=NULL) 684 if (word != NULL)
562 { 685 {
563 g_free(word); 686 g_free(word);
564 } 687 }
565} 688}
566 689
567static int
568item_bin_sort_compare(const void *p1, const void *p2) 690static int item_bin_sort_compare(const void *p1, const void *p2)
569{ 691{
570 struct item_bin *ib1=*((struct item_bin **)p1),*ib2=*((struct item_bin **)p2); 692 struct item_bin *ib1 = *((struct item_bin **) p1), *ib2 = *((struct item_bin **) p2);
571 struct attr_bin *attr1,*attr2; 693 struct attr_bin *attr1, *attr2;
572 char *s1,*s2; 694 char *s1, *s2;
573 int ret; 695 int ret;
696
574#if 0 697#if 0
575 dbg_assert(ib1->clen==2); 698 dbg_assert(ib1->clen==2);
576 dbg_assert(ib2->clen==2); 699 dbg_assert(ib2->clen==2);
577 attr1=(struct attr_bin *)((int *)(ib1+1)+ib1->clen); 700 attr1=(struct attr_bin *)((int *)(ib1+1)+ib1->clen);
578 attr2=(struct attr_bin *)((int *)(ib2+1)+ib1->clen); 701 attr2=(struct attr_bin *)((int *)(ib2+1)+ib1->clen);
579#else 702#else
580 attr1=item_bin_get_attr_bin_last(ib1); 703 attr1 = item_bin_get_attr_bin_last(ib1);
581 attr2=item_bin_get_attr_bin_last(ib2); 704 attr2 = item_bin_get_attr_bin_last(ib2);
582#endif 705#endif
583#if 0 706#if 0
584 dbg_assert(attr1->type == attr_town_name || attr1->type == attr_town_name_match); 707 dbg_assert(attr1->type == attr_town_name || attr1->type == attr_town_name_match);
585 dbg_assert(attr2->type == attr_town_name || attr2->type == attr_town_name_match); 708 dbg_assert(attr2->type == attr_town_name || attr2->type == attr_town_name_match);
586#endif 709#endif
587 s1=(char *)(attr1+1); 710 s1 = (char *) (attr1 + 1);
588 s2=(char *)(attr2+1); 711 s2 = (char *) (attr2 + 1);
589 if (attr1->type == attr_house_number && attr2->type == attr_house_number) { 712 if (attr1->type == attr_house_number && attr2->type == attr_house_number)
713 {
590 ret=atoi(s1)-atoi(s2); 714 ret = atoi(s1) - atoi(s2);
591 if (ret) 715 if (ret)
716 {
592 return ret; 717 return ret;
593 } 718 }
719 }
594 ret=strcmp(s1, s2); 720 ret = strcmp(s1, s2);
595 if (!ret) { 721 if (!ret)
722 {
596 int match1=0,match2=0; 723 int match1 = 0, match2 = 0;
597 match1=(attr1->type == attr_town_name_match || attr1->type == attr_district_name_match); 724 match1 = (attr1->type == attr_town_name_match || attr1->type == attr_district_name_match);
598 match2=(attr2->type == attr_town_name_match || attr2->type == attr_district_name_match); 725 match2 = (attr2->type == attr_town_name_match || attr2->type == attr_district_name_match);
599 ret=match1-match2; 726 ret = match1 - match2;
600 } 727 }
728
601#if 0 729#if 0
602 fprintf(stderr,"sort_countries_compare p1=%p p2=%p %s %s\n",p1,p2,s1,s2); 730 fprintf(stderr,"sort_countries_compare p1=%p p2=%p %s %s\n",p1,p2,s1,s2);
603#endif 731#endif
732
604 return ret; 733 return ret;
605} 734}
606 735
607int
608item_bin_sort_file(char *in_file, char *out_file, struct rect *r, int *size) 736int item_bin_sort_file(char *in_file, char *out_file, struct rect *r, int *size)
609{ 737{
610 int j,k,count,rc=0; 738 int j, k, count, rc = 0;
611 struct coord *c; 739 struct coord *c;
612 struct item_bin *ib; 740 struct item_bin *ib;
613 FILE *f; 741 FILE *f;
614 unsigned char *p,**idx,*buffer; 742 unsigned char *p, **idx, *buffer;
615 if (file_get_contents(in_file, &buffer, size)) { 743 if (file_get_contents(in_file, &buffer, size))
744 {
616 ib=(struct item_bin *)buffer; 745 ib = (struct item_bin *) buffer;
617 p=buffer; 746 p = buffer;
618 count=0; 747 count = 0;
619 while (p < buffer+*size) { 748 while (p < buffer + *size)
749 {
620 count++; 750 count++;
621 p+=(*((int *)p)+1)*4; 751 p += (*((int *) p) + 1) * 4;
622 } 752 }
623 idx=malloc(count*sizeof(void *)); 753 idx = malloc(count * sizeof(void *));
624 dbg_assert(idx != NULL); 754 dbg_assert(idx != NULL);
625 p=buffer; 755 p = buffer;
626 for (j = 0 ; j < count ; j++) { 756 for (j = 0; j < count; j++)
757 {
627 idx[j]=p; 758 idx[j] = p;
628 p+=(*((int *)p)+1)*4; 759 p += (*((int *) p) + 1) * 4;
629 } 760 }
630 qsort(idx, count, sizeof(void *), item_bin_sort_compare); 761 qsort(idx, count, sizeof(void *), item_bin_sort_compare);
631 f=fopen(out_file,"wb"); 762 f = fopen(out_file, "wb");
632 for (j = 0 ; j < count ; j++) { 763 for (j = 0; j < count; j++)
764 {
633 ib=(struct item_bin *)(idx[j]); 765 ib = (struct item_bin *) (idx[j]);
634 c=(struct coord *)(ib+1); 766 c = (struct coord *) (ib + 1);
635 fwrite(ib, (ib->len+1)*4, 1, f); 767 fwrite(ib, (ib->len + 1) * 4, 1, f);
636 if (r) { 768 if (r)
769 {
637 for (k = 0 ; k < ib->clen/2 ; k++) { 770 for (k = 0; k < ib->clen / 2; k++)
771 {
638 if (rc) 772 if (rc)
639 bbox_extend(&c[k], r); 773 bbox_extend(&c[k], r);
640 else { 774 else
775 {
641 r->l=c[k]; 776 r->l = c[k];
642 r->h=c[k]; 777 r->h = c[k];
643 } 778 }
644 rc++; 779 rc++;
645 } 780 }
646 } 781 }
647 } 782 }
648 fclose(f); 783 fclose(f);
649 return 1; 784 return 1;
650 } 785 }
651 return 0; 786 return 0;
652} 787}
788

Legend:
Removed from v.36  
changed lines
  Added in v.37

   
Visit the ZANavi Wiki