-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.cpp
169 lines (145 loc) · 4.84 KB
/
config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**************************************************************************
*
* This file is part of Chiton.
*
* Chiton is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiton is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiton. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright 2020-2022 Ed Martin <[email protected]>
*
**************************************************************************
*/
#include "chiton_config.hpp"
#include "util.hpp"
#include <fstream>
#include "config_parser.hpp"
#include "setting.hpp"
Config::Config(){
set_value("camera-id", std::string("-1"));//set the camera ID so other tools can use it
}
Config::Config(const Config &src) : cfg_db(src.cfg_db) {
set_value("camera-id", std::string("-1"));//set the camera ID so other tools can use it
}
bool Config::load_config(const std::string& path){
std::ifstream ifs;
ifs.open(path);
if (ifs.fail()){
LWARN( "Failed to open config file `" + path + "`");
return false;//didn't work
}
ConfigParser parser(*this, ifs);
parser.parse();
return true;
}
const std::string& Config::get_value(const std::string& key){
if (!key.compare("")){
LWARN( "Code is requesting a null key");
return EMPTY_STR;
}
auto ret = cfg_db.find(key);
if (ret == cfg_db.end()){
return get_default_value(key);
} else {
return ret->second;
}
}
void Config::set_value(const std::string& key, const std::string& value){
if (key.compare("")){
cfg_db[key] = value;
} else {
LINFO( "Ignoring empty key");
}
}
int Config::get_value_int(const std::string& key){
const std::string& val = get_value(key);
if (!val.compare("")){
//empty
return 0;
}
try {
return std::stoi(val);
} catch (const std::invalid_argument& ia){
LWARN( "Config value " + key + " ( " + val + " ) must be an integer");
} catch (const std::out_of_range& ia) {
LWARN( "Config value " + key + " ( " + val + " ) is out of range ");
}
return 0;
}
long Config::get_value_long(const std::string& key){
const std::string& val = get_value(key);
if (!val.compare("")){
//empty
return 0;
}
try {
return std::stol(val);
} catch (const std::invalid_argument& ia){
LWARN( "Config value " + key + " ( " + val + " ) must be an integer");
} catch (const std::out_of_range& ia) {
LWARN( "Config value " + key + " ( " + val + " ) is out of range ");
}
return 0;
}
long long Config::get_value_ll(const std::string& key){
const std::string& val = get_value(key);
if (!val.compare("")){
//empty
return 0;
}
try {
return std::stoll(val);
} catch (const std::invalid_argument& ia){
LWARN( "Config value " + key + " ( " + val + " ) must be an integer");
} catch (const std::out_of_range& ia) {
LWARN( "Config value " + key + " ( " + val + " ) is out of range ");
}
return 0;
}
double Config::get_value_double(const std::string& key){
const std::string& val = get_value(key);
if (!val.compare("")){
//empty
return 0;
}
try {
return std::stod(val);
} catch (const std::invalid_argument& ia){
LWARN( "Config value " + key + " ( " + val + " ) must be an integer");
} catch (const std::out_of_range& ia) {
LWARN( "Config value " + key + " ( " + val + " ) is out of range ");
}
return 0;
}
const std::string& Config::get_default_value(const std::string& key){
for (auto& itr : setting_options){
if (itr.key == key){\
//LDEBUG("Got default value of '" + itr.def + "' for '" + key + "'");//this is really verbose...
return itr.def;
}
}
LWARN("Code used undocumented config value '" + key + "'");
return EMPTY_STR;
}
bool Config::load_camera_config(int camera, Database &db){
//loads the global and then overwrites it with the local
DatabaseResult *res = db.query("SELECT name, value FROM config WHERE camera = -1 OR camera = " + std::to_string(camera) + " ORDER by camera ASC" );
if (!res){
return false;
}
while (res && res->next_row()){
set_value(res->get_field(0), res->get_field(1));
}
delete res;
set_value("camera-id", std::to_string(camera));//to allow us to pull this later
return true;
}