4 #include "linguistics.h"
13 struct item_bin *ib=(struct item_bin *) buffer;
15 r=fread(ib, sizeof(*ib), 1, in);
19 dbg_assert((ib->len+1)*4 < sizeof(buffer));
20 s=(ib->len+1)*4-sizeof(*ib);
21 r=fread(ib+1, s, 1, in);
29 item_bin_read(struct item_bin *ib, FILE *in)
31 if (fread(ib, 4, 1, in) == 0)
35 if (fread((unsigned char *)ib+4, ib->len*4, 1, in))
41 item_bin_set_type(struct item_bin *ib, enum item_type type)
47 item_bin_init(struct item_bin *ib, enum item_type type)
51 item_bin_set_type(ib, type);
56 item_bin_add_coord(struct item_bin *ib, struct coord *c, int count)
58 struct coord *c2=(struct coord *)(ib+1);
60 memcpy(c2, c, count*sizeof(struct coord));
66 item_bin_bbox(struct item_bin *ib, struct rect *r)
69 item_bin_add_coord(ib, &r->l, 1);
72 item_bin_add_coord(ib, &c, 1);
73 item_bin_add_coord(ib, &r->h, 1);
76 item_bin_add_coord(ib, &c, 1);
77 item_bin_add_coord(ib, &r->l, 1);
81 item_bin_copy_coord(struct item_bin *ib, struct item_bin *from, int dir)
83 struct coord *c=(struct coord *)(from+1);
84 int i,count=from->clen/2;
86 item_bin_add_coord(ib, c, count);
89 for (i = 1 ; i <= count ; i++)
90 item_bin_add_coord(ib, &c[count-i], 1);
94 item_bin_add_coord_rect(struct item_bin *ib, struct rect *r)
96 item_bin_add_coord(ib, &r->l, 1);
97 item_bin_add_coord(ib, &r->h, 1);
101 item_bin_add_attr_data(struct item_bin *ib, enum attr_type type, void *data, int size)
103 struct attr_bin *ab=(struct attr_bin *)((int *)ib+ib->len+1);
104 int pad=(4-(size%4))%4;
106 memcpy(ab+1, data, size);
107 memset((unsigned char *)(ab+1)+size, 0, pad);
108 ab->len=(size+pad)/4+1;
113 item_bin_add_attr(struct item_bin *ib, struct attr *attr)
115 item_bin_add_attr_data(ib, attr->type, attr_data_get(attr), attr_data_size(attr));
119 item_bin_add_attr_int(struct item_bin *ib, enum attr_type type, int val)
124 item_bin_add_attr(ib, &attr);
128 item_bin_get_attr(struct item_bin *ib, enum attr_type type, void *last)
130 unsigned char *s=(unsigned char *)ib;
131 unsigned char *e=s+(ib->len+1)*4;
132 s+=sizeof(struct item_bin)+ib->clen*4;
134 struct attr_bin *ab=(struct attr_bin *)s;
136 if (ab->type == type && (void *)(ab+1) > last) {
144 item_bin_get_attr_bin_last(struct item_bin *ib)
146 struct attr_bin *ab=NULL;
147 unsigned char *s=(unsigned char *)ib;
148 unsigned char *e=s+(ib->len+1)*4;
149 s+=sizeof(struct item_bin)+ib->clen*4;
151 ab=(struct attr_bin *)s;
158 item_bin_add_attr_longlong(struct item_bin *ib, enum attr_type type, long long val)
163 item_bin_add_attr(ib, &attr);
167 item_bin_add_attr_string(struct item_bin *ib, enum attr_type type, char *str)
174 item_bin_add_attr(ib, &attr);
178 item_bin_add_attr_range(struct item_bin *ib, enum attr_type type, short min, short max)
182 attr.u.range.min=min;
183 attr.u.range.max=max;
184 item_bin_add_attr(ib, &attr);
188 item_bin_write(struct item_bin *ib, FILE *out)
190 fwrite(ib, (ib->len+1)*4, 1, out);
194 item_bin_write_clipped(struct item_bin *ib, struct tile_parameter *param, struct item_bin_sink *out)
196 struct tile_data tile_data;
198 bbox((struct coord *)(ib+1), ib->clen/2, &tile_data.item_bbox);
199 tile_data.buffer[0]='\0';
200 tile_data.tile_depth=tile(&tile_data.item_bbox, NULL, tile_data.buffer, param->max, param->overlap, &tile_data.tile_bbox);
201 if (tile_data.tile_depth == param->max || tile_data.tile_depth >= param->min) {
202 item_bin_write_to_sink(ib, out, &tile_data);
205 for (i = 0 ; i < 4 ; i++) {
206 struct rect clip_rect;
207 tile_data.buffer[tile_data.tile_depth]='a'+i;
208 tile_data.buffer[tile_data.tile_depth+1]='\0';
209 tile_bbox(tile_data.buffer, &clip_rect, param->overlap);
210 if (ib->type < type_area)
211 clip_line(ib, &clip_rect, param, out);
213 clip_polygon(ib, &clip_rect, param, out);
218 coord_to_str(struct coord *c)
232 return g_strdup_printf("%s0x%x %s0x%x",sx,x,sy,y);
236 dump_coord(struct coord *c, FILE *out)
238 char *str=coord_to_str(c);
239 fprintf(out,"%s",str);
245 item_bin_dump(struct item_bin *ib, FILE *out)
255 c=(struct coord *)(ib+1);
256 if (ib->type < type_line) {
260 attr_start=(int *)(ib+1)+ib->clen;
261 attr_end=(int *)ib+ib->len+1;
262 fprintf(out,"type=%s", item_to_name(ib->type));
263 while (attr_start < attr_end) {
264 a=(struct attr_bin *)(attr_start);
265 attr_start+=a->len+1;
267 attr_data_set(&attr, (a+1));
268 str=attr_to_text(&attr, NULL, 1);
269 fprintf(out," %s=\"%s\"", attr_to_name(a->type), str);
272 fprintf(out," debug=\"length=%d\"", ib->len);
274 if (ib->type >= type_line) {
275 for (i = 0 ; i < ib->clen/2 ; i++) {
283 dump_itembin(struct item_bin *ib)
285 item_bin_dump(ib, stdout);
288 struct population_table {
293 static struct population_table town_population[] = {
294 {type_town_label_0e0,0},
295 {type_town_label_1e0,1},
296 {type_town_label_2e0,2},
297 {type_town_label_5e0,5},
298 {type_town_label_1e1,10},
299 {type_town_label_2e1,20},
300 {type_town_label_5e1,50},
301 {type_town_label_1e2,100},
302 {type_town_label_2e2,200},
303 {type_town_label_5e2,500},
304 {type_town_label_1e3,1000},
305 {type_town_label_2e3,2000},
306 {type_town_label_5e3,5000},
307 {type_town_label_1e4,10000},
308 {type_town_label_2e4,20000},
309 {type_town_label_5e4,50000},
310 {type_town_label_1e5,100000},
311 {type_town_label_2e5,200000},
312 {type_town_label_5e5,500000},
313 {type_town_label_1e6,1000000},
314 {type_town_label_2e6,2000000},
315 {type_town_label_5e6,5000000},
316 {type_town_label_1e7,10000000},
319 static struct population_table district_population[] = {
320 {type_district_label_0e0,0},
321 {type_district_label_1e0,1},
322 {type_district_label_2e0,2},
323 {type_district_label_5e0,5},
324 {type_district_label_1e1,10},
325 {type_district_label_2e1,20},
326 {type_district_label_5e1,50},
327 {type_district_label_1e2,100},
328 {type_district_label_2e2,200},
329 {type_district_label_5e2,500},
330 {type_district_label_1e3,1000},
331 {type_district_label_2e3,2000},
332 {type_district_label_5e3,5000},
333 {type_district_label_1e4,10000},
334 {type_district_label_2e4,20000},
335 {type_district_label_5e4,50000},
336 {type_district_label_1e5,100000},
337 {type_district_label_2e5,200000},
338 {type_district_label_5e5,500000},
339 {type_district_label_1e6,1000000},
340 {type_district_label_2e6,2000000},
341 {type_district_label_5e6,5000000},
342 {type_district_label_1e7,10000000},
346 item_bin_set_type_by_population(struct item_bin *ib, int population)
348 struct population_table *table;
353 if (item_is_district(*item_bin)) {
354 table=district_population;
355 count=sizeof(district_population)/sizeof(district_population[0]);
357 table=town_population;
358 count=sizeof(town_population)/sizeof(town_population[0]);
360 for (i = 0 ; i < count ; i++) {
361 if (population < table[i].population)
364 item_bin_set_type(ib, table[i-1].type);
369 item_bin_write_match(struct item_bin *ib, enum attr_type type, enum attr_type match, FILE *out)
371 char *word=item_bin_get_attr(ib, type, NULL);
372 int i,words=0,len=ib->len;
376 for (i = 0 ; i < 3 ; i++) {
377 char *str=linguistics_expand_special(word, i);
381 item_bin_add_attr_string(ib, match, str);
382 item_bin_write(ib, out);
386 word=linguistics_next_word(word);
392 item_bin_sort_compare(const void *p1, const void *p2)
394 struct item_bin *ib1=*((struct item_bin **)p1),*ib2=*((struct item_bin **)p2);
395 struct attr_bin *attr1,*attr2;
399 dbg_assert(ib1->clen==2);
400 dbg_assert(ib2->clen==2);
401 attr1=(struct attr_bin *)((int *)(ib1+1)+ib1->clen);
402 attr2=(struct attr_bin *)((int *)(ib2+1)+ib1->clen);
404 attr1=item_bin_get_attr_bin_last(ib1);
405 attr2=item_bin_get_attr_bin_last(ib2);
408 dbg_assert(attr1->type == attr_town_name || attr1->type == attr_town_name_match);
409 dbg_assert(attr2->type == attr_town_name || attr2->type == attr_town_name_match);
411 s1=(char *)(attr1+1);
412 s2=(char *)(attr2+1);
413 if (attr1->type == attr_house_number && attr2->type == attr_house_number) {
414 ret=atoi(s1)-atoi(s2);
420 int match1=0,match2=0;
421 match1=(attr1->type == attr_town_name_match || attr1->type == attr_district_name_match);
422 match2=(attr2->type == attr_town_name_match || attr2->type == attr_district_name_match);
426 fprintf(stderr,"sort_countries_compare p1=%p p2=%p %s %s\n",p1,p2,s1,s2);
432 item_bin_sort_file(char *in_file, char *out_file, struct rect *r, int *size)
438 unsigned char *p,**idx,*buffer;
439 if (file_get_contents(in_file, &buffer, size)) {
440 ib=(struct item_bin *)buffer;
443 while (p < buffer+*size) {
445 p+=(*((int *)p)+1)*4;
447 idx=malloc(count*sizeof(void *));
448 dbg_assert(idx != NULL);
450 for (j = 0 ; j < count ; j++) {
452 p+=(*((int *)p)+1)*4;
454 qsort(idx, count, sizeof(void *), item_bin_sort_compare);
455 f=fopen(out_file,"wb");
456 for (j = 0 ; j < count ; j++) {
457 ib=(struct item_bin *)(idx[j]);
458 c=(struct coord *)(ib+1);
459 fwrite(ib, (ib->len+1)*4, 1, f);