OpenASIP 2.2
Loading...
Searching...
No Matches
Classes | Public Member Functions | Protected Types | Protected Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
ConfigurationFile Class Reference

#include <ConfigurationFile.hh>

Inheritance diagram for ConfigurationFile:
Inheritance graph
Collaboration diagram for ConfigurationFile:
Collaboration graph

Classes

struct  Key
 

Public Member Functions

 ConfigurationFile (bool doChecking=false)
 
virtual ~ConfigurationFile ()
 
void load (std::istream &inputStream)
 
bool hasKey (const std::string &key)
 
std::string value (const std::string &key, int index=0)
 
int itemCount (const std::string &key)
 
int intValue (const std::string &key, int index=0)
 
float floatValue (const std::string &key, int index=0)
 
std::string stringValue (const std::string &key, int index=0)
 
bool booleanValue (const std::string &key, int index=0)
 
unsigned int timeStampValue (const std::string &key)
 

Protected Types

enum  ConfigurationValueType {
  VT_INTEGER , VT_FLOAT , VT_STRING , VT_BOOLEAN ,
  VT_READABLE_TIME , VT_UNIX_TIMESTAMP
}
 
enum  ConfigurationFileError { FE_SYNTAX , FE_ILLEGAL_TYPE , FE_UNKNOWN_KEY , FE_MISSING_VALUE }
 

Protected Member Functions

void addSupportedKey (const std::string &key, ConfigurationValueType type, bool caseSensitive=false)
 
virtual bool handleError (int lineNumber, ConfigurationFileError error, const std::string &line)
 

Private Types

typedef std::map< TCEString, std::vector< TCEString > > ValueContainer
 
typedef std::map< Key *, ConfigurationValueTypeKeyContainer
 

Private Member Functions

void parse (std::istream &inputStream)
 
bool checkSemantics (const std::string &key, const std::string &value, int lineNumber, const std::string &line)
 
bool isComment (const std::string &line)
 
bool legalTime (const std::string &line)
 
std::string valueOfKey (const std::string &key, int index)
 

Private Attributes

bool check_
 True if semantics of the configuration file is checked.
 
ValueContainer values_
 Contains all the values of configuration file.
 
KeyContainer keys_
 Contains all the legal keys of the configuration file.
 

Detailed Description

Class that is able to read files with key-value-pairs, such as the processor configuration file.

Can do simple sanity and type checking of the file while loading it.

Definition at line 50 of file ConfigurationFile.hh.

Member Typedef Documentation

◆ KeyContainer

Definition at line 107 of file ConfigurationFile.hh.

◆ ValueContainer

typedef std::map<TCEString, std::vector<TCEString> > ConfigurationFile::ValueContainer
private

Definition at line 104 of file ConfigurationFile.hh.

Member Enumeration Documentation

◆ ConfigurationFileError

Error types.

Enumerator
FE_SYNTAX 

Syntax error.

FE_ILLEGAL_TYPE 

Illegal type error.

FE_UNKNOWN_KEY 

Unknown key error.

FE_MISSING_VALUE 

Missing value error.

Definition at line 85 of file ConfigurationFile.hh.

85 {
86 FE_SYNTAX, ///< Syntax error.
87 FE_ILLEGAL_TYPE, ///< Illegal type error.
88 FE_UNKNOWN_KEY, ///< Unknown key error.
89 FE_MISSING_VALUE ///< Missing value error.
90 };
@ FE_UNKNOWN_KEY
Unknown key error.
@ FE_SYNTAX
Syntax error.
@ FE_MISSING_VALUE
Missing value error.
@ FE_ILLEGAL_TYPE
Illegal type error.

◆ ConfigurationValueType

Different value types.

Enumerator
VT_INTEGER 

Integer value.

VT_FLOAT 

Float value.

VT_STRING 

String value.

VT_BOOLEAN 

Boolean value.

VT_READABLE_TIME 

Time in readable format.

VT_UNIX_TIMESTAMP 

Time as seconds since starting of 1970.

Definition at line 73 of file ConfigurationFile.hh.

73 {
74 VT_INTEGER, ///< Integer value.
75 VT_FLOAT, ///< Float value.
76 VT_STRING, ///< String value.
77 VT_BOOLEAN, ///< Boolean value.
78 VT_READABLE_TIME, ///< Time in readable format.
79 VT_UNIX_TIMESTAMP ///< Time as seconds since starting of 1970.
80 };
@ VT_UNIX_TIMESTAMP
Time as seconds since starting of 1970.
@ VT_STRING
String value.
@ VT_READABLE_TIME
Time in readable format.
@ VT_BOOLEAN
Boolean value.
@ VT_INTEGER
Integer value.

Constructor & Destructor Documentation

◆ ConfigurationFile()

ConfigurationFile::ConfigurationFile ( bool  doChecking = false)

Constructor.

Parameters
doCheckingIf true, checks the semantics of the configuration file while reading it.

Definition at line 72 of file ConfigurationFile.cc.

72 : check_(doChecking) {
73}
bool check_
True if semantics of the configuration file is checked.

◆ ~ConfigurationFile()

ConfigurationFile::~ConfigurationFile ( )
virtual

Destructor.

Definition at line 78 of file ConfigurationFile.cc.

78 {
80}
KeyContainer keys_
Contains all the legal keys of the configuration file.
static void deleteAllKeys(MapType &aMap)

References MapTools::deleteAllKeys(), and keys_.

Here is the call graph for this function:

Member Function Documentation

◆ addSupportedKey()

void ConfigurationFile::addSupportedKey ( const std::string &  key,
ConfigurationValueType  type,
bool  caseSensitive = false 
)
protected

Add supported key type.

Parameters
keyKey to be added.
typeType of the value.
caseSensitiveTrue if key is case sensitive.

Definition at line 292 of file ConfigurationFile.cc.

295 {
296
297 Key* k = new Key();
298 k->name_ = key;
299 k->caseSensitive_ = caseSensitive;
300
301 keys_[k] = type;
302}

References ConfigurationFile::Key::caseSensitive_, keys_, and ConfigurationFile::Key::name_.

Referenced by ProcessorConfigurationFile::ProcessorConfigurationFile().

◆ booleanValue()

bool ConfigurationFile::booleanValue ( const std::string &  key,
int  index = 0 
)

Returns the boolean value of the given key.

Parameters
keyThe key.
indexThe index of the value.
Returns
The boolean value of the key.
Exceptions
KeyNotFoundIf key is not found.
OutOfRangeIf index is out of range.
InvalidDataIf value type is wrong.

Definition at line 202 of file ConfigurationFile.cc.

202 {
203 string value = valueOfKey(key, index);
205 if (value == "true" || value == "yes" || value == "on" ||
206 value == "enabled" || value == "1") {
207
208 return true;
209 } else if (value == "false" || value == "no" || value == "off" ||
210 value == "disabled" || value == "0") {
211
212 return false;
213 } else {
214 string msg = "Wrong type of value: " + value;
215 throw InvalidData(__FILE__, __LINE__, __func__, msg);
216 return false;
217 }
218}
#define __func__
std::string valueOfKey(const std::string &key, int index)
std::string value(const std::string &key, int index=0)
static std::string stringToLower(const std::string &source)

References __func__, StringTools::stringToLower(), value(), and valueOfKey().

Here is the call graph for this function:

◆ checkSemantics()

bool ConfigurationFile::checkSemantics ( const std::string &  key,
const std::string &  value,
int  lineNumber,
const std::string &  line 
)
private

Check the semantics of key value pair.

Parameters
keyThe key.
valueThe value.
lineNumberThe line number.
lineThe line.
Returns
True if semantics are ok, otherwise false.

Definition at line 406 of file ConfigurationFile.cc.

410 {
411
412 KeyContainer::iterator iter = keys_.begin();
413 while (iter != keys_.end()) {
414 Key* current = (*iter).first;
415 if (current->caseSensitive_) {
416 if (current->name_ == key) {
417 break;
418 }
419 } else {
420 if (StringTools::stringToUpper(current->name_) ==
422 break;
423 }
424 }
425 iter++;
426 }
427
428 if (iter == keys_.end()) {
429 return handleError(lineNumber, FE_UNKNOWN_KEY, line);
430 }
431
432 ConfigurationValueType type = (*iter).second;
433 switch (type) {
434 case VT_INTEGER:
435 try {
437 } catch (const NumberFormatException& n) {
438 return handleError(lineNumber, FE_ILLEGAL_TYPE, line);
439 }
440 break;
441 case VT_FLOAT:
442 try {
444 } catch (const NumberFormatException& n) {
445 return handleError(lineNumber, FE_ILLEGAL_TYPE, line);
446 }
447 break;
448 case VT_BOOLEAN:
449 if (value != "true" && value != "yes" && value != "on" &&
450 value != "enable" && value != "1" && value != "false" &&
451 value != "no" && value != "off" && value != "disable" &&
452 value != "0") {
453
454 return handleError(lineNumber, FE_ILLEGAL_TYPE, line);
455 }
456 break;
457 case VT_READABLE_TIME:
458 if (!legalTime(value)) {
459 return handleError(lineNumber, FE_ILLEGAL_TYPE, line);
460 }
461 break;
463 try {
465 } catch (const NumberFormatException& n) {
466 return handleError(lineNumber, FE_ILLEGAL_TYPE, line);
467 }
468 break;
469 default:
470 break;
471 }
472
473 return true;
474}
virtual bool handleError(int lineNumber, ConfigurationFileError error, const std::string &line)
bool legalTime(const std::string &line)
static float toFloat(const T &source)
static int toInt(const T &source)
static std::string stringToUpper(const std::string &source)

References ConfigurationFile::Key::caseSensitive_, FE_ILLEGAL_TYPE, FE_UNKNOWN_KEY, handleError(), keys_, legalTime(), ConfigurationFile::Key::name_, StringTools::stringToUpper(), Conversion::toFloat(), Conversion::toInt(), value(), VT_BOOLEAN, VT_FLOAT, VT_INTEGER, VT_READABLE_TIME, and VT_UNIX_TIMESTAMP.

Referenced by parse().

Here is the call graph for this function:

◆ floatValue()

float ConfigurationFile::floatValue ( const std::string &  key,
int  index = 0 
)

Returns the float value of the given key.

Parameters
keyThe key.
indexThe index of the value.
Returns
The float value of the given key.
Exceptions
KeyNotFoundIf key is not found.
OutOfRangeIf index is out of range.
InvalidDataIf data is wrong type.

Definition at line 167 of file ConfigurationFile.cc.

167 {
168 string value = valueOfKey(key, index);
169 try {
171 } catch (const NumberFormatException& n) {
172 string msg = "Wrong type of value: " + value;
173 throw InvalidData(__FILE__, __LINE__, __func__, msg);
174 }
175}

References __func__, Conversion::toFloat(), value(), and valueOfKey().

Here is the call graph for this function:

◆ handleError()

bool ConfigurationFile::handleError ( int  lineNumber,
ConfigurationFileError  error,
const std::string &  line 
)
protectedvirtual

Default implementation, all errors are handled.

Returns
True.

Reimplemented in ProcessorConfigurationFile.

Definition at line 310 of file ConfigurationFile.cc.

313 {
314
315 return true;
316}

Referenced by checkSemantics(), and parse().

◆ hasKey()

bool ConfigurationFile::hasKey ( const std::string &  key)

Returns true if there is a given key with a value in configuration.

Parameters
keyThe key.
Returns
True if there is a given key in a configuration.

Definition at line 99 of file ConfigurationFile.cc.

99 {
100 ValueContainer::iterator iter = values_.find(key);
101 return iter != values_.end();
102}
ValueContainer values_
Contains all the values of configuration file.

References values_.

◆ intValue()

int ConfigurationFile::intValue ( const std::string &  key,
int  index = 0 
)

Returns the int value of the given key.

Parameters
keyThe key.
indexThe index of the value.
Returns
The int value of the key.
Exceptions
KeyNotFoundIf key is not found.
OutOfRangeIf index is out of range.
InvalidDataIf data is wrong type.

Definition at line 146 of file ConfigurationFile.cc.

146 {
147 string value = valueOfKey(key, index);
148 try {
149 return Conversion::toInt(value);
150 } catch (const NumberFormatException& n) {
151 string msg = "Wrong type of value: " + value;
152 throw InvalidData(__FILE__, __LINE__, __func__, msg);
153 }
154}

References __func__, Conversion::toInt(), value(), and valueOfKey().

Referenced by ProcessorConfigurationFile::architectureSize(), ProcessorConfigurationFile::encodingMapSize(), and ProcessorConfigurationFile::implementationSize().

Here is the call graph for this function:

◆ isComment()

bool ConfigurationFile::isComment ( const std::string &  line)
private

Returns true if line is a comment.

Parameters
lineLine to be checked.
Returns
True if line is a comment.

Definition at line 388 of file ConfigurationFile.cc.

388 {
389 if (line.substr(0, 1) == "#") {
390 return true;
391 } else {
392 return false;
393 }
394}

Referenced by parse().

◆ itemCount()

int ConfigurationFile::itemCount ( const std::string &  key)

Returns the number of values a given key has.

Parameters
keyThe key.
Returns
The number of values key has.
Exceptions
KeyNotFoundIf key is not found.

Definition at line 126 of file ConfigurationFile.cc.

126 {
127 ValueContainer::iterator iter = values_.find(key);
128 if (iter == values_.end()) {
129 string msg = "Key " + key + " not found";
130 throw KeyNotFound(__FILE__, __LINE__, __func__, msg);
131 }
132 return (*iter).second.size();
133}

References __func__, and values_.

◆ legalTime()

bool ConfigurationFile::legalTime ( const std::string &  line)
private

Returns true if string is a legal time representation.

Parameters
lineLine to be checked.
Returns
True if string is legal time, false otherwise.

Definition at line 483 of file ConfigurationFile.cc.

483 {
484
485 const char* regExp = "([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{2})";
486 boost::regex expression(regExp);
487 boost::smatch what;
488 if (!boost::regex_match(line, what, expression)) {
489 return false;
490 } else {
491 return true;
492 }
493}

Referenced by checkSemantics(), and timeStampValue().

◆ load()

void ConfigurationFile::load ( std::istream &  inputStream)

Parses the configuration file and stores the results to internal structure.

Parameters
inputStreamInput file stream.

Definition at line 88 of file ConfigurationFile.cc.

88 {
89 parse(inputStream);
90}
void parse(std::istream &inputStream)

References parse().

Referenced by ProcessorConfigurationFile::ProcessorConfigurationFile().

Here is the call graph for this function:

◆ parse()

void ConfigurationFile::parse ( std::istream &  inputStream)
private

Parses the configuration file.

Parameters
inputStreamStream where file is read from.

Definition at line 324 of file ConfigurationFile.cc.

324 {
325
326 string line = "";
327 int lineNumber = 1;
328 while (getline(inputStream, line)) {
329 line = StringTools::trim(line);
330
331 if (isComment(line) || line == "") {
332 lineNumber++;
333 continue;
334 } else {
335 const char* regExp = "[ ]*(.+)[ ]*=[ ]*(.+)[ ]*";
336 boost::regex expression(regExp);
337 boost::smatch what;
338 if (!boost::regex_match(line, what, expression)) {
339 if (!handleError(lineNumber, FE_SYNTAX, line)) {
340 return;
341 } else {
342 lineNumber++;
343 continue;
344 }
345 }
346
347 TCEString key = std::string(what[1]);
348 TCEString value = std::string(what[2]);
349 key = StringTools::trim(key);
351
352 vector<TCEString> values = StringTools::chopString(value, " ");
353
354 if (value == "") {
355 if (!handleError(lineNumber, FE_MISSING_VALUE, line)) {
356 return;
357 } else {
358 lineNumber++;
359 continue;
360 }
361 }
362
363 if (check_) {
364 if (!checkSemantics(key, value, lineNumber, line)) {
365 return;
366 }
367 }
368 ValueContainer::iterator iter = values_.find(key);
369 if (iter == values_.end()) {
370 values_[key] = values;
371 } else {
372 for (size_t i = 0; i < values.size(); i++) {
373 (*iter).second.push_back(values[i]);
374 }
375 }
376 }
377 lineNumber++;
378 }
379}
bool checkSemantics(const std::string &key, const std::string &value, int lineNumber, const std::string &line)
bool isComment(const std::string &line)
static std::string trim(const std::string &source)
static std::vector< TCEString > chopString(const std::string &source, const std::string &delimiters)

References check_, checkSemantics(), StringTools::chopString(), FE_MISSING_VALUE, FE_SYNTAX, handleError(), isComment(), StringTools::trim(), value(), and values_.

Referenced by load().

Here is the call graph for this function:

◆ stringValue()

string ConfigurationFile::stringValue ( const std::string &  key,
int  index = 0 
)

Returns string value of the given key.

Parameters
keyThe key.
indexThe index of the value.
Returns
The string value of the given key.
Exceptions
KeyNotFoundIf key is not found.
OutOfRangeIf index is out of range.

Definition at line 187 of file ConfigurationFile.cc.

187 {
188 return valueOfKey(key, index);
189}

References valueOfKey().

Here is the call graph for this function:

◆ timeStampValue()

unsigned int ConfigurationFile::timeStampValue ( const std::string &  key)

Returns the time stamp value of the given key.

Parameters
keyThe key.
Returns
The time stamp value.
Exceptions
KeyNotFoundIf key is not found.

Definition at line 228 of file ConfigurationFile.cc.

228 {
229 ValueContainer::iterator iter = values_.find(key);
230 if (iter == values_.end()) {
231 string msg = "Key " + key + " not found";
232 throw KeyNotFound(__FILE__, __LINE__, __func__, msg);
233 }
234
235 string value = "";
236 for (size_t i = 0; i < (*iter).second.size(); i++) {
237 value += (*iter).second[i] + " ";
238 }
239
241
242 if (!legalTime(value)) {
243 return 0;
244 }
245
246 size_t pos = value.find("-");
247 string year = value.substr(0, pos);
248 value.replace(0, pos + 1, "");
249
250 pos = value.find("-");
251 string month = value.substr(0, pos);
252 value.replace(0, pos + 1, "");
253
254 pos = value.find(" ");
255 string day = value.substr(0, pos);
256 value.replace(0, pos + 1, "");
257
258 pos = value.find(":");
259 string hours = value.substr(0, pos);
260 value.replace(0, pos + 1, "");
261
262 string minutes = value;
263
264 time_t rawtime;
265 tm* timeinfo;
266
267 time(&rawtime);
268 timeinfo = localtime (&rawtime);
269
270 int temp = Conversion::toInt(year);
271 timeinfo->tm_year = temp - 1900;
272 temp = Conversion::toInt(month);
273 timeinfo->tm_mon = temp - 1;
274 temp = Conversion::toInt(day);
275 timeinfo->tm_mday = temp;
276 temp = Conversion::toInt(hours);
277 timeinfo->tm_hour = temp;
278 temp = Conversion::toInt(minutes);
279 timeinfo->tm_min = temp;
280
281 return mktime(timeinfo);
282}

References __func__, legalTime(), Conversion::toInt(), StringTools::trim(), value(), and values_.

Referenced by ProcessorConfigurationFile::architectureModified(), ProcessorConfigurationFile::encodingMapModified(), and ProcessorConfigurationFile::implementationModified().

Here is the call graph for this function:

◆ value()

std::string ConfigurationFile::value ( const std::string &  key,
int  index = 0 
)

Returns the value of the given key.

Parameters
keyThe key.
indexThe index of the value.
Returns
The value of the given key.
Exceptions
KeyNotFoundIf key is not found.
OutOfRangeIf index is out of range.

Definition at line 114 of file ConfigurationFile.cc.

114 {
115 return valueOfKey(key, index);
116}

References valueOfKey().

Referenced by ProcessorConfigurationFile::architectureName(), booleanValue(), checkSemantics(), ProcessorConfigurationFile::encodingMapName(), floatValue(), ProcessorConfigurationFile::implementationName(), intValue(), parse(), and timeStampValue().

Here is the call graph for this function:

◆ valueOfKey()

string ConfigurationFile::valueOfKey ( const std::string &  key,
int  index 
)
private

Returns the value of the given key and a given index.

Parameters
keyThe key.
indexIndex of the value.
Returns
The value of the key.
Exceptions
KeyNotFoundIf key is not found.
OutOfRangeIf index is out of range.

Definition at line 505 of file ConfigurationFile.cc.

505 {
506 ValueContainer::iterator iter = values_.find(key);
507 if (iter == values_.end()) {
508 string msg = "Key " + key + " not found";
509 throw KeyNotFound(__FILE__, __LINE__, __func__, msg);
510 }
511
512 if (index < 0 || index > static_cast<int>((*iter).second.size()) - 1) {
513 string msg = "Index out of range.";
514 throw OutOfRange(__FILE__, __LINE__, __func__, msg);
515 }
516 return (*iter).second[index];
517}

References __func__, and values_.

Referenced by booleanValue(), floatValue(), intValue(), stringValue(), and value().

Member Data Documentation

◆ check_

bool ConfigurationFile::check_
private

True if semantics of the configuration file is checked.

Definition at line 134 of file ConfigurationFile.hh.

Referenced by parse().

◆ keys_

KeyContainer ConfigurationFile::keys_
private

Contains all the legal keys of the configuration file.

Definition at line 138 of file ConfigurationFile.hh.

Referenced by addSupportedKey(), checkSemantics(), and ~ConfigurationFile().

◆ values_

ValueContainer ConfigurationFile::values_
private

Contains all the values of configuration file.

Definition at line 136 of file ConfigurationFile.hh.

Referenced by hasKey(), itemCount(), parse(), timeStampValue(), and valueOfKey().


The documentation for this class was generated from the following files: