Fix:Core:Cleanup
[navit-package] / 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 "debug.h"
23 #include "item.h"
24 #include "callback.h"
25 #include "config_.h"
26
27 struct config {
28         struct attr **attrs;
29         struct callback_list *cbl;
30 } *config;
31
32 struct attr_iter {
33         void *iter;
34 };
35
36 void
37 config_destroy(struct config *this_)
38 {
39         attr_list_free(this_->attrs);
40         callback_list_destroy(this_->cbl);
41         g_free(config);
42         exit(0);
43 }
44
45 int
46 config_get_attr(struct config *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
47 {
48         return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
49 }
50
51 static int
52 config_set_attr_int(struct config *this_, struct attr *attr)
53 {
54         switch (attr->type) {
55         case attr_language:
56                 setenv("LANG",attr->u.str,1);
57                 return 1;
58         default:
59                 return 0;
60         }
61 }
62
63 int
64 config_set_attr(struct config *this_, struct attr *attr)
65 {
66         return config_set_attr_int(this_, attr);
67 }
68
69 int
70 config_add_attr(struct config *this_, struct attr *attr)
71 {
72         switch (attr->type) {
73         case attr_callback:
74                 callback_list_add(this_->cbl, attr->u.callback);
75         default:        
76                 this_->attrs=attr_generic_add_attr(this_->attrs, attr);
77         }
78         callback_list_call_attr_2(this_->cbl, attr->type, attr->u.data, 1);
79         return 1;
80 }
81
82 int
83 config_remove_attr(struct config *this_, struct attr *attr)
84 {
85         switch (attr->type) {
86         case attr_callback:
87                 callback_list_remove(this_->cbl, attr->u.callback);
88         default:        
89                 this_->attrs=attr_generic_remove_attr(this_->attrs, attr);
90         }
91         callback_list_call_attr_2(this_->cbl, attr->type, attr->u.data, -1);
92         return 1;
93 }
94
95 struct attr_iter *
96 config_attr_iter_new()
97 {
98         return g_new0(struct attr_iter, 1);
99 }
100
101 void
102 config_attr_iter_destroy(struct attr_iter *iter)
103 {
104         g_free(iter);
105 }
106
107
108 struct config *
109 config_new(struct attr *parent, struct attr **attrs)
110 {
111         if (config) {
112                 dbg(0,"only one config allowed\n");
113                 return NULL;
114         }
115         if (parent) {
116                 dbg(0,"no parent in config allowed\n");
117                 return NULL;
118         }
119         config=g_new0(struct config, 1);
120         config->attrs=attr_list_dup(attrs);
121         config->cbl=callback_list_new();
122         while (*attrs) {
123                 if (!config_set_attr_int(config,*attrs)) {
124                         dbg(0,"failed to set attribute '%s'\n",attr_to_name((*attrs)->type));
125                         config_destroy(config);
126                         config=NULL;
127                         break;
128                 }
129                 attrs++;
130         }
131         return config;
132 }