-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCIni.h
216 lines (176 loc) · 9.76 KB
/
CIni.h
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
#pragma once
#ifndef CINI_BUFFER_SIZE
#define CINI_BUFFER_SIZE 0xFF
#endif
class CIni {
public:
explicit CIni(const std::string& file) : iniFile(file.data()) {}
explicit CIni(const char* file) : iniFile(file) {}
/* --------------- SET --------------- */
bool setString(const char* string, const char* app, const char* key) const {
try {
WritePrivateProfileStringA(app, key, string, iniFile);
return true;
}
catch(...) {
return false;
}
}
bool setInt32(const int32_t value, const std::string& app, const std::string& key) const {
return setString(std::to_string(value).data(), app.data(), key.data());
}
bool setInt32(const int32_t value, const char* app, const char* key) const {
return setString(std::to_string(value).data(), app, key);
}
bool setUint32(const uint32_t value, const std::string& app, const std::string& key) const {
return setString(std::to_string(value).data(), app.data(), key.data());
}
bool setUint32(const uint32_t value, const char* app, const char* key) const {
return setString(std::to_string(value).data(), app, key);
}
bool setInt64(const int64_t value, const std::string& app, const std::string& key) const {
return setString(std::to_string(value).data(), app.data(), key.data());
}
bool setInt64(const int64_t value, const char* app, const char* key) const {
return setString(std::to_string(value).data(), app, key);
}
bool setUint64(const uint64_t value, const std::string& app, const std::string& key) const {
return setString(std::to_string(value).data(), app.data(), key.data());
}
bool setUint64(const uint64_t value, const char* app, const char* key) const {
return setString(std::to_string(value).data(), app, key);
}
bool setFloat(const float value, const std::string& app, const std::string& key) const {
return setString(std::to_string(value).data(), app.data(), key.data());
}
bool setFloat(const float value, const char* app, const char* key) const {
return setString(std::to_string(value).data(), app, key);
}
bool setBool(const bool value, const std::string& app, const std::string& key) const {
return setString(std::to_string(value).data(), app.data(), key.data());
}
bool setBool(const bool value, const char* app, const char* key) const {
return setString(std::to_string(value).data(), app, key);
}
/* --------------- GET --------------- */
// On failure, if write on fail is true, failvalue will be written at the specified app and key.
// After that failValue will be returned, even if it wasn't written.
std::string getString(const char* app, const char* key, const char* failValue, const bool writeOnFail, const uint16_t bufferSize = 0xFF) const {
try {
char buf[CINI_BUFFER_SIZE];
GetPrivateProfileStringA(app, key, "NULL", buf, sizeof(buf), iniFile);
return buf;
}
catch(...) {
if(writeOnFail) {
setString(failValue, app, key);
}
return failValue;
}
}
// On failure, if write on fail is true, failvalue will be written at the specified app and key.
// After that failValue will be returned, even if it wasn't written.
int getInt32(const std::string& app, const std::string& key, int failValue, const bool writeOnFail) const {
return std::stoi(getString(app.data(), key.data(), std::to_string(failValue).data(), writeOnFail));
}
// On failure, if write on fail is true, failvalue will be written at the specified app and key.
// After that failValue will be returned, even if it wasn't written.
int getInt32(const char* app, const char* key, int failValue, const bool writeOnFail) const {
return std::stoi(getString(app, key, std::to_string(failValue).data(), writeOnFail));
}
// On failure, if write on fail is true, failvalue will be written at the specified app and key.
// After that failValue will be returned, even if it wasn't written.
uint32_t getUint32(const std::string& app, const std::string& key, const int failValue, const bool writeOnFail) const {
return std::strtoul(getString(app.data(), key.data(), std::to_string(failValue).data(), writeOnFail).data(), nullptr, 0);
}
// On failure, if write on fail is true, failvalue will be written at the specified app and key.
// After that failValue will be returned, even if it wasn't written.
uint32_t getUint32(const char* app, const char* key, const int failValue, const bool writeOnFail) const {
return std::strtoul(getString(app, key, std::to_string(failValue).data(), writeOnFail).data(), nullptr, 0);
}
// On failure, if write on fail is true, failvalue will be written at the specified app and key.
// After that failValue will be returned, even if it wasn't written.
int64_t getInt64(const std::string& app, const std::string& key, const int failValue, const bool writeOnFail) const {
return std::stoll(getString(app.data(), key.data(), std::to_string(failValue).data(), writeOnFail));
}
// On failure, if write on fail is true, failvalue will be written at the specified app and key.
// After that failValue will be returned, even if it wasn't written.
int64_t getInt64(const char* app, const char* key, const int failValue, const bool writeOnFail) const {
return std::stoll(getString(app, key, std::to_string(failValue).data(), writeOnFail));
}
// On failure, if write on fail is true, failvalue will be written at the specified app and key.
// After that failValue will be returned, even if it wasn't written.
uint64_t getUint64(const std::string& app, const std::string& key, const int failValue, const bool writeOnFail) const {
return std::stoull(getString(app.data(), key.data(), std::to_string(failValue).data(), writeOnFail));
}
// On failure, if write on fail is true, failvalue will be written at the specified app and key.
// After that failValue will be returned, even if it wasn't written.
uint64_t getUint64(const char* app, const char* key, const int failValue, const bool writeOnFail) const {
return std::stoull(getString(app, key, std::to_string(failValue).data(), writeOnFail));
}
// On failure, if write on fail is true, failvalue will be written at the specified app and key.
// After that failValue will be returned, even if it wasn't written.
float getFloat(const std::string& app, const std::string& key, const float failValue, const bool writeOnFail) const {
return std::stof(getString(app.data(), key.data(), std::to_string(failValue).data(), writeOnFail));
}
// On failure, if write on fail is true, failvalue will be written at the specified app and key.
// After that failValue will be returned, even if it wasn't written.
float getFloat(const char* app, const char* key, const float failValue, const bool writeOnFail) const {
return std::stof(getString(app, key, std::to_string(failValue).data(), writeOnFail));
}
// On failure, if write on fail is true, failvalue will be written at the specified app and key.
// After that failValue will be returned, even if it wasn't written.
bool getBool(const std::string& app, const std::string& key, const bool failValue, const bool writeOnFail) const {
return (std::stoi(getString(app.data(), key.data(), std::to_string(failValue).data(), writeOnFail)) ? true : false);
}
// On failure, if write on fail is true, failvalue will be written at the specified app and key.
// After that failValue will be returned, even if it wasn't written.
bool getBool(const char* app, const char* key, const bool failValue, const bool writeOnFail) const {
return (std::stoi(getString(app, key, std::to_string(failValue).data(), writeOnFail)) ? true : false);
}
/* --------------- UNSAFE GET ---------------*/
// No error handling, only use if you plan to handle errors on your own
std::string getStringUnsafe(const std::string& app, const std::string& key) const {
char buf[CINI_BUFFER_SIZE];
GetPrivateProfileStringA(app.c_str(), key.c_str(), "NULL", buf, sizeof(buf), iniFile);
return buf;
}
// No error handling, only use if you plan to handle errors on your own
int getInt32Unsafe(const std::string& app, const std::string& key) const {
char buf[CINI_BUFFER_SIZE];
GetPrivateProfileStringA(app.c_str(), key.c_str(), "NULL", buf, sizeof(buf), iniFile);
return std::stoi(buf);
}
// No error handling, only use if you plan to handle errors on your own
uint32_t getUint32Unsafe(const std::string& app, const std::string& key) const {
char buf[CINI_BUFFER_SIZE];
GetPrivateProfileStringA(app.c_str(), key.c_str(), "NULL", buf, sizeof(buf), iniFile);
return std::strtoul(buf, nullptr, 0);
}
// No error handling, only use if you plan to handle errors on your own
int64_t getInt64Unsafe(const std::string& app, const std::string& key) const {
char buf[CINI_BUFFER_SIZE];
GetPrivateProfileStringA(app.c_str(), key.c_str(), "NULL", buf, sizeof(buf), iniFile);
return std::stoll(buf);
}
// No error handling, only use if you plan to handle errors on your own
uint64_t getUint64Unsafe(const std::string& app, const std::string& key) const {
char buf[CINI_BUFFER_SIZE];
GetPrivateProfileStringA(app.c_str(), key.c_str(), "NULL", buf, sizeof(buf), iniFile);
return std::stoull(buf);
}
// No error handling, only use if you plan to handle errors on your own
float getFloatUnsafe(const std::string& app, const std::string& key) const {
char buf[CINI_BUFFER_SIZE];
GetPrivateProfileStringA(app.c_str(), key.c_str(), "NULL", buf, sizeof(buf), iniFile);
return std::stof(buf);
}
// No error handling, only use if you plan to handle errors on your own
bool getBoolUnsafe(const std::string& app, const std::string& key) const {
char buf[CINI_BUFFER_SIZE];
GetPrivateProfileStringA(app.c_str(), key.c_str(), "NULL", buf, sizeof(buf), iniFile);
return (std::stoi(buf) ? true : false);
}
private:
const char* iniFile; // maybe this :eyes:
};