dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 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.heimerl | bcf60a8 | 2012-10-26 06:25:56 +0000 | [diff] [blame] | 31 | #include <string> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace std; |
| 34 | |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 35 | ConfigurationKeyMap getConfigurationKeys(); |
| 36 | ConfigurationTable gConfig("exampleconfig.db","test", getConfigurationKeys()); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 37 | |
| 38 | void purgeConfig(void*,int,char const*, char const*, sqlite3_int64) |
| 39 | { |
| 40 | //cout << "update hook" << endl; |
| 41 | gConfig.purge(); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | int main(int argc, char *argv[]) |
| 46 | { |
| 47 | |
| 48 | gConfig.setUpdateHook(purgeConfig); |
| 49 | |
Tom Tsou | 15da7e1 | 2017-04-03 18:54:02 -0700 | [diff] [blame^] | 50 | const char *keys[5] = {"key1", "key2", "key3", "key4", "key5"}; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 51 | |
| 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 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 61 | for (int i=0; i<5; i++) { |
| 62 | cout << "defined table[" << keys[i] << "]=" << gConfig.defines(keys[i]) << endl; |
| 63 | } |
| 64 | |
kurtis.heimerl | bcf60a8 | 2012-10-26 06:25:56 +0000 | [diff] [blame] | 65 | gConfig.set("key5","100 200 300 400 "); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 66 | 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.heimerl | bcf60a8 | 2012-10-26 06:25:56 +0000 | [diff] [blame] | 70 | 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.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 81 | gConfig.getStr("newstring"); |
| 82 | gConfig.getNum("numnumber"); |
kurtis.heimerl | bcf60a8 | 2012-10-26 06:25:56 +0000 | [diff] [blame] | 83 | |
| 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.heimerl | bcf60a8 | 2012-10-26 06:25:56 +0000 | [diff] [blame] | 97 | 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.heimerl | 42267fd | 2012-11-11 10:15:43 +0000 | [diff] [blame] | 103 | try { |
| 104 | gConfig.getNum("supposedtoabort"); |
| 105 | } catch (ConfigurationTableKeyNotFound) { |
| 106 | cout << "ConfigurationTableKeyNotFound exception successfully caught." << endl; |
| 107 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 108 | } |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 109 | |
| 110 | ConfigurationKeyMap 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 | } |