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/>.
31 namespace aoi_config
{
35 * One column in database.
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
=
57 { "key", "TEXT", false , "ASC" },
58 { "val", "TEXT", false, "ASC" }
62 { "abbr", "TEXT", false, "ASC" },
63 { "desc", "TEXT", false, "ASC" }
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" }
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" }
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" }
97 { "rid", "INT", false, "ASC" },
98 { "kid", "INT", false, "ASC" }
102 { "sid", "INT", false, "ASC" },
103 { "kid", "INT", false, "ASC" }
107 { "sid", "INT", false, "ASC" },
108 { "rid", "INT", false, "ASC" }
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" }
129 { "component", "TEXT", false, "ASC" },
130 { "strokes", "INT", false, "ASC" }
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
146 { "key", "TEXT", false, "ASC" },
147 { "val", "TEXT", false, "ASC" }
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()
166 enum ValueType
{ STRING
, INT
, BOOL
, COLOR
, FONT
};
173 std::map
<string
,Value
> data_
;
174 std::map
<string
,Value
> default_
;
175 std::map
<string
,Value
> overrides_
;
179 //! Destructor. Empty.
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.
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");
203 throw std::runtime_error("Config::get(\""+var
+"\"): no value found");
204 std::stringstream ss
;
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
){
224 snprintf( buff
, 15, "0x%08x", val
);
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
);
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) )
247 inline void set_override ( const string
&var
, const string
&val
){
248 Value v
= data_
.at(var
);
255 } // namespace aoi_config
256 #endif // _CONFIG_HXX