A naive approach could look like this:
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
std::map<std::string, std::string> options; // global?
void parse(std::istream & cfgfile)
{
for (std::string line; std::getline(cfgfile, line); )
{
std::istringstream iss(line);
std::string id, eq, val;
bool error = false;
if (!(iss >> id))
{
error = true;
}
else if (id[0] == '#')
{
continue;
}
else if (!(iss >> eq >> val >> std::ws) || eq != "=" || iss.get() != EOF)
{
error = true;
}
if (error)
{
// do something appropriate: throw, skip, warn, etc.
}
else
{
options[id] = val;
}
}
}
Now you can access each option value from the global options
map anywhere in your program. If you want castability, you could make the mapped type a boost::variant
.