Add:Core:Changed vehicleprofile to new scheme
[profile/ivi/navit.git] / navit / navit / vehicleprofile.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 <glib.h>
22 #include <string.h>
23 #include "debug.h"
24 #include "item.h"
25 #include "roadprofile.h"
26 #include "vehicleprofile.h"
27 #include "xmlconfig.h"
28
29 static void
30 vehicleprofile_set_attr_do(struct vehicleprofile *this_, struct attr *attr)
31 {
32         dbg(1,"%s:%d\n", attr_to_name(attr->type), attr->u.num);
33         switch (attr->type) {
34         case attr_flags:
35                 this_->flags=attr->u.num;
36                 break;
37         case attr_flags_forward_mask:
38                 this_->flags_forward_mask=attr->u.num;
39                 break;
40         case attr_flags_reverse_mask:
41                 this_->flags_reverse_mask=attr->u.num;
42                 break;
43         case attr_maxspeed_handling:
44                 this_->maxspeed_handling=attr->u.num;
45                 break;
46         case attr_route_mode:
47                 this_->mode=attr->u.num;
48                 break;
49         case attr_name:
50                 if(this_->name)
51                         g_free(this_->name);
52                 /* previously used strdupn not available on win32 */
53                 this_->name = g_strdup(attr->u.str);
54                 break;
55         case attr_vehicle_axle_weight:
56                 this_->axle_weight=attr->u.num;
57                 break;
58         case attr_vehicle_height:
59                 this_->height=attr->u.num;
60                 break;
61         case attr_vehicle_length:
62                 this_->length=attr->u.num;
63                 break;
64         case attr_vehicle_weight:
65                 this_->weight=attr->u.num;
66                 break;
67         case attr_vehicle_width:
68                 this_->width=attr->u.num;
69                 break;
70         case attr_static_speed:
71                 this_->static_speed=attr->u.num;
72                 break;
73         case attr_static_distance:
74                 this_->static_distance=attr->u.num;
75                 break;
76         case attr_through_traffic_penalty:
77                 this_->through_traffic_penalty=attr->u.num;
78                 break;
79         default:
80                 break;
81         }
82 }
83
84 struct vehicleprofile *
85 vehicleprofile_new(struct attr *parent, struct attr **attrs)
86 {
87         struct vehicleprofile *this_;
88         struct attr **attr, *type_attr;
89         if (! (type_attr=attr_search(attrs, NULL, attr_name))) {
90                 return NULL;
91         }
92         this_=g_new0(struct vehicleprofile, 1);
93         this_->attrs=attr_list_dup(attrs);
94         this_->roadprofile_hash=g_hash_table_new(NULL, NULL);
95         this_->length=-1;
96         this_->width=-1;
97         this_->height=-1;
98         this_->weight=-1;
99         this_->axle_weight=-1;
100         this_->through_traffic_penalty=9000;
101         for (attr=attrs;*attr; attr++)
102                 vehicleprofile_set_attr_do(this_, *attr);
103         return this_;
104 }
105
106 struct attr_iter *
107 vehicleprofile_attr_iter_new(void)
108 {
109         return g_new0(void *,1);
110 }
111
112 void
113 vehicleprofile_attr_iter_destroy(struct attr_iter *iter)
114 {
115         g_free(iter);
116 }
117
118
119 int
120 vehicleprofile_get_attr(struct vehicleprofile *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
121 {
122         return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
123 }
124
125 int
126 vehicleprofile_set_attr(struct vehicleprofile *this_, struct attr *attr)
127 {
128         vehicleprofile_set_attr_do(this_, attr);
129         this_->attrs=attr_generic_set_attr(this_->attrs, attr);
130         return 1;
131 }
132
133 int
134 vehicleprofile_add_attr(struct vehicleprofile *this_, struct attr *attr)
135 {
136         struct attr item_types_attr;
137         this_->attrs=attr_generic_add_attr(this_->attrs, attr);
138         switch (attr->type) {
139         case attr_roadprofile:
140                 if (roadprofile_get_attr(attr->u.roadprofile, attr_item_types, &item_types_attr, NULL)) {
141                         enum item_type *types=item_types_attr.u.item_types;
142                         while (*types != type_none) {
143                                 g_hash_table_insert(this_->roadprofile_hash, (void *)(long)(*types), attr->u.roadprofile);
144                                 types++;
145                         }
146                 }
147                 break;
148         default:
149                 break;
150         }
151         return 1;
152 }
153
154 int
155 vehicleprofile_remove_attr(struct vehicleprofile *this_, struct attr *attr)
156 {
157         this_->attrs=attr_generic_remove_attr(this_->attrs, attr);
158         return 1;
159 }
160
161 struct roadprofile *
162 vehicleprofile_get_roadprofile(struct vehicleprofile *this_, enum item_type type)
163 {
164         return g_hash_table_lookup(this_->roadprofile_hash, (void *)(long)type);
165 }
166
167 char *
168 vehicleprofile_get_name(struct vehicleprofile *this_)
169 {
170     return this_->name;
171 }
172
173 struct object_func vehicleprofile_func = {
174         attr_vehicleprofile,
175         (object_func_new)vehicleprofile_new,
176         (object_func_get_attr)vehicleprofile_get_attr,
177         (object_func_iter_new)vehicleprofile_attr_iter_new,
178         (object_func_iter_destroy)vehicleprofile_attr_iter_destroy,
179         (object_func_set_attr)vehicleprofile_set_attr,
180         (object_func_add_attr)vehicleprofile_add_attr,
181         (object_func_remove_attr)vehicleprofile_remove_attr,
182         (object_func_init)NULL,
183         (object_func_destroy)NULL,
184         (object_func_dup)NULL,
185         (object_func_ref)NULL,
186         (object_func_unref)NULL,
187 };