This repository has been archived by the owner on Jun 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.cpp
123 lines (100 loc) · 2.28 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
#include "config.h"
#include "util.h"
#include <Wt/Json/Array>
#include <Wt/Json/Object>
#include <Wt/Json/Parser>
#include <Wt/Json/Value>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
Config* Config::getConfig()
{
static Config config;
return &config;
}
std::string Config::getConfigFileName()
{
return "./wt_comments_config.json";
}
bool Config::readConfigFile()
{
/* Read the file */
std::string file;
std::ifstream db(getConfigFileName().c_str());
if (db.is_open())
{
std::string line;
while (db.good()) {
getline(db, line);
file += line;
}
db.close();
}
else {
std::cerr << "The file '" + getConfigFileName() + "' cannot be opened" << "\n";
return false;
}
/* Parse the file we've just read */
Wt::Json::Object result;
try {
Wt::Json::parse(file, result);
} catch (Wt::Json::ParseError e) {
std::string error = e.what();
std::cerr << "Error while parsing the configuration file: " + error << "\n";
return false;
}
_websiteName = readJSONValue<Wt::WString>(result, "websitename", "");
_websiteURL = readJSONValue<Wt::WString>(result, "websiteurl", "");
_isEnabled = readJSONValue<bool>(result, "enable", false);
_verbose = readJSONValue<bool>(result, "verbose", false);
_login = readJSONValue<Wt::WString>(result, "login", "");
_pwd = readJSONValue<Wt::WString>(result, "pwd", "");
_smtp_server = readJSONValue<Wt::WString>(result, "smtp_server", "");
_from = readJSONValue<Wt::WString>(result, "from", "");
Wt::Json::Array jsonRecipients = readJSONValue<Wt::Json::Array>(result, "to", Wt::Json::Array());
for (size_t i = 0; i < jsonRecipients.size(); ++i)
_recipients.push_back(readJSONValue<Wt::WString>(jsonRecipients[i], "email", ""));
return true;
}
Wt::WString Config::websiteName()
{
return _websiteName;
}
Wt::WString Config::websiteURL()
{
return _websiteURL;
}
bool Config::emailsEnabled()
{
return _emailsEnabled;
}
Wt::WString Config::login()
{
return _login;
}
Wt::WString Config::pwd()
{
return _pwd;
}
Wt::WString Config::smtp_server()
{
return _smtp_server;
}
Wt::WString Config::from()
{
return _from;
}
std::vector<Wt::WString> Config::recipients()
{
return _recipients;
}
bool Config::isEnabled()
{
return _isEnabled;
}
bool Config::verbose()
{
return _verbose;
}