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