File: conf.cpp

package info (click to toggle)
opencity 0.0.6.5stable-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,348 kB
  • sloc: cpp: 26,622; sh: 11,267; xml: 4,453; ansic: 587; makefile: 521
file content (281 lines) | stat: -rw-r--r-- 7,233 bytes parent folder | download | duplicates (3)
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
/***************************************************************************
						conf.cpp  -  description
							-------------------
	begin                : august 1st, 2004
	copyright            : (C) 2004-2010 by Duong Khang NGUYEN
	email                : neoneurone @ gmail com

	$Id: conf.cpp 450 2010-11-21 19:11:43Z neoneurone $
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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     *
 *   any later version.                                                    *
 *                                                                         *
 ***************************************************************************/

#include "conf.h"

#include <fstream>		// for file IO
#include <cerrno>
#include <cctype>		// for isspace()
#include <cstring>		// for C string functions


   /*=====================================================================*/
const OPENCITY_ERR_CODE
Conf::Open( const string& fileName )
{
	std::ifstream inFile( fileName.c_str() );

// Is the file opened ?
	if ( inFile == NULL ) {
		OPENCITY_DEBUG( "WARNING: File open error, see below: " );
		OPENCITY_DEBUG( fileName.c_str() );
		return OC_ERR_FILE;
	}

	char strTemp[ OC_MAX_CONF_LINE ];
	char* strNew;
	char* strFirst;
	char* strSecond;
	char* strEmpty = (char*)"";

// Read the first line
	inFile.getline( strTemp, OC_MAX_CONF_LINE );
	if ( !inFile.good() ) {
		inFile.close();
		OPENCITY_DEBUG( "WARNING: File read error" );
		return OC_ERR_FILE;
	}

// Process the file
	while ( inFile.good() ) {
		if ( (strlen(strTemp) != 0)
		  && (strTemp[0] != OC_CONF_COMMENT_START) ) {

			strNew = strTemp;

		// Get the first token, it's the name of the parameter
			strFirst = strtok( strNew, OC_CONF_KEY_VALUE_SEPARATOR );

		// Get the second token, it's the value of the parameter
			strSecond = strtok( NULL, OC_CONF_KEY_VALUE_SEPARATOR );

		// Trim out spaces from the datas if applicable
			(strFirst != NULL) ? strFirst = Conf::RTrim( strFirst ) : strFirst = strEmpty;
			(strSecond != NULL) ? strSecond = Conf::LTrim( strSecond ) : strSecond = strEmpty;

		// Add the pair to the _mapData if the key is not empty
			if (strlen(strFirst) > 0)
				_mapData[ strFirst ] = strSecond;

//debug cout << "StrFirst/StrSecond: " << strFirst << "/" << strSecond << endl;
//debug
/*
__gnu_cxx::hash<const char *> H;
cout << "'" << (string) strFirst << "' = '" << (string) strSecond << "'" << endl;
cout << "hash : " << H(strFirst) << " / " << H(strSecond) << endl;
cout << (long) strFirst << "=" << (long) strSecond << endl;
cout << "new" << (long) strNew << endl;
			delete strNew;
*/
		}

	// Read the next line
		inFile.getline( strTemp, OC_MAX_CONF_LINE );
	}

	// Close the file.
	if (inFile.eof()) {
		inFile.close();
		return OC_ERR_FREE;
	}

	// Close the file on error.
	inFile.close();
	OPENCITY_DEBUG("FATAL: out of buffer ?");
	assert( 0 );
	return OC_ERR_SOMETHING;
}


   /*=====================================================================*/
void
Conf::Close()
{
//debug
/*
__gnu_cxx::hash_map<string, string, myHash>::iterator iter;
for ( iter = _mapData.begin(); iter != _mapData.end(); iter++ ) {
	cout << "first '" << iter->first << "', second '" << iter->second << "'" << endl;
}
*/
	_mapData.clear();
}


   /*=====================================================================*/
const string &
Conf::GetValue(
	const string& key,
	const string& defaultValue )
{
//debug
/*cout << "key is : '" << key << "', data is : '" << _mapData[ key ] << "'" << endl;
	__gnu_cxx::hash_map<string, string, myHash>::iterator
		iter = _mapData.find( key );
	return iter != _mapData.end() ? iter->second : "";
*/

// IF the key is not in the hash_map THEN return the default value
	if (_mapData.find( key ) == _mapData.end())
		return defaultValue;
	else
		return _mapData[ key ];
}


   /*=====================================================================*/
const OPENCITY_ERR_CODE
Conf::GetBool(
	const string & key,
	bool & rbool,
	const bool defaultValue )
{
// IF the key is not in the hash_map THEN return the default value
	if (_mapData.find( key ) == _mapData.end()) {
		rbool = defaultValue;
		return OC_ERR_FREE;
	}

	if (_mapData[key] == "") {
		return OC_ERR_INVALID;
	}

	if ((strcasecmp(_mapData[key].c_str(), "no") == 0)
	 || (strcasecmp(_mapData[key].c_str(), "n") == 0)
	 || (strcasecmp(_mapData[key].c_str(), "false") == 0)
	 || (strcasecmp(_mapData[key].c_str(), "off") == 0)
	 || (strcasecmp(_mapData[key].c_str(), "0") == 0)) {
		rbool = false;
	}
	else {
		rbool = true;
	}

	return OC_ERR_FREE;
}


   /*=====================================================================*/
const OPENCITY_ERR_CODE
Conf::GetLint(
	const string & key,
	OC_LINT & rlint,
	const OC_LINT defaultValue )
{
/* debug
for (__gnu_cxx::hash_map<string, string, myHash>::iterator i = _mapData.begin();
i != _mapData.end(); i++) {
	cout << "Map key: " << i->first << "/ value: " << i->second << endl;
}
*/

// IF the key is not in the hash_map THEN return the default value
	if (_mapData.find( key ) == _mapData.end()) {
//debug cout << "key: " << key << "/ default: " << def << endl;
		rlint = defaultValue;
		return OC_ERR_FREE;
	}

	errno = 0;		// Reset the errno integer
	rlint = strtol(_mapData[key].c_str(), NULL, 0);

//debug cout << __PRETTY_FUNCTION__ << "read: " << (long int)rlint << endl;

// FIXME: better check
	if (errno != 0) {
		OPENCITY_DEBUG( "Errno: " << errno << "/ Str: " << strerror(errno) );
		return OC_ERR_INVALID;
	}
	else {
		return OC_ERR_FREE;
	}

//	return OC_ERR_FREE;
}


   /*=====================================================================*/
const OPENCITY_ERR_CODE
Conf::GetFloat(
	const string& key,
	float& rfloat,
	const float defaultValue )
{
// IF the key is not in the hash_map THEN return the default value
	if (_mapData.find( key ) == _mapData.end()) {
		rfloat = defaultValue;
		return OC_ERR_FREE;
	}

	// Win32 port
	//rfloat = strtof(_mapData[key].c_str(), NULL);
	rfloat = atof(_mapData[key].c_str());

	return OC_ERR_FREE;
}


   /*=====================================================================*/
   /*                             STATIC     METHOD                       */
   /*=====================================================================*/
char* const
Conf::RTrim( char* const str )
{
	char* strSpace = NULL;

	if (str != NULL) {
		strSpace = str + strlen( str ) - 1;
		while ((strSpace >= str) && (isspace(*strSpace) != 0))
			*strSpace-- = '\0';
	}

	return str;
}


   /*=====================================================================*/
char* const
Conf::LTrim( char* const str )
{
	char* strSpace = NULL;
	char* strEnd = NULL;

	if (str != NULL) {
		strSpace = str;
		strEnd = str;
		strEnd = strEnd + strlen( str );
		while ((strSpace < strEnd) && (isspace((unsigned char)*strSpace) != 0))
			*strSpace++ = '\0';
	}

	return strSpace;
}