Fix:Core:Made routing more flexible
[navit-package] / 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 <glib.h>
21 #include <string.h>
22 #include "debug.h"
23 #include "item.h"
24 #include "vehicleprofile.h"
25
26 void
27 vehicleprofile_set_attr_do(struct vehicleprofile *this_, struct attr *attr)
28 {
29         switch (attr->type) {
30         case attr_flags:
31                 this_->flags=attr->u.num;
32                 break;
33         case attr_flags_forward_mask:
34                 this_->flags_forward_mask=attr->u.num;
35                 break;
36         case attr_flags_reverse_mask:
37                 this_->flags_forward_mask=attr->u.num;
38                 break;
39         case attr_maxspeed_handling:
40                 this_->maxspeed_handling=attr->u.num;
41                 break;
42         case attr_route_mode:
43                 this_->mode=attr->u.num;
44                 break;
45         default:
46                 break;
47         }
48 }
49
50 struct vehicleprofile *
51 vehicleprofile_new(struct attr *parent, struct attr **attrs)
52 {
53         struct vehicleprofile *this_;
54         struct attr **attr, *type_attr;
55         if (! (type_attr=attr_search(attrs, NULL, attr_name))) {
56                 return NULL;
57         }
58         this_=g_new0(struct vehicleprofile, 1);
59         this_->attrs=attr_list_dup(attrs);
60         this_->roadprofile_hash=g_hash_table_new(NULL, NULL);
61         for (attr=attrs;*attr; attr++)
62                 vehicleprofile_set_attr_do(this_, *attr);
63         return this_;
64 }
65
66 int
67 vehicleprofile_get_attr(struct vehicleprofile *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
68 {
69         return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
70 }
71
72 int
73 vehicleprofile_set_attr(struct vehicleprofile *this_, struct attr *attr)
74 {
75         vehicleprofile_set_attr_do(this_, attr);
76         this_->attrs=attr_generic_set_attr(this_->attrs, attr);
77         return 1;
78 }
79
80 int
81 vehicleprofile_add_attr(struct vehicleprofile *this_, struct attr *attr)
82 {
83         this_->attrs=attr_generic_add_attr(this_->attrs, attr);
84         struct attr item_types_attr;
85         switch (attr->type) {
86         case attr_roadprofile:
87                 if (roadprofile_get_attr(attr->u.roadprofile, attr_item_types, &item_types_attr)) {
88                         enum item_type *types=item_types_attr.u.item_types;
89                         while (*types != type_none) {
90                                 g_hash_table_insert(this_->roadprofile_hash, (void *)(long)(*types), attr->u.roadprofile);
91                                 types++;
92                         }
93                         dbg(0,"ok\n");
94                 }
95                 break;
96         default:
97                 break;
98         }
99         return 1;
100 }
101
102 int
103 vehicleprofile_remove_attr(struct vehicleprofile *this_, struct attr *attr)
104 {
105         this_->attrs=attr_generic_remove_attr(this_->attrs, attr);
106         return 1;
107 }
108
109 struct roadprofile_data *
110 vehicleprofile_get_roadprofile(struct vehicleprofile *this_, enum item_type type)
111 {
112         return g_hash_table_lookup(this_->roadprofile_hash, (void *)(long)type);
113 }