NAWA 0.9
Web Application Framework for C++
Config.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 Tobias Flaig.
3 *
4 * This file is part of nawa.
5 *
6 * nawa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License,
8 * version 3, as published by the Free Software Foundation.
9 *
10 * nawa is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with nawa. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24#include <boost/functional/hash.hpp>
25#include <inih/ini.h>
26#include <nawa/Exception.h>
27#include <nawa/config/Config.h>
28#include <unordered_map>
29
30using namespace nawa;
31using namespace std;
32
33// implementation
34struct Config::Data {
35 std::unordered_map<std::pair<std::string, std::string>, std::string, boost::hash<std::pair<std::string, std::string>>> values;
36};
37
39
41
43
45
47
49
50Config::Config(std::initializer_list<std::pair<std::pair<std::string, std::string>, std::string>> init) : Config() {
51 data->values.insert(init.begin(), init.end());
52}
53
54Config::Config(std::string const& iniFile) : Config() {
55 read(iniFile);
56}
57
58void Config::read(std::string const& iniFile) {
59 auto valueHandler = [](void* obj, char const* section, char const* name, char const* value) -> int {
60 auto _this = (Config*) obj;
61 pair<string, string> keyToInsert(section, name);
62 pair<pair<string, string>, string> pairToInsert(keyToInsert, value);
63 _this->data->values.insert(pairToInsert);
64 return 1;
65 };
66 if (ini_parse(iniFile.c_str(), valueHandler, this) < 0) {
67 throw Exception(__PRETTY_FUNCTION__, 1, "Could not read config file.");
68 }
69}
70
71void Config::insert(std::initializer_list<std::pair<std::pair<std::string, std::string>, std::string>> init) {
72 data->values.insert(init.begin(), init.end());
73}
74
75void Config::override(std::vector<std::pair<std::pair<std::string, std::string>, std::string>> const& overrides) {
76 for (auto& [k, v] : overrides) {
77 data->values[k] = v;
78 }
79}
80
81bool Config::isSet(std::pair<std::string, std::string> const& key) const {
82 return (data->values.count(key) == 1);
83}
84
85std::string Config::operator[](std::pair<std::string, std::string> const& key) const {
86 if (data->values.count(key) == 1) {
87 return data->values.at(key);
88 } else {
89 return {};
90 }
91}
92
93// doxygen bug requires std:: here
94void Config::set(std::pair<string, string> key, std::string value) {
95 data->values[std::move(key)] = std::move(value);
96}
97
98void Config::set(std::string section, std::string key, std::string value) {
99 set(pair<string, string>(std::move(section), std::move(key)), std::move(value));
100}
Reader for config files and accessor to config values.
Exception class that can be used by apps to catch errors resulting from nawa function calls.
int init(nawa::AppInit &appInit)
Definition: contactform.cpp:40
std::string operator[](std::pair< std::string, std::string > const &key) const
Definition: Config.cpp:85
bool isSet(std::pair< std::string, std::string > const &key) const
Definition: Config.cpp:81
void insert(std::initializer_list< std::pair< std::pair< std::string, std::string >, std::string > > init)
Definition: Config.cpp:71
void read(std::string const &iniFile)
Definition: Config.cpp:58
void set(std::pair< std::string, std::string > key, std::string value)
Definition: Config.cpp:94
void override(std::vector< std::pair< std::pair< std::string, std::string >, std::string > > const &overrides)
Definition: Config.cpp:75
#define NAWA_DEFAULT_DESTRUCTOR_IMPL(Class)
Definition: macros.h:36
#define NAWA_COPY_CONSTRUCTOR_IMPL(Class)
Definition: macros.h:46
#define NAWA_MOVE_ASSIGNMENT_OPERATOR_IMPL(Class)
Definition: macros.h:98
#define NAWA_DEFAULT_CONSTRUCTOR_IMPL(Class)
Definition: macros.h:40
#define NAWA_COPY_ASSIGNMENT_OPERATOR_IMPL(Class)
Definition: macros.h:56
#define NAWA_MOVE_CONSTRUCTOR_IMPL(Class)
Definition: macros.h:88
Definition: AppInit.h:31