databases separate (main->main+user
[aoi.git] / src / config.hxx
blobfefe6a47fdaaf5ff1449e68442ac1509e12eea9d
1 /*
2 Copyright 2013 Karel Matas
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef _CONFIG_HXX
18 #define _CONFIG_HXX
20 #include <string>
21 #include <sstream>
22 #include <map>
23 #include <stdexcept>
24 #include <vector>
25 #include <cstring>
26 #include <cstdio>
28 using std::string;
29 using std::vector;
31 namespace aoi_config {
34 /*!
35 * One column in database.
36 * \sa db_tables_main
37 * \sa db_tables_user
39 struct DBTableColumn
41 const char *name;
42 const char *type;
43 bool index;
44 const char *sort;
47 /*!
48 * List of the tables in the database.
49 * \sa App::check_tables()
50 * \sa App::check_indexes()
52 static const std::map<const char*, std::map<const char*,vector<DBTableColumn>>> db_tables =
54 // main database
55 { "main", {
56 { "aoi", {
57 { "key", "TEXT", false , "ASC" },
58 { "val", "TEXT", false, "ASC" }
61 { "entities", {
62 { "abbr", "TEXT", false, "ASC" },
63 { "desc", "TEXT", false, "ASC" }
66 { "d_reading", {
67 { "rid", "INT", true, "ASC" },
68 { "did", "INT", true, "ASC" },
69 { "reading", "TEXT", true, "ASC" },
70 { "inf", "TEXT", false, "ASC" },
71 { "nokanji", "INT", false, "ASC" },
72 { "freq", "INT", false, "ASC" }
75 { "d_kanji", {
76 { "kid", "INT", true, "ASC" },
77 { "did", "INT", true, "ASC" },
78 { "kanji", "TEXT", true, "ASC" },
79 { "inf", "TEXT", false, "ASC" },
80 { "freq", "INT", false, "ASC" }
83 { "d_sense", {
84 { "sid", "INT", true, "ASC" },
85 { "did", "INT", true, "ASC" },
86 { "gloss", "TEXT", false, "ASC" },
87 { "xref", "TEXT", false, "ASC" },
88 { "ant" , "TEXT", false, "ASC" },
89 { "inf", "TEXT", false, "ASC" },
90 { "pos", "TEXT", false, "ASC" },
91 { "field", "TEXT", false, "ASC" },
92 { "misc", "TEXT", false, "ASC" },
93 { "dial", "TEXT", false, "ASC" }
96 { "d_re_restr", {
97 { "rid", "INT", false, "ASC" },
98 { "kid", "INT", false, "ASC" }
101 { "d_stagk", {
102 { "sid", "INT", false, "ASC" },
103 { "kid", "INT", false, "ASC" }
106 { "d_stagr", {
107 { "sid", "INT", false, "ASC" },
108 { "rid", "INT", false, "ASC" }
111 { "k_kanji", {
112 { "kanji", "TEXT", true, "ASC" },
113 { "ucs", "TEXT", false, "ASC" },
114 { "onyomi", "TEXT", false, "ASC" },
115 { "kunyomi", "TEXT", false, "ASC" },
116 { "meaning", "TEXT", false, "ASC" },
117 { "nanori", "TEXT", false, "ASC" },
118 { "flags", "TEXT", false, "ASC" },
119 { "jlpt", "INT", false, "ASC" },
120 { "grade", "INT", false, "ASC" },
121 { "freq", "INT", false, "ASC" },
122 { "strokes", "INT", false, "ASC" },
123 { "rad_classic","INT", false, "ASC" },
124 { "rad_nelson", "INT", false, "ASC" },
125 { "components", "TEXT", false, "ASC" }
128 { "components", {
129 { "component", "TEXT", false, "ASC" },
130 { "strokes", "INT", false, "ASC" }
133 { "k_skip", {
134 { "kanji", "TEXT", true, "ASC" },
135 { "skip1", "INT", false, "ASC" },
136 { "skip2", "INT", false, "ASC" },
137 { "skip3", "INT", false, "ASC" },
138 { "misclass", "TEXT", false, "ASC" },
141 } // main database tables
142 }, // main database
143 // user database
144 { "user", {
145 { "config", {
146 { "key", "TEXT", false, "ASC" },
147 { "val", "TEXT", false, "ASC" }
150 } // user db tables
151 } // user database
152 }; // db_tables;
157 * Contains current and default (compile time) configuration.
158 * Colors are always saved as in hexadecimal form as strings 0xRRGGBB00.
159 * Does not acces to database - this is a responsibility of App.
160 * \sa App::save_config()
161 * \sa App::load_config()
163 class Config
165 public:
166 enum ValueType { STRING, INT, BOOL, COLOR, FONT };
167 struct Value {
168 string val;
169 string desc;
170 ValueType type;
172 private:
173 std::map<string,Value> data_;
174 std::map<string,Value> default_;
175 std::map<string,Value> overrides_;
177 public:
178 Config();
179 //! Destructor. Empty.
180 ~Config(){};
182 //! Returns current configuration.
183 inline std::map<string,Value> get_map () const { return data_; };
184 //! Return default (compile time configuration.
185 inline std::map<string,Value> get_default_map () const { return default_; };
189 * Returns a copy of the config variable <i>var</i> (for int, bool).
190 * \exception std::runtime_error When key <i>var</i> does not exist or when val can not be found.
192 template<class T>
193 inline T get ( const string &var )
195 Value *val = nullptr;
196 if ( overrides_.find(var) != overrides_.end() )
197 val = &overrides_[var];
198 else if ( data_.find(var) == data_.end())
199 throw std::runtime_error("Config::get(\""+var+"\"): unknown variable");
200 else
201 val = &data_[var];
202 if ( !val )
203 throw std::runtime_error("Config::get(\""+var+"\"): no value found");
204 std::stringstream ss;
205 T v;
206 ss << val->val;
207 ss >> v;
208 return v;
213 * Sets a config variable (int, bool) <i>var</i> to <i>val</i>.
214 * Colors are saved as strings.
215 * \exception std::runtime_error When key <i>var</i> does not exist.
216 * \note C++: implicit conversion bool->int
218 inline void set ( const string &var, int val ) {
219 if ( data_.find(var) == data_.end())
220 throw std::runtime_error("Config::get(\""+var+"\"): unknown variable");
221 Value *v = &data_.at(var);
222 if ( v->type == COLOR ){
223 char buff[16];
224 snprintf( buff, 15, "0x%08x", val);
225 v->val = buff;
227 else
228 v->val = std::to_string(val);
231 * Sets a config variable to <i>val</i>.
232 * \exception std::runtime_error When key <i>var</i> does not exist.
233 * \note C++: implicit conversion bool->int
235 inline void set ( const string &var, const string &val ) {
236 if ( data_.find(var) == data_.end())
237 throw std::runtime_error("Config::get(\""+var+"\"): unknown variable");
238 Value *v = &data_.at(var);
239 v->val = val;
240 v->val = val;
243 * Overrides a config variable. Overriden value is used but not saved.
244 * \exception std::out_of_range whet var does not exist ( map.at(var) )
245 * \sa set()
247 inline void set_override ( const string &var, const string &val ){
248 Value v = data_.at(var);
249 v.val = val;
250 overrides_[var] = v;
255 } // namespace aoi_config
256 #endif // _CONFIG_HXX