Fix:Core:Correct attribute alloc and free
[navit-package] / navit / attr.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
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
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <glib.h>
24 #include "debug.h"
25 #include "item.h"
26 #include "coord.h"
27 #include "transform.h"
28 #include "color.h"
29 #include "attr.h"
30 #include "map.h"
31 #include "config.h"
32 #include "endianess.h"
33
34 struct attr_name {
35         enum attr_type attr;
36         char *name;
37 };
38
39
40 static struct attr_name attr_names[]={
41 #define ATTR2(x,y) ATTR(y)
42 #define ATTR(x) { attr_##x, #x },
43 #include "attr_def.h"
44 #undef ATTR2
45 #undef ATTR
46 };
47
48 enum attr_type
49 attr_from_name(const char *name)
50 {
51         int i;
52
53         for (i=0 ; i < sizeof(attr_names)/sizeof(struct attr_name) ; i++) {
54                 if (! strcmp(attr_names[i].name, name))
55                         return attr_names[i].attr;
56         }
57         return attr_none;
58 }
59
60
61 static int attr_match(enum attr_type search, enum attr_type found);
62
63
64
65 char *
66 attr_to_name(enum attr_type attr)
67 {
68         int i;
69
70         for (i=0 ; i < sizeof(attr_names)/sizeof(struct attr_name) ; i++) {
71                 if (attr_names[i].attr == attr)
72                         return attr_names[i].name;
73         }
74         return NULL; 
75 }
76
77 struct attr *
78 attr_new_from_text(const char *name, const char *value)
79 {
80         enum attr_type attr;
81         struct attr *ret;
82         struct coord_geo *g;
83         struct coord c;
84         char *pos,*type_str,*str,*tok;
85         int min,max,count;
86
87         ret=g_new0(struct attr, 1);
88         dbg(1,"enter name='%s' value='%s'\n", name, value);
89         attr=attr_from_name(name);
90         ret->type=attr;
91         switch (attr) {
92         case attr_item_type:
93                 ret->u.item_type=item_from_name(value);
94                 break;
95         case attr_item_types:
96                 count=0;
97                 type_str=g_strdup(value);
98                 str=type_str;
99                 while ((tok=strtok(str, ","))) {
100                         ret->u.item_types=g_realloc(ret->u.item_types, (count+2)*sizeof(enum item_type));
101                         ret->u.item_types[count++]=item_from_name(tok);
102                         ret->u.item_types[count]=type_none;
103                         str=NULL;
104                 }
105                 g_free(type_str);
106                 break;
107         case attr_attr_types:
108                 count=0;
109                 type_str=g_strdup(value);
110                 str=type_str;
111                 while ((tok=strtok(str, ","))) {
112                         ret->u.attr_types=g_realloc(ret->u.attr_types, (count+2)*sizeof(enum attr_type));
113                         ret->u.attr_types[count++]=attr_from_name(tok);
114                         ret->u.attr_types[count]=attr_none;
115                         str=NULL;
116                 }
117                 g_free(type_str);
118                 break;
119         case attr_dash:
120                 count=0;
121                 type_str=g_strdup(value);
122                 str=type_str;
123                 while ((tok=strtok(str, ","))) {
124                         ret->u.dash=g_realloc(ret->u.dash, (count+2)*sizeof(int));
125                         ret->u.dash[count++]=g_ascii_strtoull(tok,NULL,0);
126                         ret->u.dash[count]=0;
127                         str=NULL;
128                 }
129                 g_free(type_str);
130                 break;
131         case attr_order:
132         case attr_sequence_range:
133         case attr_angle_range:
134         case attr_speed_range:
135                 pos=strchr(value, '-');
136                 min=0;
137                 max=32767;
138                 if (! pos) {
139                         sscanf(value,"%d",&min);
140                         max=min;
141                 } else if (pos == value)
142                         sscanf(value,"-%d",&max);
143                 else
144                         sscanf(value,"%d-%d",&min, &max);
145                 ret->u.range.min=min;
146                 ret->u.range.max=max;
147                 break;
148         default:
149                 if (attr >= attr_type_string_begin && attr <= attr_type_string_end) {
150                         ret->u.str=g_strdup(value);
151                         break;
152                 }
153                 if (attr >= attr_type_int_begin && attr <= attr_type_int_end) {
154                         if (value[0] == '0' && value[1] == 'x')
155                                 ret->u.num=strtoul(value, NULL, 0);
156                         else
157                                 ret->u.num=strtol(value, NULL, 0);
158                         
159                         if ((attr >= attr_type_rel_abs_begin) && (attr < attr_type_boolean_begin)) {
160                                 /* Absolute values are from -0x40000000 - 0x40000000, with 0x0 being 0 (who would have thought that?)
161                                          Relative values are from 0x40000001 - 0x80000000, with 0x60000000 being 0 */
162                                 if (strchr(value, '%')) {
163                                         if ((ret->u.num > 0x20000000) || (ret->u.num < -0x1FFFFFFF)) {
164                                                 dbg(0, "Relative possibly-relative attribute with invalid value %i\n", ret->u.num);
165                                         }
166
167                                         ret->u.num += 0x60000000;
168                                 } else {
169                                         if ((ret->u.num > 0x40000000) || (ret->u.num < -0x40000000)) {
170                                                 dbg(0, "Non-relative possibly-relative attribute with invalid value %i\n", ret->u.num);
171                                         }
172                                 }
173                         } else  if (attr >= attr_type_boolean_begin) { // also check for yes and no
174                                 if (g_ascii_strcasecmp(value,"no") && g_ascii_strcasecmp(value,"0") && g_ascii_strcasecmp(value,"false")) 
175                                         ret->u.num=1;
176                                 else
177                                         ret->u.num=0;
178                         }
179                         break;
180                 }
181                 if (attr >= attr_type_color_begin && attr <= attr_type_color_end) {
182                         struct color *color=g_new0(struct color, 1);
183                         int r,g,b,a;
184                         ret->u.color=color;
185                         if(strlen(value)==7){
186                                 sscanf(value,"#%02x%02x%02x", &r, &g, &b);
187                                 color->r = (r << 8) | r;
188                                 color->g = (g << 8) | g;
189                                 color->b = (b << 8) | b;
190                                 color->a = (65535);
191                         } else if(strlen(value)==9){
192                                 sscanf(value,"#%02x%02x%02x%02x", &r, &g, &b, &a);
193                                 color->r = (r << 8) | r;
194                                 color->g = (g << 8) | g;
195                                 color->b = (b << 8) | b;
196                                 color->a = (a << 8) | a;
197                         } else {
198                                 dbg(0,"color %s has unknown format\n",value);
199                         }
200                         break;
201                 }
202                 if (attr >= attr_type_coord_geo_begin && attr <= attr_type_coord_geo_end) {
203                         g=g_new(struct coord_geo, 1);
204                         ret->u.coord_geo=g;
205                         coord_parse(value, projection_mg, &c);
206                         transform_to_geo(projection_mg, &c, g);
207                         break;
208                 }
209                 dbg(1,"default\n");
210                 g_free(ret);
211                 ret=NULL;
212         }
213         return ret;
214 }
215
216 char *
217 attr_to_text(struct attr *attr, struct map *map, int pretty)
218 {
219         char *ret;
220         enum attr_type type=attr->type;
221
222         if (type >= attr_type_item_begin && type <= attr_type_item_end) {
223                 struct item *item=attr->u.item;
224                 struct attr type, data;
225                 if (! item)
226                         return g_strdup("(nil)");
227                 if (! item->map || !map_get_attr(item->map, attr_type, &type, NULL))
228                         type.u.str="";
229                 if (! item->map || !map_get_attr(item->map, attr_data, &data, NULL))
230                         data.u.str="";
231                 return g_strdup_printf("type=0x%x id=0x%x,0x%x map=%p (%s:%s)", item->type, item->id_hi, item->id_lo, item->map, type.u.str, data.u.str);
232         }
233         if (type >= attr_type_string_begin && type <= attr_type_string_end) {
234                 if (map) {
235                         char *mstr;
236                         if (attr->u.str) {
237                                 mstr=map_convert_string(map, attr->u.str);
238                                 ret=g_strdup(mstr);
239                                 map_convert_free(mstr);
240                         } else
241                                 ret=g_strdup("(null)");
242                         
243                 } else
244                         ret=g_strdup(attr->u.str);
245                 return ret;
246         }
247         if (type == attr_flags)
248                 return g_strdup_printf("0x%x", attr->u.num);
249         if (type >= attr_type_int_begin && type <= attr_type_int_end) 
250                 return g_strdup_printf("%d", attr->u.num);
251         if (type >= attr_type_int64_begin && type <= attr_type_int64_end) 
252                 return g_strdup_printf("%Ld", *attr->u.num64);
253         if (type >= attr_type_double_begin && type <= attr_type_double_end) 
254                 return g_strdup_printf("%f", *attr->u.numd);
255         if (type >= attr_type_object_begin && type <= attr_type_object_end) 
256                 return g_strdup_printf("(object[%s])", attr_to_name(type));
257         if (type >= attr_type_color_begin && type <= attr_type_color_end) {
258                 if (attr->u.color->a != 65535) 
259                         return g_strdup_printf("#%02x%02x%02x%02x", attr->u.color->r>>8,attr->u.color->g>>8,attr->u.color->b>>8, attr->u.color->a>>8);
260                 else
261                         return g_strdup_printf("#%02x%02x%02x", attr->u.color->r>>8,attr->u.color->g>>8,attr->u.color->b>>8);
262         }
263         if (type >= attr_type_coord_geo_begin && type <= attr_type_coord_geo_end) 
264                 return g_strdup_printf("%f %f",attr->u.coord_geo->lng,attr->u.coord_geo->lat);
265         return g_strdup_printf("(no text[%s])", attr_to_name(type));    
266 }
267
268 struct attr *
269 attr_search(struct attr **attrs, struct attr *last, enum attr_type attr)
270 {
271         dbg(1, "enter attrs=%p\n", attrs);
272         while (*attrs) {
273                 dbg(1,"*attrs=%p\n", *attrs);
274                 if ((*attrs)->type == attr) {
275                         return *attrs;
276                 }
277                 attrs++;
278         }
279         return NULL;
280 }
281
282 static int
283 attr_match(enum attr_type search, enum attr_type found)
284 {
285         switch (search) {
286         case attr_any:
287                 return 1;
288         case attr_any_xml:
289                 switch (found) {
290                 case attr_callback:
291                         return 0;
292                 default:
293                         return 1;
294                 }
295         default:
296                 return search == found;
297         }
298 }
299
300 int
301 attr_generic_get_attr(struct attr **attrs, struct attr **def_attrs, enum attr_type type, struct attr *attr, struct attr_iter *iter)
302 {
303         while (attrs && *attrs) {
304                 if (attr_match(type,(*attrs)->type)) {
305                         *attr=**attrs;
306                         if (!iter)
307                                 return 1;
308                         if (*((void **)iter) < (void *)attrs) {
309                                 *((void **)iter)=(void *)attrs;
310                                 return 1;
311                         }
312                 }
313                 attrs++;
314         }
315         if (type == attr_any || type == attr_any_xml)
316                 return 0;
317         while (def_attrs && *def_attrs) {
318                 if ((*def_attrs)->type == type) {
319                         *attr=**def_attrs;
320                         return 1;
321                 }
322                 def_attrs++;
323         }
324         return 0;
325 }
326
327 struct attr **
328 attr_generic_set_attr(struct attr **attrs, struct attr *attr)
329 {
330         struct attr **curr=attrs;
331         int i,count=0;
332         while (curr && *curr) {
333                 if ((*curr)->type == attr->type) {
334                         attr_free(*curr);
335                         *curr=attr_dup(attr);
336                         return attrs;
337                 }
338                 curr++;
339                 count++;
340         }
341         curr=g_new0(struct attr *, count+2);
342         for (i = 0 ; i < count ; i++)
343                 curr[i]=attrs[i];
344         curr[count]=attr_dup(attr);
345         curr[count+1]=NULL;
346         g_free(attrs);
347         return curr;
348 }
349
350 struct attr **
351 attr_generic_add_attr(struct attr **attrs, struct attr *attr)
352 {
353         struct attr **curr=attrs;
354         int i,count=0;
355         while (curr && *curr) {
356                 curr++;
357                 count++;
358         }
359         curr=g_new0(struct attr *, count+2);
360         for (i = 0 ; i < count ; i++)
361                 curr[i]=attrs[i];
362         curr[count]=attr_dup(attr);
363         curr[count+1]=NULL;
364         g_free(attrs);
365         return curr;
366 }
367
368 struct attr **
369 attr_generic_remove_attr(struct attr **attrs, struct attr *attr)
370 {
371         struct attr **curr=attrs;
372         int i,j,count=0,found=0;
373         while (curr && *curr) {
374                 if ((*curr)->type == attr->type && (*curr)->u.data == attr->u.data)
375                         found=1;
376                 curr++;
377                 count++;
378         }
379         if (!found)
380                 return attrs;
381         curr=g_new0(struct attr *, count);
382         j=0;
383         for (i = 0 ; i < count ; i++) {
384                 if (attrs[i]->type != attr->type || attrs[i]->u.data != attr->u.data)
385                         curr[j++]=attrs[i];
386                 else
387                         attr_free(attrs[i]);
388         }
389         curr[j]=NULL;
390         g_free(attrs);
391         return curr;
392 }
393
394 enum attr_type
395 attr_type_begin(enum attr_type type)
396 {
397         if (type < attr_type_item_begin)
398                 return attr_none;
399         if (type < attr_type_int_begin)
400                 return attr_type_item_begin;
401         if (type < attr_type_string_begin)
402                 return attr_type_int_begin;
403         if (type < attr_type_special_begin)
404                 return attr_type_string_begin;
405         if (type < attr_type_double_begin)
406                 return attr_type_special_begin;
407         if (type < attr_type_coord_geo_begin)
408                 return attr_type_double_begin;
409         if (type < attr_type_color_begin)
410                 return attr_type_coord_geo_begin;
411         if (type < attr_type_object_begin)
412                 return attr_type_color_begin;
413         if (type < attr_type_coord_begin)
414                 return attr_type_object_begin;
415         if (type < attr_type_pcoord_begin)
416                 return attr_type_coord_begin;
417         if (type < attr_type_callback_begin)
418                 return attr_type_pcoord_begin;
419         if (type < attr_type_int64_begin)
420                 return attr_type_callback_begin;
421         if (type <= attr_type_int64_end)
422                 return attr_type_int64_begin;
423         return attr_none;
424 }
425
426 int
427 attr_data_size(struct attr *attr)
428 {
429         if (attr->type >= attr_type_string_begin && attr->type <= attr_type_string_end) 
430                 return strlen(attr->u.str)+1;
431         if (attr->type >= attr_type_int_begin && attr->type <= attr_type_int_end) 
432                 return sizeof(attr->u.num);
433         if (attr->type >= attr_type_coord_geo_begin && attr->type <= attr_type_coord_geo_end) 
434                 return sizeof(*attr->u.coord_geo);
435         if (attr->type >= attr_type_color_begin && attr->type <= attr_type_color_end) 
436                 return sizeof(*attr->u.color);
437         if (attr->type >= attr_type_object_begin && attr->type <= attr_type_object_end) 
438                 return sizeof(void *);
439         if (attr->type >= attr_type_int64_begin && attr->type <= attr_type_int64_end) 
440                 return sizeof(*attr->u.num64);
441         if (attr->type == attr_order)
442                 return sizeof(attr->u.range);
443         if (attr->type == attr_item_types) {
444                 int i=0;
445                 while (attr->u.item_types[i++] != type_none);
446                 return i*sizeof(enum item_type);
447         }
448         if (attr->type == attr_attr_types) {
449                 int i=0;
450                 while (attr->u.attr_types[i++] != attr_none);
451                 return i*sizeof(enum attr_type);
452         }
453         dbg(0,"size for %s unknown\n", attr_to_name(attr->type));
454         return 0;
455 }
456
457 void *
458 attr_data_get(struct attr *attr)
459 {
460         if (attr->type >= attr_type_int_begin && attr->type <= attr_type_int_end) 
461                 return &attr->u.num;
462         if (attr->type == attr_order)
463                 return &attr->u.range;
464         return attr->u.data;
465 }
466
467 void
468 attr_data_set(struct attr *attr, void *data)
469 {
470         if (attr->type >= attr_type_int_begin && attr->type <= attr_type_int_end) 
471                 attr->u.num=*((int *)data);
472         else
473                 attr->u.data=data;
474 }
475
476 void
477 attr_data_set_le(struct attr * attr, void * data)
478 {
479         if (attr->type >= attr_type_int_begin && attr->type <= attr_type_int_end) 
480                 attr->u.num=le32_to_cpu(*((int *)data));
481         else if (attr->type == attr_order) {
482                 attr->u.num=le32_to_cpu(*((int *)data));
483                 attr->u.range.min=le16_to_cpu(attr->u.range.min);
484                 attr->u.range.max=le16_to_cpu(attr->u.range.max);
485         }
486         else
487 /* Fixme: Handle long long */
488                 attr->u.data=data;
489
490 }
491
492 void
493 attr_free(struct attr *attr)
494 {
495         if (!attr)
496                 return;
497         if (!(attr->type >= attr_type_int_begin && attr->type <= attr_type_int_end) && 
498             !(attr->type >= attr_type_object_begin && attr->type <= attr_type_object_end))
499                 g_free(attr->u.data);
500         g_free(attr);
501 }
502
503 struct attr *
504 attr_dup(struct attr *attr)
505 {
506         int size;
507         struct attr *ret=g_new0(struct attr, 1);
508         ret->type=attr->type;
509         if (attr->type >= attr_type_int_begin && attr->type <= attr_type_int_end) 
510                 ret->u.num=attr->u.num;
511         else if (attr->type >= attr_type_object_begin && attr->type <= attr_type_object_end) 
512                 ret->u.data=attr->u.data;
513         else {
514                 size=attr_data_size(attr);
515                 if (size) {
516                         ret->u.data=g_malloc(size);
517                         memcpy(ret->u.data, attr->u.data, size);
518                 }
519         }
520         return ret;
521 }
522
523 void
524 attr_list_free(struct attr **attrs)
525 {
526         int count=0;
527         while (attrs && attrs[count]) {
528                 attr_free(attrs[count++]);
529         }
530         g_free(attrs);
531 }
532
533 struct attr **
534 attr_list_dup(struct attr **attrs)
535 {
536         struct attr **ret=attrs;
537         int i,count=0;
538
539         while (attrs[count])
540                 count++;
541         ret=g_new0(struct attr *, count+1);
542         for (i = 0 ; i < count ; i++)
543                 ret[i]=attr_dup(attrs[i]);
544         return ret;
545 }
546
547
548 int
549 attr_from_line(char *line, char *name, int *pos, char *val_ret, char *name_ret)
550 {
551         int len=0,quoted;
552         char *p,*e,*n;
553
554         dbg(1,"get_tag %s from %s\n", name, line); 
555         if (name)
556                 len=strlen(name);
557         if (pos) 
558                 p=line+*pos;
559         else
560                 p=line;
561         for(;;) {
562                 while (*p == ' ') {
563                         p++;
564                 }
565                 if (! *p)
566                         return 0;
567                 n=p;
568                 e=strchr(p,'=');
569                 if (! e)
570                         return 0;
571                 p=e+1;
572                 quoted=0;
573                 while (*p) {
574                         if (*p == ' ' && !quoted)
575                                 break;
576                         if (*p == '"')
577                                 quoted=1-quoted;
578                         p++;
579                 }
580                 if (name == NULL || (e-n == len && !strncmp(n, name, len))) {
581                         if (name_ret) {
582                                 len=e-n;
583                                 strncpy(name_ret, n, len);
584                                 name_ret[len]='\0';
585                         }
586                         e++;
587                         len=p-e;
588                         if (e[0] == '"') {
589                                 e++;
590                                 len-=2;
591                         }
592                         strncpy(val_ret, e, len);
593                         val_ret[len]='\0';
594                         if (pos)
595                                 *pos=p-line;
596                         return 1;
597                 }
598         }       
599         return 0;
600 }
601
602 int
603 attr_types_contains(enum attr_type *types, enum attr_type type)
604 {
605         while (types && *types != attr_none) {
606                 if (*types == type)
607                         return 1;
608                 types++;
609         }
610         return 0;
611 }
612
613 int
614 attr_types_contains_default(enum attr_type *types, enum attr_type type, int deflt)
615 {
616         if (!types) {
617                 return deflt;
618         }
619         return attr_types_contains(types, type);        
620 }