-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.cpp
315 lines (236 loc) · 7.08 KB
/
utils.cpp
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
//
// Copyright (C) 2013-2018 University of Amsterdam
//
// 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 2 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, see <http://www.gnu.org/licenses/>.
//
#include "utils.h"
#ifdef _WIN32
#include <windows.h>
#include <fileapi.h>
#include <winternl.h>
#else
#include <sys/stat.h>
#include <utime.h>
#endif
#include <cmath>
#include "utilenums.h"
#include <iomanip>
#include <chrono>
using namespace std;
Utils::FileType Utils::getTypeFromFileName(const std::string &path)
{
size_t lastPoint = path.find_last_of('.');
FileType filetype = lastPoint == string::npos ? FileType::empty : FileType::unknown;
if (lastPoint != string::npos && (lastPoint + 1) < path.length())
{
std::string suffix = path.substr(lastPoint + 1);
for (int i = 0; i < int(FileType::empty); i++)
{
FileType it = static_cast<FileType>(i);
if (suffix == FileTypeBaseToString(it))
{
filetype = it;
break;
}
}
}
return filetype;
}
const string &Utils::currentDateTime()
{
static std::string currentDateTimeCache;
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
currentDateTimeCache = ss.str();
return currentDateTimeCache;
}
long Utils::currentMillis()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
long Utils::currentSeconds()
{
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
long Utils::getFileModificationTime(const std::string &filename)
{
#ifdef _WIN32
HANDLE file = CreateFileA(filename.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
return -1;
FILETIME modTime;
bool success = GetFileTime(file, NULL, NULL, &modTime);
CloseHandle(file);
if (success)
{
LARGE_INTEGER li;
ULONG seconds;
li.QuadPart = modTime.dwHighDateTime;
li.QuadPart = (li.QuadPart << 32) | modTime.dwLowDateTime;
RtlTimeToSecondsSince1970(&li, &seconds);
return seconds;
}
else
{
return -1;
}
#elif __APPLE__
struct stat attrib;
stat(filename.c_str(), &attrib);
time_t modificationTime = attrib.st_mtimespec.tv_sec;
return modificationTime;
#else
struct stat attrib;
stat(filename.c_str(), &attrib);
time_t modificationTime = attrib.st_mtim.tv_sec;
return modificationTime;
#endif
}
long Utils::getFileSize(const string &filename)
{
std::error_code ec;
std::filesystem::path path;
path = filename;
uintmax_t fileSize = std::filesystem::file_size(path, ec);
if (!ec)
return fileSize;
return -1;
}
void Utils::touch(const string &filename)
{
#ifdef _WIN32
HANDLE file = CreateFileA(filename.c_str(), FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
return;
FILETIME ft;
SYSTEMTIME st;
GetSystemTime(&st);
SystemTimeToFileTime(&st, &ft);
SetFileTime(file, NULL, NULL, &ft);
CloseHandle(file);
#else
struct utimbuf newTime;
time_t newTimeT;
time(&newTimeT);
newTime.actime = newTimeT;
newTime.modtime = newTimeT;
utime(filename.c_str(), &newTime);
#endif
}
bool Utils::renameOverwrite(const string &oldName, const string &newName)
{
std::filesystem::path o = osPath(oldName);
std::filesystem::path n = osPath(newName);
std::error_code ec;
#ifdef _WIN32
std::error_code er;
if (std::filesystem::exists(n, er))
{
std::filesystem::file_status s = std::filesystem::status(n);
bool readOnly = (s.permissions() & std::filesystem::perms::owner_write) == std::filesystem::perms::none;
if (readOnly)
std::filesystem::permissions(n, std::filesystem::perms::owner_write);
}
#endif
std::filesystem::rename(o, n, ec);
return !ec;
}
bool Utils::removeFile(const string &path)
{
std::filesystem::path p = osPath(path);
std::error_code ec;
std::filesystem::remove(p, ec);
return !ec;
}
std::filesystem::path Utils::osPath(const string &path)
{
return std::filesystem::path(path);
}
string Utils::osPath(const std::filesystem::path &path)
{
return path.generic_string();
}
void Utils::remove(vector<string> &target, const vector<string> &toRemove)
{
for (const string &remove : toRemove)
target.erase(std::remove_if(target.begin(), target.end(), [&remove](const string& str){return (str == remove);}), target.end());
}
void Utils::sleep(int ms)
{
#ifdef _WIN32
Sleep(DWORD(ms));
#else
struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
nanosleep(&ts, NULL);
#endif
}
bool Utils::isEqual(const double a, const double b)
{
if (isnan(a) || isnan(b)) return (isnan(a) && isnan(b));
return (fabs(a - b) <= ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * std::numeric_limits<double>::epsilon()));
}
bool Utils::isEqual(const float a, const float b)
{
if (isnan(a) || isnan(b)) return (isnan(a) && isnan(b));
return fabs(a - b) <= ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * std::numeric_limits<float>::epsilon());
}
#ifdef _WIN32
std::wstring Utils::getShortPathWin(const std::wstring & longPath)
{
//See: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getshortpathnamew
long length = 0;
TCHAR* buffer = NULL;
// First obtain the size needed by passing NULL and 0.
length = GetShortPathName(longPath.c_str(), NULL, 0);
if (length == 0)
return longPath;
// Dynamically allocate the correct size
// (terminating null char was included in length)
buffer = new TCHAR[length];
// Now simply call again using same long path.
length = GetShortPathName(longPath.c_str(), buffer, length);
if (length == 0)
{
delete[] buffer;
return longPath;
}
std::wstring shortPath(buffer, length);
delete [] buffer;
return shortPath;
}
string Utils::wstringToString(const std::wstring & wstr)
{
std::string str;
//get size of buffer we need
int requiredSize = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), -1, NULL, 0, NULL, NULL );
str.resize(requiredSize);
//convert it
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), -1, str.data(), str.size(), NULL, NULL );
str.resize(requiredSize-1);//drop /nul
return str;
}
wstring Utils::stringToWString(const std::string &str)
{
std::wstring wstr;
//get size of buffer we need
int requiredSize = MultiByteToWideChar(CP_UTF8, 0, str.data(), -1, NULL, 0);
wstr.resize(requiredSize);
//convert it
MultiByteToWideChar(CP_UTF8, 0, str.data(), -1, wstr.data(), wstr.size());
wstr.resize(requiredSize-1);//drop /nul
return wstr;
}
#endif