Line data Source code
1 : // Snap Websites Server -- configuration reader
2 : // Copyright (c) 2011-2019 Made to Order Software Corp. All Rights Reserved
3 : //
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 2 of the License, or
7 : // (at your option) any later version.
8 : //
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.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 :
18 : #pragma once
19 :
20 : // ourselves
21 : //
22 : #include "snapwebsites/snap_exception.h"
23 :
24 : // C++ lib
25 : //
26 : #include <map>
27 : #include <memory>
28 :
29 : namespace snap
30 : {
31 :
32 0 : class snap_configurations_exception : public snap_exception
33 : {
34 : public:
35 0 : explicit snap_configurations_exception(char const * what_msg) : snap_exception("snap_configurations", what_msg) {}
36 0 : explicit snap_configurations_exception(std::string const & what_msg) : snap_exception("snap_configurations", what_msg) {}
37 : explicit snap_configurations_exception(QString const & what_msg) : snap_exception("snap_configurations", what_msg) {}
38 : };
39 :
40 0 : class snap_configurations_exception_too_late : public snap_configurations_exception
41 : {
42 : public:
43 0 : explicit snap_configurations_exception_too_late(char const * what_msg) : snap_configurations_exception(what_msg) {}
44 : explicit snap_configurations_exception_too_late(std::string const & what_msg) : snap_configurations_exception(what_msg) {}
45 : explicit snap_configurations_exception_too_late(QString const & what_msg) : snap_configurations_exception(what_msg) {}
46 : };
47 :
48 0 : class snap_configurations_exception_config_error : public snap_configurations_exception
49 : {
50 : public:
51 : explicit snap_configurations_exception_config_error(char const * what_msg) : snap_configurations_exception(what_msg) {}
52 0 : explicit snap_configurations_exception_config_error(std::string const & what_msg) : snap_configurations_exception(what_msg) {}
53 : explicit snap_configurations_exception_config_error(QString const & what_msg) : snap_configurations_exception(what_msg) {}
54 : };
55 :
56 :
57 :
58 :
59 : class snap_configurations
60 : {
61 : public:
62 : typedef std::shared_ptr<snap_configurations> pointer_t;
63 : typedef std::map<std::string, std::string> parameter_map_t;
64 :
65 : static pointer_t get_instance();
66 :
67 : std::string const & get_configuration_path() const;
68 : void set_configuration_path(std::string const & path);
69 :
70 : bool configuration_file_exists( std::string const & configuration_filename, std::string const &override_filename );
71 :
72 : parameter_map_t const & get_parameters(std::string const & configuration_filename, std::string const & override_filename) const;
73 : void set_parameters(std::string const & configuration_filename, std::string const & override_filename, parameter_map_t const & params);
74 :
75 : std::string get_parameter(std::string const & configuration_filename, std::string const & override_filename, std::string const & parameter_name) const;
76 : bool has_parameter(std::string const & configuration_filename, std::string const & override_filename, std::string const & parameter_name) const;
77 : void set_parameter(std::string const & configuration_filename, std::string const & override_filename, std::string const & parameter_name, std::string const & value);
78 :
79 : bool save(std::string const & configuration_filename, std::string const & override_filename, bool override_file);
80 :
81 : private:
82 : snap_configurations();
83 : snap_configurations(snap_configurations const &) = delete;
84 : snap_configurations & operator = (snap_configurations const &) = delete;
85 : };
86 :
87 :
88 0 : class snap_config
89 : {
90 : public:
91 0 : class snap_config_parameter_ref
92 : {
93 : public:
94 0 : snap_config_parameter_ref(std::string const & configuration_filename, std::string const & override_filename, std::string const & parameter_name)
95 0 : : f_config(snap_configurations::get_instance())
96 : , f_configuration_filename(configuration_filename)
97 : , f_override_filename(override_filename)
98 0 : , f_parameter_name(parameter_name)
99 : {
100 0 : }
101 :
102 0 : snap_config_parameter_ref & operator = (char const * value)
103 : {
104 0 : f_config->set_parameter(f_configuration_filename, f_override_filename, f_parameter_name, value);
105 0 : return *this;
106 : }
107 :
108 0 : snap_config_parameter_ref & operator = (std::string const & value)
109 : {
110 0 : f_config->set_parameter(f_configuration_filename, f_override_filename, f_parameter_name, value);
111 0 : return *this;
112 : }
113 :
114 0 : snap_config_parameter_ref & operator = (QString const & value)
115 : {
116 0 : f_config->set_parameter(f_configuration_filename, f_override_filename, f_parameter_name, value.toUtf8().data());
117 0 : return *this;
118 : }
119 :
120 : snap_config_parameter_ref & operator = (snap_config_parameter_ref const & rhs)
121 : {
122 : // get the value from the rhs
123 : //
124 : std::string const value(rhs.f_config->get_parameter(rhs.f_configuration_filename, f_override_filename, rhs.f_parameter_name));
125 :
126 : // save the value in the lhs
127 : //
128 : f_config->set_parameter(f_configuration_filename, f_override_filename, f_parameter_name, value);
129 :
130 : return *this;
131 : }
132 :
133 : bool operator == (char const * value)
134 : {
135 : return f_config->get_parameter(f_configuration_filename, f_override_filename, f_parameter_name) == value;
136 : }
137 :
138 : bool operator == (std::string const & value)
139 : {
140 : return f_config->get_parameter(f_configuration_filename, f_override_filename, f_parameter_name) == value;
141 : }
142 :
143 : bool operator == (QString const & value)
144 : {
145 : return f_config->get_parameter(f_configuration_filename, f_override_filename, f_parameter_name) == value.toUtf8().data();
146 : }
147 :
148 : bool operator != (char const * value)
149 : {
150 : return f_config->get_parameter(f_configuration_filename, f_override_filename, f_parameter_name) != value;
151 : }
152 :
153 : bool operator != (std::string const & value)
154 : {
155 : return f_config->get_parameter(f_configuration_filename, f_override_filename, f_parameter_name) != value;
156 : }
157 :
158 : bool operator != (QString const & value)
159 : {
160 : return f_config->get_parameter(f_configuration_filename, f_override_filename, f_parameter_name) != value.toUtf8().data();
161 : }
162 :
163 0 : operator std::string () const
164 : {
165 0 : return f_config->get_parameter(f_configuration_filename, f_override_filename, f_parameter_name);
166 : }
167 :
168 0 : operator QString () const
169 : {
170 0 : return QString::fromUtf8(f_config->get_parameter(f_configuration_filename, f_override_filename, f_parameter_name).c_str());
171 : }
172 :
173 0 : bool empty() const
174 : {
175 0 : return f_config->get_parameter(f_configuration_filename, f_override_filename, f_parameter_name).empty();
176 : }
177 :
178 : size_t length() const
179 : {
180 : return f_config->get_parameter(f_configuration_filename, f_override_filename, f_parameter_name).length();
181 : }
182 :
183 : private:
184 : snap_configurations::pointer_t f_config = snap_configurations::pointer_t();
185 : std::string const f_configuration_filename = std::string();
186 : std::string const f_override_filename = std::string();
187 : std::string const f_parameter_name = std::string();
188 : };
189 :
190 : snap_config()
191 : : f_config(snap_configurations::get_instance())
192 : {
193 : }
194 :
195 0 : snap_config(std::string const & configuration_filename)
196 0 : : f_config(snap_configurations::get_instance())
197 0 : , f_configuration_filename(configuration_filename)
198 : {
199 0 : }
200 :
201 : snap_config(std::string const & configuration_filename, std::string const & override_filename)
202 : : f_config(snap_configurations::get_instance())
203 : , f_configuration_filename(configuration_filename)
204 : , f_override_filename(override_filename)
205 : {
206 : }
207 :
208 : // get/set_configuration_filename()
209 0 : std::string const & get_configuration_filename() const
210 : {
211 0 : return f_configuration_filename;
212 : }
213 :
214 0 : void set_configuration_filename(std::string const & configuration_filename)
215 : {
216 0 : f_configuration_filename = configuration_filename;
217 0 : }
218 :
219 0 : bool configuration_file_exists() const
220 : {
221 0 : return f_config->configuration_file_exists(f_configuration_filename, f_override_filename);
222 : }
223 :
224 : // get/set_override_filename()
225 : std::string const & get_override_filename() const
226 : {
227 : return f_override_filename;
228 : }
229 :
230 : void set_override_filename(std::string const & override_filename)
231 : {
232 : f_override_filename = override_filename;
233 : }
234 :
235 : // set_configuration_path() -- set path where configuration file(s) are installed
236 0 : std::string const & get_configuration_path() const
237 : {
238 0 : return f_config->get_configuration_path();
239 : }
240 :
241 0 : void set_configuration_path(std::string const & path)
242 : {
243 0 : f_config->set_configuration_path(path);
244 0 : }
245 :
246 : // get_parameters() -- retrieve map to all parameters
247 : snap_configurations::parameter_map_t const & get_parameters(std::string const & configuration_filename, std::string const & override_filename) const
248 : {
249 : return f_config->get_parameters(configuration_filename, override_filename);
250 : }
251 :
252 : snap_configurations::parameter_map_t const & get_parameters(std::string const & configuration_filename) const
253 : {
254 : return f_config->get_parameters(configuration_filename, f_override_filename);
255 : }
256 :
257 : snap_configurations::parameter_map_t const & get_parameters() const
258 : {
259 : return f_config->get_parameters(f_configuration_filename, f_override_filename);
260 : }
261 :
262 : // set_parameters() -- replace specified parameters
263 : void set_parameters(std::string const & configuration_filename, std::string const & override_filename, snap_configurations::parameter_map_t const & params)
264 : {
265 : f_config->set_parameters(configuration_filename, override_filename, params);
266 : }
267 :
268 : void set_parameters(std::string const & configuration_filename, snap_configurations::parameter_map_t const & params)
269 : {
270 : f_config->set_parameters(configuration_filename, f_override_filename, params);
271 : }
272 :
273 : void set_parameters(snap_configurations::parameter_map_t const & params)
274 : {
275 : f_config->set_parameters(f_configuration_filename, f_override_filename, params);
276 : }
277 :
278 : // get_parameter() -- retrieve parameter value, specifying the configuration filename or not
279 : std::string get_parameter(std::string const & configuration_filename, std::string const & override_filename, std::string const & parameter_name) const
280 : {
281 : return f_config->get_parameter(configuration_filename, override_filename, parameter_name);
282 : }
283 :
284 : std::string get_parameter(std::string const & configuration_filename, std::string const & parameter_name) const
285 : {
286 : return f_config->get_parameter(configuration_filename, f_override_filename, parameter_name);
287 : }
288 :
289 0 : std::string get_parameter(std::string const & parameter_name) const
290 : {
291 0 : return f_config->get_parameter(f_configuration_filename, f_override_filename, parameter_name);
292 : }
293 :
294 : // has_parameter() -- check whether parameter is defined, specifying the configuration filename or not
295 : bool has_parameter(char const * configuration_filename, char const * parameter_name) const
296 : {
297 : return f_config->has_parameter(configuration_filename, f_override_filename, parameter_name);
298 : }
299 :
300 : bool has_parameter(char const * configuration_filename, std::string const & parameter_name) const
301 : {
302 : return f_config->has_parameter(configuration_filename, f_override_filename, parameter_name);
303 : }
304 :
305 : bool has_parameter(std::string const & configuration_filename, char const * parameter_name) const
306 : {
307 : return f_config->has_parameter(configuration_filename, f_override_filename, parameter_name);
308 : }
309 :
310 : bool has_parameter(std::string const & configuration_filename, std::string const & parameter_name) const
311 : {
312 : return f_config->has_parameter(configuration_filename, f_override_filename, parameter_name);
313 : }
314 :
315 : bool has_parameter(QString const & configuration_filename, QString const & parameter_name) const
316 : {
317 : return f_config->has_parameter(configuration_filename.toUtf8().data(), f_override_filename, parameter_name.toUtf8().data());
318 : }
319 :
320 0 : bool has_parameter(char const * parameter_name) const
321 : {
322 0 : return f_config->has_parameter(f_configuration_filename, f_override_filename, parameter_name);
323 : }
324 :
325 : bool has_parameter(std::string const & parameter_name) const
326 : {
327 : return f_config->has_parameter(f_configuration_filename, f_override_filename, parameter_name);
328 : }
329 :
330 : bool has_parameter(QString const & parameter_name) const
331 : {
332 : return f_config->has_parameter(f_configuration_filename, f_override_filename, parameter_name.toUtf8().data());
333 : }
334 :
335 : // set_parameter() -- set parameter value, specifying the configuration filename or not
336 : void set_parameter(std::string const & configuration_filename, std::string const & parameter_name, std::string const & value)
337 : {
338 : f_config->set_parameter(configuration_filename, f_override_filename, parameter_name, value);
339 : }
340 :
341 : void set_parameter(std::string const & parameter_name, std::string const & value)
342 : {
343 : f_config->set_parameter(f_configuration_filename, f_override_filename, parameter_name, value);
344 : }
345 :
346 : // set_parameter_default() -- set parameter value if still undefined, specifying the configuration filename or not
347 : void set_parameter_default(std::string const & configuration_filename, std::string const & parameter_name, std::string const & value)
348 : {
349 : if(!f_config->has_parameter(configuration_filename, f_override_filename, parameter_name))
350 : {
351 : f_config->set_parameter(configuration_filename, f_override_filename, parameter_name, value);
352 : }
353 : }
354 :
355 0 : void set_parameter_default(std::string const & parameter_name, std::string const & value)
356 : {
357 0 : if(!f_config->has_parameter(f_configuration_filename, f_override_filename, parameter_name))
358 : {
359 0 : f_config->set_parameter(f_configuration_filename, f_override_filename, parameter_name, value);
360 : }
361 0 : }
362 :
363 : // f_config() -- get parameter, specifying the configuration filename or not
364 0 : std::string operator () (char const * configuration_filename, char const * parameter_name) const
365 : {
366 0 : return f_config->get_parameter(configuration_filename, f_override_filename, parameter_name);
367 : }
368 :
369 : std::string operator () (char const * configuration_filename, std::string const & parameter_name) const
370 : {
371 : return f_config->get_parameter(configuration_filename, f_override_filename, parameter_name);
372 : }
373 :
374 : std::string operator () (std::string const & configuration_filename, char const * parameter_name) const
375 : {
376 : return f_config->get_parameter(configuration_filename, f_override_filename, parameter_name);
377 : }
378 :
379 : std::string operator () (std::string const & configuration_filename, std::string const & parameter_name) const
380 : {
381 : return f_config->get_parameter(configuration_filename, f_override_filename, parameter_name);
382 : }
383 :
384 0 : QString operator () (QString const & configuration_filename, QString const & parameter_name) const
385 : {
386 0 : return QString::fromUtf8(f_config->get_parameter(configuration_filename.toUtf8().data(), f_override_filename, parameter_name.toUtf8().data()).c_str());
387 : }
388 :
389 : std::string operator () (char const * parameter_name) const
390 : {
391 : return f_config->get_parameter(f_configuration_filename, f_override_filename, parameter_name);
392 : }
393 :
394 : std::string operator () (std::string const & parameter_name) const
395 : {
396 : return f_config->get_parameter(f_configuration_filename, f_override_filename, parameter_name);
397 : }
398 :
399 : QString operator () (QString const & parameter_name) const
400 : {
401 : return QString::fromUtf8(f_config->get_parameter(f_configuration_filename, f_override_filename, parameter_name.toUtf8().data()).c_str());
402 : }
403 :
404 : // f_config[] -- get parameter, only from context configuration
405 : std::string operator [] (char const * parameter_name) const
406 : {
407 : return f_config->get_parameter(f_configuration_filename, f_override_filename, parameter_name);
408 : }
409 :
410 : std::string operator [] (std::string const & parameter_name) const
411 : {
412 : return f_config->get_parameter(f_configuration_filename, f_override_filename, parameter_name);
413 : }
414 :
415 0 : QString operator [] (QString const & parameter_name) const
416 : {
417 0 : return QString::fromUtf8(f_config->get_parameter(f_configuration_filename, f_override_filename, parameter_name.toUtf8().data()).c_str());
418 : }
419 :
420 : // f_config[] = ... -- set parameter, only to context configuration
421 0 : snap_config_parameter_ref operator [] (char const * parameter_name)
422 : {
423 0 : return snap_config_parameter_ref(f_configuration_filename, f_override_filename, parameter_name);
424 : }
425 :
426 : snap_config_parameter_ref operator [] (std::string const & parameter_name)
427 : {
428 : return snap_config_parameter_ref(f_configuration_filename, f_override_filename, parameter_name);
429 : }
430 :
431 0 : snap_config_parameter_ref operator [] (QString const & parameter_name)
432 : {
433 0 : return snap_config_parameter_ref(f_configuration_filename, f_override_filename, parameter_name.toUtf8().data());
434 : }
435 :
436 0 : bool save(bool override_file = true)
437 : {
438 0 : return f_config->save(f_configuration_filename, f_override_filename, override_file);
439 : }
440 :
441 : private:
442 : snap_configurations::pointer_t f_config = snap_configurations::pointer_t();
443 : std::string f_configuration_filename = std::string();
444 : std::string f_override_filename = std::string();
445 : };
446 :
447 :
448 :
449 : }
450 : // namespace snap
451 : // vim: ts=4 sw=4 et
|