aboutsummaryrefslogtreecommitdiffstats
path: root/samples/c++/stubcpp.cpp
blob: 59c9769bd4beff3274d6f1129ef9d9735d5a75b3 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

#include "libconfig.h++"

using namespace libconfig;

int main(int argc, char **argv)
{
  Config cfg;
  char *locale = NULL;

#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) \
  && ! defined(__MINGW32__)

  locale = "French";

#elif defined(__APPLE__)

  locale = "fr_CA.ISO8859-1";
  
#else

  locale = "fr_FR.ISO-8859-1";

#endif
  
  printf("new locale: %s\n", setlocale(LC_NUMERIC, locale));
  printf("before locale override; pi is: %f\n", 3.141592);
  
  try
  {
    FILE *fp = fopen("test.cfg", "r");

    if(! fp)
    {
      printf("Unable to open test.cfg\n");
      exit(1);
    }
    
    cfg.read(fp);
    fclose(fp);
    
//  const ConfigSetting& setting = cfg.lookup("application.window.size.w");
//  long val = setting;
    
    long val = cfg.lookup("application.window.size.w");
    printf("val: %ld\n", val);

    std::string title = cfg.lookup("application.window.title");
    std::cout << "title: " << title << std::endl;

    Setting &ss = cfg.lookup("application.window.title");
    std::string title2 = ss;
    std::cout << "title: " << title2 << std::endl;

    std::string rr = "foo";

    rr = (const char *)cfg.lookup("application.window.title");
//    rr = (std::string)(cfg.lookup("application.window.title"));
    std::cout << "rr: " << rr << std::endl;

    const char *rrr = cfg.lookup("application.window.title");
    std::cout << "rrr: " << rrr << std::endl;
    
    Setting &s = cfg.lookup("application.group1.my_array");  
    long val4;
    val4 = s[4];
    printf("item #4 is: %ld\n", val4);
    printf("location of my_array is %d\n", s.getSourceLine());

    Setting &grp = cfg.lookup("application.group1.group2");

    Setting &zzz = cfg.lookup("application.group1.group2.zzz");
    printf("location of zzz is at %d\n", zzz.getSourceLine());

    Setting &root = cfg.getRoot();

    Setting &rootn = root.add("new-one-at-top", Setting::TypeGroup);

    Setting &ngp = rootn.add("element",  Setting::TypeFloat);

    Setting &misc = root["misc"];
    unsigned int portnum = 0;
    misc.lookupValue("port", portnum);
    printf("port # is: %d\n", portnum);
    
    ngp = 1.1234567890123;
    
//    long val22 = s[22];
//    printf("item #22 is: %d\n", val22);

    Setting &snew = grp.add("foobar",  Setting::TypeArray);

    snew.add(Setting::TypeInt);
    snew.add(Setting::TypeInt);

    snew.add(Setting::TypeInt);
    snew.add(Setting::TypeInt);
    
    puts("created new array");

    snew[0] = 55;
    puts("elem 0");
    snew[1] = 66;
    puts("elem 1");

    cfg.setAutoConvert(true);

    double dd = cfg.lookup("application.group1.x");
    printf("auto-converted int->double: %f\n", dd);

    int ii = cfg.lookup("misc.pi");
    printf("auto-converted double->int: %d\n", ii);
   
    Setting &sdel = cfg.lookup("application");

    sdel.remove("group1");

    
    Setting &books = cfg.lookup("books");
    puts("found books");
    Setting &book = books.add(Setting::TypeGroup);
    puts("added book");

    Setting &sss = book.add("Title", Setting::TypeString);
    puts("added title");
    
    sss = "Alice in Wonderland";

    Setting &sss2 = book.add("Price", Setting::TypeFloat);
    sss2 = 9.99;

    cfg.write(stdout);

    Setting &good = cfg.lookup("books.[2].author");
    std::string author = good;
    std::cout << "author: " << author << std::endl;
    
    Setting &bad = books[1]["blargh"];
  }
  catch(ParseException& ex)
  {
    printf("error on line %d: %s\n", ex.getLine(),
           ex.getError());
  }
  catch(SettingNotFoundException nfex)
  {
    printf("setting not found: %s\n", nfex.getPath());
  }
  catch(ConfigException& cex)
  {
    printf("config exception!\n");
  }
 
  printf("locale restored; pi is: %f\n", 3.141592);
 
  return(0);
}