File: configure.cpp

package info (click to toggle)
fact%2B%2B 1.6.5~dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 4,496 kB
  • sloc: cpp: 28,000; java: 22,674; xml: 3,268; makefile: 102; ansic: 61; sh: 3
file content (257 lines) | stat: -rw-r--r-- 5,907 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
/* This file is part of the FaCT++ DL reasoner
Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester
Copyright (C) 2015-2016 Dmitry Tsarkov

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "configure.h"

#include <fstream>

#undef USE_DEBUG

Configuration :: ~Configuration ( void )
{
	for ( ConfSectBase::iterator i = Base.begin (); i != Base.end (); ++i )
		delete *i;
}

ConfSection :: ~ConfSection ( void )
{
	for ( ConfBase::iterator i = Base.begin (); i != Base.end (); ++i )
		delete *i;
}

ConfSection* Configuration :: FindSection ( const std::string& pc ) const
{
	for ( ConfSectBase::const_iterator i = Base.begin (); i != Base.end (); ++i )
		if ( **i == pc )
			return *i;

	// can not find section
	return (ConfSection*) NULL;
}

ConfElem* ConfSection :: FindByName ( const std::string& name ) const
{
	for ( ConfBase::const_iterator i = Base.begin (); i != Base.end (); ++i )
		if ( (*i)->Name == name )
			return *i;

	// can not find element in section
	return (ConfElem*) NULL;
}


// add section; set Section to new pointer. Ret 1 if couldn't.
void Configuration :: createSection ( const std::string& name )
{
	// if section already exists -- nothing to do
	if ( !useSection ( name ) )
		return;

	Section = new ConfSection ( name );
	Base.push_back(Section);
	isSaved = false;
}

// add Field.value to current Section; sets Element to new p.
bool Configuration :: setValue ( const std::string& Field, const std::string& Value )
{
	if ( !Section )
		return true;

	//changing things
	isSaved = false;

	// check for existing field
	if ( (Element = Section->FindByName(Field)) )
	{
		Element->Value = Value;
		return false;
	}
	else
	{
		Section -> addEntry ( Field, Value );
		return !(Element = Section->FindByName(Field));
	}
}

// check if Field exists if Section is set;
bool Configuration :: checkValue ( const std::string& Field )
{
	if ( !Section )
		return true;

	Element = Section->FindByName(Field);
	return Element == NULL;
}

// check if Section:Field exists;
bool Configuration :: checkValue ( const std::string& Sect, const std::string& Field )
{
	if ( useSection(Sect) )
		return true;

	Element = Section->FindByName(Field);
	return Element == NULL;
}

// Manipulation part
void ConfSection :: addEntry ( const std::string& name, const std::string& value )
{
	#ifdef USE_DEBUG
		cerr << "\nadd pair \'" << name.c_str() << "\',\'" << value.c_str() << "\' to section \'" << Name.c_str() << "\'";
	#endif
	Base.push_back ( new ConfElem ( name, value ) );
}

// Load part
int Configuration :: SplitLine ( char*& pName, char*& pValue )
{
	char* p = Line;

	while ( *p && isspace (*p) ) ++p;	// skip leading spaces
	pName = p;
	// skip the property name
	for ( ; *p && *p != '='; ++p ) (void)NULL;
	if (!*p) return 1;

	// we found '='
	pValue = p+1;	// the next char after '='
	// skip last spaces
	for ( *p=0, --p; p!=Line && isspace (*p); --p ) *p=0;
	if ( p == Line && isspace (*p) ) return 2;

	// here we have name
	for ( p=pValue; *p && isspace (*p); ++p ) (void)NULL; // skip leading spaces
	if (!*p) return 3;
	pValue = p;

	// skip last spaces
	for ( p=pValue+strlen(pValue)-1; isspace (*p) && p!=pValue; --p ) *p=0;
	if ( p == pValue && isspace (*p) ) return 4;

	#ifdef USE_DEBUG
		cerr << "\nfound string \'" << pName << "\'=\'" << pValue << "\'";
	#endif
	// all right!
	return 0;
}

void Configuration :: loadString ( std::istream& i )
{
	do
		i.getline ( Line, MaxConfLineLen );
	while ( i && isComment () );

	#ifdef USE_DEBUG
		cerr << "\nload string \'" << Line << "\'";
	#endif
}

bool Configuration :: isComment ( void ) const
{
	size_t n = strlen (Line);

	if ( n == 0 )
		return true;

	if ( Line [0] == ';' || Line [0] == '#' || ( Line [0] == '/' && Line [1] == '/' ) )
		return true;

	for ( size_t i = 0; i < n; i++ )
		if ( !isspace (Line [i]) )
			return false;

	return true;
}

bool Configuration :: Load ( const char* Filename )
{
	std::ifstream in ( Filename );
	char *pName, *pValue;

	isLoaded = false;
	if ( !in ) return true;

	loadString (in);
	while ( !in.eof () )
	{
		if ( isSection () )
			loadSection ();
		else
			return true;

		do
		{
			loadString (in);
			if ( in.eof () )
				break;

			if ( isSection () )
				break;

			if ( SplitLine ( pName, pValue ) )
				return true;

			if ( setValue ( pName, pValue ) )
				return true;
		} while ( !in.eof () );
	}

	isLoaded = isSaved = true;
	fileName = Filename;
	return false;
}

void Configuration :: loadSection ( void )
{
	Line [strlen(Line)-1] = (char) 0;	// kill ']' of section
	#ifdef USE_DEBUG
		cerr << "\nfound section \'" << Line+1 << "\'";
	#endif
	createSection ( Line+1 );	// skip '['
}

// Save part
void ConfElem :: Save ( std::ostream& o ) const
{
	o << ' ' << Name.c_str () << " = " << Value.c_str () << std::endl;
}

void ConfSection :: Save ( std::ostream& o ) const
{
	o << "[" << Name.c_str () << "]\n";

	for ( ConfBase::const_iterator i = Base.begin (); i != Base.end (); ++i )
		(*i)->Save (o);

	o << std::endl;
}

bool Configuration :: Save ( const char* Filename )
{
	std::ofstream o ( Filename );
	if ( o.bad () )
		return true;

	for ( ConfSectBase::iterator i = Base.begin (); i != Base.end (); ++i )
		(*i)->Save (o);

	isLoaded = isSaved = true;
	return false;
}