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
|
// Copyright (C) 2020 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "Win32Util.hpp"
#include "Util.hpp"
#include <chrono>
#include <thread>
namespace Win32Util {
std::string
add_exe_suffix(const std::string& path)
{
auto ext = Util::to_lowercase(Util::get_extension(path));
if (ext == ".exe" || ext == ".bat" || ext == ".sh") {
return path;
} else {
return path + ".exe";
}
}
std::string
error_message(DWORD error_code)
{
LPSTR buffer;
size_t size =
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
error_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&buffer),
0,
nullptr);
std::string message(buffer, size);
LocalFree(buffer);
return message;
}
std::string
argv_to_string(const char* const* argv, const std::string& prefix)
{
std::string result;
size_t i = 0;
const char* arg = prefix.empty() ? argv[i++] : prefix.c_str();
do {
int bs = 0;
result += '"';
for (size_t j = 0; arg[j]; ++j) {
switch (arg[j]) {
case '\\':
++bs;
break;
// Fallthrough.
case '"':
bs = (bs << 1) + 1;
// Fallthrough.
default:
while (bs > 0) {
result += '\\';
--bs;
}
result += arg[j];
}
}
bs <<= 1;
while (bs > 0) {
result += '\\';
--bs;
}
result += "\" ";
} while ((arg = argv[i++]));
result.resize(result.length() - 1);
return result;
}
} // namespace Win32Util
// From: https://stackoverflow.com/a/58162122/262458
#ifdef _MSC_VER
int
gettimeofday(struct timeval* tp, struct timezone* /*tzp*/)
{
namespace sc = std::chrono;
sc::system_clock::duration d = sc::system_clock::now().time_since_epoch();
sc::seconds s = sc::duration_cast<sc::seconds>(d);
tp->tv_sec = static_cast<long>(s.count());
tp->tv_usec =
static_cast<long>(sc::duration_cast<sc::microseconds>(d - s).count());
return 0;
}
#endif
void
usleep(int64_t usec)
{
std::this_thread::sleep_for(std::chrono::microseconds(usec));
}
struct tm*
localtime_r(time_t* _clock, struct tm* _result)
{
struct tm* p = localtime(_clock);
if (p)
*(_result) = *p;
return p;
}
// From: https://stackoverflow.com/a/40160038/262458
#ifdef _MSC_VER
int
vasprintf(char** strp, const char* fmt, va_list ap)
{
// _vscprintf tells you how big the buffer needs to be
int len = _vscprintf(fmt, ap);
if (len == -1) {
return -1;
}
size_t size = (size_t)len + 1;
char* str = static_cast<char*>(malloc(size));
if (!str) {
return -1;
}
// vsprintf_s is the "secure" version of vsprintf
int r = vsprintf_s(str, len + 1, fmt, ap);
if (r == -1) {
free(str);
return -1;
}
*strp = str;
return r;
}
#endif
// Also from: https://stackoverflow.com/a/40160038/262458
#ifdef _MSC_VER
int
asprintf(char** strp, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int r = vasprintf(strp, fmt, ap);
va_end(ap);
return r;
}
#endif
|