It should be fairly easy to write your own parser for this if you use streams. You can read a file using an std::ifstream
:
std::ifstream ifs("filename.ext");
if(!ifs.good()) throw my_exceptions("cannot open file");
read_file(ifs);
Since it seems line-oriented, you would then first read lines, and then process these:
void read_file(std::istream& is)
{
for(;;) {
std::string line;
std::getline(is, line);
if(!is) break;
std::istringstream iss(line);
// read from iss
}
if(!is.eof()) throw my_exceptions("error reading file");
}
For the actual parsing, you could 1)
first peek at the first character. If that's a [
, pop it from the stream, and use std::getline(is,identifier,']')
to read whatever is within '[' and ']'. If it isn't a [
, use std::getline(is, key, '=')
to read the left side of a key-value pair, and then std::getline(is, value)
to read the right side.
Note: Stream input, unfortunately, is usually not exactly lightning fast. (This doesn't have to be that way, but in practice this often is.) However, it is really easy to do and it is fairly easy to do it right, once you know a very few patterns to work with its peculiarities (like if(strm.good())
not being the same as if(strm)
and not being the opposite of if(strm.bad())
and a few other things you'll have to get used to). For something as performance-critical (har har!) as reading an ini file from disk, it should be fast enough in 999,999 out of 1,000,000 cases.