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
153 lines (116 loc) · 2.59 KB
/
System.cpp
File metadata and controls
153 lines (116 loc) · 2.59 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
#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)
{
char class_name[65] = {0};
::GetClassName(hWnd, class_name, 64);
if (0 == ::strcmp(class_name, 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::vector<std::string::value_type> buf(MAX_PATH + 1);
size_t len = ::GetTempPath(MAX_PATH + 1, buf.data() );
return std::string(buf.cbegin(), buf.cbegin() + len);
#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 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,
-1
};
*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
}
};