User Tools

Site Tools


tools:config

Configuration Files

There is often a need to load a configuration from a text file on the disk or write configuration into such a file. This could be calibration data, ip-numbers, etc. This can be achieved in EEROS by packing this configuration data into a class as follows:

TestConfig : public eeros::config::FileConfig {
public:
  TestConfig(const char *name) : 
    FileConfig(name),
    // set default values
    a(1),
    b(2.2),
    c{0.1, 0.2, 0.3},
    d("127.0.0.0") {
      add("id", a);
      add("offset", b);
      add("initValues", c);
      add("ip", d);
  }
  int a;
  double b;
  std::array<double, 3> c;
  std::string d;
};

When such a configuration class object is created, its values are initialized with default values.

  TestConfig configFile("config.txt");	// choose an appropriate path

However, if a configuration file exists at the given location, you can load the values from there and overwrite these default values with the values given in the file.

  TestConfig configFile("config.txt");	// choose an appropriate path
  configFile.load();

WARNING Valid name for the identifiers of the values include numbers and letters only, no spaces, underlines or dots. 'myText10' is valid while 'my_text_10' is not!

You can manually create a suitable configuration file at the given location, e.g.

id = 1
offset = 2.200000
initValues = 0.1, 0.4, 0.3
ip = 127.0.0.0

or you could save the values of your configuration class object into the file (and possible change an existing content).

  configFile.save();

It's also possible to save to or restore from another file by writing:

  configFile.save("config2.txt");
  configFile.load("config2.txt");
tools/config.txt · Last modified: 2018/01/12 09:28 by graf