@@ -29,65 +29,66 @@ IniFileReader::Section::getValue(const std::string &_key) const {
2929bool
3030IniFileReader::load (const std::string &_path) {
3131 std::ifstream configStream (_path);
32- if (configStream.is_open ()) {
33- // COMMONAPI_INFO("Loading ini file from ", _path);
32+ if (!configStream.is_open ()) {
33+ COMMONAPI_ERROR (" Failed to load ini file: " , _path);
34+ return false ;
35+ }
3436
35- int lineCounter (0 );
36- std::string currentSectionName;
37- std::shared_ptr<Section> currentSection;
37+ int lineCounter (0 );
38+ std::string currentSectionName;
39+ std::shared_ptr<Section> currentSection;
3840
39- while (!configStream.eof ()) {
40- std::string line;
41- std::getline (configStream, line);
42- lineCounter++;
41+ while (!configStream.eof ()) {
42+ std::string line;
43+ std::getline (configStream, line);
44+ lineCounter++;
4345
44- trim (line);
46+ trim (line);
4547
46- std::size_t start = line.find (' [' );
47- if (start == 0 ) {
48- std::size_t end = line.find (' ]' );
49- if (end != line.npos ) {
50- currentSectionName = line.substr (++start, --end);
51- if (sections_.end () == sections_.find (currentSectionName)) {
52- currentSection = std::make_shared<Section>();
53- if (currentSection) {
54- sections_[currentSectionName] = currentSection;
55- }
56- } else {
57- COMMONAPI_ERROR (" Double definition of section \' " ,
58- currentSectionName,
59- " \' ignoring definition (line " ,
60- lineCounter,
61- " )" );
62- currentSection = nullptr ;
48+ std::size_t start = line.find (' [' );
49+ if (start == 0 ) {
50+ std::size_t end = line.find (' ]' );
51+ if (end != line.npos ) {
52+ currentSectionName = line.substr (++start, --end);
53+ if (sections_.end () == sections_.find (currentSectionName)) {
54+ currentSection = std::make_shared<Section>();
55+ if (currentSection) {
56+ sections_[currentSectionName] = currentSection;
6357 }
6458 } else {
65- COMMONAPI_ERROR (" Missing \' ]\' in section definition (line " ,
66- lineCounter, " )" );
59+ COMMONAPI_ERROR (" Double definition of section \' " ,
60+ currentSectionName,
61+ " \' ignoring definition (line " ,
62+ lineCounter,
63+ " )" );
64+ currentSection = nullptr ;
6765 }
68- } else if (currentSection) {
69- std:: size_t pos = line. find ( ' = ' );
70- if (pos != line. npos ) {
71- std::string key = line. substr ( 0 , pos);
72- trim (key);
73- if (currentSection-> mappings_ . end ()
74- != currentSection-> mappings_ . find (key) ) {
75- COMMONAPI_ERROR ( " Double definition for key \' " ,
76- key,
77- " ' \' in section \' " ,
78- currentSectionName,
79- " \' (line " ,
80- lineCounter ,
81- " ) " );
82- } else {
83- std::string value = line. substr (pos+ 1 );
84- trim (value);
85- currentSection-> mappings_ [key] = value ;
86- }
87- } else if ( line.size () > 0 ) {
88- COMMONAPI_ERROR ( " Missing \' = \' in key= value definition (line " ,
89- lineCounter, " ) " ) ;
66+ } else {
67+ COMMONAPI_ERROR ( " Missing \' ] \' in section definition (line " ,
68+ lineCounter, " ) " );
69+ }
70+ } else if (currentSection) {
71+ std:: size_t pos = line. find ( ' = ' );
72+ if (pos != line. npos ) {
73+ std::string key = line. substr ( 0 , pos);
74+ trim ( key);
75+ if (currentSection-> mappings_ . end ()
76+ != currentSection-> mappings_ . find (key)) {
77+ COMMONAPI_ERROR ( " Double definition for key \' " ,
78+ key ,
79+ " ' \' in section \' " ,
80+ currentSectionName,
81+ " \' (line " ,
82+ lineCounter,
83+ " ) " ) ;
84+ } else {
85+ std::string value = line.substr (pos+ 1 );
86+ trim ( value);
87+ currentSection-> mappings_ [key] = value ;
9088 }
89+ } else if (line.size () > 0 ) {
90+ COMMONAPI_ERROR (" Missing \' =\' in key=value definition (line " ,
91+ lineCounter, " )" );
9192 }
9293 }
9394 }
0 commit comments