/
Util.cpp
352 lines (303 loc) · 6.81 KB
/
Util.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "stdafx.h"
#include "Util.h"
#include <sys/stat.h>
#ifdef _WIN32
#include <windows.h>
#include <shlwapi.h>
#if defined(WINAPI_FAMILY) && defined(WINAPI_FAMILY_PARTITION)
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP
#define ARMIPS_WINDOWS_UWP
#endif
#endif
#else
#include <unistd.h>
#endif
std::wstring convertUtf8ToWString(const char* source)
{
std::wstring result;
int index = 0;
while (source[index] != 0)
{
int extraBytes = 0;
int value = source[index++];
if ((value & 0xE0) == 0xC0)
{
extraBytes = 1;
value &= 0x1F;
} else if ((value & 0xF0) == 0xE0)
{
extraBytes = 2;
value &= 0x0F;
} else if (value > 0x7F)
{
// error
return std::wstring();
}
for (int i = 0; i < extraBytes; i++)
{
int b = source[index++];
if ((b & 0xC0) != 0x80)
{
// error
return std::wstring();
}
value = (value << 6) | (b & 0x3F);
}
result += value;
}
return result;
}
std::string convertWCharToUtf8(wchar_t character)
{
std::string result;
if (character < 0x80)
{
result += character & 0x7F;
} else if (character < 0x800)
{
result += 0xC0 | ((character >> 6) & 0x1F);
result += (0x80 | (character & 0x3F));
} else {
result += 0xE0 | ((character >> 12) & 0xF);
result += 0x80 | ((character >> 6) & 0x3F);
result += 0x80 | (character & 0x3F);
}
return result;
}
std::string convertWStringToUtf8(const std::wstring& source)
{
std::string result;
for (size_t i = 0; i < source.size(); i++)
{
wchar_t character = source[i];
if (character < 0x80)
{
result += character & 0x7F;
} else if (character < 0x800)
{
result += 0xC0 | ((character >> 6) & 0x1F);
result += (0x80 | (character & 0x3F));
} else {
result += 0xE0 | ((character >> 12) & 0xF);
result += 0x80 | ((character >> 6) & 0x3F);
result += 0x80 | (character & 0x3F);
}
}
return result;
}
std::wstring intToHexString(unsigned int value, int digits, bool prefix)
{
std::wstring result;
result.reserve((digits+prefix) ? 2 : 0);
if (prefix)
{
result += '0';
result += 'x';
}
while (digits > 8)
{
result += '0';
digits--;
}
wchar_t buf[9];
swprintf(buf,9,L"%0*X",digits,value);
result += buf;
return result;
}
std::wstring intToString(unsigned int value, int digits)
{
std::wstring result;
result.reserve(digits);
while (digits > 8)
{
result += ' ';
digits--;
}
wchar_t buf[9];
swprintf(buf,9,L"%*d",digits,value);
result += buf;
return result;
}
int32_t getFloatBits(float value)
{
union { float f; int32_t i; } u;
u.f = value;
return u.i;
}
int64_t getDoubleBits(double value)
{
union { double f; int64_t i; } u;
u.f = value;
return u.i;
}
StringList getStringListFromArray(wchar_t** source, int count)
{
StringList result;
for (int i = 0; i < count; i++)
{
result.push_back(std::wstring(source[i]));
}
return result;
}
int64_t fileSize(const std::wstring& fileName)
{
#ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA attr;
if (!GetFileAttributesEx(fileName.c_str(),GetFileExInfoStandard,&attr)
|| (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
return 0;
return ((int64_t) attr.nFileSizeHigh << 32) | (int64_t) attr.nFileSizeLow;
#else
std::string utf8 = convertWStringToUtf8(fileName);
struct stat fileStat;
int err = stat(utf8.c_str(),&fileStat);
if (0 != err)
return 0;
return fileStat.st_size;
#endif
}
bool fileExists(const std::wstring& strFilename)
{
#ifdef _WIN32
#ifdef ARMIPS_WINDOWS_UWP
return GetFileAttributes(strFilename.c_str()) != INVALID_FILE_ATTRIBUTES;
#else
int OldMode = SetErrorMode(SEM_FAILCRITICALERRORS);
bool success = GetFileAttributes(strFilename.c_str()) != INVALID_FILE_ATTRIBUTES;
SetErrorMode(OldMode);
return success;
#endif
#else
std::string utf8 = convertWStringToUtf8(strFilename);
struct stat stFileInfo;
int intStat = stat(utf8.c_str(),&stFileInfo);
return intStat == 0;
#endif
}
bool copyFile(const std::wstring& existingFile, const std::wstring& newFile)
{
#ifdef _WIN32
return CopyFileW(existingFile.c_str(),newFile.c_str(),false) != FALSE;
#else
unsigned char buffer[BUFSIZ];
bool error = false;
std::string existingUtf8 = convertWStringToUtf8(existingFile);
std::string newUtf8 = convertWStringToUtf8(newFile);
FILE* input = fopen(existingUtf8.c_str(),"rb");
FILE* output = fopen(newUtf8.c_str(),"wb");
if (input == nullptr || output == nullptr)
return false;
size_t n;
while ((n = fread(buffer,1,BUFSIZ,input)) > 0)
{
if (fwrite(buffer,1,n,output) != n)
error = true;
}
fclose(input);
fclose(output);
return !error;
#endif
}
bool deleteFile(const std::wstring& fileName)
{
#ifdef _WIN32
return DeleteFileW(fileName.c_str()) != FALSE;
#else
std::string utf8 = convertWStringToUtf8(fileName);
return unlink(utf8.c_str()) == 0;
#endif
}
FILE* openFile(const std::wstring& fileName, OpenFileMode mode)
{
#ifdef _WIN32
switch (mode)
{
case OpenFileMode::ReadBinary:
return _wfopen(fileName.c_str(),L"rb");
case OpenFileMode::WriteBinary:
return _wfopen(fileName.c_str(),L"wb");
case OpenFileMode::ReadWriteBinary:
return _wfopen(fileName.c_str(),L"rb+");
}
#else
std::string nameUtf8 = convertWStringToUtf8(fileName);
switch (mode)
{
case OpenFileMode::ReadBinary:
return fopen(nameUtf8.c_str(),"rb");
case OpenFileMode::WriteBinary:
return fopen(nameUtf8.c_str(),"wb");
case OpenFileMode::ReadWriteBinary:
return fopen(nameUtf8.c_str(),"rb+");
}
#endif
return nullptr;
}
std::wstring getCurrentDirectory()
{
#ifdef _WIN32
wchar_t dir[MAX_PATH];
_wgetcwd(dir,MAX_PATH-1);
return dir;
#else
char* dir = getcwd(nullptr,0);
std::wstring result = convertUtf8ToWString(dir);
free(dir);
return result;
#endif
}
void changeDirectory(const std::wstring& dir)
{
#ifdef _WIN32
_wchdir(dir.c_str());
#else
std::string utf8 = convertWStringToUtf8(dir);
chdir(utf8.c_str());
#endif
}
std::wstring toWLowercase(const std::string& str)
{
std::wstring result;
for (size_t i = 0; i < str.size(); i++)
{
result += tolower(str[i]);
}
return result;
}
std::wstring getFileNameFromPath(const std::wstring& path)
{
size_t n = path.find_last_of(L"/\\");
if (n == path.npos)
return path;
return path.substr(n);
}
size_t replaceAll(std::wstring& str, const wchar_t* oldValue,const std::wstring& newValue)
{
size_t pos = 0;
size_t len = wcslen(oldValue);
size_t count = 0;
while ((pos = str.find(oldValue, pos)) != std::string::npos)
{
str.replace(pos,len,newValue);
pos += newValue.length();
count++;
}
return count;
}
bool startsWith(const std::wstring& str, const wchar_t* value, size_t stringPos)
{
while (*value != 0 && stringPos < str.size())
{
if (str[stringPos++] != *value++)
return false;
}
return *value == 0;
}
bool isAbsolutePath(const std::wstring& path)
{
#ifdef _WIN32
return path.size() > 2 && (path[1] == ':' || (path[0] == '\\' && path[1] == '\\'));
#else
return path.size() >= 1 && path[0] == '/';
#endif
}