the code:
SectionData::iterator coniter; in.open( cpath_.c_str( ) ); if( in.fail( ) ) throw "Failed to open file [ " + cpath_ + " ]\n"; string Line, section, key, data; if( in.is_open( ) ) { while( !in.eof( ) ){ bool has_sections = false; bool lineIsSection = false; getline(in, Line); /* Skip empty lines and lines begining with '#' * To skip lines in the config file just put '#' in front of * the line. that will make the handler skip the line */ if(Line.empty( ) || Line[0] == '#') continue; //remove new lines and partialy new lines rebuild_string(Line, "\r"); rebuild_string(Line, "\n"); if(exist("[", Line) && exist("]", Line)) { std::cout << "Section found\n" << std::endl; has_sections = true; lineIsSection = true; }else{ lineIsSection = false; } if(has_sections && lineIsSection) { SectionStart = find("[", Line ); SectionEnd = find("]", Line); section = Line.substr(SectionStart +1 , SectionEnd - SectionStart -1); continue; } Split = find("=", Line ); KeyStart = find_first_not_empty(Line); KeyEnd = find_first_not_of("=", Line, KeyStart); ValueStart = find_first_not_empty(Line, Split+1 ); ValueEnd = Line.size( ); key = Line.substr( KeyStart, KeyEnd - KeyStart ); data = Line.substr( ValueStart, ValueEnd - ValueStart); //remove all empty spaces from the strings to prevent invalid results section = rebuild_string(section, " "); key = rebuild_string(key, " "); data = rebuild_string(data, " "); SectionContents[section] = std::make_pair(key, data); } SectionContents.begin(); while (coniter != SectionContents.end()) { std::cout << "Key [ " << coniter->first << " ]\n"; coniter++; } } //Close the file in.close( );
Here is a cfg prototype
[section1]Key1 = 0xF36DKey2 = 0x1337Key3 = 0xFDA4Key4 = 0x87LD[section2]default=config.cfg
SectionContents is a typedef of my map type
and the rest of the values are defined in my class. Hope u could help out.