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
Here is a simple work around for white space between the '=' sign and the data, in the config file. Assign to the istringstream from the location after the '=' sign and when reading from it, any leading white space is ignored.
Note: while using an istringstream in a loop, make sure you call clear() before assigning a new string to it.
//config.txt
//Input name = image1.png
//Num. of rows = 100
//Num. of cols = 150
std::string ipName;
int nR, nC;
std::ifstream fin("config.txt");
std::string line;
std::istringstream sin;
while (std::getline(fin, line)) {
sin.str(line.substr(line.find("=")+1));
if (line.find("Input name") != std::string::npos) {
std::cout<<"Input name "<<sin.str()<<std::endl;
sin >> ipName;
}
else if (line.find("Num. of rows") != std::string::npos) {
sin >> nR;
}
else if (line.find("Num. of cols") != std::string::npos) {
sin >> nC;
}
sin.clear();
}
How about formatting your configuration as JSON, and using a library like jsoncpp?
e.g.
{"url": "http://mysite dot com",
"file": "main.exe",
"true": 0}
You can then read it into named variables, or even store it all in a std::map, etc. The latter means you can add options without having to change and recompile your configuration parser.
I was looking for something that worked like the python module ConfigParser and found this: https://github.com/jtilly/inih
This is a header only C++ version of inih.
inih (INI Not Invented Here) is a simple .INI file parser written in C. It's only a couple of pages of code, and it was designed to be small and simple, so it's good for embedded systems. It's also more or less compatible with Python's ConfigParser style of .INI files, including RFC 822-style multi-line syntax and name: value entries.
I was searching for a similar simple C++ config file parser and this tutorial website provided me with a basic yet working solution. Its quick and dirty soultion to get the job done.
myConfig.txt
gamma=2.8
mode = 1
path = D:\Photoshop\Projects\Workspace\Images\
The following program reads the previous configuration file:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
int main()
{
double gamma = 0;
int mode = 0;
std::string path;
// std::ifstream is RAII, i.e. no need to call close
std::ifstream cFile("myConfig.txt");
if (cFile.is_open())
{
std::string line;
while (getline(cFile, line))
{
line.erase(std::remove_if(line.begin(), line.end(), isspace),line.end());
if (line[0] == '#' || line.empty()) continue;
auto delimiterPos = line.find("=");
auto name = line.substr(0, delimiterPos);
auto value = line.substr(delimiterPos + 1);
//Custom coding
if (name == "gamma") gamma = std::stod(value);
else if (name == "mode") mode = std::stoi(value);
else if (name == "path") path = value;
}
}
else
{
std::cerr << "Couldn't open config file for reading.\n";
}
std::cout << "\nGamma=" << gamma;
std::cout << "\nMode=" << mode;
std::cout << "\nPath=" << path;
std::getchar();
}
As others have pointed out, it will probably be less work to make use of an existing configuration-file parser library rather than re-invent the wheel.
For example, if you decide to use the Config4Cpp library (which I maintain), then your configuration file syntax will be slightly different (put double quotes around values and terminate assignment statements with a semicolon) as shown in the example below:
# File: someFile.cfg
url = "http://mysite.com";
file = "main.exe";
true_false = "true";
The following program parses the above configuration file, copies the desired values into variables and prints them:
#include <config4cpp/Configuration.h>
#include <iostream>
using namespace config4cpp;
using namespace std;
int main(int argc, char ** argv)
{
Configuration * cfg = Configuration::create();
const char * scope = "";
const char * configFile = "someFile.cfg";
const char * url;
const char * file;
bool true_false;
try {
cfg->parse(configFile);
url = cfg->lookupString(scope, "url");
file = cfg->lookupString(scope, "file");
true_false = cfg->lookupBoolean(scope, "true_false");
} catch(const ConfigurationException & ex) {
cerr << ex.c_str() << endl;
cfg->destroy();
return 1;
}
cout << "url=" << url << "; file=" << file
<< "; true_false=" << true_false
<< endl;
cfg->destroy();
return 0;
}
The Config4Cpp website provides comprehensive documentation, but reading just Chapters 2 and 3 of the "Getting Started Guide" should be more than sufficient for your needs.
So I merged some of the above solutions into my own, which for me made more sense, became more intuitive and a bit less error prone. I use a public stp::map
to keep track of the possible config ids, and a struct
to keep track of the possible values. Her it goes:
struct{
std::string PlaybackAssisted = "assisted";
std::string Playback = "playback";
std::string Recording = "record";
std::string Normal = "normal";
} mode_def;
std::map<std::string, std::string> settings = {
{"mode", mode_def.Normal},
{"output_log_path", "/home/root/output_data.log"},
{"input_log_path", "/home/root/input_data.log"},
};
void read_config(const std::string & settings_path){
std::ifstream settings_file(settings_path);
std::string line;
if (settings_file.fail()){
LOG_WARN("Config file does not exist. Default options set to:");
for (auto it = settings.begin(); it != settings.end(); it++){
LOG_INFO("%s=%s", it->first.c_str(), it->second.c_str());
}
}
while (std::getline(settings_file, line)){
std::istringstream iss(line);
std::string id, eq, val;
if (std::getline(iss, id, '=')){
if (std::getline(iss, val)){
if (settings.find(id) != settings.end()){
if (val.empty()){
LOG_INFO("Config \"%s\" is empty. Keeping default \"%s\"", id.c_str(), settings[id].c_str());
}
else{
settings[id] = val;
LOG_INFO("Config \"%s\" read as \"%s\"", id.c_str(), settings[id].c_str());
}
}
else{ //Not present in map
LOG_ERROR("Setting \"%s\" not defined, ignoring it", id.c_str());
continue;
}
}
else{
// Comment line, skiping it
continue;
}
}
else{
//Empty line, skipping it
continue;
}
}
}