-
Notifications
You must be signed in to change notification settings - Fork 6
/
confparser.cpp
76 lines (64 loc) · 1.53 KB
/
confparser.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
#include "confparser.h"
#include <Wt/Json/Parser>
#include <Wt/Json/Array>
#include <Wt/Json/Value>
#include <fstream>
#include <string>
std::string ConfParser::confFilePossibleLocation (int id)
{
switch (id) {
case 0:
return "wt-rpm.json";
case 1:
return "../wt-rpm.json";
case 2:
return getenv("HOME") + std::string("/.wt-rpm.json");
case 3:
return "/etc/wt-rpm.json";
default:
return std::string();
}
}
ConfParser::ConfParser()
{
std::string path, file;
int id = 0;
/* read the file */
while ((path = confFilePossibleLocation(id)) != std::string()) {
std::ifstream db(path);
if (db.is_open())
{
std::cerr << "Use configuration file '" + path + "'" << std::endl;
std::string line;
while (db.good()) {
getline(db, line);
file += line;
}
db.close();
break;
}
else {
std::cerr << "The configuration file '" + path + "' cannot be opened" << std::endl;
id++;
}
}
if (file.size() == 0)
return;
/* parse the configuration file */
try {
Wt::Json::Object parsed_file;
Wt::WString backend;
Wt::Json::parse(file, parsed_file);
backend = readJSONValue<Wt::WString>(parsed_file, "backend");
if (backend == std::string()) {
std::cerr << "No backend found in configuration file '" << path << "'. Abort." << std::endl;
return;
}
this->_backend = backend;
_backendConfig = readJSONValue<Wt::Json::Object>(parsed_file, "configuration");
}
catch (Wt::WException error)
{
std::cerr << "Error while parsing file '" << path << "': " << error.what() << "\n" << std::endl;
}
}