-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathproperties.cpp
129 lines (100 loc) · 3.16 KB
/
properties.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
#include "properties.h"
#include <fstream>
#include "stringUtil.h"
#include "log.h"
namespace Util {
std::regex Properties::regex = std::regex(" *.+ *: *.+ *");
std::string Properties::get(const std::string& property) const {
try {
std::string value = properties.at(property);
return value;
} catch (const std::out_of_range&) {
return "";
}
}
std::map<std::string, std::string> Properties::get() {
return properties;
}
void Properties::set(const std::string& property, const std::string& value) {
if (properties.find(property) == properties.end())
properties.insert(std::make_pair(property, value));
else
properties[property] = value;
}
void Properties::remove(const std::string& property) {
for (auto iterator = properties.begin(); iterator != properties.end(); iterator++) {
if (iterator->first == property) {
properties.erase(iterator);
return;
}
}
}
namespace PropertiesParser {
Properties read(const std::string& file) {
Log::subject s(file);
std::ifstream inputstream;
inputstream.open(file.c_str());
if (!inputstream.is_open()) {
Log::error("Properties file can't be found");
return Properties();
}
Properties properties(file);
std::string line;
while (getline(inputstream, line)) {
line = trim(line);
if (line.empty() || line.at(0) == '#') {
continue;
} else if (!std::regex_match(line, Properties::regex)) {
Log::warn("Incorrect syntax: %s", line.c_str());
} else {
size_t pos = line.find(':');
std::string property = rtrim(line.substr(0, pos));
std::string value = until(ltrim(line.substr(pos + 1, line.length())), ' ');
properties.set(property, value);
}
}
inputstream.close();
return properties;
}
void write(const std::string& filename, Properties& properties) {
Log::subject s(filename);
Log::info("Writing %d properties", properties.get().size());
std::ifstream ifile;
ifile.open(filename.c_str(), std::ios::in);
if (!ifile.is_open())
Log::error("Properties file can't be found, creating new one");
// Read and save
std::string line;
std::vector<std::string> lines;
while (getline(ifile, line)) {
std::string trimmedLine = trim(line);
if (trimmedLine.empty() || trimmedLine.at(0) == '#' || !std::regex_match(trimmedLine, Properties::regex)) {
lines.push_back(line);
continue;
} else {
size_t pos = line.find(':');
std::string property = line.substr(0, pos);
std::string value = line.substr(pos + 1, line.length());
std::string trimmedProperty = rtrim(property);
std::string trimmedValue = until(ltrim(value), ' ');
std::string newTrimmedValue = properties.get(trimmedProperty);
if (trimmedValue == newTrimmedValue) {
lines.push_back(line);
continue;
}
auto position = value.find(trimmedValue);
std::string newValue = value.replace(position, position + trimmedValue.length(), newTrimmedValue);
std::string newLine = property + ":" + newValue;
lines.push_back(newLine);
}
}
ifile.close();
// Write
std::ofstream ofile;
ofile.open(filename.c_str(), std::ios::out | std::ios::trunc);
for (std::string& line : lines)
ofile << line << std::endl;
ofile.close();
}
}
};