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), b(2.2), 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;
};

The above example shows such a configuration class. When an object of this class is created, some of the values are initialized with default values and then added to the configuration.

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

If a configuration file exists at the given location, you can load the values from there and overwrite the 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 must not include spaces. '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");

There is a complete example in the example directory of EEROS. Open a shell in the build directory of your EEROS library and run

$ ./examples/config/configExample
tools/config.txt · Last modified: 2020/12/03 12:01 by ursgraf