NAWA  0.8
Web Application Framework for C++
Config.cpp
Go to the documentation of this file.
1 
6 /*
7  * Copyright (C) 2019-2021 Tobias Flaig.
8  *
9  * This file is part of nawa.
10  *
11  * nawa is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License,
13  * version 3, as published by the Free Software Foundation.
14  *
15  * nawa is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with nawa. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
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 
30 using namespace nawa;
31 using namespace std;
32 
33 // implementation
34 struct 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 
50 Config::Config(initializer_list<pair<pair<string, string>, string>> init) : Config() {
51  data->values.insert(init.begin(), init.end());
52 }
53 
54 Config::Config(string const& iniFile) : Config() {
55  read(iniFile);
56 }
57 
58 void Config::read(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 
71 void 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 
75 void 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 
81 bool Config::isSet(pair<string, string> const& key) const {
82  return (data->values.count(key) == 1);
83 }
84 
85 string Config::operator[](pair<string, 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
94 void Config::set(std::pair<string, string> key, std::string value) {
95  data->values[move(key)] = move(value);
96 }
97 
98 void Config::set(string section, string key, string value) {
99  set(pair<string, string>(move(section), move(key)), 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
bool isSet(std::pair< std::string, std::string > const &key) const
Definition: Config.cpp:81
void read(std::string const &iniFile)
Definition: Config.cpp:58
void insert(std::initializer_list< std::pair< std::pair< std::string, std::string >, std::string >> init)
Definition: Config.cpp:71
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
std::string operator[](std::pair< std::string, std::string > const &key) const
Definition: Config.cpp:85
#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