Repository navigation
Expand file tree
/
Copy pathFormattingWide.cpp
More file actions
420 lines (393 loc) · 18.5 KB
/
Copy pathFormattingWide.cpp
File metadata and controls
420 lines (393 loc) · 18.5 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
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
#include <cerrno>
#include <climits>
#include <cstdint>
#include <cstddef>
#include <cstdio>
#include <cstdarg>
#include <cstring>
#include <stdexcept>
#include <string>
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/FileStream.hpp"
#include "prx/libc/include/WindowsFormatting.hpp"
#ifdef _WIN32
#define APS5_VA_BEGIN(last) __builtin_sysv_va_list args; __builtin_sysv_va_start(args, last)
#define APS5_VA_END() __builtin_sysv_va_end(args)
#else
#define APS5_VA_BEGIN(last) std::va_list args; va_start(args, last)
#define APS5_VA_END() va_end(args)
#endif
namespace {
bool In(char16_t character, const char* set) {
return character != 0 && character < 128 && std::strchr(set, static_cast<char>(character)) != nullptr;
}
void AppendUtf16(std::u16string& out, const char* text, std::size_t limit) {
for (std::size_t index = 0; out.size() < limit && text[index] != '\0';) {
const unsigned char lead = static_cast<unsigned char>(text[index]);
std::size_t extra = lead >= 0xf0 ? 3 : lead >= 0xe0 ? 2 : lead >= 0xc0 ? 1 : 0;
std::uint32_t code = extra == 3 ? lead & 0x07 : extra == 2 ? lead & 0x0f : extra == 1 ? lead & 0x1f : lead;
std::size_t used = 1;
for (; used <= extra; ++used) {
const unsigned char next = static_cast<unsigned char>(text[index + used]);
if ((next & 0xc0) != 0x80) { extra = 0; code = lead; used = 1; break; }
code = (code << 6) | (next & 0x3f);
}
if (extra != 0) used = extra + 1;
index += used;
if (code >= 0x10000) {
if (limit - out.size() < 2) break;
code -= 0x10000;
out.push_back(static_cast<char16_t>(0xd800 + (code >> 10)));
out.push_back(static_cast<char16_t>(0xdc00 + (code & 0x3ff)));
} else out.push_back(static_cast<char16_t>(code));
}
}
std::string ToUtf8(const std::u16string& text) {
std::string out;
for (std::size_t index = 0; index < text.size(); ++index) {
std::uint32_t code = text[index];
if (code >= 0xd800 && code < 0xdc00 && index + 1 < text.size() && text[index + 1] >= 0xdc00 && text[index + 1] < 0xe000)
code = 0x10000 + ((code - 0xd800) << 10) + (text[++index] - 0xdc00);
if (code < 0x80) out.push_back(static_cast<char>(code));
else if (code < 0x800) {
out.push_back(static_cast<char>(0xc0 | (code >> 6)));
out.push_back(static_cast<char>(0x80 | (code & 0x3f)));
} else if (code < 0x10000) {
out.push_back(static_cast<char>(0xe0 | (code >> 12)));
out.push_back(static_cast<char>(0x80 | ((code >> 6) & 0x3f)));
out.push_back(static_cast<char>(0x80 | (code & 0x3f)));
} else {
out.push_back(static_cast<char>(0xf0 | (code >> 18)));
out.push_back(static_cast<char>(0x80 | ((code >> 12) & 0x3f)));
out.push_back(static_cast<char>(0x80 | ((code >> 6) & 0x3f)));
out.push_back(static_cast<char>(0x80 | (code & 0x3f)));
}
}
return out;
}
template<class TValue> void AppendNumber(std::u16string& out, const std::string& spec, TValue value) {
const int size = std::snprintf(nullptr, 0, spec.c_str(), value);
if (size < 0) throw std::runtime_error("Formatting conversion failed");
std::string text(static_cast<std::size_t>(size) + 1, '\0');
std::snprintf(&text[0], text.size(), spec.c_str(), value);
out.append(text.begin(), text.begin() + size);
}
void AppendPadded(std::u16string& out, const std::u16string& text, bool left, int width) {
const std::size_t pad = width > 0 && static_cast<std::size_t>(width) > text.size() ? static_cast<std::size_t>(width) - text.size() : 0;
if (!left) out.append(pad, u' ');
out += text;
if (left) out.append(pad, u' ');
}
std::u16string FormatWide(const char16_t* format, VaList* source, bool secure = false) {
if (format == nullptr || source == nullptr) throw std::invalid_argument("Null formatting argument");
LibcDetail::FormatArguments args(source);
const char16_t* const start = format;
std::string ascii;
for (const char16_t* unit = format; *unit != 0; ++unit) ascii.push_back(*unit < 0x80 ? static_cast<char>(*unit) : '?');
args.Number(LibcDetail::NumberedArgumentClasses(ascii.c_str()));
const auto position = [&] {
const char* cursor = ascii.c_str() + (format - start);
const std::size_t found = LibcDetail::ArgumentPosition(cursor);
format = start + (cursor - ascii.c_str());
return found;
};
const auto star = [&] {
const std::size_t found = position();
return found ? args.At<int>(found) : args.Next<int>();
};
std::u16string out;
while (*format) {
if (*format != u'%') { out.push_back(*format++); continue; }
++format;
if (*format == u'%') { out.push_back(*format++); continue; }
if (const std::size_t found = position()) args.Seek(found);
std::string spec = "%";
bool left = false;
int width = 0;
int precision = -1;
while (In(*format, "-+ #0'")) {
if (*format == u'-') left = true;
if (*format != u'\'') spec += static_cast<char>(*format);
++format;
}
if (*format == u'*') {
++format;
width = star();
if (width < 0) { left = true; spec += '-'; width = -width; }
spec += std::to_string(width);
} else {
while (*format >= u'0' && *format <= u'9') { width = width * 10 + (*format - u'0'); spec += static_cast<char>(*format++); }
}
if (*format == u'.') {
++format;
precision = 0;
if (*format == u'*') {
++format;
precision = star();
if (precision >= 0) spec += "." + std::to_string(precision);
} else {
spec += '.';
while (*format >= u'0' && *format <= u'9') { precision = precision * 10 + (*format - u'0'); spec += static_cast<char>(*format++); }
}
}
std::string length;
if (In(*format, "hljztL")) {
length += static_cast<char>(*format++);
if ((length == "h" && *format == u'h') || (length == "l" && *format == u'l')) length += static_cast<char>(*format++);
}
const char16_t conversion = *format;
if (!conversion) throw std::invalid_argument("Incomplete format conversion");
++format;
const bool integerLength = length.empty() || length == "h" || length == "hh" || length == "l" || length == "ll" ||
length == "j" || length == "z" || length == "t";
if (conversion == u'd' || conversion == u'i') {
if (!integerLength) throw std::invalid_argument("Invalid integer length");
long long value;
if (length.empty() || length == "h" || length == "hh") {
value = args.Next<int>();
if (length == "h") value = static_cast<short>(value);
if (length == "hh") value = static_cast<signed char>(value);
} else value = args.Next<long long>();
AppendNumber(out, spec + "ll" + static_cast<char>(conversion), value);
} else if (In(conversion, "ouxX")) {
if (!integerLength) throw std::invalid_argument("Invalid integer length");
unsigned long long value;
if (length.empty() || length == "h" || length == "hh") {
value = args.Next<unsigned int>();
if (length == "h") value = static_cast<unsigned short>(value);
if (length == "hh") value = static_cast<unsigned char>(value);
} else value = args.Next<unsigned long long>();
if (conversion == u'o' && value == 0 && precision == 0 && spec.find('#') != std::string::npos)
spec = spec.substr(0, spec.find('.')) + ".1";
AppendNumber(out, spec + "ll" + static_cast<char>(conversion), value);
} else if (In(conversion, "aAeEfFgG")) {
if (length == "L") AppendNumber(out, spec + "L" + static_cast<char>(conversion), args.Next<long double>());
else {
if (!length.empty() && length != "l") throw std::invalid_argument("Invalid floating length");
AppendNumber(out, spec + static_cast<char>(conversion), args.Next<double>());
}
} else if (conversion == u'p' && length.empty()) {
const std::string text = LibcDetail::FormatPointer(spec, args.Next<void*>());
out.append(text.begin(), text.end());
} else if (conversion == u'c' || conversion == u'C') {
const int value = args.Next<int>();
std::u16string text;
if (conversion == u'C' || length == "l") text.push_back(static_cast<char16_t>(value));
else text.push_back(static_cast<char16_t>(static_cast<unsigned char>(value)));
AppendPadded(out, text, left, width);
} else if (conversion == u's' || conversion == u'S') {
std::u16string text;
const std::size_t limit = precision < 0 ? SIZE_MAX : static_cast<std::size_t>(precision);
if (conversion == u'S' || length == "l") {
const char16_t* value = args.Next<const char16_t*>();
if (value == nullptr && secure) throw std::invalid_argument("Null string argument");
if (value == nullptr) value = u"(null)";
for (std::size_t index = 0; index < limit && value[index] != 0; ++index) text.push_back(value[index]);
} else {
const char* value = args.Next<const char*>();
if (value == nullptr && secure) throw std::invalid_argument("Null string argument");
AppendUtf16(text, value != nullptr ? value : "(null)", limit);
}
AppendPadded(out, text, left, width);
} else if (conversion == u'n' && integerLength && spec == "%" && !secure) {
void* pointer = args.Next<void*>();
if (!pointer) throw std::invalid_argument("Null format count pointer");
const int count = static_cast<int>(out.size());
if (length == "hh") *static_cast<signed char*>(pointer) = static_cast<signed char>(count);
else if (length == "h") *static_cast<short*>(pointer) = static_cast<short>(count);
else if (length.empty()) *static_cast<int*>(pointer) = count;
else *static_cast<long long*>(pointer) = count;
} else {
throw std::invalid_argument("Unsupported format conversion");
}
}
return out;
}
} // namespace
extern "C" {
extern FileStream* __stdoutp_nid_postfix;
std::int32_t APS5_VABI fgetwc_nid_postfix(FileStream* stream);
int APS5_VABI ferror_nid_postfix(FileStream* stream);
int APS5_VABI vswprintf_nid_postfix(char16_t* buffer, std::size_t size, const char16_t* format, VaList* args) {
if (buffer == nullptr || size == 0) { errno = 22; return -1; }
try {
const std::u16string text = FormatWide(format, args);
const std::size_t copied = text.size() < size - 1 ? text.size() : size - 1;
std::memcpy(buffer, text.data(), copied * sizeof(char16_t));
buffer[copied] = 0;
return copied == text.size() ? static_cast<int>(copied) : -1;
} catch (const std::exception&) {
buffer[0] = 0;
errno = 22;
return -1;
}
}
int APS5_VABI swprintf_nid_postfix(char16_t* buffer, std::size_t size, const char16_t* format, ...) {
APS5_VA_BEGIN(format);
const int result = vswprintf_nid_postfix(buffer, size, format, reinterpret_cast<VaList*>(args));
APS5_VA_END();
return result;
}
int APS5_VABI snwprintf_s_nid_postfix(char16_t* buffer, std::size_t size, const char16_t* format, ...) {
constexpr std::size_t RsizeMax = SIZE_MAX >> 1;
int result = -1;
if (buffer != nullptr && format != nullptr && size != 0 && size <= RsizeMax) {
APS5_VA_BEGIN(format);
try {
const std::u16string text = FormatWide(format, reinterpret_cast<VaList*>(args), true);
if (text.size() <= static_cast<std::size_t>(INT_MAX)) {
const std::size_t copied = text.size() < size - 1 ? text.size() : size - 1;
std::memcpy(buffer, text.data(), copied * sizeof(char16_t));
buffer[copied] = 0;
result = static_cast<int>(text.size());
}
} catch (const std::invalid_argument&) {
}
APS5_VA_END();
}
if (result < 0 && buffer != nullptr && size != 0 && size < RsizeMax) buffer[0] = 0;
return result;
}
int APS5_VABI vswprintf_s_nid_postfix(char16_t* buffer, std::size_t size, const char16_t* format, VaList* args) {
constexpr std::size_t RsizeMax = (SIZE_MAX >> 1) / sizeof(char16_t);
int result = -1;
if (buffer != nullptr && format != nullptr && args != nullptr && size != 0 && size <= RsizeMax) {
try {
const std::u16string text = FormatWide(format, args, true);
if (text.size() + 1 <= size && text.size() <= static_cast<std::size_t>(INT_MAX)) {
std::memcpy(buffer, text.data(), text.size() * sizeof(char16_t));
buffer[text.size()] = 0;
result = static_cast<int>(text.size());
}
} catch (const std::invalid_argument&) {
}
}
if (result < 0 && buffer != nullptr && size != 0 && size <= RsizeMax) buffer[0] = 0;
return result;
}
int APS5_VABI swprintf_s_nid_postfix(char16_t* buffer, std::size_t size, const char16_t* format, ...) {
constexpr std::size_t RsizeMax = (SIZE_MAX >> 1) / sizeof(char16_t);
int result = -1;
if (buffer != nullptr && format != nullptr && size != 0 && size <= RsizeMax) {
APS5_VA_BEGIN(format);
try {
const std::u16string text = FormatWide(format, reinterpret_cast<VaList*>(args), true);
if (text.size() + 1 <= size && text.size() <= static_cast<std::size_t>(INT_MAX)) {
std::memcpy(buffer, text.data(), text.size() * sizeof(char16_t));
buffer[text.size()] = 0;
result = static_cast<int>(text.size());
}
} catch (const std::invalid_argument&) {
}
APS5_VA_END();
}
if (result < 0 && buffer != nullptr && size != 0 && size <= RsizeMax) buffer[0] = 0;
return result;
}
int APS5_VABI vsnwprintf_s_nid_postfix(char16_t* buffer, std::size_t size, const char16_t* format, VaList* args) {
constexpr std::size_t RsizeMax = (SIZE_MAX >> 1) / sizeof(char16_t);
int result = -1;
if (buffer != nullptr && format != nullptr && args != nullptr && size != 0 && size <= RsizeMax) {
try {
const std::u16string text = FormatWide(format, args, true);
if (text.size() <= static_cast<std::size_t>(INT_MAX)) {
const std::size_t copied = text.size() < size - 1 ? text.size() : size - 1;
std::memcpy(buffer, text.data(), copied * sizeof(char16_t));
buffer[copied] = 0;
result = static_cast<int>(text.size());
}
} catch (const std::invalid_argument&) {
}
}
if (result < 0 && buffer != nullptr && size != 0 && size <= RsizeMax) buffer[0] = 0;
return result;
}
int APS5_VABI wprintf_nid_postfix(const char16_t* format, ...) {
APS5_VA_BEGIN(format);
int result = -1;
try {
const std::u16string text = FormatWide(format, reinterpret_cast<VaList*>(args));
const std::string bytes = ToUtf8(text);
if (std::fwrite(bytes.data(), 1, bytes.size(), GetNativeStream(__stdoutp_nid_postfix)) == bytes.size()) result = static_cast<int>(text.size());
} catch (const std::exception&) {
errno = 22;
}
APS5_VA_END();
return result;
}
int APS5_VABI vfwprintf_nid_postfix(FileStream* stream, const char16_t* format, VaList* args) {
if (stream == nullptr || format == nullptr) { errno = 22; return -1; }
if (!stream->IsOpen()) { errno = 9; return -1; }
int result = -1;
try {
const std::u16string text = FormatWide(format, args);
const std::string bytes = ToUtf8(text);
if (std::fwrite(bytes.data(), 1, bytes.size(), GetNativeStream(stream)) == bytes.size()) result = static_cast<int>(text.size());
} catch (const std::exception&) {
errno = 22;
}
stream->SyncStatus();
return result;
}
int APS5_VABI fwprintf_nid_postfix(FileStream* stream, const char16_t* format, ...) {
APS5_VA_BEGIN(format);
const int result = vfwprintf_nid_postfix(stream, format, reinterpret_cast<VaList*>(args));
APS5_VA_END();
return result;
}
int APS5_VABI vwprintf_nid_postfix(const char16_t* format, VaList* args) {
return vfwprintf_nid_postfix(__stdoutp_nid_postfix, format, args);
}
int APS5_VABI fputwc_nid_postfix(char16_t value, FileStream* stream) {
if (stream == nullptr) { errno = 22; return -1; }
try {
const std::string bytes = ToUtf8(std::u16string(1, value));
auto* native = GetNativeStream(stream);
if (std::fwrite(bytes.data(), 1, bytes.size(), native) != bytes.size()) throw std::runtime_error("stream write failed");
stream->SyncStatus();
return value;
} catch (const std::exception&) {
errno = 22;
return -1;
}
}
int APS5_VABI fputws_nid_postfix(const char16_t* str, FileStream* stream) {
if (str == nullptr || stream == nullptr) { errno = 22; return -1; }
try {
const std::u16string text(str);
const std::string bytes = ToUtf8(text);
auto* native = GetNativeStream(stream);
if (std::fwrite(bytes.data(), 1, bytes.size(), native) != bytes.size()) throw std::runtime_error("stream write failed");
stream->SyncStatus();
return static_cast<int>(text.size());
} catch (const std::exception&) {
errno = 22;
return -1;
}
}
int APS5_VABI putwc_nid_postfix(char16_t value, FileStream* stream) {
return fputwc_nid_postfix(value, stream);
}
int APS5_VABI putwchar_nid_postfix(char16_t value) {
return fputwc_nid_postfix(value, __stdoutp_nid_postfix);
}
char16_t* APS5_VABI fgetws_nid_postfix(char16_t* buffer, int size, FileStream* stream) {
if (size < 2) return nullptr;
if (buffer == nullptr || stream == nullptr) {
if (stream != nullptr) stream->SetError();
errno = 22;
return nullptr;
}
int count = 0;
while (count < size - 1) {
const std::int32_t character = fgetwc_nid_postfix(stream);
if (character == -1) break;
buffer[count++] = static_cast<char16_t>(character);
if (character == static_cast<std::int32_t>(u'\n')) break;
}
if (count == 0) return nullptr;
buffer[count] = 0;
return buffer;
}
}