Simple fixes to get navit compiling with MSVC
[profile/ivi/navit.git] / navit / navit / config_.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 <signal.h>
23 #include "debug.h"
24 #include "item.h"
25 #include "callback.h"
26 #include "navit.h"
27 #include "config_.h"
28 #ifdef HAVE_API_WIN32_CE
29 #include "libc.h"
30 #endif
31
32 struct config {
33         struct attr **attrs;
34         struct callback_list *cbl;
35 } *config;
36
37 struct config *
38 config_get(void)
39 {
40         return config;
41 }
42
43 int config_empty_ok;
44
45 static int configured;
46
47 struct attr_iter {
48         void *iter;
49 };
50
51 void
52 config_destroy(struct config *this_)
53 {
54         attr_list_free(this_->attrs);
55         callback_list_destroy(this_->cbl);
56         g_free(config);
57         exit(0);
58 }
59
60 static void
61 config_terminate(int sig)
62 {
63         dbg(0,"terminating\n");
64         config_destroy(config);
65 }
66
67 static void
68 config_new_int(void)
69 {
70         config=g_new0(struct config, 1);
71         config->cbl=callback_list_new();
72 #ifndef HAVE_API_WIN32_CE
73         signal(SIGTERM, config_terminate);
74 #ifndef HAVE_API_WIN32
75 #ifndef _MSC_VER
76 #ifndef __MINGW32__
77         signal(SIGPIPE, SIG_IGN);
78 #endif /* __MINGW32__ */
79 #endif
80 #endif
81 #endif
82 }
83
84 int
85 config_get_attr(struct config *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
86 {
87         return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
88 }
89
90 static int
91 config_set_attr_int(struct config *this_, struct attr *attr)
92 {
93         switch (attr->type) {
94         case attr_language:
95                 setenv("LANG",attr->u.str,1);
96                 return 1;
97         default:
98                 return 0;
99         }
100 }
101
102 int
103 config_set_attr(struct config *this_, struct attr *attr)
104 {
105         return config_set_attr_int(this_, attr);
106 }
107
108 int
109 config_add_attr(struct config *this_, struct attr *attr)
110 {
111         if (!config) {
112                 config_new_int();
113                 this_=config;
114         }
115         switch (attr->type) {
116         case attr_callback:
117                 callback_list_add(this_->cbl, attr->u.callback);
118         default:        
119                 this_->attrs=attr_generic_add_attr(this_->attrs, attr);
120         }
121         callback_list_call_attr_2(this_->cbl, attr->type, attr->u.data, 1);
122         return 1;
123 }
124
125 int
126 config_remove_attr(struct config *this_, struct attr *attr)
127 {
128         switch (attr->type) {
129         case attr_callback:
130                 callback_list_remove(this_->cbl, attr->u.callback);
131         default:        
132                 this_->attrs=attr_generic_remove_attr(this_->attrs, attr);
133         }
134         callback_list_call_attr_2(this_->cbl, attr->type, attr->u.data, -1);
135         return 1;
136 }
137
138 struct attr_iter *
139 config_attr_iter_new()
140 {
141         return g_new0(struct attr_iter, 1);
142 }
143
144 void
145 config_attr_iter_destroy(struct attr_iter *iter)
146 {
147         g_free(iter);
148 }
149
150
151 struct config *
152 config_new(struct attr *parent, struct attr **attrs)
153 {
154         if (configured) {
155                 dbg(0,"only one config allowed\n");
156                 return config;
157         }
158         if (parent) {
159                 dbg(0,"no parent in config allowed\n");
160                 return NULL;
161         }
162         if (!config)
163                 config_new_int();
164         config->attrs=attr_list_dup(attrs);
165         while (*attrs) {
166                 if (!config_set_attr_int(config,*attrs)) {
167                         dbg(0,"failed to set attribute '%s'\n",attr_to_name((*attrs)->type));
168                         config_destroy(config);
169                         config=NULL;
170                         break;
171                 }
172                 attrs++;
173         }
174         configured=1;
175         return config;
176 }