Example of reading and saving configuration files using Laral Utilities
This is an example of how to read and write configuration files
#include "configuration/configurationparameters.h" #include "configuration/inifilesupport.h" #include "configuration/evorobotfilesupport.h" #include <iostream> #include <QDebug> // Prints to stdout num tab characters and returns the stream std::ostream& indent(int num, std::ostream &outStream) { for (int i = 0; i < num; i++) { outStream << '\t'; } return outStream; } // A function for printing parameters void printParameters( const ConfigurationParameters& confs, std::ostream &outStream, int level=0, QString group = "" ) { indent(level, outStream) << "GROUP: " << group.section('/',1).toAscii().data() << '\n'; QStringList paramList = confs.getParametersList( group ); bool noparams = true; foreach( QString param, paramList ) { indent(level, outStream) << "- " << param.toAscii().data() << " = " << confs.getValue( group+"/"+param ).toAscii().data() << '\n'; noparams = false; } if ( noparams ) { indent(level, outStream) << "{NO PARAMETERS}" << '\n'; } QStringList groupsList = confs.getGroupsList( group ); foreach( QString subgroup, groupsList ) { printParameters( confs, outStream, level+1, group+"/"+subgroup ); } return; } int main(int argc, char* argv[]) { // We expect the name of the file to read on the command line if (argc != 4) { std::cerr << "Usage: " << argv[0] << " <infile> <outfile> <0|1>" << std::endl; std::cerr << "where 0 means case insensitive mode, 1 case sensitive mode. File format is guessed from file extension" << std::endl; return 1; } const bool caseSensitive = (argv[3][0] == '1') ? true : false; // Creating a configuration parameters object ConfigurationParameters confs(caseSensitive); // Loading parameters. The kind of file is automatically guessed from extension (you can however explicitly set it) if (!confs.loadParameters(argv[1])) { std::cerr << "Loading parameters from file " << argv[1] << " failed" << std::endl; } // Printing the complete list of parameters and recursively all sub-groups printParameters( confs, std::cout ); // Getting a parameter { QString prop = "CROSSOVER/BLU/ciccio"; std::cout << "READING Parameter" << std::endl; QString propVal = confs.getValue(prop); if (propVal.isNull()) { std::cout << "Parameter " << prop.toAscii().data() << " doesn't exist" << std::endl; } else { std::cout << prop.toAscii().data() << " = " << propVal.toAscii().data() << std::endl; } } // Modifying a parameter value { QString prop = "CROSSOVER/BLU/ciccio"; std::cout << "MODIFYING Parameter" << std::endl; bool retval = confs.setValue(prop, "A digiuno"); if (!retval) { std::cout << "Parameter " << prop.toAscii().data() << " doesn't exist" << std::endl; } } // Getting a parameter { QString prop = "CROSSOVER/BLU/ciccio"; std::cout << "READING Parameter" << std::endl; QString propVal = confs.getValue(prop); if (propVal.isNull()) { std::cout << "Parameter " << prop.toAscii().data() << " doesn't exist" << std::endl; } else { std::cout << prop.toAscii().data() << " = " << propVal.toAscii().data() << std::endl; } } // Getting a parameter with alsoMatchParents { QString prop = "REPRODUCTION/SUB/SUB/nelited"; std::cout << "READING Parameter" << std::endl; QString propVal = confs.getValue(prop, true); if (propVal.isNull()) { std::cout << "Parameter " << prop.toAscii().data() << " doesn't exist" << std::endl; } else { std::cout << prop.toAscii().data() << " = " << propVal.toAscii().data() << std::endl; } } // Now saving to file. The kind of file is automatically guessed from extension (you can however explicitly set it) if (!confs.saveParameters(argv[2])) { std::cerr << "Saving parameters to file " << argv[2] << " failed" << std::endl; } return 0; }