forked from edman007/chiton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
121 lines (103 loc) · 3.29 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
/**************************************************************************
*
* 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 Ed Martin <[email protected]>
*
**************************************************************************
*/
#include "chiton_config.hpp"
#include "util.hpp"
#include <fstream>
#include "config_parser.hpp"
Config::Config(){
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 EMPTY_STR;
} 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;
}
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;
}