Modularized vehicle
[navit-package] / src / attr.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <glib.h>
5 #include "debug.h"
6 #include "item.h"
7 #include "color.h"
8 #include "attr.h"
9
10 struct attr_name {
11         enum attr_type attr;
12         char *name;
13 };
14
15
16 static struct attr_name attr_names[]={
17 #define ATTR2(x,y) ATTR(y)
18 #define ATTR(x) { attr_##x, #x },
19 #include "attr_def.h"
20 #undef ATTR2
21 #undef ATTR
22 };
23
24 enum attr_type
25 attr_from_name(const char *name)
26 {
27         int i;
28
29         for (i=0 ; i < sizeof(attr_names)/sizeof(struct attr_name) ; i++) {
30                 if (! strcmp(attr_names[i].name, name))
31                         return attr_names[i].attr;
32         }
33         return attr_none;
34 }
35
36 char *
37 attr_to_name(enum attr_type attr)
38 {
39         int i;
40
41         for (i=0 ; i < sizeof(attr_names)/sizeof(struct attr_name) ; i++) {
42                 if (attr_names[i].attr == attr)
43                         return attr_names[i].name;
44         }
45         return NULL; 
46 }
47
48 struct attr *
49 attr_new_from_text(const char *name, const char *value)
50 {
51         enum attr_type attr;
52         struct attr *ret;
53
54         ret=g_new0(struct attr, 1);
55         dbg(1,"enter name='%s' value='%s'\n", name, value);
56         attr=attr_from_name(name);
57         ret->type=attr;
58         switch (attr) {
59         case attr_item_type:
60                 ret->u.item_type=item_from_name(value);
61                 break;
62         default:
63                 if (attr >= attr_type_string_begin && attr <= attr_type_string_end) {
64                         ret->u.str=(char *)value;
65                         break;
66                 }
67                 if (attr >= attr_type_int_begin && attr <= attr_type_int_end) {
68                         ret->u.num=atoi(value);
69                         break;
70                 }
71                 if (attr >= attr_type_color_begin && attr <= attr_type_color_end) {
72                         struct color *color=g_new0(struct color, 1);
73                         int r,g,b,a;
74                         ret->u.color=color;
75                         if(strlen(value)==7){
76                                 sscanf(value,"#%02x%02x%02x", &r, &g, &b);
77                                 color->r = (r << 8) | r;
78                                 color->g = (g << 8) | g;
79                                 color->b = (b << 8) | b;
80                                 color->a = (65535);
81                         } else if(strlen(value)==9){
82                                 sscanf(value,"#%02x%02x%02x%02x", &r, &g, &b, &a);
83                                 color->r = (r << 8) | r;
84                                 color->g = (g << 8) | g;
85                                 color->b = (b << 8) | b;
86                                 color->a = (a << 8) | a;
87                         } else {
88                                 dbg(0,"color %s has unknown format\n",value);
89                         }
90                         break;
91                 }
92                 dbg(1,"default\n");
93                 g_free(ret);
94                 ret=NULL;
95         }
96         return ret;
97 }
98
99 struct attr *
100 attr_search(struct attr **attrs, struct attr *last, enum attr_type attr)
101 {
102         dbg(1, "enter attrs=%p\n", attrs);
103         while (*attrs) {
104                 dbg(1,"*attrs=%p\n", *attrs);
105                 if ((*attrs)->type == attr) {
106                         return *attrs;
107                 }
108                 attrs++;
109         }
110         return NULL;
111 }
112
113 int
114 attr_data_size(struct attr *attr)
115 {
116         if (attr->type >= attr_type_string_begin && attr->type <= attr_type_string_end) {
117                 return strlen(attr->u.str)+1;
118         }
119         if (attr->type >= attr_type_int_begin && attr->type <= attr_type_int_end) {
120                 return sizeof(attr->u.num);
121         }
122         return 0;
123 }
124
125 void *
126 attr_data_get(struct attr *attr)
127 {
128         if (attr->type >= attr_type_string_begin && attr->type <= attr_type_string_end) {
129                 return attr->u.str;
130         }
131         if (attr->type >= attr_type_int_begin && attr->type <= attr_type_int_end) {
132                 return &attr->u.num;
133         }
134         return NULL;
135 }
136
137 void
138 attr_data_set(struct attr *attr, void *data)
139 {
140         if (attr->type >= attr_type_string_begin && attr->type <= attr_type_string_end) {
141                 attr->u.str=data;
142         }
143         if (attr->type >= attr_type_int_begin && attr->type <= attr_type_int_end) {
144                 attr->u.num=*((int *)data);
145         }
146 }
147
148 void
149 attr_free(struct attr *attr)
150 {
151         if (attr->type >= attr_type_color_begin && attr->type <= attr_type_color_end) 
152                 g_free(attr->u.color);
153         g_free(attr);
154 }