-
Notifications
You must be signed in to change notification settings - Fork 1
/
Configuration.cpp
355 lines (285 loc) · 7.99 KB
/
Configuration.cpp
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "Configuration.hpp"
//#include "Util.h"
//#include "Singleton.h"
//#include "Context.h"
#include <fstream>
#include <iostream>
#include <algorithm> // remove(), erase()
//namespace Gemalto
//{
/*
*/
Configuration::Configuration( )
{
m_szConfigurationfilePath = "";
}
/*
*/
void Configuration::load( const std::string& configurationFileName )
{
m_szConfigurationfilePath = configurationFileName;
if( false == parse( configurationFileName ) )
{
std::string msg = "## ERROR ## Failed opening configuration file (" + configurationFileName + ")";
std::cout << msg << std::endl;
throw new std::exception( );
}
}
/*
*/
void Configuration::getConfigurationFilePath( std::string &result )
{
result = m_szConfigurationfilePath;
}
///*
//*/
//void Configuration::print( )
//{
// std::string msg = "print configuration <BEGIN>";
// Gemalto::Context::getInstance( ).getLog( ).write( msg );
//
// for( TConfiguration::iterator i = m_configuration.begin( ); i != m_configuration.end( ); i++ )
// {
// std::string sectionName = (*i).first;
// msg = "CurrentSection <" + sectionName + ">";
// Gemalto::Context::getInstance( ).getLog( ).write( msg );
// TSection sectionMap = (*i).second;
// for( TSection::iterator j = sectionMap.begin( ); j != sectionMap.end( ); j++ )
// {
// std::string key = (*j).first;
// std::string value = (*j).second;
// msg = " Key <" + key + "> - Value <" + value + ">";
// Gemalto::Context::getInstance( ).getLog( ).write( msg );
// }
// }
//
// msg = "print configuration <END>";
// Gemalto::Context::getInstance( ).getLog( ).write( msg );
//}
/*
*/
bool Configuration::checkSection( const std::string& sectionName )
{
// Find the section into the map of sections
TConfigurationIterator it = m_configuration.find( sectionName );
if( m_configuration.end( ) != it )
{
return true;
}
return false;
}
/*
*/
void Configuration::getValue( const std::string& sectionName, const std::string& keyName, std::string& value )
{
// Find the section into the map of sections
TConfigurationIterator it = m_configuration.find( sectionName );
if( m_configuration.end( ) != it )
{
TSection section = it->second;
// Find the value into the map of entries of the selected section
TSectionIterator it2 = section.find( keyName );
if( section.end( ) != it2 )
{
value = it2->second;
}
}
}
/* Read the whole configuration file
*/
bool Configuration::parse( const std::string& configurationFileName )
{
//std::string msg = "Configuration file name <" + configurationFileName + ">";
//std::cout << " ==== " << std::endl;
//std::cout << msg << std::endl;
// Open the configuration file
std::fstream file;
file.open( configurationFileName.c_str( ), std::ios_base::in );
if( !file )
{
return false;
}
// Get the file size
file.seekg( 0, std::ios_base::end );
std::streamoff size = file.tellg( );
file.seekg( 0, std::ios_base::beg );
//std::cout << "FILE SIZE <" << size << ">" << std::endl;
// Read each line of the configuration file
std::string currentSectionName = "";
char cStopCharacter = '\n';
while( !file.eof( ) )
{
std::string tmp;
std::getline( file, tmp, cStopCharacter );
//std::cout << "tmp <" << tmp << ">" << std::endl;
//std::cout << "tmp.size <" << tmp.size( ) << ">" << std::endl;
// If the read line is the whole file
// Then we must restart the parsing changing the ned-line character
if( size == (int)tmp.size( ) )
{
// Reset the read offset to the beginning of the file
file.seekg( 0, std::ios_base::beg );
cStopCharacter = '\r';
tmp = "";
std::getline( file, tmp, cStopCharacter );
//std::cout << "tmp AGAIN <" << tmp << ">" << std::endl;
//std::cout << "tmp.size <" << tmp.size( ) << ">" << std::endl;
if( size == (int)tmp.size( ) )
{
std::cout << "## Error - The configuration file is not readable" << std::endl;
throw new std::exception;
}
}
std::string currentLine = "";
// Suppress all known end-of-line characters
suppressAllOccurencesOfThisCharacter( tmp, '\r', currentLine );
suppressAllOccurencesOfThisCharacter( currentLine, '\n', currentLine );
//std::cout << "currentLine <" << currentLine << ">" << std::endl;
if( currentLine.empty( ) )
{
// The current line is empty
// We must iterate the next current line
continue;
}
// Try to found a comment
std::string::size_type pos = findComment( currentLine );
// Try to find a tag before the comment
std::string::size_type end_pos = 0;
if( findTag( pos, end_pos, currentLine.substr( 0, pos ) ) )
{
// Isolate the potentiel tag
std::string tag = currentLine.substr( pos, end_pos - pos + 1 );
// Verify the tag is a real one
// (check if the '[' and ']' characters are present)
if( isSection( tag ) )
{
getSectionName( tag, currentSectionName );
// Populate the section list with the new section
m_configuration.insert( TConfigurationPair( currentSectionName, TSection( ) ) );
//std::string msg = "CurrentSection <" + currentSectionName + ">";
//std::cout << msg << std::endl;
}
else if( isKey( tag ) )
{
std::string currentKeyName = "";
getKeyName( tag, currentKeyName );
std::string currentKeyValue = "";
getKeyValue( tag, currentKeyValue );
m_configuration[ currentSectionName ].insert( TEntryPair( currentKeyName, currentKeyValue ) );
//std::string msg = " Key <" + currentKeyName + "> - Value <" + currentKeyValue + ">";
//std::cout << msg << std::endl;
}
}
}
file.close( );
//std::cout << " ==== " << std::endl;
return true;
}
/* Return the index of the comment into the incomming string.
A comment is marked with with the ';' character
*/
std::string::size_type Configuration::findComment( const std::string& str )
{
return str.find( ";" );
}
/*
*/
bool Configuration::findTag( std::string::size_type& start, std::string::size_type& end, const std::string& str )
{
start = str.find_first_not_of( " \t" );
if( start == std::string::npos )
{
return false;
}
end = str.find_last_not_of( " \t" );
if( end == std::string::npos )
{
return false;
}
if( start >= end )
{
return false;
}
return true;
}
/*
*/
void Configuration::strip( const std::string& str, std::string &result, const std::string& what )
{
if( false == str.empty( ) )
{
std::string::size_type start = str.find_first_not_of( what );
std::string::size_type end = str.find_last_not_of( what );
if( ( std::string::npos != start ) && ( std::string::npos != end ) )
{
result.assign( str.substr( start, end - start + 1 ) );
}
}
}
/*
*/
bool Configuration::isSection( const std::string& str )
{
std::string::size_type size = str.size( );
if( size <= 2 )
{
return false;
}
if( ( str[ 0 ] == '[' ) && ( str[ size - 1 ] == ']' ) )
{
std::string tmp = "";
strip( str.substr( 1, size - 2 ), tmp );
if( false == tmp.empty( ) )
{
return true;
}
}
return false;
}
/*
*/
bool Configuration::isKey( const std::string& str )
{
if( str.size() < 2 )
return false;
std::string::size_type pos = str.find( "=" );
if( pos == 0 || pos == std::string::npos )
return false;
return true;
}
/*
*/
void Configuration::getKeyName( const std::string& str, std::string &result )
{
std::string::size_type pos = str.find_first_of( " \t=" );
if( std::string::npos != pos )
{
result.assign( str.substr( 0, pos ) );
}
}
/*
*/
void Configuration::getKeyValue( const std::string& str, std::string &result )
{
std::string::size_type start = str.find( "=" );
std::string::size_type pos = str.find_first_not_of( " \t=", start );
if( std::string::npos != pos )
{
std::string tmp = str.substr( pos );
result.assign( tmp );
}
}
/*
*/
void Configuration::getSectionName( const std::string& str, std::string &result )
{
strip( str.substr( 1, str.size( ) - 2 ), result );
}
/* Suppress all occurences of the targeted character
*/
void Configuration::suppressAllOccurencesOfThisCharacter( const std::string& s, char c, std::string& result )
{
result = s;
result.erase( std::remove( result.begin( ), result.end( ), c ), result.end( ) );
}
//} //namespace