-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.cpp
More file actions
198 lines (172 loc) · 5.13 KB
/
Copy pathutility.cpp
File metadata and controls
198 lines (172 loc) · 5.13 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
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
#include "utility.h"
#include <fstream>
#include <algorithm>
LPARAM TreeView_GetItemParam(HWND hwnd, HTREEITEM hti)
{
TVITEM tvi = {};
tvi.hItem = hti;
tvi.mask = TVIF_PARAM;
TreeView_GetItem(hwnd, &tvi);
return tvi.lParam;
}
tchar::tstring RegGetFullKeyName(HKEY hRootKey)
{
tchar::tstring rootName = TEXT("");
// Map the predefined root keys to their names
if (hRootKey == HKEY_CLASSES_ROOT)
rootName = TEXT("HKEY_CLASSES_ROOT");
else if (hRootKey == HKEY_CURRENT_USER)
rootName = TEXT("HKEY_CURRENT_USER");
else if (hRootKey == HKEY_LOCAL_MACHINE)
rootName = TEXT("HKEY_LOCAL_MACHINE");
else if (hRootKey == HKEY_USERS)
rootName = TEXT("HKEY_USERS");
else if (hRootKey == HKEY_CURRENT_CONFIG)
rootName = TEXT("HKEY_CURRENT_CONFIG");
return rootName;
}
tchar::tstring RegGetFullKeyName(HKEY hRootKey, const tchar::tstring& subKey)
{
tchar::tstring rootName = TEXT("");
// Map the predefined root keys to their names
if (hRootKey == HKEY_CLASSES_ROOT)
rootName = TEXT("HKEY_CLASSES_ROOT");
else if (hRootKey == HKEY_CURRENT_USER)
rootName = TEXT("HKEY_CURRENT_USER");
else if (hRootKey == HKEY_LOCAL_MACHINE)
rootName = TEXT("HKEY_LOCAL_MACHINE");
else if (hRootKey == HKEY_USERS)
rootName = TEXT("HKEY_USERS");
else if (hRootKey == HKEY_CURRENT_CONFIG)
rootName = TEXT("HKEY_CURRENT_CONFIG");
// If subKey is empty, just return the root
if (subKey.empty())
return rootName;
// Otherwise, append the subkey with backslash
return rootName + TEXT("\\") + subKey;
}
std::vector<std::string> SplitCommandLine(LPCSTR cmdLine)
{
std::vector<std::string> args;
std::string current;
bool inQuotes = false;
while (*cmdLine)
{
if (*cmdLine == '"')
{
inQuotes = !inQuotes;
++cmdLine;
}
else if (!inQuotes && (*cmdLine == ' ' || *cmdLine == '\t'))
{
if (!current.empty())
{
args.push_back(current);
current.clear();
}
while (*cmdLine == ' ' || *cmdLine == '\t') ++cmdLine; // skip spaces
}
else
{
current += *cmdLine++;
}
}
if (!current.empty())
args.push_back(current);
return args;
}
BOOL SplitRegistryPath(const tchar::tstring& fullPath, HKEY* outRoot, tchar::tstring* outSubKey)
{
// Define a mapping from string to HKEY
struct RootMap { LPCTSTR name; HKEY key; };
static RootMap roots[] = {
{ TEXT("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT },
{ TEXT("HKCR"), HKEY_CLASSES_ROOT },
{ TEXT("HKEY_CURRENT_USER"), HKEY_CURRENT_USER },
{ TEXT("HKCU"), HKEY_CURRENT_USER },
{ TEXT("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE },
{ TEXT("HKLM"), HKEY_LOCAL_MACHINE },
{ TEXT("HKEY_USERS"), HKEY_USERS },
{ TEXT("HKU"), HKEY_USERS },
{ TEXT("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG },
{ TEXT("HKCC"), HKEY_CURRENT_CONFIG },
{ TEXT("HKEY_DYN_DATA"), HKEY_DYN_DATA },
{ TEXT("HKDD"), HKEY_DYN_DATA },
{ TEXT("HKEY_PERFORMANCE_DATA"), HKEY_PERFORMANCE_DATA },
{ TEXT("HKPD"), HKEY_PERFORMANCE_DATA }
};
// Find the first backslash
size_t pos = fullPath.find(TEXT('\\'));
tchar::tstring rootStr = (pos == tchar::tstring::npos) ? fullPath : fullPath.substr(0, pos);
*outSubKey = (pos == tchar::tstring::npos) ? TEXT("") : fullPath.substr(pos + 1);
// Match root string
std::transform(rootStr.begin(), rootStr.end(), rootStr.begin(), tchar::totupper);
for (const auto& r : roots)
{
if (rootStr == r.name)
{
*outRoot = r.key;
return TRUE;
}
}
// Not found
return FALSE;
}
std::vector<tchar::tstring> SplitSubKeys(const tchar::tstring& subKey)
{
std::vector<tchar::tstring> parts;
size_t start = 0;
size_t pos;
while ((pos = subKey.find(TEXT('\\'), start)) != tchar::tstring::npos)
{
parts.push_back(subKey.substr(start, pos - start));
start = pos + 1;
}
if (start < subKey.length())
parts.push_back(subKey.substr(start));
return parts;
}
bool IsElevated()
{
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
return false;
TOKEN_ELEVATION elev{};
DWORD cb;
GetTokenInformation(hToken, TokenElevation, &elev, sizeof(elev), &cb);
CloseHandle(hToken);
return elev.TokenIsElevated;
}
#ifdef UNICODE
wint_t tchar::totupper(wint_t ch)
{
return towupper(ch);
}
#else
int tchar::totupper(int ch)
{
return toupper(ch);
}
#endif
tchar::tstring RemoveMenuAmpersands(const tchar::tstring& s)
{
tchar::tstring out;
out.reserve(s.size());
for (size_t i = 0; i < s.size(); ++i)
{
if (s[i] == '&')
{
if (i + 1 < s.size() && s[i + 1] == '&')
{
out.push_back('&'); // escaped ampersand
++i;
}
// skip mnemonic '&'
}
else
{
out.push_back(s[i]);
}
}
return out;
}