forked from ParadoxGameConverters/commonItems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinuxUtils.cpp
634 lines (545 loc) · 15.8 KB
/
LinuxUtils.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#include "Log.h"
#include "OSCompatibilityLayer.h"
#include <algorithm>
#include <cerrno>
#include <cstdarg>
#include <cstdlib>
#include <cstring>
#include <dirent.h>
#include <fcntl.h>
#include <filesystem>
#include <iconv.h>
#include <iostream>
#include <set>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
namespace fs = std::filesystem;
void sprintf_s_Linux(char* __restrict __s, size_t __maxlength, const char* __restrict __format, ...)
{
va_list arg_ptr;
va_start(arg_ptr, __format);
snprintf(__s, __maxlength, __format, arg_ptr);
va_end(arg_ptr);
}
void strcpy_s_Linux(char* __restrict __dest, const char* __restrict __src)
{
strcpy(__dest, __src);
}
int fopen_s_Linux(FILE** file, const char* filename, const char* mode)
{
*file = fopen(filename, mode);
return *file == NULL;
}
void fprintf_s_Linux(FILE* file, const char* format, ...)
{
va_list arg_ptr;
va_start(arg_ptr, format);
fprintf(file, format, arg_ptr);
va_end(arg_ptr);
}
#ifdef __linux__
HANDLE GetStdHandle(int nothing)
{
return 1;
}
#endif
namespace commonItems
{
/*
Helper function to determine that returns false if the filename is a:
- a hidden folder or folder (starting with '.')
- empty
- a parent directory ('.')
- the current directory ('..')
*/
bool IsRegularNodeName(const std::string& name)
{
return !name.empty() && *name.begin() != '.';
}
/*
returns false if a node name is empty or a placeholder for the current or parent directory, true otherwise
*/
bool IsActualNodeName(const std::string& name)
{
for (auto i = name.begin(); i != name.end(); ++i)
{
if (*i != '.')
{
return true;
}
}
return false;
}
const char* StripTrailingSeparators(const char* path, std::size_t length)
{
switch (length)
{
case 0:
return path;
case 1:
if (*path != '/')
{
++path;
}
return path;
default:
const char* i = path + length - 1;
while (i != path && *i == '/')
{
--i;
}
++i;
return i;
}
}
const char* StripLeadingSeparators(const char* path)
{
while (true)
{
if (*path != '/')
{
return path;
}
++path;
}
}
/*
Concatenates two paths and strips out leading and trailing '/'
*/
std::string ConcatenatePaths(const std::string& first, const std::string& second)
{
const char* first_begin = first.c_str();
const char* first_end = StripTrailingSeparators(first_begin, first.length());
const char* second_begin = StripLeadingSeparators(second.c_str());
return std::string(first_begin, first_end) + "/" + std::string(second_begin);
}
/*
Concatenates a path and a valid node name
A valid node name is a non empty string that does not contain '/'
use ConcatenatePaths for other concatenations
*/
std::string ConcatenateNodeName(const std::string& path, const std::string& name)
{
const char* path_begin = path.c_str();
const char* path_end = StripTrailingSeparators(path_begin, path.length());
return std::string(path_begin, path_end) + "/" + name;
}
/*
Splits the node name from the rest of the path and returns both values as a pair (path, node_name)
*/
std::pair<std::string, std::string> SplitNodeNameFromPath(const std::string& path)
{
if (path.empty())
{
return std::make_pair(std::string(), std::string());
}
const char* buffer = path.c_str();
const char* end = buffer + path.length() - 1;
while (end != buffer && *end == '/')
{
--end;
}
if (end == buffer)
{
return std::make_pair(std::string(buffer, buffer + 1), std::string());
}
const char* begin = end;
++end;
while (begin != buffer && *begin != '/')
{
--begin;
}
if (*begin == '/')
{
++begin;
}
return std::make_pair(std::string(buffer, begin), std::string(begin, end));
}
bool IsLinuxPathElementSeparator(char c)
{
return c == '/';
}
bool isLinuxPathCharacter(char c)
{
return c != '/';
}
char* CopyFolderPathElement(const char*& input_begin, const char* input_end, char* output)
{
while (input_begin != input_end)
{
const char c = *input_begin;
++input_begin;
if (c == '/')
{
return output;
}
*output = c;
++output;
}
return output;
}
bool GetFileMode(const std::string& path, mode_t& result)
{
struct stat status;
if (stat(path.c_str(), &status) != 0)
{
return false;
}
result = status.st_mode;
return true;
}
bool IsRegularFile(const std::string& path)
{
return !fs::is_directory(fs::u8path(path));
}
bool IsDirectory(const std::string& path)
{
return fs::is_directory(fs::u8path(path));
}
/*
Note: since the function signature did not allow for a return value, it clears the fileNames set when an
error occurs to make sure no operations are done on an incomplete list of files
*/
std::set<std::string> GetAllFilesInFolderRecursive(const std::string& path)
{
auto validatedPath = path;
if (validatedPath.ends_with('/') || validatedPath.ends_with('\\'))
validatedPath = validatedPath.substr(0, validatedPath.size() - 1); // remove the trailing slash
const auto origPathStr = fs::u8path(validatedPath).native();
if (const auto tempPath = fs::u8path(path); !fs::exists(tempPath) || !fs::is_directory(tempPath))
{
return {};
}
std::set<std::string> fileNames;
for (const auto& p: fs::recursive_directory_iterator(fs::u8path(path)))
{
if (!p.is_directory())
{
const auto currentPath = p.path().native();
const auto requestedPath = currentPath.substr(origPathStr.length() + 1, currentPath.length() - origPathStr.length() - 1);
fileNames.insert(requestedPath);
}
}
return fileNames;
}
void WriteToConsole(LogLevel level, const std::string& logMessage)
{
if (level != LogLevel::Debug) // Don't log debug messages to console.
{
std::cout << logMessage;
}
}
/*
Implemented all messages for error codes used in LinuxUtils.cpp.
*/
std::string GetLastErrorString()
{
return strerror(errno);
}
bool deleteFile(const std::string& unresolvedFile)
{
fs::remove(fs::u8path(unresolvedFile));
return true;
}
/*
* Forward declarations for conversion helper function and types
*/
class ConversionInputBuffer;
class ConversionOutputBuffer;
bool ConvertString(const char* toCode, const char* fromCode, ConversionInputBuffer& in, ConversionOutputBuffer& out);
/*
* helper class to copy buffer from string and keep track of remainder and current position
*/
class ConversionInputBuffer
{
char* data;
char* in_buffer;
std::size_t remainder;
public:
explicit ConversionInputBuffer(const std::string& input): data(new char[input.length()]), in_buffer(data), remainder(input.length())
{
// POSIX iconv expects a pointer to char *, not to const char * and consequently does not guarantee that the input
// sequence is not modified so we copy it into the buffer before attempting conversion
std::copy(input.begin(), input.end(), data);
}
ConversionInputBuffer(const ConversionInputBuffer&) = delete;
ConversionInputBuffer& operator=(const ConversionInputBuffer&) = delete;
ConversionInputBuffer(ConversionInputBuffer&&) = delete;
ConversionInputBuffer& operator=(ConversionInputBuffer&&) = delete;
template <typename String> explicit ConversionInputBuffer(const String& input)
{
typedef typename String::value_type Char;
remainder = sizeof(Char) * input.length();
data = new char[remainder];
in_buffer = data;
const auto* const input_str = reinterpret_cast<const char*>(input.c_str());
std::copy(input_str, input_str + remainder, data);
}
~ConversionInputBuffer() { delete[] data; }
[[nodiscard]] bool has_remaining_bytes() const { return remainder != 0; }
friend bool ConvertBuffer(const char* fromCode, const char* toCode, ConversionInputBuffer& from, ConversionOutputBuffer& to);
};
/*
* helper class to manage buffer pointers and sizes while providing access to internal buffers for use in iconv
* functions
*/
class ConversionOutputBuffer
{
std::size_t size;
std::size_t remainder;
std::size_t block_size;
char* data = nullptr;
char* out_buffer = nullptr;
template <typename String> struct OutputStrHelper
{
typedef typename String::value_type Char;
static void str(String& output, const char* buffer, std::size_t length)
{
size_t output_length = length / sizeof(Char);
wchar_t output_buffer[output_length];
memset(output_buffer, 0, output_length * sizeof(wchar_t));
std::mbstowcs(output_buffer, buffer, output_length);
output.assign(output_buffer, output_length);
}
};
template <typename Traits, typename Alloc> struct OutputStrHelper<std::basic_string<char, Traits, Alloc>>
{
static void str(std::basic_string<char, Traits, Alloc>& output, char* buffer, std::size_t length) { output.assign(buffer, length); }
};
public:
explicit ConversionOutputBuffer(std::size_t initial_size = 0, std::size_t increment_block_size = 1024 * 1024):
size(initial_size), remainder(initial_size), block_size(increment_block_size)
{
if (size != 0)
{
data = new char[size];
out_buffer = data;
}
}
ConversionOutputBuffer(const ConversionOutputBuffer&) = delete;
ConversionOutputBuffer& operator=(const ConversionOutputBuffer&) = delete;
ConversionOutputBuffer(ConversionOutputBuffer&&) = delete;
ConversionOutputBuffer& operator=(ConversionOutputBuffer&&) = delete;
~ConversionOutputBuffer() { delete[] data; }
[[nodiscard]] bool full() const { return remainder != 0; }
[[nodiscard]] size_t length() const { return size - remainder; }
[[nodiscard]] std::string str() const { return std::string(data, data + length()); }
template <typename String> void str(String& output) const { OutputStrHelper<String>::str(output, data, length()); }
bool ensure_capacity(std::size_t capacity)
{
if (size < capacity)
{
const size_t len = length();
char* new_data = new char[capacity];
std::copy(data, data + len, new_data);
delete[] data;
data = new_data;
out_buffer = data + len;
remainder += capacity - size;
size = capacity;
return true;
}
return false;
}
bool grow() { return ensure_capacity(size + block_size); }
friend bool ConvertBuffer(const char* fromCode, const char* toCode, ConversionInputBuffer& from, ConversionOutputBuffer& to);
};
bool ConvertBuffer(const char* fromCode, const char* toCode, ConversionInputBuffer& from, ConversionOutputBuffer& to)
{
iconv_t descriptor = iconv_open(toCode, fromCode);
if (descriptor == reinterpret_cast<iconv_t>(-1))
{
LOG(LogLevel::Error) << "unable to recode string from '" << fromCode << "' to '" << toCode << ": not supported on this system";
return false;
}
while (from.has_remaining_bytes())
{
if (iconv(descriptor, &from.in_buffer, &from.remainder, &to.out_buffer, &to.remainder) == static_cast<size_t>(-1))
{
switch (errno)
{
case E2BIG:
to.grow();
break;
case EILSEQ:
LOG(LogLevel::Error) << "invalid input sequence encountered during conversion of " << from.data << " from " << fromCode << " to " << toCode
<< " : " << std::hex << *reinterpret_cast<uint16_t*>(from.in_buffer);
return false;
case EINVAL:
LOG(LogLevel::Error) << "incomplete input sequence encountered during conversion from " << fromCode << " to " << toCode;
return false;
default:
break;
}
}
}
iconv_close(descriptor);
return true;
}
/*
* InputString and OutputString should be instantiations of basic_string, or have a similar API
* InputString should implement:
* member type value_type
* member function value_type begin() const
* member function value_type end() const
* member function size_t length() const
*
*/
template <typename InputString, typename OutputString>
bool ConvertString(const char* fromCode, const char* toCode, const InputString& from, OutputString& to, std::size_t to_buffer_size = 0)
{
if (to_buffer_size == 0)
{
to_buffer_size = from.length();
}
ConversionInputBuffer from_buffer(from);
ConversionOutputBuffer to_buffer(to_buffer_size);
if (ConvertBuffer(fromCode, toCode, from_buffer, to_buffer))
{
to_buffer.str(to);
return true;
}
return false;
}
template <typename InputString, typename OutputString>
OutputString ConvertString(const char* fromCode, const char* toCode, const InputString& from, std::size_t to_buffer_size = 0)
{
OutputString to;
ConvertString(fromCode, toCode, from, to, to_buffer_size);
return to;
}
std::string convertUTF8ToASCII(const std::string& UTF8)
{
return ConvertString<std::string, std::string>("UTF-8", "ASCII//TRANSLIT", UTF8);
}
std::string convertUTF8To8859_15(const std::string& UTF8)
{
return ConvertString<std::string, std::string>("UTF−8", "ISO−8859−15//TRANSLIT", UTF8);
}
std::string convertUTF8ToWin1252(const std::string& UTF8)
{
return ConvertString<std::string, std::string>("UTF−8", "CP1252//TRANSLIT", UTF8);
}
std::string convertUTF8ToWin1251(const std::string& UTF8)
{
return ConvertString<std::string, std::string>("UTF−8", "CP1251//TRANSLIT", UTF8);
}
std::string convertUTF8ToWin1250(const std::string& UTF8)
{
return ConvertString<std::string, std::string>("UTF−8", "CP1250//TRANSLIT", UTF8);
}
std::string convert8859_15ToUTF8(const std::string& input)
{
return ConvertString<std::string, std::string>("ISO−8859−15", "UTF-8//TRANSLIT", input);
}
/*
Warning: Does not actually return an UTF-16 sequence.
This is an implementation of the original Windows-based API which uses UTF-16 LE as the system dependent wchar_t
encoding This behaviour is replicated on Linux but it uses the (system dependent) wchar_t encoding.
*/
std::wstring convert8859_15ToUTF16(const std::string& input)
{
return ConvertString<std::string, std::wstring>("ISO−8859−15", "wchar_t//TRANSLIT", input);
}
std::string convertWin1250ToUTF8(const std::string& Win1250)
{
return ConvertString<std::string, std::string>("CP1250", "UTF-8//TRANSLIT", Win1250);
}
std::string convertWin1251ToUTF8(const std::string& Win1251)
{
return ConvertString<std::string, std::string>("CP1251", "UTF-8//TRANSLIT", Win1251);
}
std::string convertWin1252ToUTF8(const std::string& Win1252)
{
return ConvertString<std::string, std::string>("CP1252", "UTF-8//TRANSLIT", Win1252);
}
std::wstring convertWin1252ToUTF16(const std::string& Win1252)
{
return ConvertString<std::string, std::wstring>("CP1252", "wchar_t//TRANSLIT", Win1252);
}
/*
Warning: Does not actually return an UTF-16 sequence.
This is an implementation of the original Windows-based API which uses UTF-16 LE as the system dependent wchar_t
encoding This behaviour is replicated on Linux but it uses the (system dependent) wchar_t encoding.
*/
std::wstring convertUTF8ToUTF16(const std::string& UTF8)
{
std::vector<unsigned long> unicode;
for (size_t i = 0; i < UTF8.size();)
{
uint32_t uni = '\0';
size_t todo = 0;
unsigned char ch = UTF8[i++];
if (ch <= 0x7F)
{
uni = ch;
todo = 0;
}
else if (ch <= 0xBF)
{
throw std::logic_error("not a UTF-8 string");
}
else if (ch <= 0xDF)
{
uni = ch & 0x1F;
todo = 1;
}
else if (ch <= 0xEF)
{
uni = ch & 0x0F;
todo = 2;
}
else if (ch <= 0xF7)
{
uni = ch & 0x07;
todo = 3;
}
else
{
throw std::logic_error("not a UTF-8 string");
}
for (size_t j = 0; j < todo; ++j)
{
if (i == UTF8.size())
throw std::logic_error("not a UTF-8 string");
ch = UTF8[i++];
if (ch < 0x80 || ch > 0xBF)
throw std::logic_error("not a UTF-8 string");
uni <<= 6;
uni += ch & 0x3F;
}
if (uni >= 0xD800 && uni <= 0xDFFF)
throw std::logic_error("not a UTF-8 string");
if (uni > 0x10FFFF)
throw std::logic_error("not a UTF-8 string");
unicode.push_back(uni);
}
std::wstring utf16;
for (size_t i = 0; i < unicode.size(); ++i)
{
unsigned long uni = unicode[i];
if (uni <= 0xFFFF)
{
utf16 += static_cast<wchar_t>(uni);
}
else
{
uni -= 0x10000;
utf16 += static_cast<wchar_t>((uni >> 10) + 0xD800);
utf16 += static_cast<wchar_t>((uni & 0x3FF) + 0xDC00);
}
}
return utf16;
}
std::string convertToUTF8(const std::wstring& input)
{
return ConvertString<std::wstring, std::string>("wchar_t", "UTF-8//TRANSLIT", input);
}
std::optional<std::wstring> getSteamInstallPath(const std::string& steamID)
{
// TODO: Write some actual code for this when able.
return std::nullopt;
}
} // namespace commonItems