-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathUtility.cpp
More file actions
303 lines (253 loc) · 7.93 KB
/
Copy pathUtility.cpp
File metadata and controls
303 lines (253 loc) · 7.93 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
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
#include "Utility.h"
#include <CommCtrl.h>
#include <memory>
#include <windowsx.h>
#include <filesystem>
#include <shlobj.h>
#include <commdlg.h>
#include <Shlwapi.h>
#include <algorithm>
#pragma comment(lib, "Version.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "Comctl32.lib")
std::wstring CUtility::GetVersion(const std::wstring& filePath)
{
std::wstring retVer;
if (!filePath.empty() && ::PathFileExists(filePath.c_str()))
{
DWORD handle = 0;
DWORD bufferSize = ::GetFileVersionInfoSize(filePath.c_str(), &handle);
if (bufferSize > 0)
{
auto buffer = std::make_unique<unsigned char[]>(bufferSize);
::GetFileVersionInfo(filePath.c_str(), 0, bufferSize, reinterpret_cast<LPVOID>(buffer.get()));
VS_FIXEDFILEINFO* lpFileInfo = nullptr;
UINT cbFileInfo = 0;
VerQueryValue(buffer.get(), TEXT("\\"), reinterpret_cast<LPVOID*>(&lpFileInfo), &cbFileInfo);
if (cbFileInfo)
{
std::wostringstream os;
os << ((lpFileInfo->dwFileVersionMS & 0xFFFF0000) >> 16);
os << '.';
os << (lpFileInfo->dwFileVersionMS & 0x0000FFFF);
os << '.';
os << ((lpFileInfo->dwFileVersionLS & 0xFFFF0000) >> 16);
os << '.';
os << (lpFileInfo->dwFileVersionLS & 0x0000FFFF);
retVer = os.str();
}
}
}
return retVer;
}
HWND CUtility::CreateToolTip(HWND hWnd, int nCtrlID, const std::wstring& tooltipText, HINSTANCE hInst)
{
if (nCtrlID == 0 || hWnd == nullptr || tooltipText.empty())
{
return nullptr;
}
// Get the window of the tool.
HWND hControl = GetDlgItem(hWnd, nCtrlID);
// Create the tooltip. g_hInst is the global instance handle.
HWND hToolTip
= CreateWindowEx(NULL, TOOLTIPS_CLASS, nullptr, WS_POPUP | TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hWnd, nullptr, hInst, nullptr);
if (hControl == nullptr || hToolTip == nullptr)
{
return nullptr;
}
// Associate the tooltip with the control.
TOOLINFO toolInfo = {};
toolInfo.cbSize = sizeof(toolInfo);
toolInfo.hwnd = hWnd;
toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
toolInfo.uId = reinterpret_cast<UINT_PTR>(hControl);
toolInfo.lpszText = const_cast<wchar_t*>(tooltipText.c_str());
SendMessage(hToolTip, TTM_ADDTOOL, 0, reinterpret_cast<LPARAM>(&toolInfo));
return hToolTip;
}
float CUtility::GetDesktopScale(HWND hWnd)
{
HDC hdc = GetDC(hWnd);
float fScale = GetDeviceCaps(hdc, LOGPIXELSX) / 96.0f;
ReleaseDC(hWnd, hdc);
return fScale;
}
short CUtility::GetXFromLPARAM(LPARAM lp)
{
return static_cast<short>(LOWORD(lp));
}
short CUtility::GetYFromLPARAM(LPARAM lp)
{
return static_cast<short>(HIWORD(lp));
}
std::wstring CUtility::GetEditCtrlText(HWND hWnd)
{
auto length = Edit_GetTextLength(hWnd);
std::vector<wchar_t> buf(length + 1);
Edit_GetText(hWnd, buf.data(), static_cast<int>(buf.size()));
return buf.data();
}
void CUtility::SetEditCtrlText(HWND hWnd, const std::wstring& txt)
{
Edit_SetText(hWnd, txt.data());
}
bool CUtility::GetCheckboxStatus(HWND hWnd)
{
return Button_GetCheck(hWnd);
}
void CUtility::SetCheckboxStatus(HWND hWnd, bool bCheck)
{
Button_SetCheck(hWnd, bCheck);
}
bool CUtility::DirExist(const std::wstring& dirPath)
{
return std::filesystem::exists(dirPath);
}
bool CUtility::FileExist(const std::wstring& filePath)
{
bool r = std::filesystem::exists(filePath);
return r;
}
long CUtility::FileSize(const std::wstring& filePath)
{
auto r = std::filesystem::file_size(filePath);
return static_cast<long>(r);
}
bool CUtility::CreateDir(const std::wstring& dirPath)
{
bool r = std::filesystem::create_directories(dirPath);
return r;
}
bool CUtility::Copy(const std::wstring& srcFile, const std::wstring& dstFile)
{
bool r = std::filesystem::copy_file(srcFile, dstFile);
return r;
}
auto CUtility::GetFileName(const std::wstring& fullPath, bool withExtension) -> std::wstring
{
std::filesystem::path pathObj(fullPath);
// Check if file name is required without extension
if (withExtension == false)
{
// Check if file has stem i.e. filename without extension
if (pathObj.has_stem())
{
// return the stem (file name without extension) from path object
return pathObj.stem().wstring();
}
}
// return the file name with extension from path object
return pathObj.filename().wstring();
}
auto CUtility::GetFileExtension(const std::wstring& fileName) -> std::wstring
{
std::wstring retVal;
std::filesystem::path pathObj(fileName);
// Check if file has stem i.e. filename without extension
if (pathObj.has_stem())
{
retVal = pathObj.filename().extension().wstring();
}
return retVal;
}
auto CUtility::GetTempFilePath() -> std::wstring
{
TCHAR tmpDir[1024] {};
GetTempPath(1024, tmpDir);
return tmpDir;
}
auto CUtility::GetSpecialFolderLocation(int folderKind) -> std::wstring
{
wchar_t path[MAX_PATH] = {};
const HRESULT specialLocationResult = SHGetFolderPath(nullptr, folderKind, nullptr, SHGFP_TYPE_CURRENT, path);
std::wstring result;
if (SUCCEEDED(specialLocationResult))
{
result = path;
}
return result;
}
bool CUtility::OpenFileDlg(std::wstring& filePath, const std::wstring& dlgTitle, const std::vector<wchar_t>& dlgFilter, DWORD flags)
{
bool bRetVal = false;
OPENFILENAME ofn;
::memset(&ofn, 0, sizeof(ofn));
wchar_t fileName[MAX_PATH] = {};
ofn.lStructSize = sizeof(ofn);
ofn.lpstrTitle = dlgTitle.c_str();
ofn.lpstrFilter = dlgFilter.data();
ofn.nFilterIndex = 1;
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = flags ? flags : OFN_FILEMUSTEXIST;
if (::GetOpenFileName(&ofn) != FALSE)
{
filePath = ofn.lpstrFile; // will have the full path and file name.
bRetVal = true;
}
return bRetVal;
}
bool CUtility::CopyToClipboard(const std::wstring& str2cpy, HWND hWnd)
{
size_t len2Allocate = (str2cpy.size() + 1) * sizeof(TCHAR);
HGLOBAL hglbCopy = ::GlobalAlloc(GMEM_MOVEABLE, len2Allocate);
if (hglbCopy == NULL)
{
return false;
}
if (!::OpenClipboard(hWnd))
{
::GlobalFree(hglbCopy);
::CloseClipboard();
return false;
}
if (!::EmptyClipboard())
{
::GlobalFree(hglbCopy);
::CloseClipboard();
return false;
}
// Lock the handle and copy the text to the buffer.
wchar_t* pStr = reinterpret_cast<wchar_t*>(::GlobalLock(hglbCopy));
if (pStr == NULL)
{
::GlobalUnlock(hglbCopy);
::GlobalFree(hglbCopy);
::CloseClipboard();
return false;
}
wcscpy_s(pStr, len2Allocate / sizeof(wchar_t), str2cpy.c_str());
::GlobalUnlock(hglbCopy);
// Place the handle on the clipboard.
unsigned int clipBoardFormat = CF_UNICODETEXT;
if (::SetClipboardData(clipBoardFormat, hglbCopy) == NULL)
{
::GlobalFree(hglbCopy);
::CloseClipboard();
return false;
}
if (!::CloseClipboard())
{
return false;
}
return true;
}
bool CUtility::IsNumber(const std::wstring& str)
{
return !str.empty()
&& std::find_if(
str.begin(),
str.end(),
[](wchar_t c)
{
return !std::isdigit(c);
})
== str.end();
}
auto CUtility::GetNumber(const std::wstring& str) -> std::optional<int>
{
std::optional<int> retVal = std::nullopt;
if (IsNumber(str))
retVal = std::make_optional<int>(std::stoi(str));
return retVal;
}