I am trying to create a simple configuration file that looks like this
url = http://mysite.com file = main.exe true = 0
when the program ru
A naive approach could look like this:
#include #include #include #include std::map 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.
options
boost::variant