-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.cpp
More file actions
147 lines (121 loc) · 3.32 KB
/
profile.cpp
File metadata and controls
147 lines (121 loc) · 3.32 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
#include "profile.h"
ProFile::ProFile(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> ProFile::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))
{
delete[] buffer;
buffer = nullptr;
size *= 2;
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;
buffer = nullptr;
return sections;
}
int32_t ProFile::ReadProfileIntegerA(std::string section, std::string key)
{
return GetPrivateProfileIntA(section.c_str(), key.c_str(), 0, this->m_file.c_str());
}
uint32_t ProFile::ReadProfileStringsA(std::string section, std::string key, char* buffer, uint32_t size)
{
return GetPrivateProfileStringA(section.c_str(), key.c_str(), NULL, buffer, size, this->m_file.c_str());
}
std::map<std::string, std::string> ProFile::ReadProfileSectionsA(std::string section)
{
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))
{
delete[] buffer;
buffer = nullptr;
size *= 2;
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;
buffer = nullptr;
return result;
}
int32_t ProFile::WriteProfileStringsA(std::string section, std::string key, std::string buffer)
{
return WritePrivateProfileStringA(section.c_str(), key.c_str(), buffer.c_str(), this->m_file.c_str());
}
int32_t ProFile::WriteProfileIntegerA(std::string section, std::string key, int32_t contect)
{
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 ProFile::WriteProfileSectonsA(std::string section, std::string buffer)
{
return WritePrivateProfileSectionA(section.c_str(), buffer.c_str(), this->m_file.c_str());
}
int32_t ProFile::DeleteProfileString(std::string section, std::string key)
{
return WritePrivateProfileStringA(section.c_str(), key.c_str(), NULL, this->m_file.c_str());
}
int32_t ProFile::DeleteProfileSection(std::string section)
{
return WritePrivateProfileStringA(section.c_str(), NULL, NULL, this->m_file.c_str());
}