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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2001 Ludvig Strigeus
* Copyright (C) 2001/2002 The ScummVM project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header$
*
*/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config-file.h"
#include "engine.h" // for debug()
#define MAXLINELEN 256
static char *ltrim(char *t)
{
for (; *t && (*t == ' '); t++);
return t;
}
static char *rtrim(char *t)
{
int l;
for (l = strlen(t) - 1; l; l--) {
if (t[l] == ' ') {
*t = 0;
} else {
return t;
}
}
return t;
}
// The config-class itself.
Config::Config (const String &cfg, const String &d)
: filename(cfg), defaultDomain(d), willwrite(false)
{
FILE *cfg_file;
char t[MAXLINELEN];
if (!(cfg_file = fopen(filename.c_str(), "r"))) {
debug(1, "Unable to open configuration file: %s.\n", filename.c_str());
} else {
while (!feof(cfg_file)) {
if (!fgets(t, MAXLINELEN, cfg_file))
continue;
if (t[0] != '#') {
if (t[0] == '[') {
// It's a new domain which begins here.
char *p = strchr(t, ']');
if (!p) {
debug(1, "Config file buggy: no ] at the end of the domain name.\n");
} else {
*p = 0;
set_domain(t + 1);
}
} else {
// It's a new key in the domain.
if (defaultDomain.isEmpty()) {
debug(1, "Config file buggy: we have a key without a domain first.\n");
}
char *p = strchr(t, '\n');
if (p)
*p = 0;
p = strchr(t, '\r');
if (p)
*p = 0;
if (!(p = strchr(t, '='))) {
if (strlen(t))
debug(1, "Config file buggy: there is junk: %s\n", t);
} else {
char *key, *value;
*p = 0;
key = ltrim(rtrim(t));
value = ltrim(p + 1);
set(key, value);
}
}
}
}
set_domain(d);
fclose(cfg_file);
}
}
const char *Config::get(const String &key, const String &d) const
{
String domain;
if (d.isEmpty())
domain = defaultDomain;
else
domain = d;
domain.toLowercase();
if (domains.contains(domain) && domains[domain].contains(key))
return domains[domain][key].c_str();
return 0;
}
const int Config::getInt(const String &key, int def, const String &d) const
{
const char *value = get(key, d);
if (value)
return atoi(value);
return def;
}
const bool Config::getBool(const String &key, bool def, const String &d) const
{
const char *value = get(key, d);
if (value)
return !scumm_stricmp(value, "true");
return def;
}
void Config::set(const String &key, const String &value, const String &d)
{
String domain(d);
if (domain.isEmpty())
domain = defaultDomain;
domain.toLowercase();
domains[domain][key] = value;
}
void Config::setInt(const String &key, int value_i, const String &d)
{
char value[MAXLINELEN];
sprintf(value, "%i", value_i);
set(key, String(value), d);
}
void Config::setBool(const String &key, bool value_b, const String &d)
{
String value(value_b ? "true" : "false");
set(key, value, d);
}
void Config::set_domain(const String &d)
{
defaultDomain = d;
defaultDomain.toLowercase();
}
bool Config::has_domain(const String &d) const
{
String temp(d);
temp.toLowercase();
return domains.contains(temp);
}
void Config::flush() const
{
FILE *cfg_file;
if (!willwrite)
return;
if (!(cfg_file = fopen(filename.c_str(), "w"))) {
debug(1, "Unable to write configuration file: %s.\n", filename.c_str());
} else {
DomainMap::Iterator d;
for (d = domains.begin(); d != domains.end(); ++d) {
fprintf(cfg_file, "[%s]\n", d->_key.c_str());
const StringMap &data = d->_value;
StringMap::Iterator x;
for (x = data.begin(); x != data.end(); ++x) {
const String &value = x->_value;
if (!value.isEmpty())
fprintf(cfg_file, "%s=%s\n", x->_key.c_str(), value.c_str());
}
fprintf(cfg_file, "\n");
}
fclose(cfg_file);
}
}
void Config::rename_domain(const String &oldD, const String &newD)
{
String oldDomain(oldD);
String newDomain(newD);
oldDomain.toLowercase();
newDomain.toLowercase();
if (oldDomain == newDomain)
return;
StringMap &oldHash = domains[oldDomain];
StringMap &newHash = domains[newDomain];
newHash.merge(oldHash);
domains.remove(oldDomain);
}
void Config::delete_domain(const String &d)
{
String domain(d);
domain.toLowercase();
domains.remove(d);
}
void Config::set_filename(const String &f)
{
filename = f;
}
void Config::merge_config(const Config &c)
{
DomainMap::Iterator d, end(c.domains.end());
for (d = c.domains.begin(); d != end; ++d) {
domains[d->_key].merge(d->_value);
}
}
void Config::set_writing(bool w)
{
willwrite = w;
}
const int Config::count_domains() {
int count = 0;
DomainMap::Iterator d, end(domains.end());
for (d = domains.begin(); d != end; ++d)
count++;
return count;
}
ScummVM::StringList Config::get_domains() {
StringList domainNames;
DomainMap::Iterator d, end(domains.end());
for (d = domains.begin(); d != end; ++d) {
domainNames.push_back(d->_key);
}
return domainNames;
}
|