WebFramework v3.0.12
Web framework for C++.
Loading...
Searching...
No Matches
Config.cpp
1#include "Config.h"
2
3#include "Exceptions/FileDoesNotExistException.h"
4
5using namespace std;
6
7namespace framework
8{
9 namespace utility
10 {
11 template<ranges::range T>
12 Config& Config::overrideConfigurationArray(string_view key, const T& value, bool recursive)
13 {
14 vector<json::utility::jsonObject> data;
15
16 data.reserve(distance(value.begin(), value.end()));
17
18 for (const auto& temp : value)
19 {
20 json::utility::appendArray(temp, data);
21 }
22
23 currentConfiguration.overrideValue(key, data, recursive);
24
25 return *this;
26 }
27
28 Config::Config(const filesystem::path& configPath) :
29 basePath(filesystem::absolute(configPath))
30 {
31 if (!filesystem::exists(configPath))
32 {
33 throw file_manager::exceptions::FileDoesNotExistException(configPath.string());
34 }
35
36 basePath = basePath.parent_path();
37
38 currentConfiguration.setJSONData(ifstream(configPath));
39 }
40
41 Config::Config(string_view serverConfiguration, string_view applicationDirectory) :
42 currentConfiguration(serverConfiguration),
43 basePath(filesystem::absolute(applicationDirectory))
44 {
45
46 }
47
48 Config& Config::overrideConfiguration(string_view key, const json::utility::jsonObject::variantType& value, bool recursive)
49 {
50 currentConfiguration.overrideValue(key, value, recursive);
51
52 return *this;
53 }
54
55 Config& Config::overrideConfiguration(string_view key, const vector<int64_t>& value, bool recursive)
56 {
57 return this->overrideConfigurationArray(key, value, recursive);
58 }
59
60 Config& Config::overrideConfiguration(string_view key, const vector<string>& value, bool recursive)
61 {
62 return this->overrideConfigurationArray(key, value, recursive);
63 }
64
65 Config& Config::overrideBasePath(const filesystem::path& basePath) //-V688
66 {
67 if (!filesystem::exists(basePath))
68 {
69 throw file_manager::exceptions::FileDoesNotExistException(basePath.string());
70 }
71
72 this->basePath = basePath;
73
74 return *this;
75 }
76
77 const string& Config::getConfigurationString(string_view key, bool recursive) const
78 {
79 return currentConfiguration.getString(key, recursive);
80 }
81
82 int64_t Config::getConfigurationInteger(string_view key, bool recursive) const
83 {
84 return currentConfiguration.getInt(key, recursive);
85 }
86
87 bool Config::getConfigurationBoolean(string_view key, bool recursive) const
88 {
89 return currentConfiguration.getBool(key, recursive);
90 }
91
92 const filesystem::path& Config::getBasePath() const
93 {
94 return basePath;
95 }
96
97 string Config::getConfiguration() const
98 {
99 return (ostringstream() << currentConfiguration).str();
100 }
101
102 string_view Config::getRawConfiguration() const
103 {
104 return currentConfiguration.getRawData();
105 }
106
107 const json::JSONParser& Config::operator * () const
108 {
109 return currentConfiguration;
110 }
111 }
112}
Config file representation.
Definition Config.h:15