blob: 70422285ec6f7c6f2a1ec421532547aad04326b2 [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2009, 2010 Free Software Foundation, Inc.
3* Copyright 2010 Kestrel Signal Processing, Inc.
4*
5*
6* This software is distributed under the terms of the GNU Affero Public License.
7* See the COPYING file in the main directory for details.
8*
9* This use of this software may be subject to additional restrictions.
10* See the LEGAL file in the main directory for details.
11
12 This program is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Affero General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Affero General Public License for more details.
21
22 You should have received a copy of the GNU Affero General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
24
25*/
26
27
28
29#include "Configuration.h"
30#include <iostream>
kurtis.heimerlbcf60a82012-10-26 06:25:56 +000031#include <string>
dburgess82c46ff2011-10-07 02:40:51 +000032
33using namespace std;
34
kurtis.heimerl5a872472013-05-31 21:47:25 +000035ConfigurationKeyMap getConfigurationKeys();
36ConfigurationTable gConfig("exampleconfig.db","test", getConfigurationKeys());
dburgess82c46ff2011-10-07 02:40:51 +000037
38void purgeConfig(void*,int,char const*, char const*, sqlite3_int64)
39{
40 //cout << "update hook" << endl;
41 gConfig.purge();
42}
43
44
45int main(int argc, char *argv[])
46{
47
48 gConfig.setUpdateHook(purgeConfig);
49
Tom Tsou15da7e12017-04-03 18:54:02 -070050 const char *keys[5] = {"key1", "key2", "key3", "key4", "key5"};
dburgess82c46ff2011-10-07 02:40:51 +000051
52 for (int i=0; i<5; i++) {
53 gConfig.set(keys[i],i);
54 }
55
56 for (int i=0; i<5; i++) {
57 cout << "table[" << keys[i] << "]=" << gConfig.getStr(keys[i]) << endl;
58 cout << "table[" << keys[i] << "]=" << gConfig.getNum(keys[i]) << endl;
59 }
60
dburgess82c46ff2011-10-07 02:40:51 +000061 for (int i=0; i<5; i++) {
62 cout << "defined table[" << keys[i] << "]=" << gConfig.defines(keys[i]) << endl;
63 }
64
kurtis.heimerlbcf60a82012-10-26 06:25:56 +000065 gConfig.set("key5","100 200 300 400 ");
dburgess82c46ff2011-10-07 02:40:51 +000066 std::vector<unsigned> vect = gConfig.getVector("key5");
67 cout << "vect length " << vect.size() << ": ";
68 for (unsigned i=0; i<vect.size(); i++) cout << " " << vect[i];
69 cout << endl;
kurtis.heimerlbcf60a82012-10-26 06:25:56 +000070 std::vector<string> svect = gConfig.getVectorOfStrings("key5");
71 cout << "vect length " << svect.size() << ": ";
72 for (unsigned i=0; i<svect.size(); i++) cout << " " << svect[i] << ":";
73 cout << endl;
74
75 cout << "bool " << gConfig.getBool("booltest") << endl;
76 gConfig.set("booltest",1);
77 cout << "bool " << gConfig.getBool("booltest") << endl;
78 gConfig.set("booltest",0);
79 cout << "bool " << gConfig.getBool("booltest") << endl;
80
kurtis.heimerl5a872472013-05-31 21:47:25 +000081 gConfig.getStr("newstring");
82 gConfig.getNum("numnumber");
kurtis.heimerlbcf60a82012-10-26 06:25:56 +000083
84
85 SimpleKeyValue pairs;
86 pairs.addItems(" a=1 b=34 dd=143 ");
87 cout<< pairs.get("a") << endl;
88 cout<< pairs.get("b") << endl;
89 cout<< pairs.get("dd") << endl;
90
91 gConfig.set("fkey","123.456");
92 float fval = gConfig.getFloat("fkey");
93 cout << "fkey " << fval << endl;
94
95 cout << "search fkey:" << endl;
96 gConfig.find("fkey",cout);
kurtis.heimerlbcf60a82012-10-26 06:25:56 +000097 cout << "search fkey:" << endl;
98 gConfig.find("fkey",cout);
99 gConfig.remove("fkey");
100 cout << "search fkey:" << endl;
101 gConfig.find("fkey",cout);
102
kurtis.heimerl42267fd2012-11-11 10:15:43 +0000103 try {
104 gConfig.getNum("supposedtoabort");
105 } catch (ConfigurationTableKeyNotFound) {
106 cout << "ConfigurationTableKeyNotFound exception successfully caught." << endl;
107 }
dburgess82c46ff2011-10-07 02:40:51 +0000108}
kurtis.heimerl5a872472013-05-31 21:47:25 +0000109
110ConfigurationKeyMap getConfigurationKeys()
111{
112 ConfigurationKeyMap map;
113 ConfigurationKey *tmp;
114
115 tmp = new ConfigurationKey("booltest","0",
116 "",
117 ConfigurationKey::DEVELOPER,
118 ConfigurationKey::BOOLEAN,
119 "",
120 false,
121 ""
122 );
123 map[tmp->getName()] = *tmp;
124 free(tmp);
125
126 tmp = new ConfigurationKey("numnumber","42",
127 "",
128 ConfigurationKey::DEVELOPER,
129 ConfigurationKey::VALRANGE,
130 "0-100",
131 false,
132 ""
133 );
134 map[tmp->getName()] = *tmp;
135 free(tmp);
136
137 tmp = new ConfigurationKey("newstring","new string value",
138 "",
139 ConfigurationKey::DEVELOPER,
140 ConfigurationKey::STRING,
141 "",
142 false,
143 ""
144 );
145 map[tmp->getName()] = *tmp;
146 free(tmp);
147
148 return map;
149}