removed x11 color names
[aoi.git] / src / config.hxx
blob020f0f366b44be807b33ba7a357e577d24288a5f
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
38 struct DBTableColumn
40 const char *name;
41 const char *type;
42 bool index;
43 const char *sort;
46 /*!
47 * List of the tables in database.
48 * \sa App::check_tables()
49 * \sa App::check_indexes()
51 static const std::map<const char*,vector<DBTableColumn>> db_tables =
53 { "aoi", {
54 { "key", "TEXT", false , "ASC" },
55 { "val", "TEXT", false, "ASC" }
58 { "entities", {
59 { "abbr", "TEXT", false, "ASC" },
60 { "desc", "TEXT", false, "ASC" }
63 { "d_reading", {
64 { "rid", "INT", true, "ASC" },
65 { "did", "INT", true, "ASC" },
66 { "reading", "TEXT", true, "ASC" },
67 { "inf", "TEXT", false, "ASC" },
68 { "nokanji", "INT", false, "ASC" },
69 { "freq", "INT", false, "ASC" }
72 { "d_kanji", {
73 { "kid", "INT", true, "ASC" },
74 { "did", "INT", true, "ASC" },
75 { "kanji", "TEXT", true, "ASC" },
76 { "inf", "TEXT", false, "ASC" },
77 { "freq", "INT", false, "ASC" }
80 { "d_sense", {
81 { "sid", "INT", true, "ASC" },
82 { "did", "INT", true, "ASC" },
83 { "gloss", "TEXT", false, "ASC" },
84 { "xref", "TEXT", false, "ASC" },
85 { "ant" , "TEXT", false, "ASC" },
86 { "inf", "TEXT", false, "ASC" },
87 { "pos", "TEXT", false, "ASC" },
88 { "field", "TEXT", false, "ASC" },
89 { "misc", "TEXT", false, "ASC" },
90 { "dial", "TEXT", false, "ASC" }
93 { "d_re_restr", {
94 { "rid", "INT", false, "ASC" },
95 { "kid", "INT", false, "ASC" }
98 { "d_stagk", {
99 { "sid", "INT", false, "ASC" },
100 { "kid", "INT", false, "ASC" }
103 { "d_stagr", {
104 { "sid", "INT", false, "ASC" },
105 { "rid", "INT", false, "ASC" }
108 { "k_kanji", {
109 { "kanji", "TEXT", true, "ASC" },
110 { "ucs", "TEXT", false, "ASC" },
111 { "onyomi", "TEXT", false, "ASC" },
112 { "kunyomi", "TEXT", false, "ASC" },
113 { "meaning", "TEXT", false, "ASC" },
114 { "nanori", "TEXT", false, "ASC" },
115 { "flags", "TEXT", false, "ASC" },
116 { "jlpt", "INT", false, "ASC" },
117 { "grade", "INT", false, "ASC" },
118 { "freq", "INT", false, "ASC" },
119 { "strokes", "INT", false, "ASC" },
120 { "rad_classic","INT", false, "ASC" },
121 { "rad_nelson", "INT", false, "ASC" },
122 { "components", "TEXT", false, "ASC" }
125 { "components", {
126 { "component", "TEXT", false, "ASC" },
127 { "strokes", "INT", false, "ASC" }
130 { "k_skip", {
131 { "kanji", "TEXT", true, "ASC" },
132 { "skip1", "INT", false, "ASC" },
133 { "skip2", "INT", false, "ASC" },
134 { "skip3", "INT", false, "ASC" },
135 { "misclass", "TEXT", false, "ASC" },
138 { "config", {
139 { "key", "TEXT", false, "ASC" },
140 { "val", "TEXT", false, "ASC" }
143 }; // db_tables;
147 * Contains current and default (compile time) configuration.
148 * Colors are always saved as in hexadecimal form as strings 0xRRGGBB00.
149 * Does not acces to database - this is a responsibility of App.
150 * \sa App::save_config()
151 * \sa App::load_config()
153 class Config
155 public:
156 enum ValueType { STRING, INT, BOOL, COLOR, FONT };
157 struct Value {
158 string val;
159 string desc;
160 ValueType type;
162 private:
163 std::map<string,Value> data_;
164 std::map<string,Value> default_;
165 std::map<string,Value> overrides_;
167 public:
168 Config();
169 //! Destructor. Empty.
170 ~Config(){};
172 //! Returns current configuration.
173 inline std::map<string,Value> get_map () const { return data_; };
174 //! Return default (compile time configuration.
175 inline std::map<string,Value> get_default_map () const { return default_; };
179 * Returns a copy of the config variable <i>var</i> (for int, bool).
180 * \exception std::runtime_error When key <i>var</i> does not exist or when val can not be found.
182 template<class T>
183 inline T get ( const string &var )
185 Value *val = nullptr;
186 if ( overrides_.find(var) != overrides_.end() )
187 val = &overrides_[var];
188 else if ( data_.find(var) == data_.end())
189 throw std::runtime_error("Config::get(\""+var+"\"): unknown variable");
190 else
191 val = &data_[var];
192 if ( !val )
193 throw std::runtime_error("Config::get(\""+var+"\"): no value found");
194 std::stringstream ss;
195 T v;
196 ss << val->val;
197 ss >> v;
198 return v;
203 * Sets a config variable (int, bool) <i>var</i> to <i>val</i>.
204 * Colors are saved as strings.
205 * \exception std::runtime_error When key <i>var</i> does not exist.
206 * \note C++: implicit conversion bool->int
208 inline void set ( const string &var, int val ) {
209 if ( data_.find(var) == data_.end())
210 throw std::runtime_error("Config::get(\""+var+"\"): unknown variable");
211 Value *v = &data_.at(var);
212 if ( v->type == COLOR ){
213 char buff[16];
214 snprintf( buff, 15, "0x%08x", val);
215 v->val = buff;
217 else
218 v->val = std::to_string(val);
221 * Sets a config variable to <i>val</i>.
222 * \exception std::runtime_error When key <i>var</i> does not exist.
223 * \note C++: implicit conversion bool->int
225 inline void set ( const string &var, const string &val ) {
226 if ( data_.find(var) == data_.end())
227 throw std::runtime_error("Config::get(\""+var+"\"): unknown variable");
228 Value *v = &data_.at(var);
229 v->val = val;
230 v->val = val;
233 * Overrides a config variable. Overriden value is used but not saved.
234 * \exception std::out_of_range whet var does not exist ( map.at(var) )
235 * \sa set()
237 inline void set_override ( const string &var, const string &val ){
238 Value v = data_.at(var);
239 v.val = val;
240 overrides_[var] = v;
245 } // namespace aoi_config
246 #endif // _CONFIG_HXX