-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.cpp
More file actions
246 lines (204 loc) · 6.62 KB
/
profile.cpp
File metadata and controls
246 lines (204 loc) · 6.62 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "profile.h"
ProFileA::ProFileA(std::string file)
{
char buffer[MAX_PATH]{ NULL };
GetModuleFileNameA(NULL, buffer, sizeof(buffer));
this->m_file.append(buffer);
std::size_t pos = this->m_file.find_last_of('\\');
if (pos != std::string::npos) this->m_file.erase(pos);
this->m_file.append("\\");
this->m_file.append(file);
}
std::list<std::string> ProFileA::ReadPrivateProfileSectionNames()
{
int32_t size = 4096;
char *buffer = new char[size];
ZeroMemory(buffer, size);
int32_t resul = GetPrivateProfileSectionNamesA(buffer, size, this->m_file.c_str());
while(true)
{
if(resul == (size - 2))
{
size *= 2;
delete[] buffer;
buffer = new char[size];
ZeroMemory(buffer, size);
}
else
{
break;
}
}
std::list<std::string> sections;
char* segment = buffer;
while (*segment)
{
sections.push_back(std::string(segment));
segment += strlen(segment) + 1;
}
delete[] buffer;
return sections;
}
int32_t ProFileA::ReadProfileIntegerA(std::string section, std::string key)
{
std::lock_guard<std::recursive_mutex> lock(this->m_mtx); // ����
return GetPrivateProfileIntA(section.c_str(), key.c_str(), 0, this->m_file.c_str());
}
uint32_t ProFileA::ReadProfileStringsA(std::string section, std::string key, char* buffer, uint32_t size)
{
std::lock_guard<std::recursive_mutex> lock(this->m_mtx);
return GetPrivateProfileStringA(section.c_str(), key.c_str(), NULL, buffer, size, this->m_file.c_str());
}
std::map<std::string, std::string> ProFileA::ReadProfileSectionsA(std::string section)
{
std::lock_guard<std::recursive_mutex> lock(this->m_mtx);
std::map<std::string, std::string> result;
uint32_t size = 0x256;
char* buffer = new char[size];
ZeroMemory(buffer, size);
int res = 0;
while (true)
{
res = GetPrivateProfileSectionA(section.c_str(), buffer, size, this->m_file.c_str());
if (res == (size - 2))
{
size *= 2;
delete[] buffer;
buffer = new char[size];
ZeroMemory(buffer, size);
}
else
{
break;
}
}
std::string_view str(buffer, res);
size_t pos = 0;
while (pos < str.size())
{
size_t end_pos = str.find('\0', pos);
if (end_pos == std::wstring_view::npos)
{
break;
}
std::string_view pair(&str[pos], end_pos - pos);
size_t eq_pos = pair.find("=");
if (eq_pos != std::string_view::npos)
{
std::string key(pair.data(), eq_pos);
std::string value(pair.data() + eq_pos + 1, pair.size() - eq_pos - 1);
result.emplace(key, value);
}
pos = end_pos + 1;
}
delete[] buffer;
return result;
}
int32_t ProFileA::WriteProfileStringsA(std::string section, std::string key, std::string buffer)
{
std::lock_guard<std::recursive_mutex> lock(this->m_mtx);
return WritePrivateProfileStringA(section.c_str(), key.c_str(), buffer.c_str(), this->m_file.c_str());
}
int32_t ProFileA::WriteProfileIntegerA(std::string section, std::string key, int32_t contect)
{
std::lock_guard<std::recursive_mutex> lock(this->m_mtx);
char buffer[0x32]{ NULL };
sprintf_s(buffer, "%d", contect);
return WritePrivateProfileStringA(section.c_str(), key.c_str(), buffer, this->m_file.c_str());
}
int32_t ProFileA::WriteProfileSectonsA(std::string section, std::string buffer)
{
std::lock_guard<std::recursive_mutex> lock(this->m_mtx);
return WritePrivateProfileSectionA(section.c_str(), buffer.c_str(), this->m_file.c_str());
}
int32_t ProFileA::DeleteProfileString(std::string section, std::string key)
{
std::lock_guard<std::recursive_mutex> lock(this->m_mtx);
return WritePrivateProfileStringA(section.c_str(), key.c_str(),NULL, this->m_file.c_str());
}
int32_t ProFileA::DeleteProfileSection(std::string section)
{
return WritePrivateProfileStringA(section.c_str(),NULL,NULL,this->m_file.c_str());
}
ProFileW::ProFileW(std::wstring file)
{
wchar_t buffer[MAX_PATH]{ NULL };
GetModuleFileNameW(NULL, buffer, sizeof(buffer));
this->m_file = buffer;
std::size_t pos = this->m_file.find_last_of('\\');
if (pos != std::wstring::npos) this->m_file.erase(pos);
this->m_file.append(L"\\");
this->m_file.append(file);
}
uint32_t ProFileW::ReadProfileStringsW(std::wstring section, std::wstring key, wchar_t* buffer, uint32_t size)
{
std::lock_guard<std::recursive_mutex> lock(this->m_mtx); // ����
return GetPrivateProfileStringW(section.c_str(), key.c_str(), NULL, buffer, size, this->m_file.c_str());
}
int32_t ProFileW::ReadProfileIntegerW(std::wstring section, std::wstring key)
{
std::lock_guard<std::recursive_mutex> lock(this->m_mtx); // ����
return GetPrivateProfileIntW(section.c_str(), key.c_str(), 0, this->m_file.c_str());
}
std::map<std::wstring, std::wstring> ProFileW::ReadProfileSectionsW(std::wstring section)
{
std::lock_guard<std::recursive_mutex> lock(this->m_mtx); // ����
std::map<std::wstring, std::wstring> result;
uint32_t size = 0x256;
wchar_t* buffer = new wchar_t[size];
ZeroMemory(buffer, size);
int res = 0;
while (true)
{
res = GetPrivateProfileSectionW(section.c_str(), buffer, size, this->m_file.c_str());
if (res == 0 || (res == (size - 2)))
{
size *= 2;
delete[] buffer;
buffer = new wchar_t[size];
ZeroMemory(buffer, size);
}
else
{
break;
}
}
std::wstring_view str(buffer, res);
size_t pos = 0;
while (pos < str.size())
{
size_t end_pos = str.find(L'\0', pos);
if (end_pos == std::wstring_view::npos)
{
break;
}
std::wstring_view pair(&str[pos], end_pos - pos);
size_t eq_pos = pair.find(L"=");
if (eq_pos != std::wstring_view::npos)
{
std::wstring key(pair.data(), eq_pos);
std::wstring value(pair.data() + eq_pos + 1, pair.size() - eq_pos - 1);
result.emplace(key, value);
}
pos = end_pos + 1;
}
delete[] buffer;
return result;
}
int32_t ProFileW::WriteProfileStringsW(std::wstring section, std::wstring key, std::wstring buffer)
{
std::lock_guard<std::recursive_mutex> lock(this->m_mtx); // ����
return WritePrivateProfileStringW(section.c_str(), key.c_str(), buffer.c_str(), this->m_file.c_str());
}
int32_t ProFileW::WriteProfileIntegerW(std::wstring section, std::wstring key, int64_t contect)
{
std::lock_guard<std::recursive_mutex> lock(this->m_mtx); // ����
wchar_t buffer[0x32]{ NULL };
swprintf_s(buffer, L"%lld", contect);
return WritePrivateProfileStringW(section.c_str(), key.c_str(), buffer, this->m_file.c_str());
}
int32_t ProFileW::WriteProfileSectonsW(std::wstring section, std::wstring buffer)
{
std::lock_guard<std::recursive_mutex> lock(this->m_mtx); // ����
return WritePrivateProfileSectionW(section.c_str(), buffer.c_str(), this->m_file.c_str());
}