-
Notifications
You must be signed in to change notification settings - Fork 1
/
file-ini.h
84 lines (66 loc) · 1.87 KB
/
file-ini.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
/*
** iniÎļþ²Ù×÷Àà
** author
** taoabc@gmail.com
*/
#ifndef ULT_FILE_FILEINI_H_
#define ULT_FILE_FILEINI_H_
#include <string>
#include <Windows.h>
namespace ult {
class Ini {
public:
Ini(void) {
config_filename_.clear();
}
~Ini(void) {
}
Ini(const std::wstring& filename) : config_filename_(filename) {
}
void AssignFile(const std::wstring& filename) {
config_filename_.assign(filename);
}
bool WriteString(const std::wstring& section,
const std::wstring& entry,
const std::wstring& str) {
if (config_filename_.empty()) {
return false;
}
return 0 != WritePrivateProfileString(section.c_str(),
entry.c_str(), str.c_str(), config_filename_.c_str());
}
std::wstring GetString(const std::wstring& section,
const std::wstring& entry,
const std::wstring& string_default = L"") {
if (config_filename_.empty()) {
return L"";
}
wchar_t buf[kMaxValueLen];
const wchar_t* pdef = string_default.empty() ? NULL : string_default.c_str();
GetPrivateProfileString(section.c_str(), entry.c_str(), pdef, buf,
kMaxValueLen, config_filename_.c_str());
std::wstring result(buf);
return result;
}
bool WriteInt(const std::wstring& section,
const std::wstring& entry,
int number) {
wchar_t buf[kMaxIntLength];
swprintf(buf, kMaxIntLength, L"%d", number);
return WriteString(section, entry, buf);
}
int GetInt(const std::wstring& section,
const std::wstring& entry,
int num_default = -1) {
return GetPrivateProfileInt(section.c_str(), entry.c_str(), num_default,
config_filename_.c_str());
}
private:
enum {
kMaxValueLen = 1024,
kMaxIntLength = 256
};
std::wstring config_filename_;
};
} //namespace ult
#endif // ULT_INI_H_