-
Notifications
You must be signed in to change notification settings - Fork 0
/
IniHelper.h
499 lines (440 loc) · 11.3 KB
/
IniHelper.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#pragma once
# Author: [ATFmxyl]
# Date: [2023.06.23]
# Description: [Qt平台操作ini文件的帮助类]
#include <QtCore>
#include <QList>
#include <QStringList>
#include <QDebug>
#include <iostream>
#include <string>
#include <Windows.h>
//ini读写键值对用字典
class INIDictionary
{
public:
QList<QStringList> dict;
INIDictionary()
{
dict.clear();
}
//将指定的键和值添加到
void Add(const QVariant& key, const QVariant& value)
{
if(!ContainsKey(key))
{
QList<QString> str;
str.append(key.toString());
str.append(value.toString());
dict.append(str);
}
}
void RemoveAt(const QVariant& key)
{
for (int i = 0; i < dict.count(); i++)
{
if (dict.at(i).at(0) == key)
{
dict.removeAt(i);
}
}
}
void RemoveAt(const int key)
{
if (key < dict.count())
dict.removeAt(key);
}
QVariant at(const int pos)
{
if (pos < dict.count())
return dict.at(pos).at(1);
else
return QVariant();
}
QVariant TryGetValue(const QVariant& key)
{
for (auto item : dict)
{
if (item.at(0) == key.toString())
{
return item.at(1);
}
}
return QVariant();
}
bool ContainsKey(const QVariant& key)
{
for (auto item : dict)
{
if (item.at(0) == key.toString())
{
return true;
}
}
return false;
}
bool ContainsValue(const QVariant& value)
{
for (auto item : dict)
{
if (item.at(1) == value.toString())
{
return true;
}
}
return false;
}
bool Clear()
{
dict.clear();
}
int Count()
{
return dict.count();
}
QStringList keys()
{
QStringList info;
for (auto item : dict)
{
info.append(item.at(0));
}
return info;
}
QStringList Values()
{
QStringList info;
for (auto item : dict)
{
info.append(item.at(1));
}
return info;
}
// 重载 [] 运算符
QVariant operator[](const QVariant& key)
{
for (auto& item : dict)
{
if (item[0] == key.toString())
{
return QVariant(item[1]);
}
}
return QVariant();
}
};
class IniHelper
{
private:
inline const static int maxsize = 10240;
public:
//键值对模型
struct KeyValue
{
//索引
int Index;
//键
QString Key;
//值
QString Value;
//构造函数
KeyValue(int index = 0, const QString& key = "", const QString& value = "")
: Index(index), Key(key), Value(value)
{}
//清空方法
void Clear()
{
Index = 0;
Key.clear();
Value.clear();
}
//重载=赋值运算符
KeyValue& operator=(const KeyValue& input)
{
//判断是否是自我赋值
if (this != &input)
{
Index = input.Index;
Key = input.Key;
Value = input.Value;
}
return *this;
}
};
//读取对应的值
static QString Read(const QString& section, const QString& key, const QString& filePath)
{
// 动态分配缓冲区
wchar_t* buffer = new wchar_t[maxsize];
memset(buffer, 0, sizeof(wchar_t) * maxsize);
GetPrivateProfileString((LPCWSTR)section.utf16(), (LPCWSTR)key.utf16(), L"", buffer, maxsize, (LPCWSTR)filePath.utf16());
QString value = QString::fromWCharArray(buffer);
// 释放动态分配的内存
delete[] buffer;
return value;
}
//写入对应的值
static bool Write(const QString& section, const QString& key, const QString& value, const QString& filePath)
{
return WritePrivateProfileString((LPCWSTR)section.utf16(), (LPCWSTR)key.utf16(), (LPCWSTR)value.utf16(), (LPCWSTR)filePath.utf16());
}
//修改节点名称
static bool RenameSection(const QString& oldSectionName, const QString& newSectionName, const QString& filePath)
{
INIDictionary keyValuePairs = ReadKeyValues(oldSectionName, filePath);
for (auto item : keyValuePairs.dict)
{
Write(newSectionName, item.at(0), item.at(1), filePath);
}
return RemoveSection(oldSectionName, filePath);
}
//删除某个节点
static bool RemoveSection(const QString& section, const QString& filePath)
{
return WritePrivateProfileString((LPCWSTR)section.utf16(), NULL, NULL, (LPCWSTR)filePath.utf16());
}
//删除某个节点下的某个Key
static bool RemoveKey(const QString& section, const QString& key, const QString& filePath)
{
return WritePrivateProfileString((LPCWSTR)section.utf16(), (LPCWSTR)key.utf16(), NULL, (LPCWSTR)filePath.utf16());
}
//获取所有Section节点
static QStringList ReadSections(const QString& filePath)
{
// 动态分配缓冲区
wchar_t* buffer = new wchar_t[maxsize];
memset(buffer, 0, sizeof(wchar_t) * maxsize);
QStringList sectionNames;
DWORD bufferSize = GetPrivateProfileSectionNames(buffer, maxsize, (LPCWSTR)filePath.utf16());
if (bufferSize > 0)
{
// 分离并解析节点名称
int startPos = 0;
for (DWORD i = 0; i < bufferSize; i++)
{
if (buffer[i] == L'\0')
{
QString sectionName = QString::fromWCharArray(&buffer[startPos]);
startPos = i + 1;
// 如果sectionName不为空,则添加到列表中
if (!sectionName.isEmpty())
sectionNames.append(sectionName);
}
}
}
// 释放动态分配的内存
delete[] buffer;
return sectionNames;
}
//获取Section节点下所有Key
static QStringList ReadKeys(const QString& sectionName, const QString& fileName)
{
// 动态分配缓冲区
wchar_t* buffer = new wchar_t[maxsize];
memset(buffer, 0, sizeof(wchar_t) * maxsize);
QStringList keys;
DWORD bufferSize = GetPrivateProfileSection(sectionName.toStdWString().c_str(), buffer, maxsize, fileName.toStdWString().c_str());
if (bufferSize > 0)
{
// 分离并解析节点名称
int startPos = 0;
for (DWORD i = 0; i < bufferSize; i++)
{
if (buffer[i] == L'\0')
{
QStringList info = QString::fromWCharArray(&buffer[startPos]).split('=', Qt::SkipEmptyParts);
if (info.size() == 2)
{
if (!info.at(0).isEmpty())
keys.append(info.at(0));
startPos = i + 1;
}
}
}
}
// 释放动态分配的内存
delete[] buffer;
return keys;
}
//获取Section节点下所有Key,Value值
static INIDictionary ReadKeyValues(QString sectionName, QString filePath)
{
INIDictionary keyValuePairs;
QList<QString> keys = ReadKeys(sectionName, filePath);
for (auto key : keys)
{
if (!keyValuePairs.ContainsKey(key))
{
keyValuePairs.Add(key, Read(sectionName, key, filePath));
}
}
return keyValuePairs;
}
//获取Section节点下所有Key,Value值
static QList<KeyValue> ReadKeyValue(const QString& sectionName, const QString& filePath)
{
QList<KeyValue> keyValues;
QStringList keys = ReadKeys(sectionName, filePath);
int index = 0;
for (auto key : keys)
{
keyValues.append(KeyValue(index, key, Read(sectionName, key, filePath)));
index++;
}
return keyValues;
}
//修改Key
static void ModifyKey(const QString& section, const QString& oldKey, const QString& newKey, const QString& value, const QString& filePath)
{
QList<KeyValue> keyValues = ReadKeyValue(section, filePath);
int count = std::count_if(keyValues.begin(), keyValues.end(), [oldKey](const KeyValue& kv) { return kv.Key == oldKey; });
if (count == 0)
{
Write(section, newKey, value, filePath);
return;
}
// 1. 找到第一对 key == oldKey 的键值对,并获取其索引 index
int index = -1;
for (int i = 0; i < keyValues.size(); i++)
{
if (keyValues.at(i).Key == oldKey)
{
index = keyValues.at(i).Index;
break;
}
}
// 2. 删除 index 之后的所有 key-value 对
for (int i = keyValues.size() - 1; i >= 0; i--)
{
if (keyValues.at(i).Index >= index)
{
RemoveKey(section, keyValues.at(i).Key, filePath);
}
}
// 3. 根据 index,向配置文件中写入新的 key-value 对,并将后续的 key-value 继续写入到配置文件中
for (int i = 0; i < keyValues.size(); i++)
{
if (keyValues.at(i).Index == index)
{
Write(section, newKey, value, filePath);
}
else if (keyValues.at(i).Index > index)
{
Write(section, keyValues.at(i).Key, keyValues.at(i).Value, filePath);
}
}
}
};
//重载操作ini读写
//可以支持 QXINI int(path); ini[Section][Key] = Value;的方式写入数据 以及 Qt支持的基础数据类型 Value = ini[Section][Key]; 读取数据
class QXINI
{
private:
QString m_filePath;
public:
QXINI(const QString& filePath) : m_filePath(filePath) {}
//// Close the INI file.
~QXINI() {}
//读取所有节点
QStringList ReadSections()
{
return ::IniHelper::ReadSections(m_filePath);
}
//读取所有键
QStringList ReadKeys(const QString& sectionName)
{
return ::IniHelper::ReadKeys(sectionName,m_filePath);
}
class Section
{
public:
Section(const QString& sectionName, const QString& filePath) : m_sectionName(sectionName), m_filePath(filePath) {}
class Key
{
public:
Key(const QString& keyName, const Section& section): m_keyName(keyName), m_section(section){}
//支持std标准字符串
operator std::string() const
{
return IniHelper::Read(m_section.m_sectionName, m_keyName, m_section.m_filePath).toStdString();
}
operator char* () const
{
QString str = IniHelper::Read(m_section.m_sectionName, m_keyName, m_section.m_filePath);
if (!str.isEmpty())
{
std::string result = str.toStdString();
char* buffer = strdup(result.c_str());
return buffer;
}
// 返回空指针
return nullptr;
}
template<typename T>
operator T() const
{
if constexpr (std::is_same_v<T, int>)
return std::stoi(IniHelper::Read(m_section.m_sectionName, m_keyName, m_section.m_filePath).toStdString());
else
return QVariant(IniHelper::Read(m_section.m_sectionName, m_keyName, m_section.m_filePath)).value<T>();
}
template<typename T>
T operator()(const T& defaultValue) const
{
QVariant value = IniHelper::Read(m_section.m_sectionName, m_keyName, m_section.m_filePath);
if (value.toString().isEmpty())
{
//写入空键
IniHelper::Write(m_section.m_sectionName, m_keyName, QVariant("").toString(), m_section.m_filePath);
return defaultValue;
}
if constexpr (std::is_same_v<T, int>)
return std::stoi(value.toString().toStdString());
else
return value.value<T>();
}
Key& operator=(std::string value)
{
IniHelper::Write(m_section.m_sectionName, m_keyName, QString::fromStdString(value), m_section.m_filePath);
return *this;
}
Key& operator=(ulong value)
{
IniHelper::Write(m_section.m_sectionName, m_keyName, QString::number(value), m_section.m_filePath);
return *this;
}
Key& operator=(const char* value)
{
IniHelper::Write(m_section.m_sectionName, m_keyName, QString::fromStdWString(ToWString(value)), m_section.m_filePath);
return *this;
}
template<typename T>
Key& operator=(const T& value)
{
IniHelper::Write(m_section.m_sectionName, m_keyName, QVariant(value).toString(), m_section.m_filePath);
return *this;
}
private:
QString m_keyName;
const Section& m_section;
std::wstring ToWString(const char* str) const
{
int length = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
std::wstring result(length, L'\0');
MultiByteToWideChar(CP_UTF8, 0, str, -1, &result[0], length);
return result;
}
};
Key operator[](const QString& keyName) const
{
return Key(keyName, *this);
}
private:
QString m_sectionName;
QString m_filePath;
};
Section operator[](const QString& sectionName) const
{
return Section(sectionName, m_filePath);
}
};