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
210 lines (162 loc) · 3.84 KB
/
System.cpp
File metadata and controls
210 lines (162 loc) · 3.84 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
#include "System.h"
#include <array>
#ifdef WIN32
#include <tchar.h>
::TCHAR myWndClassName[] = TEXT("WndClassNameConstant");
#ifdef UNICODE
#include <codecvt>
#endif
#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(), class_name.size() - 1);
if (0 == ::_tcscmp(class_name.data(), myWndClassName) )
{
ed.hWnd = hWnd;
return false;
}
}
return true;
}
#endif
bool sendSignal(const native_processid_type pid, const int signal)
{
#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
}
std::string getTempDir()
{
#ifdef WIN32
std::array<TCHAR, MAX_PATH + 1> buf;
auto const len = ::GetTempPath(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);
struct ::tm tm_time {
stUtc.wSecond,
stUtc.wMinute,
stUtc.wHour,
stUtc.wDay,
stUtc.wMonth - 1,
stUtc.wYear - 1900,
0,
0,
-1
};
*fileTime = ::mktime(&tm_time);
return true;
#elif POSIX
struct ::stat attrib;
if (-1 == ::stat(filePath.c_str(), &attrib) )
{
return false;
}
*fileSize = attrib.st_size;
struct ::tm clock = {};
::gmtime_r(&(attrib.st_mtime), &clock);
*fileTime = ::mktime(&clock);
return true;
#else
#error "Undefine platform"
#endif
}
};