summaryrefslogtreecommitdiff
path: root/navit/config_.c
blob: 4eecfe1019b71ed55232fe4fad4b42e4feccdd31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2008 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <stdlib.h>
#include <glib.h>
#include "debug.h"
#include "item.h"
#include "callback.h"
#include "config_.h"

struct config {
	struct attr **attrs;
	struct callback_list *cbl;
} *config;

struct attr_iter {
	void *iter;
};

void
config_destroy(struct config *this_)
{
	attr_list_free(this_->attrs);
	callback_list_destroy(this_->cbl);
	g_free(config);
	dbg(0,"exit");
	exit(0);
}

int
config_get_attr(struct config *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
{
	dbg(0,"enter\n");
	return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
}

static int
config_set_attr_int(struct config *this_, struct attr *attr)
{
	switch (attr->type) {
	case attr_language:
		setenv("LANG",attr->u.str,1);
		return 1;
	default:
		return 0;
	}
}

int
config_set_attr(struct config *this_, struct attr *attr)
{
	return config_set_attr_int(this_, attr);
}

int
config_add_attr(struct config *this_, struct attr *attr)
{
	switch (attr->type) {
	case attr_callback:
		callback_list_add(this_->cbl, attr->u.callback);
	default:	
		this_->attrs=attr_generic_add_attr(this_->attrs, attr);
	}
	callback_list_call_attr_2(this_->cbl, attr->type, attr->u.data, 1);
	return 1;
}

int
config_remove_attr(struct config *this_, struct attr *attr)
{
	switch (attr->type) {
	case attr_callback:
		callback_list_remove(this_->cbl, attr->u.callback);
	default:	
		this_->attrs=attr_generic_remove_attr(this_->attrs, attr);
	}
	callback_list_call_attr_2(this_->cbl, attr->type, attr->u.data, -1);
	return 1;
}

struct attr_iter *
config_attr_iter_new()
{
	dbg(0,"enter\n");
	return g_new0(struct attr_iter, 1);
}

void
config_attr_iter_destroy(struct attr_iter *iter)
{
	dbg(0,"enter\n");
	g_free(iter);
}


struct config *
config_new(struct attr *parent, struct attr **attrs)
{
	dbg(0,"enter\n");
	if (config) {
		dbg(0,"only one config allowed\n");
		return NULL;
	}
	if (parent) {
		dbg(0,"no parent in config allowed\n");
		return NULL;
	}
	config=g_new0(struct config, 1);
	config->attrs=attr_list_dup(attrs);
	config->cbl=callback_list_new();
	while (*attrs) {
		if (!config_set_attr_int(config,*attrs)) {
			dbg(0,"failed to set attribute '%s'\n",attr_to_name((*attrs)->type));
			config_destroy(config);
			config=NULL;
			break;
		}
		attrs++;
	}
	return config;
}