-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathini.cpp
178 lines (157 loc) · 5.54 KB
/
ini.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
170
171
172
173
174
175
176
177
178
#include "ini.h"
#include "util.h"
#include "error_codes.h"
#include <cctype>
#include <cstdlib>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iostream>
#include <regex>
#include <cerrno>
#include <cstring>
using namespace std;
INIReader::INIReader(const string& filename) {
m_error = parse(filename);
}
int INIReader::ParseError() const {
return m_error;
}
string INIReader::Get(const string& section, const string& name, const string& default_value) const {
string key = makeKey(section, name);
return m_values.count(key) ? m_values.at(key) : default_value;
}
int INIReader::GetInteger(const string& section, const string& name, int default_value) const {
string valstr = Get(section, name, "");
const char* value = valstr.c_str();
char* end;
long n = strtol(value, &end, 0);
return end > value ? n : default_value;
}
bool INIReader::GetBoolean(const string& section, const string& name, bool default_value) const {
string valstr = Get(section, name, "");
transform(valstr.begin(), valstr.end(), valstr.begin(), ::tolower);
if (valstr == "true" || valstr == "yes" || valstr == "on" || valstr == "1")
return true;
else if (valstr == "false" || valstr == "no" || valstr == "off" || valstr == "0")
return false;
else
return default_value;
}
vector<string> INIReader::GetSections() const {
return vector<string>(m_sections.begin(), m_sections.end());
}
vector<string> INIReader::GetKeys(const string& section) const {
vector<string> keys;
string prefix = section + ".";
for (const auto& kv : m_values) {
if (kv.first.find(prefix) == 0) {
keys.push_back(kv.first.substr(prefix.length()));
}
}
return keys;
}
string INIReader::makeKey(const string& section, const string& name) {
string key = section + "." + name;
transform(key.begin(), key.end(), key.begin(), ::tolower);
return key;
}
int INIReader::parse(const string& filename) {
ifstream infile(filename.c_str());
if (!infile) {
std::cerr << "Error opening file: " << strerror(errno) << std::endl;
m_error = ERROR_CANNOT_OPEN_FILE;
throw runtime_error("Cannot open file: " + filename);
}
string line, section;
int lineno = 0;
while (getline(infile, line)) {
lineno++;
if (line.empty() || line[0] == ';' || line[0] == '#') continue;
if (line.front() == '[' && line.back() == ']') {
section = line.substr(1, line.length() - 2);
m_sections.insert(section);
} else {
istringstream isline(line);
string name;
if (getline(isline, name, '=') && name.length()) {
string value;
if (getline(isline, value)) {
m_values[makeKey(section, name)] = value;
if (section == "SCAN" && name == "path") {
if (!IsDirectory(value)) {
throw std::runtime_error("Invalid scan path: " + value);
}
}
if (section == "SCAN" && name == "scantype") {
int scanType = std::stoi(value);
if (scanType != 1 && scanType != 2) {
throw std::runtime_error("Invalid scan type: " + value);
}
}
if (section == "NOTIFICATION" && name == "emailaddress") {
const std::regex pattern(R"((\w+)(\.\w+)*@(\w+\.)+[A-Za-z]+)");
if (!std::regex_match(value, pattern)) {
throw std::runtime_error("Invalid email address: " + value);
}
}
} else {
m_error = ERROR_INVALID_OPTION;
throw runtime_error(GetErrorMessage(m_error) + " at line " + to_string(lineno));
}
}
}
}
m_error = SUCCESS_CODE;
return m_error;
}
INIWriter::INIWriter(const string& filepath) : m_filename(filepath) {}
bool INIWriter::Write(const map<string, map<string, string>>& data) const {
ofstream file(m_filename);
if (!file.is_open()) {
return false;
}
for (const auto& section : data) {
file << "[" << section.first << "]\n";
for (const auto& key : section.second) {
file << key.first << "=" << key.second << "\n";
}
file << "\n";
}
file.close();
return true;
}
bool INIWriter::DeleteSection(const string& section) {
INIReader reader(m_filename);
if (reader.ParseError() != 0) {
return false;
}
map<string, map<string, string>> data;
for (const auto& sec : reader.GetSections()) {
if (sec != section) {
map<string, string> sectionData;
for (const auto& key : reader.GetKeys(sec)) {
sectionData[key] = reader.Get(sec, key, "");
}
data[sec] = sectionData;
}
}
return Write(data);
}
bool INIWriter::DeleteKey(const string& section, const string& key) {
INIReader reader(m_filename);
if (reader.ParseError() != 0) {
return false;
}
map<string, map<string, string>> data;
for (const auto& sec : reader.GetSections()) {
map<string, string> sectionData;
for (const auto& k : reader.GetKeys(sec)) {
if (!(sec == section && k == key)) {
sectionData[k] = reader.Get(sec, k, "");
}
}
data[sec] = sectionData;
}
return Write(data);
}