1 | |
---|
2 | #include <stdio.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <iostream> |
---|
5 | |
---|
6 | #include "libconfig.h++" |
---|
7 | |
---|
8 | using namespace libconfig; |
---|
9 | |
---|
10 | int main(int argc, char **argv) |
---|
11 | { |
---|
12 | Config cfg; |
---|
13 | char *locale = NULL; |
---|
14 | |
---|
15 | #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) \ |
---|
16 | && ! defined(__MINGW32__) |
---|
17 | |
---|
18 | locale = "French"; |
---|
19 | |
---|
20 | #elif defined(__APPLE__) |
---|
21 | |
---|
22 | locale = "fr_CA.ISO8859-1"; |
---|
23 | |
---|
24 | #else |
---|
25 | |
---|
26 | locale = "fr_FR.ISO-8859-1"; |
---|
27 | |
---|
28 | #endif |
---|
29 | |
---|
30 | printf("new locale: %s\n", setlocale(LC_NUMERIC, locale)); |
---|
31 | printf("before locale override; pi is: %f\n", 3.141592); |
---|
32 | |
---|
33 | try |
---|
34 | { |
---|
35 | FILE *fp = fopen("test.cfg", "r"); |
---|
36 | |
---|
37 | if(! fp) |
---|
38 | { |
---|
39 | printf("Unable to open test.cfg\n"); |
---|
40 | exit(1); |
---|
41 | } |
---|
42 | |
---|
43 | cfg.read(fp); |
---|
44 | fclose(fp); |
---|
45 | |
---|
46 | // const ConfigSetting& setting = cfg.lookup("application.window.size.w"); |
---|
47 | // long val = setting; |
---|
48 | |
---|
49 | long val = cfg.lookup("application.window.size.w"); |
---|
50 | printf("val: %ld\n", val); |
---|
51 | |
---|
52 | std::string title = cfg.lookup("application.window.title"); |
---|
53 | std::cout << "title: " << title << std::endl; |
---|
54 | |
---|
55 | Setting &ss = cfg.lookup("application.window.title"); |
---|
56 | std::string title2 = ss; |
---|
57 | std::cout << "title: " << title2 << std::endl; |
---|
58 | |
---|
59 | std::string rr = "foo"; |
---|
60 | |
---|
61 | rr = (const char *)cfg.lookup("application.window.title"); |
---|
62 | // rr = (std::string)(cfg.lookup("application.window.title")); |
---|
63 | std::cout << "rr: " << rr << std::endl; |
---|
64 | |
---|
65 | const char *rrr = cfg.lookup("application.window.title"); |
---|
66 | std::cout << "rrr: " << rrr << std::endl; |
---|
67 | |
---|
68 | Setting &s = cfg.lookup("application.group1.my_array"); |
---|
69 | long val4; |
---|
70 | val4 = s[4]; |
---|
71 | printf("item #4 is: %ld\n", val4); |
---|
72 | printf("location of my_array is %d\n", s.getSourceLine()); |
---|
73 | |
---|
74 | Setting &grp = cfg.lookup("application.group1.group2"); |
---|
75 | |
---|
76 | Setting &zzz = cfg.lookup("application.group1.group2.zzz"); |
---|
77 | printf("location of zzz is at %d\n", zzz.getSourceLine()); |
---|
78 | |
---|
79 | Setting &root = cfg.getRoot(); |
---|
80 | |
---|
81 | Setting &rootn = root.add("new-one-at-top", Setting::TypeGroup); |
---|
82 | |
---|
83 | Setting &ngp = rootn.add("element", Setting::TypeFloat); |
---|
84 | |
---|
85 | Setting &misc = root["misc"]; |
---|
86 | unsigned int portnum = 0; |
---|
87 | misc.lookupValue("port", portnum); |
---|
88 | printf("port # is: %d\n", portnum); |
---|
89 | |
---|
90 | ngp = 1.1234567890123; |
---|
91 | |
---|
92 | // long val22 = s[22]; |
---|
93 | // printf("item #22 is: %d\n", val22); |
---|
94 | |
---|
95 | Setting &snew = grp.add("foobar", Setting::TypeArray); |
---|
96 | |
---|
97 | snew.add(Setting::TypeInt); |
---|
98 | snew.add(Setting::TypeInt); |
---|
99 | |
---|
100 | snew.add(Setting::TypeInt); |
---|
101 | snew.add(Setting::TypeInt); |
---|
102 | |
---|
103 | puts("created new array"); |
---|
104 | |
---|
105 | snew[0] = 55; |
---|
106 | puts("elem 0"); |
---|
107 | snew[1] = 66; |
---|
108 | puts("elem 1"); |
---|
109 | |
---|
110 | cfg.setAutoConvert(true); |
---|
111 | |
---|
112 | double dd = cfg.lookup("application.group1.x"); |
---|
113 | printf("auto-converted int->double: %f\n", dd); |
---|
114 | |
---|
115 | int ii = cfg.lookup("misc.pi"); |
---|
116 | printf("auto-converted double->int: %d\n", ii); |
---|
117 | |
---|
118 | Setting &sdel = cfg.lookup("application"); |
---|
119 | |
---|
120 | sdel.remove("group1"); |
---|
121 | |
---|
122 | |
---|
123 | Setting &books = cfg.lookup("books"); |
---|
124 | puts("found books"); |
---|
125 | Setting &book = books.add(Setting::TypeGroup); |
---|
126 | puts("added book"); |
---|
127 | |
---|
128 | Setting &sss = book.add("Title", Setting::TypeString); |
---|
129 | puts("added title"); |
---|
130 | |
---|
131 | sss = "Alice in Wonderland"; |
---|
132 | |
---|
133 | Setting &sss2 = book.add("Price", Setting::TypeFloat); |
---|
134 | sss2 = 9.99; |
---|
135 | |
---|
136 | cfg.write(stdout); |
---|
137 | |
---|
138 | Setting &good = cfg.lookup("books.[2].author"); |
---|
139 | std::string author = good; |
---|
140 | std::cout << "author: " << author << std::endl; |
---|
141 | |
---|
142 | Setting &bad = books[1]["blargh"]; |
---|
143 | } |
---|
144 | catch(ParseException& ex) |
---|
145 | { |
---|
146 | printf("error on line %d: %s\n", ex.getLine(), |
---|
147 | ex.getError()); |
---|
148 | } |
---|
149 | catch(SettingNotFoundException nfex) |
---|
150 | { |
---|
151 | printf("setting not found: %s\n", nfex.getPath()); |
---|
152 | } |
---|
153 | catch(ConfigException& cex) |
---|
154 | { |
---|
155 | printf("config exception!\n"); |
---|
156 | } |
---|
157 | |
---|
158 | printf("locale restored; pi is: %f\n", 3.141592); |
---|
159 | |
---|
160 | return(0); |
---|
161 | } |
---|