Debian Codegraph @

Kernel/configure.cpp

raw symbols SCIP
1 /* This file is part of the FaCT++ DL reasoner
2 Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester
3 Copyright (C) 2015-2017 Dmitry Tsarkov
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "configure.h"
21
22 #include <fstream>
23
24 #undef USE_DEBUG
25
26 Configuration :: ~Configuration()
27 {
28 for ( auto* elem : Base )
29 delete elem;
30 }
31
32 ConfSection :: ~ConfSection()
33 {
34 for ( auto* elem : Base )
35 delete elem;
36 }
37
38 ConfSection* Configuration :: FindSection ( const std::string& pc ) const
39 {
40 for ( ConfSection* section : Base )
41 if ( *section == pc )
42 return section;
43
44 // can not find section
45 return nullptr;
46 }
47
48 ConfElem* ConfSection :: FindByName ( const std::string& name ) const
49 {
50 for ( ConfElem* elem : Base )
51 if ( elem->Name == name )
52 return elem;
53
54 // can not find element in section
55 return nullptr;
56 }
57
58
59 // add section; set Section to new pointer. Ret 1 if couldn't.
60 void Configuration :: createSection ( const std::string& name )
61 {
62 // if section already exists -- nothing to do
63 if ( !useSection ( name ) )
64 return;
65
66 Section = new ConfSection ( name );
67 Base.push_back(Section);
68 isSaved = false;
69 }
70
71 // add Field.value to current Section; sets Element to new p.
72 bool Configuration :: setValue ( const std::string& Field, const std::string& Value )
73 {
74 if ( !Section )
75 return true;
76
77 //changing things
78 isSaved = false;
79
80 // check for existing field
81 if ( (Element = Section->FindByName(Field)) )
82 {
83 Element->Value = Value;
84 return false;
85 }
86 else
87 {
88 Section -> addEntry ( Field, Value );
89 return !(Element = Section->FindByName(Field));
90 }
91 }
92
93 // check if Field exists if Section is set;
94 bool Configuration :: checkValue ( const std::string& Field )
95 {
96 if ( !Section )
97 return true;
98
99 Element = Section->FindByName(Field);
100 return Element == nullptr;
101 }
102
103 // check if Section:Field exists;
104 bool Configuration :: checkValue ( const std::string& Sect, const std::string& Field )
105 {
106 if ( useSection(Sect) )
107 return true;
108
109 Element = Section->FindByName(Field);
110 return Element == nullptr;
111 }
112
113 // Manipulation part
114 void ConfSection :: addEntry ( const std::string& name, const std::string& value )
115 {
116 #ifdef USE_DEBUG
117 std::cerr << "\nadd pair \'" << name << "\',\'" << value << "\' to section \'" << Name << "\'";
118 #endif
119 Base.push_back ( new ConfElem ( name, value ) );
120 }
121
122 // Load part
123 int Configuration :: SplitLine ( char*& pName, char*& pValue )
124 {
125 char* p = Line;
126
127 while ( *p && isspace (*p) ) ++p; // skip leading spaces
128 pName = p;
129 // skip the property name
130 for ( ; *p && *p != '='; ++p )
131 ;
132 if (!*p) return 1;
133
134 // we found '='
135 pValue = p+1; // the next char after '='
136 // skip last spaces
137 for ( *p=0, --p; p!=Line && isspace (*p); --p ) *p=0;
138 if ( p == Line && isspace (*p) ) return 2;
139
140 // here we have name
141 for ( p=pValue; *p && isspace (*p); ++p )
142 ; // skip leading spaces
143 if (!*p) return 3;
144 pValue = p;
145
146 // skip last spaces
147 for ( p=pValue+strlen(pValue)-1; isspace (*p) && p!=pValue; --p ) *p=0;
148 if ( p == pValue && isspace (*p) ) return 4;
149
150 #ifdef USE_DEBUG
151 std::cerr << "\nfound string \'" << pName << "\'=\'" << pValue << "\'";
152 #endif
153 // all right!
154 return 0;
155 }
156
157 void Configuration :: loadString ( std::istream& i )
158 {
159 do
160 i.getline ( Line, MaxConfLineLen );
161 while ( i && isComment () );
162
163 #ifdef USE_DEBUG
164 cerr << "\nload string \'" << Line << "\'";
165 #endif
166 }
167
168 bool Configuration :: isComment ( void ) const
169 {
170 size_t n = strlen (Line);
171
172 if ( n == 0 )
173 return true;
174
175 if ( Line [0] == ';' || Line [0] == '#' || ( Line [0] == '/' && Line [1] == '/' ) )
176 return true;
177
178 for ( size_t i = 0; i < n; i++ )
179 if ( !isspace (Line [i]) )
180 return false;
181
182 return true;
183 }
184
185 bool Configuration :: Load ( const char* Filename )
186 {
187 std::ifstream in ( Filename );
188 char *pName, *pValue;
189
190 isLoaded = false;
191 if ( !in ) return true;
192
193 loadString (in);
194 while ( !in.eof () )
195 {
196 if ( isSection () )
197 loadSection ();
198 else
199 return true;
200
201 do
202 {
203 loadString (in);
204 if ( in.eof () )
205 break;
206
207 if ( isSection () )
208 break;
209
210 if ( SplitLine ( pName, pValue ) )
211 return true;
212
213 if ( setValue ( pName, pValue ) )
214 return true;
215 } while ( !in.eof () );
216 }
217
218 isLoaded = isSaved = true;
219 fileName = Filename;
220 return false;
221 }
222
223 void Configuration :: loadSection ( void )
224 {
225 Line [strlen(Line)-1] = (char) 0; // kill ']' of section
226 #ifdef USE_DEBUG
227 std::cerr << "\nfound section \'" << Line+1 << "\'";
228 #endif
229 createSection ( Line+1 ); // skip '['
230 }
231
232 // Save part
233 void ConfElem :: Save ( std::ostream& o ) const
234 {
235 o << ' ' << Name << " = " << Value << std::endl;
236 }
237
238 void ConfSection :: Save ( std::ostream& o ) const
239 {
240 o << "[" << Name << "]\n";
241
242 for ( const ConfElem* elem : Base )
243 elem->Save(o);
244
245 o << std::endl;
246 }
247
248 bool Configuration :: Save ( const char* Filename )
249 {
250 std::ofstream o ( Filename );
251 if ( o.bad () )
252 return true;
253
254 for ( ConfSection* section : Base )
255 section->Save(o);
256
257 isLoaded = isSaved = true;
258 return false;
259 }