forked from awwit/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystem.cpp
More file actions
320 lines (261 loc) · 6.18 KB
/
System.cpp
File metadata and controls
320 lines (261 loc) · 6.18 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "System.h"
#include <array>
#ifdef WIN32
#include <tchar.h>
::TCHAR myWndClassName[] = TEXT("WndClassNameConstant");
#ifdef UNICODE
#include <codecvt>
#endif
#elif POSIX
#include <csignal>
#include <sys/sysinfo.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
namespace System
{
#ifdef WIN32
struct EnumData
{
native_processid_type process_id;
::HWND hWnd;
};
static ::BOOL WINAPI EnumProc(const ::HWND hWnd, const ::LPARAM lParam)
{
EnumData &ed = *reinterpret_cast<EnumData * const>(lParam);
native_processid_type process_id = 0;
::GetWindowThreadProcessId(hWnd, &process_id);
if (process_id == ed.process_id && ::GetConsoleWindow() != hWnd)
{
std::array<::TCHAR, 257> class_name;
::GetClassName(hWnd, class_name.data(), static_cast<int>(class_name.size() - 1) );
if (0 == ::_tcscmp(class_name.data(), myWndClassName) )
{
ed.hWnd = hWnd;
return false;
}
}
return true;
}
#endif
native_processid_type getProcessId() noexcept
{
#ifdef WIN32
return ::GetCurrentProcessId();
#elif POSIX
return ::getpid();
#else
#error "Undefine platform"
#endif
}
bool changeCurrentDirectory(const std::string &dir)
{
#ifdef WIN32
#ifdef UNICODE
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> > converter;
const std::wstring target = converter.from_bytes(dir);
#else
const std::string &target = dir;
#endif
return 0 != ::SetCurrentDirectory(target.c_str() );
#elif POSIX
return 0 == ::chdir(dir.c_str() );
#else
#error "Undefine platform"
#endif
}
bool isProcessExists(const native_processid_type pid) noexcept
{
#ifdef WIN32
HANDLE hProcess = ::OpenProcess(SYNCHRONIZE, false, pid);
::CloseHandle(hProcess);
return 0 != hProcess;
#elif POSIX
return 0 == ::kill(pid, 0);
#else
#error "Undefine platform"
#endif
}
bool sendSignal(const native_processid_type pid, const int signal) noexcept
{
#ifdef WIN32
EnumData ed = {pid, 0};
::EnumWindows(EnumProc, reinterpret_cast<::LPARAM>(&ed) );
if (0 == ed.hWnd)
{
return false;
}
return 0 != ::PostMessage(ed.hWnd, signal, 0, 0);
#elif POSIX
return 0 == ::kill(pid, signal);
#else
#error "Undefine platform"
#endif
}
bool isDoneThread(const std::thread::native_handle_type handle) noexcept
{
#ifdef WIN32
return WAIT_OBJECT_0 == ::WaitForSingleObject(handle, 0);
#elif POSIX
return 0 != ::pthread_kill(handle, 0);
#else
#error "Undefine platform"
#endif
}
std::string getTempDir()
{
#ifdef WIN32
std::array<::TCHAR, MAX_PATH + 1> buf;
auto const len = ::GetTempPath(static_cast<::DWORD>(buf.size() ), buf.data() );
#ifdef UNICODE
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> > converter;
return converter.to_bytes(buf.data() );
#else
return std::string(buf.cbegin(), buf.cbegin() + len);
#endif
#elif POSIX
const char *buf = ::getenv("TMPDIR");
if (nullptr == buf)
{
return std::string("/tmp/");
}
std::string str(buf);
if ('/' != str.back() )
{
str.push_back('/');
}
return str;
#else
#error "Undefine platform"
#endif
}
bool isFileExists(const std::string &fileName)
{
#ifdef WIN32
#ifdef UNICODE
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
const std::wstring file_name = converter.from_bytes(fileName);
#else
const std::string &file_name = fileName;
#endif
const ::DWORD attrib = ::GetFileAttributes(file_name.c_str() );
if (INVALID_FILE_ATTRIBUTES == attrib)
{
return false;
}
return FILE_ATTRIBUTE_DIRECTORY != (attrib & FILE_ATTRIBUTE_DIRECTORY);
#elif POSIX
struct ::stat attrib;
if (-1 == ::stat(fileName.c_str(), &attrib) )
{
return false;
}
return S_ISREG(attrib.st_mode);
#else
#error "Undefine platform"
#endif
}
bool getFileSizeAndTimeGmt(const std::string &filePath, size_t *fileSize, time_t *fileTime)
{
#ifdef WIN32
#ifdef UNICODE
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
const std::wstring file_path = converter.from_bytes(filePath);
#else
const std::string &file_path = filePath;
#endif
const ::HANDLE hFile = ::CreateFile(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (INVALID_HANDLE_VALUE == hFile)
{
return false;
}
if (false == ::GetFileSizeEx(hFile, reinterpret_cast<::PLARGE_INTEGER>(fileSize) ) )
{
::CloseHandle(hFile);
return false;
}
::FILETIME ftWrite;
::BOOL result = ::GetFileTime(hFile, nullptr, nullptr, &ftWrite);
::CloseHandle(hFile);
if (false == result)
{
return false;
}
::SYSTEMTIME stUtc;
::FileTimeToSystemTime(&ftWrite, &stUtc);
std::tm tm_time {
stUtc.wSecond,
stUtc.wMinute,
stUtc.wHour,
stUtc.wDay,
stUtc.wMonth - 1,
stUtc.wYear - 1900,
0,
0,
-1
};
*fileTime = std::mktime(&tm_time);
return true;
#elif POSIX
struct ::stat attrib {};
if (-1 == ::stat(filePath.c_str(), &attrib) )
{
return false;
}
*fileSize = attrib.st_size;
std::tm clock {};
::gmtime_r(&(attrib.st_mtime), &clock);
*fileTime = std::mktime(&clock);
return true;
#else
#error "Undefine platform"
#endif
}
void filterSharedMemoryName(std::string &memName)
{
#ifdef WIN32
#ifdef UNICODE
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> > converter;
std::wstring memory_name = converter.from_bytes(memName);
const std::wstring file_ext = L".exe";
#else
std::string &memory_name = memName;
const std::string file_ext = ".exe";
#endif
const size_t pos = memory_name.rfind(file_ext);
if (pos == memory_name.length() - file_ext.length() )
{
memory_name.erase(memory_name.begin() + pos, memory_name.end() );
}
::TCHAR buf[MAX_PATH + 1] {};
::GetFullPathName(memory_name.c_str(), MAX_PATH, buf, nullptr);
#ifdef UNICODE
memName = converter.to_bytes(buf);
#else
memName = buf;
#endif
for (size_t i = 1; i < memName.length(); ++i)
{
if ('/' == memName[i] || '\\' == memName[i])
{
memName[i] = '-';
}
}
#elif POSIX
if ('/' != memName.front() )
{
memName = '/' + memName;
}
for (size_t i = 1; i < memName.length(); ++i)
{
if ('/' == memName[i] || '\\' == memName[i])
{
memName[i] = '-';
}
}
#else
#error "Undefine platform"
#endif
}
};