aboutsummaryrefslogtreecommitdiffstats
path: root/samples/c++/sample3.cpp
blob: da1de791e1847f1ab7f527079f29a4e0b0bef1ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*************************************************************************
 ** Sample3
 ** Load sample.cfg and try to add a setting "foo"..
 **   on success save to testfoo.cfg
 *************************************************************************/

#include <iostream>
#include <libconfig.h++>

using namespace libconfig;
using namespace std;

/***************************************************************************/

int main()
{
  Config cfg;
  try
  {
    /* Load the configuration.. */
    cout << "loading [sample.cfg]...";
    cfg.readFile("sample.cfg");
    cout << "ok" << endl;

    /* Add setting "foo" */
    cout << "add setting \"foo\"/...";
    Setting &root = cfg.getRoot();
    Setting &foo  = root.add("foo", Setting::TypeInt);
    foo = 1234;
    cout << "ok" << endl;

    /** Look up an array element */
    cout << "looking up array element...";
    Setting &elem = cfg.lookup("arrays.values.[0]");
    int val = elem;
    std::cout << "value is: " << val << std::endl;
    std::cout << "path is: " << elem.getPath() << std::endl;
    
    /* Save to "samplefoo.cfg" */
    cout << "saving [samplefoo.cfg]...";
    cfg.writeFile("samplefoo.cfg");
    cout << "ok" << endl;

    cout << "Done!" << endl;
  }
  catch (...)
  {
    cout << "failed" << endl;
  }

  return 0;
}

/***************************************************************************/