-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystem_utils.cpp
179 lines (163 loc) · 5.58 KB
/
system_utils.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
// CEZEO software Ltd. https://www.cezeo.com
#include "system_utils.h"
#include "string_utils.h"
// windows
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#include <windows.h>
#define TEMP_FILE_NAME_PREFIX L"cabf"
namespace SystemUtils
{
extern const wchar_t localAppData[] = L"LOCALAPPDATA";
extern const wchar_t pathSeparator = L'\\';
namespace
{
const wchar_t forwardSlash = L'/';
const char hexDigits[] = "0123456789abcdef";
const wchar_t defaultTempFolder[] = L"C:\\Temp";
} // namespace
// used to add filename to the path with checking the trailing backward and forward slashes
std::wstring PathAppendFileName(const std::wstring& path, const std::wstring& fileName)
{
std::wstring result(path);
if (result.size() > 0)
{
if (result[ result.size() - 1 ] != pathSeparator && result[ result.size() - 1 ] != forwardSlash)
{
result += pathSeparator;
}
result += fileName;
}
return result;
}
// used to extract filename from the path
std::wstring GetFileNameFromPath(const std::wstring& fileFullPath)
{
wchar_t fileFullName[ _MAX_PATH ];
wchar_t fileName[ _MAX_FNAME ];
wchar_t fileExtension[ _MAX_EXT ];
_wsplitpath_s(fileFullPath.c_str(), NULL, 0, NULL, 0, fileName, _MAX_FNAME, fileExtension, _MAX_EXT);
_wmakepath_s(fileFullName, _MAX_PATH, NULL, NULL, fileName, fileExtension);
return std::wstring(fileFullName);
}
std::wstring GetModuleFolder()
{
wchar_t filePath[ _MAX_PATH ];
GetModuleFileName(NULL, filePath, _MAX_PATH);
return std::wstring(GetFolderFromPath(filePath));
}
std::wstring GetModuleFile()
{
wchar_t filePath[ _MAX_PATH ];
GetModuleFileName(NULL, filePath, _MAX_PATH);
return std::wstring(GetFileNameFromPath(filePath));
}
std::wstring GetModulePath()
{
wchar_t filePath[ _MAX_PATH ];
GetModuleFileName(NULL, filePath, _MAX_PATH);
return std::wstring(filePath);
}
std::wstring GetFolderFromPath(const std::wstring& fileName)
{
wchar_t filePath[ _MAX_PATH ];
wchar_t driveName[ _MAX_DRIVE ], path[ _MAX_PATH ];
_wsplitpath_s(fileName.c_str(), driveName, _MAX_DRIVE, path, _MAX_PATH, NULL, 0, NULL, 0);
_wmakepath_s(filePath, _MAX_PATH, driveName, path, NULL, NULL);
return std::wstring(filePath);
}
// return lowercase file extension
std::wstring GetExtensionFromPath(const std::wstring& fileName)
{
wchar_t fileExtension[ _MAX_EXT ];
ZeroMemory(fileExtension, _MAX_EXT);
_wsplitpath_s(fileName.c_str(), NULL, 0, NULL, 0, NULL, 0, fileExtension, _MAX_EXT);
CharLower(fileExtension);
return std::wstring(fileExtension);
}
// get file size
bool GetFileSize(const std::wstring& path, uint64_t& fileSize)
{
bool result = false;
HANDLE fileHandle = CreateFile(path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fileHandle != INVALID_HANDLE_VALUE)
{
LARGE_INTEGER size;
result = GetFileSizeEx(fileHandle, &size) != FALSE;
fileSize = size.QuadPart;
CloseHandle(fileHandle);
}
return result;
}
// get last write time for file
uint64_t GetFileLastWriteTime(const std::wstring& file)
{
uint64_t result = 0;
FILETIME lastWriteTime{0};
HANDLE fileHandle = CreateFile(file.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fileHandle != INVALID_HANDLE_VALUE)
{
GetFileTime(fileHandle, NULL, NULL, &lastWriteTime);
result = lastWriteTime.dwLowDateTime | ((uint64_t)lastWriteTime.dwHighDateTime << 32);
CloseHandle(fileHandle);
}
return result;
}
// load file to buffer
bool FileToString(const std::wstring& filePath, std::string& data, size_t maxSize)
{
bool result = false;
uint64_t size = 0;
if (GetFileSize(filePath, size) && size <= maxSize )
{
data.resize((size_t)size); // safe uint64_t to size_t
HANDLE fileHandle = CreateFile(filePath.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fileHandle != INVALID_HANDLE_VALUE)
{
DWORD read = 0;
result = ReadFile(fileHandle, &data[0], (DWORD)size, &read, NULL) != FALSE && read == size; // safe uint64_t to DWORD
CloseHandle(fileHandle);
}
}
return result;
}
// generate temp file name
bool GenerateTempFileName(std::wstring& tempFileName)
{
bool result = false;
wchar_t tempFileNameBuffer[ MAX_PATH ];
std::wstring tempFolder;
if (GetTempFolder(tempFolder))
{
// Generates a temporary file name.
UINT returnedId = GetTempFileName(tempFolder.c_str(), // directory for tmp files
TEMP_FILE_NAME_PREFIX, // temp file name prefix
0, // create unique name
tempFileNameBuffer); // buffer for name
if (returnedId > 0)
{
tempFileName.assign(tempFileNameBuffer);
result = true;
}
}
return result;
}
// get temp path to temp folder, the returned string ends with a backslash (if success)
bool GetTempFolder(std::wstring& tempFolder)
{
bool result = false;
wchar_t tempPathBuffer[ MAX_PATH ];
// Gets the temp path env string (no guarantee it's a valid path).
DWORD resultInChars = GetTempPath(MAX_PATH, tempPathBuffer);
if (resultInChars > (MAX_PATH + 1) || resultInChars == 0)
{
// error
}
else
{
tempFolder.assign(tempPathBuffer, size_t(resultInChars));
result = true;
}
return result;
}
} // namespace SystemUtils