added searching en->jp
[aoi.git] / src / config.hxx
blobedad4ede88b5465353cc72205f02bbf278018ad3
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>
26 using std::string;
27 using std::vector;
29 namespace aoi_config {
32 struct DBTableColumn
34 const char *name;
35 const char *type;
36 bool index;
37 const char *sort;
40 static const std::map<const char*,vector<DBTableColumn>> db_tables =
42 { "aoi", {
43 { "key", "TEXT", false , "ASC" },
44 { "val", "TEXT", false, "ASC" }
47 { "entities", {
48 { "abbr", "TEXT", false, "ASC" },
49 { "desc", "TEXT", false, "ASC" }
52 { "d_reading", {
53 { "rid", "INT", true, "ASC" },
54 { "did", "INT", true, "ASC" },
55 { "reading", "TEXT", true, "ASC" },
56 { "inf", "TEXT", false, "ASC" },
57 { "nokanji", "INT", false, "ASC" },
58 { "freq", "INT", false, "ASC" }
61 { "d_kanji", {
62 { "kid", "INT", true, "ASC" },
63 { "did", "INT", true, "ASC" },
64 { "kanji", "TEXT", true, "ASC" },
65 { "inf", "TEXT", false, "ASC" },
66 { "freq", "INT", false, "ASC" }
69 { "d_sense", {
70 { "sid", "INT", true, "ASC" },
71 { "did", "INT", true, "ASC" },
72 { "gloss", "TEXT", false, "ASC" },
73 { "xref", "TEXT", false, "ASC" },
74 { "ant" , "TEXT", false, "ASC" },
75 { "inf", "TEXT", false, "ASC" },
76 { "pos", "TEXT", false, "ASC" },
77 { "field", "TEXT", false, "ASC" },
78 { "misc", "TEXT", false, "ASC" },
79 { "dial", "TEXT", false, "ASC" }
82 { "d_re_restr", {
83 { "rid", "INT", false, "ASC" },
84 { "kid", "INT", false, "ASC" }
87 { "d_stagk", {
88 { "sid", "INT", false, "ASC" },
89 { "kid", "INT", false, "ASC" }
92 { "d_stagr", {
93 { "sid", "INT", false, "ASC" },
94 { "rid", "INT", false, "ASC" }
97 { "k_kanji", {
98 { "kanji", "TEXT", true, "ASC" },
99 { "ucs", "TEXT", false, "ASC" },
100 { "onyomi", "TEXT", false, "ASC" },
101 { "kunyomi", "TEXT", false, "ASC" },
102 { "meaning", "TEXT", false, "ASC" },
103 { "nanori", "TEXT", false, "ASC" },
104 { "flags", "TEXT", false, "ASC" },
105 { "jlpt", "INT", false, "ASC" },
106 { "grade", "INT", false, "ASC" },
107 { "freq", "INT", false, "ASC" },
108 { "strokes", "INT", false, "ASC" },
109 { "rad_classic","INT", false, "ASC" },
110 { "rad_nelson", "INT", false, "ASC" },
111 { "components", "TEXT", false, "ASC" }
114 { "components", {
115 { "component", "TEXT", false, "ASC" },
116 { "strokes", "INT", false, "ASC" }
119 { "k_skip", {
120 { "kanji", "TEXT", true, "ASC" },
121 { "skip1", "INT", false, "ASC" },
122 { "skip2", "INT", false, "ASC" },
123 { "skip3", "INT", false, "ASC" },
124 { "misclass", "TEXT", false, "ASC" },
127 { "config", {
128 { "key", "TEXT", false, "ASC" },
129 { "val", "TEXT", false, "ASC" }
132 }; // db_tables;
135 // bool is treated as int
136 class Config
138 public:
139 enum ValueType { STRING, INT, BOOL, COLOR, FONT };
140 struct Value {
141 string val;
142 string desc;
143 ValueType type;
145 private:
146 std::map<string,Value> data_;
148 public:
149 Config();
150 ~Config(){};
152 inline std::map<string,Value> get_map () const { return data_; };
154 // returns copy (for int, bool)
155 template<class T>
156 inline T get ( const string &var )
158 if ( data_.find(var) == data_.end())
159 throw std::runtime_error("Config::get(\""+var+"\"): unknown variable");
160 std::stringstream ss;
161 T v;
162 ss << data_.at(var).val;
163 ss >> v;
164 return v;
168 // returns reference (for string, c_char)
169 inline string &get ( const string &var )
171 if ( data_.find(var) == data_.end())
172 throw std::runtime_error("Config::get(\""+var+"\"): unknown variable");
173 return data_.at(var).val;
176 // C++: bool to int conversion is implicit
177 // bool: true -> 1, false -> 0
178 inline void set ( const string &var, int val ) { data_.at(var).val = std::to_string(val); };
179 inline void set ( const string &var, const string &val ) {
180 if ( data_.find(var) == data_.end())
181 throw std::runtime_error("Config::get(\""+var+"\"): unknown variable");
182 data_.at(var).val = val;
187 } // namespace aoi_config
188 #endif // _CONFIG_HXX