File: Configuration.cc

package info (click to toggle)
exult 1.12.1-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,856 kB
  • sloc: cpp: 170,016; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 550; objc: 416
file content (325 lines) | stat: -rw-r--r-- 8,255 bytes parent folder | download
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 *  Copyright (C) 2000-2022  The Exult Team
 *
 *  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.
 */

#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#include "Configuration.h"

#include "exceptions.h"
#include "ignore_unused_variable_warning.h"
#include "utils.h"

#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <iostream>

using std::atoi;
using std::endl;
using std::ostream;
using std::perror;
using std::string;
#ifdef _WIN32
using std::cerr;
#endif
using std::isspace;

#define TRACE_CONF 0

#if TRACE_CONF
#	define CTRACE(X) cerr << X << endl
#else
#	define CTRACE(X)
#endif

void Configuration::value(
		const string& key, string& ret, const std::string& defaultvalue) const {
	const XMLnode* sub = xmltree->subtree(key);
	if (sub) {
		ret = sub->value();
	} else {
		ret = defaultvalue;
	}
}

void Configuration::value(
		const string& key, bool& ret, bool defaultvalue) const {
	const XMLnode* sub = xmltree->subtree(key);
	if (sub) {
		ret = (to_uppercase(sub->value()) == "YES");
	} else {
		ret = defaultvalue;
	}
}

void Configuration::value(const string& key, int& ret, int defaultvalue) const {
	const XMLnode* sub = xmltree->subtree(key);
	if (sub) {
		ret = atoi(sub->value().c_str());
	} else {
		ret = defaultvalue;
	}
}

bool Configuration::key_exists(const string& key) const {
	const XMLnode* sub = xmltree->subtree(key);
	return sub != nullptr;
}

void Configuration::set(
		const string& key, const string& value, bool write_out) {
	// Break k up into '/' separated elements.
	// start advancing walk, one element at a time, creating nodes
	// as needed.
	// At the end of that, walk is the target node, and we
	// can set the value.

	// We must also properly encode the value before writing it out.
	// Must remember that.

	xmltree->xmlassign(key, value);
	if (write_out) {
		write_back();
	}
}

void Configuration::set(const char* key, const char* value, bool write_out) {
	const string k(key);
	const string v(value);
	set(k, v, write_out);
}

void Configuration::set(const char* key, const string& value, bool write_out) {
	const string k(key);
	set(k, value, write_out);
}

void Configuration::set(const char* key, int value, bool write_out) {
	const string k(key);
	const string v = std::to_string(value);
	set(k, v, write_out);
}

void Configuration::set(const string& key, int value, bool write_out) {
	const string v = std::to_string(value);
	set(key, v, write_out);
}

void Configuration::remove(const string& key, bool write_out) {
	xmltree->remove(key, true);
	if (write_out) {
		write_back();
	}
}

bool Configuration::read_config_string(const string& s) {
	const string& sbuf(s);
	size_t        nn = 0;
	while (isspace(static_cast<char>(s[nn]))) {
		++nn;
	}

	assert(s[nn] == '<');
	++nn;

	xmltree->xmlparse(sbuf, nn);
	is_file = false;
	return true;
}

static inline bool is_path_absolute(const string& path) {
#ifdef _WIN32
	const auto is_win32_abs_path = [](const string& path) {
		return (path.find(".\\") == 0) || (path.find("..\\") == 0)
			   || (path[0] == '\\')
			   || (std::isalpha(path[0]) && path[1] == ':'
				   && (path[2] == '/' || path[2] == '\\'));
	};
#else
	const auto is_win32_abs_path = [](const string& path) {
		ignore_unused_variable_warning(path);
		return false;
	};
#endif

	return (path.find("./") == 0) || (path.find("../") == 0) || (path[0] == '/')
		   || is_win32_abs_path(path);
}

bool Configuration::read_config_file(
		const string& input_filename, const string& root) {
	string fname;

	CTRACE("Configuration::read_config_file");

	fname = input_filename;
	// Don't frob the filename if it starts with a dot and
	// a slash or with two dots and a slash.
	// Or if it's not a relative path.
	if (!is_path_absolute(get_system_path(input_filename))) {
#if (defined(XWIN) || defined(MACOSX) || defined(_WIN32) \
	 || defined(__IPHONEOS__))
		fname = "<CONFIG>/";
#	if (defined(XWIN) && !defined(MACOSX) && !defined(__IPHONEOS__))
		fname += ".";
#	endif
		fname += input_filename;
#else
		// For now, just read file from current directory
		fname = input_filename;
#endif

#if defined(_WIN32)
		// Note: this first check misses some cases of path equality, but it
		// does eliminates some spurious warnings.
		if (fname != input_filename && U7exists(input_filename.c_str())
			&& get_system_path("<HOME>") != ".") {
			if (!U7exists(fname.c_str())) {
				cerr << "Warning: configuration file '" << input_filename
					 << "' is being copied to file '" << fname
					 << "' and will no longer be used." << endl;
				try {
					const size_t pos = fname.find_last_of("/\\");
					if (pos != string::npos) {
						// First, try to make the directory.
						const std::string path = fname.substr(0, pos);
						U7mkdir(path.c_str(), 0755);
					}
					U7copy(input_filename.c_str(), fname.c_str());
				} catch (exult_exception& /*e*/) {
					cerr << "File copy FAILED. Old settings will be lost"
						 << endl;
				}
			} else {
				cerr << "Warning: configuration file '" << input_filename
					 << "' is being ignored in favor of file '" << fname << "'."
					 << endl;
			}
		}
#endif    // _WIN32
	}
	return read_abs_config_file(fname, root);
}

// read config from file, without pre-processing the filename
bool Configuration::read_abs_config_file(
		const string& input_filename, const string& root) {
	filename = input_filename;

	CTRACE("Configuration::read_abs_config_file");

	clear(root);

	is_file = true;    // set to file, even if file not found

	std::unique_ptr<std::istream> pIfile;
	try {
		pIfile = U7open_in(filename.c_str(), true);
	} catch (exult_exception&) {
		// configuration file not found
		return false;
	}
	auto& ifile = *pIfile;

	if (ifile.fail()) {
		return false;
	}

	string sbuf;
	string line;
	// copies the entire contents of the input file into sbuf
	getline(ifile, line);
	while (ifile.good()) {
		sbuf += line + "\n";
		getline(ifile, line);
	}
	// empty file just do nothing
	if (sbuf.empty()) {
		CTRACE("Configuration::read_config_file - no data read");
		return false;
	}
	CTRACE("Configuration::read_config_file - file read");
	read_config_string(sbuf);

	is_file = true;
	return true;
}

string Configuration::dump() {
	return xmltree->dump();
}

ostream& Configuration::dump(ostream& o, const string& indentstr) {
	xmltree->dump(o, indentstr);
	return o;
}

void Configuration::write_back() {
	if (!is_file) {
		return;    // Don't write back if not from a file
	}

	std::unique_ptr<std::ostream> pOfile;
	try {
		pOfile = U7open_out(filename.c_str(), true);
	} catch (const file_open_exception&) {
		perror("Failed to write configuration file");
		return;
	}
	auto& ofile = *pOfile;
	if (ofile.fail()) {
		perror("Failed to write configuration file");
	}
	ofile << dump() << endl;
}

std::vector<string> Configuration::listkeys(
		const string& key, bool longformat) {
	std::vector<string> vs;
	const XMLnode*      sub = xmltree->subtree(key);
	if (sub) {
		sub->listkeys(key, vs, longformat);
	}

	return vs;
}

std::vector<string> Configuration::listkeys(const char* key, bool longformat) {
	const string s(key);
	return listkeys(s, longformat);
}

void Configuration::clear(const string& new_root) {
	CTRACE("Configuration::clear");

	delete xmltree;
	CTRACE("Configuration::clear - xmltree deleted");
	if (!new_root.empty()) {
		rootname = new_root;
	}
	CTRACE("Configuration::clear - new root specified");
	xmltree = new XMLnode(rootname);
	CTRACE("Configuration::clear - fin");
}

void Configuration::getsubkeys(KeyTypeList& ktl, const string& basekey) {
	xmltree->searchpairs(ktl, basekey, string(), 0);
}