-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg.cpp
179 lines (145 loc) · 3.91 KB
/
cfg.cpp
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
#include "stdafx.h"
#include "cfg.h"
#define SECTION_UI L"UI"
#define SECTION_APP L"App"
#define KEY_UI_ONIMAGE L"OnImage"
#define KEY_UI_ONIMAGE_DEFAULT L"image.ico"
#define KEY_UI_OFFIMAGE L"OffImage"
#define KEY_UI_OFFIMAGE_DEFAULT L"image.ico"
#define KEY_APP_PATH L"Path"
#define KEY_APP_PATH_DEFAULT NULL
#define KEY_APP_ARGS L"Args"
#define KEY_APP_ARGS_DEFAULT L""
#define KEY_APP_WORKDIR_PATH L"WorkDir"
#define KEY_APP_WORKDIR_PATH_DEFAULT L"."
#define KEY_APP_HIDE L"Hide"
#define KEY_APP_HIDE_DEFAULT 1
constexpr WCHAR SECTION_ENV[] = L"Env";
constexpr WCHAR SECTION_ENV_PREFIX[] = L"Env.Prefix";
using namespace std;
using namespace std::filesystem;
path get_module_file_path()
{
const unique_ptr<WCHAR[]> buf = make_unique<WCHAR[]>(MAX_PATH);
const DWORD size = GetModuleFileName(nullptr, buf.get(), MAX_PATH);
assert(size > 0);
path p{ wstring{ buf.get(), size} };
return p;
}
path Canonicalize(const path& p)
{
if (p.is_relative())
{
const path root = CIniConfig::GetModuleDirectory();
path rv = absolute(root / p);
return rv;
}
return p;
}
path CIniConfig::GetPathValueFromIni(LPCWSTR section, LPCWSTR key, LPCWSTR default_value)
{
const path ini_path = GetIniPath();
const unique_ptr<WCHAR[]> buf = make_unique<WCHAR[]>(MAX_PATH);
const DWORD size = GetPrivateProfileString(
section,
key,
default_value,
buf.get(),
MAX_PATH,
ini_path.c_str());
assert(size > 0 && size < MAX_PATH);
path p{ wstring{ buf.get(), size} };
path rv = Canonicalize(p);
return rv;
}
wstring CIniConfig::GetStringValueFromIni(LPCWSTR section, LPCWSTR key, LPCWSTR default_value)
{
constexpr int kSize = 4 * 1024;
const path ini_path = GetIniPath();
const unique_ptr<WCHAR[]> buf = make_unique<WCHAR[]>(kSize);
const DWORD size = GetPrivateProfileString(
section,
key,
default_value,
buf.get(),
kSize,
ini_path.c_str());
assert(size > 0 && size < kSize);
wstring rv{ buf.get(),size };
return rv;
}
CIniConfig::CIniConfig()
{
}
void CIniConfig::Initialize()
{
}
path CIniConfig::GetModuleDirectory()
{
const path p = get_module_file_path();
const path dir = p.parent_path();
return dir;
}
path CIniConfig::GetIniPath()
{
path p = get_module_file_path();
p.replace_extension(L".ini");
return p;
}
path CIniConfig::GetWorkDirPath()
{
path rv = GetPathValueFromIni(SECTION_APP, KEY_APP_WORKDIR_PATH, KEY_APP_WORKDIR_PATH_DEFAULT);
return rv;
}
path CIniConfig::GetOnIconPath()
{
path rv = GetPathValueFromIni(SECTION_UI, KEY_UI_ONIMAGE, KEY_UI_ONIMAGE_DEFAULT);
return rv;
}
path CIniConfig::GetOffIconPath()
{
path rv = GetPathValueFromIni(SECTION_UI, KEY_UI_OFFIMAGE, KEY_UI_OFFIMAGE_DEFAULT);
return rv;
}
path CIniConfig::GetAppPath()
{
path rv = GetPathValueFromIni(SECTION_APP, KEY_APP_PATH, nullptr);
return rv;
}
wstring CIniConfig::GetAppArgs()
{
wstring args = GetStringValueFromIni(SECTION_APP, KEY_APP_ARGS, KEY_APP_ARGS_DEFAULT);
return args;
}
void CIniConfig::GetSectionList(LPCWSTR section, std::list<std::wstring>& env_list)
{
constexpr int kBufSize = 4 * 1024;
const path ini_path = GetIniPath();
const wstring int_path_str = ini_path.wstring();
const wchar_t* ini_path_ptr = int_path_str.c_str();
const unique_ptr<WCHAR[]> buf = make_unique<WCHAR[]>(kBufSize);
[[maybe_unused]] const int read_size = GetPrivateProfileSection(section, buf.get(), kBufSize, ini_path_ptr);
assert(read_size <= kBufSize - 2);
WCHAR* p = buf.get();
while (*p) {
const size_t n = wcslen(p);
wstring s{ p, n };
env_list.emplace_back(s);
p += n + 1;
}
}
void CIniConfig::GetEnvList(std::list<std::wstring>& env_list)
{
GetSectionList(SECTION_ENV, env_list);
}
void CIniConfig::GetEnvPrefixList(std::list<std::wstring>& env_list)
{
GetSectionList(SECTION_ENV_PREFIX, env_list);
}
bool CIniConfig::GetAppHide()
{
const path ini_path = GetIniPath();
const UINT val = GetPrivateProfileInt(SECTION_APP, KEY_APP_HIDE, KEY_APP_HIDE_DEFAULT, ini_path.c_str());
const bool rv = val != 0;
return rv;
}