forked from awwit/httpserverapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystem.cpp
More file actions
117 lines (89 loc) · 1.94 KB
/
System.cpp
File metadata and controls
117 lines (89 loc) · 1.94 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
#include "System.h"
namespace System
{
#ifdef WIN32
struct EnumData
{
native_processid_type process_id;
::HWND hWnd;
};
BOOL WINAPI EnumProc(::HWND hWnd, ::LPARAM lParam)
{
EnumData &ed = *reinterpret_cast<EnumData *>(lParam);
native_processid_type process_id = 0;
::GetWindowThreadProcessId(hWnd, &process_id);
if (process_id == ed.process_id && GetConsoleWindow() != hWnd)
{
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
}
bool getFileSizeAndTimeGmt(const std::string &filePath, size_t *fileSize, time_t *fileTime)
{
#ifdef WIN32
::HANDLE hFile = ::CreateFile(filePath.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) ) )
{
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,
0
};
*fileTime = ::mktime(&tm_time);
return true;
#elif POSIX
struct ::tm *clock;
struct ::stat attrib;
if (-1 == ::stat(filePath.c_str(), &attrib) )
{
return false;
}
*fileSize = attrib.st_size;
clock = ::gmtime(&(attrib.st_mtime) );
*fileTime = ::mktime(clock);
return true;
#else
#error "Undefine platform"
#endif
}
};