tables indices and sentences added to config.hxx
[aoi.git] / src / config.hxx
blob7cdd6479b5561feadce430b4f6b97dba88efa4cc
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 { "indices", {
67 { "sid", "INT", false, "ASC" },
68 { "mid", "INT", false, "ASC" },
69 { "headword", "TEXT", false, "ASC" },
70 { "good_example", "INT",false, "ASC" },
71 { "reading", "TEXT", false, "ASC" },
72 { "form", "TEXT", false, "ASC" },
73 { "sense_no", "INT", false, "ASC" }
76 { "sentences", {
77 { "id", "INT", false, "ASC" },
78 { "lang", "TEXT", false, "ASC" },
79 { "text", "TEXT", false, "ASC" }
82 { "d_reading", {
83 { "rid", "INT", true, "ASC" },
84 { "did", "INT", true, "ASC" },
85 { "reading", "TEXT", true, "ASC" },
86 { "inf", "TEXT", false, "ASC" },
87 { "nokanji", "INT", false, "ASC" },
88 { "freq", "INT", false, "ASC" }
91 { "d_kanji", {
92 { "kid", "INT", true, "ASC" },
93 { "did", "INT", true, "ASC" },
94 { "kanji", "TEXT", true, "ASC" },
95 { "inf", "TEXT", false, "ASC" },
96 { "freq", "INT", false, "ASC" }
99 { "d_sense", {
100 { "sid", "INT", true, "ASC" },
101 { "did", "INT", true, "ASC" },
102 { "gloss", "TEXT", false, "ASC" },
103 { "xref", "TEXT", false, "ASC" },
104 { "ant" , "TEXT", false, "ASC" },
105 { "inf", "TEXT", false, "ASC" },
106 { "pos", "TEXT", false, "ASC" },
107 { "field", "TEXT", false, "ASC" },
108 { "misc", "TEXT", false, "ASC" },
109 { "dial", "TEXT", false, "ASC" }
112 { "d_re_restr", {
113 { "rid", "INT", false, "ASC" },
114 { "kid", "INT", false, "ASC" }
117 { "d_stagk", {
118 { "sid", "INT", false, "ASC" },
119 { "kid", "INT", false, "ASC" }
122 { "d_stagr", {
123 { "sid", "INT", false, "ASC" },
124 { "rid", "INT", false, "ASC" }
127 { "k_kanji", {
128 { "kanji", "TEXT", true, "ASC" },
129 { "ucs", "TEXT", false, "ASC" },
130 { "onyomi", "TEXT", false, "ASC" },
131 { "kunyomi", "TEXT", false, "ASC" },
132 { "meaning", "TEXT", false, "ASC" },
133 { "nanori", "TEXT", false, "ASC" },
134 { "flags", "TEXT", false, "ASC" },
135 { "jlpt", "INT", false, "ASC" },
136 { "grade", "INT", false, "ASC" },
137 { "freq", "INT", false, "ASC" },
138 { "strokes", "INT", false, "ASC" },
139 { "rad_classic","INT", false, "ASC" },
140 { "rad_nelson", "INT", false, "ASC" },
141 { "components", "TEXT", false, "ASC" }
144 { "components", {
145 { "component", "TEXT", false, "ASC" },
146 { "strokes", "INT", false, "ASC" }
149 { "k_skip", {
150 { "kanji", "TEXT", true, "ASC" },
151 { "skip1", "INT", false, "ASC" },
152 { "skip2", "INT", false, "ASC" },
153 { "skip3", "INT", false, "ASC" },
154 { "misclass", "TEXT", false, "ASC" },
157 } // main database tables
158 }, // main database
159 // user database
160 { "user", {
161 { "config", {
162 { "key", "TEXT", false, "ASC" },
163 { "val", "TEXT", false, "ASC" }
166 } // user db tables
167 } // user database
168 }; // db_tables;
173 * Contains current and default (compile time) configuration.
174 * Colors are always saved as in hexadecimal form as strings 0xRRGGBB00.
175 * Does not acces to database - this is a responsibility of App.
176 * \sa App::save_config()
177 * \sa App::load_config()
179 class Config
181 public:
182 enum ValueType { STRING, INT, BOOL, COLOR, FONT };
183 struct Value {
184 string val;
185 string desc;
186 ValueType type;
188 private:
189 std::map<string,Value> data_;
190 std::map<string,Value> default_;
191 std::map<string,Value> overrides_;
193 public:
194 Config();
195 //! Destructor. Empty.
196 ~Config(){};
198 //! Returns current configuration.
199 inline std::map<string,Value> get_map () const { return data_; };
200 //! Return default (compile time configuration.
201 inline std::map<string,Value> get_default_map () const { return default_; };
205 * Returns a copy of the config variable <i>var</i> (for int, bool).
206 * \exception std::runtime_error When key <i>var</i> does not exist or when val can not be found.
208 template<class T>
209 inline T get ( const string &var )
211 Value *val = nullptr;
212 if ( overrides_.find(var) != overrides_.end() )
213 val = &overrides_[var];
214 else if ( data_.find(var) == data_.end())
215 throw std::runtime_error("Config::get(\""+var+"\"): unknown variable");
216 else
217 val = &data_[var];
218 if ( !val )
219 throw std::runtime_error("Config::get(\""+var+"\"): no value found");
220 std::stringstream ss;
221 T v;
222 ss << val->val;
223 ss >> v;
224 return v;
229 * Sets a config variable (int, bool) <i>var</i> to <i>val</i>.
230 * Colors are saved as strings.
231 * \exception std::runtime_error When key <i>var</i> does not exist.
232 * \note C++: implicit conversion bool->int
234 inline void set ( const string &var, int val ) {
235 if ( data_.find(var) == data_.end())
236 throw std::runtime_error("Config::get(\""+var+"\"): unknown variable");
237 Value *v = &data_.at(var);
238 if ( v->type == COLOR ){
239 char buff[16];
240 snprintf( buff, 15, "0x%08x", val);
241 v->val = buff;
243 else
244 v->val = std::to_string(val);
247 * Sets a config variable to <i>val</i>.
248 * \exception std::runtime_error When key <i>var</i> does not exist.
249 * \note C++: implicit conversion bool->int
251 inline void set ( const string &var, const string &val ) {
252 if ( data_.find(var) == data_.end())
253 throw std::runtime_error("Config::get(\""+var+"\"): unknown variable");
254 Value *v = &data_.at(var);
255 v->val = val;
256 v->val = val;
259 * Overrides a config variable. Overriden value is used but not saved.
260 * \exception std::out_of_range whet var does not exist ( map.at(var) )
261 * \sa set()
263 inline void set_override ( const string &var, const string &val ){
264 Value v = data_.at(var);
265 v.val = val;
266 overrides_[var] = v;
271 } // namespace aoi_config
272 #endif // _CONFIG_HXX