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.
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
=
54 { "key", "TEXT", false , "ASC" },
55 { "val", "TEXT", false, "ASC" }
59 { "abbr", "TEXT", false, "ASC" },
60 { "desc", "TEXT", false, "ASC" }
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" }
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" }
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" }
94 { "rid", "INT", false, "ASC" },
95 { "kid", "INT", false, "ASC" }
99 { "sid", "INT", false, "ASC" },
100 { "kid", "INT", false, "ASC" }
104 { "sid", "INT", false, "ASC" },
105 { "rid", "INT", false, "ASC" }
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" }
126 { "component", "TEXT", false, "ASC" },
127 { "strokes", "INT", false, "ASC" }
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" },
139 { "key", "TEXT", false, "ASC" },
140 { "val", "TEXT", false, "ASC" }
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()
156 enum ValueType
{ STRING
, INT
, BOOL
, COLOR
, FONT
};
163 std::map
<string
,Value
> data_
;
164 std::map
<string
,Value
> default_
;
165 std::map
<string
,Value
> overrides_
;
169 //! Destructor. Empty.
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.
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");
193 throw std::runtime_error("Config::get(\""+var
+"\"): no value found");
194 std::stringstream ss
;
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
){
214 snprintf( buff
, 15, "0x%08x", val
);
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
);
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) )
237 inline void set_override ( const string
&var
, const string
&val
){
238 Value v
= data_
.at(var
);
245 } // namespace aoi_config
246 #endif // _CONFIG_HXX