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

Legend:
Removed from v.30  
changed lines
  Added in v.31

   
Visit the ZANavi Wiki