-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfFile.cpp
229 lines (189 loc) · 7.02 KB
/
ConfFile.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* Copyright (C) 2016-2020 Thomas Hauck - All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
The author would be happy if changes and
improvements were reported back to him.
Author: Thomas Hauck
Email: Thomas@fam-hauck.de
*/
#include <fstream>
#include <codecvt>
#include <algorithm>
#include <functional>
#include "ConfFile.h"
#if defined(_WIN32) || defined(_WIN64)
#define FN_CA(x) x.c_str()
#define FN_STR(x) x
#else
#include <locale>
#include <sys/stat.h>
#include <math.h>
#define _stat stat
#define _wstat stat
#define FN_CA(x) wstring_convert<codecvt_utf8<wchar_t>, wchar_t>().to_bytes(x).c_str()
#define FN_STR(x) wstring_convert<codecvt_utf8<wchar_t>, wchar_t>().to_bytes(x).c_str()
#endif
map<wstring, ConfFile> ConfFile::s_lstConfFiles;
ConfFile& ConfFile::GetInstance(const wstring& strConfigFile)
{
const auto& instance = s_lstConfFiles.find(strConfigFile);
if (instance == end(s_lstConfFiles))
{
s_lstConfFiles.emplace(strConfigFile, ConfFile(strConfigFile));
return s_lstConfFiles.find(strConfigFile)->second;
}
return instance->second;
}
ConfFile::ConfFile(const ConfFile& src) : m_strFileName(src.m_strFileName), m_tLastCheck(src.m_tLastCheck), m_tFileTime(src.m_tFileTime), m_mSections(src.m_mSections)
{
}
vector<wstring> ConfFile::get()
{
CheckFileLoaded();
vector<wstring> vReturn;
for (const auto& item : m_mSections)
{
vReturn.push_back(item.first);
}
return vReturn;
}
vector<wstring> ConfFile::get(const wstring& strSektion)
{
CheckFileLoaded();
vector<wstring> vReturn;
const auto& section = m_mSections.find(strSektion);
if (section != end(m_mSections))
{
for (const auto& item : section->second)
{
if (find(begin(vReturn), end(vReturn), item.first) == end(vReturn))
vReturn.push_back(item.first);
}
}
return vReturn;
}
vector<wstring> ConfFile::get(const wstring& strSektion, const wstring& strValue)
{
CheckFileLoaded();
vector<wstring> vReturn;
const auto& section = m_mSections.find(strSektion);
if (section != end(m_mSections))
{
auto item = section->second.equal_range(strValue);
for (; item.first != item.second; ++item.first)
{
vReturn.push_back(item.first->second);
}
}
return vReturn;
}
const wstring& ConfFile::getUnique(const wstring& strSektion, const wstring& strValue)
{
CheckFileLoaded();
const auto section = m_mSections.find(strSektion);
if (section != m_mSections.end())
{
const auto item = section->second.equal_range(strValue);
if (item.first != item.second)
{
if (distance(item.first, item.second) > 1)
MyTrace("Warnung: Configfile has hidden entry in section \'", strSektion, "\', key \'", strValue, "\' exist more than once");
unordered_multimap<wstring, wstring>::const_iterator it = item.first, itNext = it;
while (++itNext != item.second && it != item.second) ++it;
return it->second; // Letztes Element //item.first->second;
}
}
static wstring strEmpty;
return strEmpty;
}
void ConfFile::CheckFileLoaded()
{
lock_guard<mutex> lock(m_mtxLoad);
if (m_mSections.empty() == true || AreFilesModified() == true)
{
LoadFile(m_strFileName);
}
}
void ConfFile::LoadFile(const wstring& strFilename)
{
m_mSections.clear();
function<void(const wstring&)> fnLoadFileRecursive = [&](const wstring& strFilename)
{
wifstream fin;
fin.open(FN_STR(strFilename), ios::in | ios::binary);
if (fin.is_open() == true)
{
fin.imbue(std::locale(fin.getloc(), new codecvt_utf8<wchar_t>));
unordered_multimap<wstring, wstring>* LastSection = nullptr;
auto TrimString = [](wstring strVal) -> wstring
{
size_t nPos = strVal.find_last_not_of(L" \t\r\n");
strVal.erase(nPos + 1); // Trim Whitespace character on the right
nPos = strVal.find_first_not_of(L" \t");
strVal.erase(0, nPos);
return strVal;
};
while (fin.eof() == false)
{
wstring strLine;
getline(fin, strLine);
size_t nPos = strLine.find_first_of(L"#;\r\n");
if (nPos != string::npos) strLine.erase(nPos); // erase commends from line
strLine = TrimString(strLine);
if (strLine.empty() == false)
{
if (strLine[0] == L'[' && strLine[strLine.size() - 1] == L']')
{
const auto strTmp = TrimString(strLine.substr(1, strLine.size() - 2));
if (strTmp.empty() == false)
{
const auto& paRet = m_mSections.insert(make_pair(strTmp, unordered_multimap<wstring, wstring>()));
if (paRet.second == true)
{
LastSection = &paRet.first->second;
continue;
}
}
LastSection = nullptr;
}
else if (nPos = strLine.find(L'='), nPos != string::npos && LastSection != nullptr)
{
const auto strTmp = TrimString(strLine.substr(0, nPos));
if (strTmp.empty() == false)
LastSection->insert(make_pair(strTmp, TrimString(strLine.substr(nPos + 1))));
}
else if (strLine[0] == L'@')
{
fnLoadFileRecursive(TrimString(strLine.substr(1)));
LastSection = nullptr;
}
}
}
fin.close();
// We get the file time of the config file we just read, and safe it
struct _stat stFileInfo;
if (::_wstat(FN_CA(strFilename), &stFileInfo) == 0)
{
m_tFileTime = stFileInfo.st_mtime;
m_tLastCheck = chrono::steady_clock::now();
}
}
else
MyTrace("Error: Configfile \'", strFilename, "\' could not be opened");
};
fnLoadFileRecursive(strFilename);
}
bool ConfFile::AreFilesModified() const noexcept
{
if (m_tFileTime == 0) // We have no files, we return true as if the file is modified
return true;
if (chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - m_tLastCheck).count() < 1000)
return false;
struct _stat stFileInfo;
if (::_wstat(FN_CA(m_strFileName), &stFileInfo) != 0 || ::fabs(::difftime(stFileInfo.st_mtime, m_tFileTime)) > 0.00001)
{ // error on getting the file time or the file was modified
return true;
}
m_tLastCheck = chrono::steady_clock::now();
return false;
}