forked from beefytech/Beef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.cpp
1282 lines (1116 loc) · 27.7 KB
/
Common.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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Common.h"
#include "util/BumpAllocator.h"
#include "util/UTF8.h"
#include "util/String.h"
//#include "farts.z"
#include <string.h>
#include "platform/PlatformHelper.h"
#ifndef BF_SMALL
#define STB_SPRINTF_DECORATE(name) BF_stbsp_##name
#define STB_SPRINTF_IMPLEMENTATION
#include "third_party/stb/stb_sprintf.h"
#endif
extern "C"
{
#include "third_party/utf8proc/utf8proc.h"
}
#ifdef BF_PLATFORM_WINDOWS
#include <shellapi.h>
#include <direct.h>
#endif
#pragma warning(disable:4996)
#pragma comment(lib, "winmm.lib")
int gBFArgC;
char** gBFArgV;
#ifdef BUMPALLOC_ETW_TRACING
VSHeapTracker::CHeapTracker* Beefy::BumpAllocator::mVSHeapTracker = NULL;
#pragma comment(lib, "VSCustomNativeHeapEtwProvider.lib")
#endif
USING_NS_BF;
UTF16String Beefy::ToWString(const StringImpl& theString)
{
UTF16String aString;
aString.Reserve(theString.length() + 1);
for (intptr i = 0; i < theString.length(); ++i)
aString.Add((unsigned char)theString[i]);
aString.Add(0);
return aString;
}
// String Beefy::ToString(const UTF16String& theString)
// {
// String newString;
// newString.Reserve((intptr)theString.length());
// for (int i = 0; i < (int)theString.length(); ++i)
// {
// const unsigned int c = (unsigned int)theString[i];
// newString.Append(char(c & 0xFF));
// }
// return newString;
// }
String Beefy::ToUpper(const StringImpl& theString)
{
String aString = theString;
for (int i = 0; i < (int)aString.length(); ++i)
aString[i] = toupper(aString[i]);
return aString;
}
UTF16String Beefy::ToUpper(const UTF16String& theString)
{
UTF16String aString = theString;
for (int i = 0; i < (int)aString.length(); ++i)
aString[i] = toupper(aString[i]);
return aString;
}
UTF16String Beefy::ToLower(const UTF16String& theString)
{
UTF16String aString = theString;
for (int i = 0; i < (int)aString.length(); ++i)
aString[i] = tolower(aString[i]);
return aString;
}
String Beefy::ToLower(const StringImpl& theString)
{
String aString = theString;
for (int i = 0; i < (int)aString.length(); ++i)
aString[i] = (char)tolower((int)(uint8)aString[i]);
return aString;
}
// UTF16String Beefy::Trim(const UTF16String& theString)
// {
// int left = 0;
// while ((left < (int) theString.length() - 1) && (iswspace(theString[left])))
// left++;
//
// int right = (int)theString.length() - 1;
// while ((right >= left) && (iswspace(theString[right])))
// right--;
//
// if ((left == 0) && (right == theString.length() - 1))
// return theString;
//
// return theString.substr(left, right - left + 1);
// }
String Beefy::Trim(const StringImpl& theString)
{
int left = 0;
while ((left < (int) theString.length() - 1) && (iswspace(theString[left])))
left++;
int right = (int)theString.length() - 1;
while ((right >= left) && (iswspace(theString[right])))
right--;
if ((left == 0) && (right == theString.length() - 1))
return theString;
return theString.Substring(left, right - left + 1);
}
bool Beefy::StrReplace(StringImpl& str, const StringImpl& from, const StringImpl& to)
{
str.Replace(from, to);
return true;
}
bool Beefy::StrStartsWith(const StringImpl& str, const StringImpl& subStr)
{
if (subStr.length() < str.length())
return false;
return strncmp(str.c_str(), subStr.c_str(), subStr.length()) == 0;
}
bool Beefy::StrEndsWith(const StringImpl& str, const StringImpl& subStr)
{
if (subStr.length() < str.length())
return false;
return strncmp(str.c_str() - (str.length() - subStr.length()), subStr.c_str(), subStr.length()) == 0;
}
#ifndef BF_SMALL
String Beefy::SlashString(const StringImpl& str, bool utf8decode, bool utf8encode, bool beefString)
{
bool prevEndedInSlashedNum = false;
String outStr;
bool noNextHex = false;
bool lastWasVisibleChar = false;
for (int idx = 0; idx < (int)str.length(); idx++)
{
bool endingInSlashedNum = false;
char c = str[idx];
if (noNextHex)
{
if (((c >= '0') && (c <= '9')) ||
((c >= 'a') && (c <= 'f')) ||
((c >= 'A') && (c <= 'F')))
{
outStr += "\"\"";
}
noNextHex = false;
}
bool isVisibleChar = false;
switch (c)
{
case '\0':
outStr += "\\0";
endingInSlashedNum = true;
break;
case '\a':
outStr += "\\a";
break;
case '\b':
outStr += "\\b";
break;
case '\t':
outStr += "\\t";
break;
case '\n':
outStr += "\\n";
break;
case '\v':
outStr += "\\v";
break;
case '\f':
outStr += "\\f";
break;
case '\r':
outStr += "\\r";
break;
case '\"':
outStr += "\\\"";
break;
case '\\':
outStr += "\\\\";
break;
default:
{
if ((c >= '0') && (c <= '9'))
{
// Need to break string to allow proper evaluation of slashed string
if (prevEndedInSlashedNum)
outStr += "\" \"";
}
// May be UTF8 byte. Make sure it's valid before we add it, otherwise write the bytes in direct "\x" style
if (((uint8)c >= 0x80) && (utf8decode))
{
int seqLen = u8_seqlen((char*)&str[idx]);
if (seqLen > 1)
{
if (idx + seqLen <= str.length())
{
uint32 c32 = u8_toucs((char*)&str[idx], (int)str.length() - idx);
bool wantHex = false;
// Combining mark without previous visible char?
if ((((c32 >= L'\u0300') && (c32 <= L'\u036F')) || ((c32 >= L'\u1DC0') && (c32 <= L'\u1DFF'))) &&
(!lastWasVisibleChar))
wantHex = true;
utf8proc_category_t cat = utf8proc_category(c32);
switch (cat)
{
case UTF8PROC_CATEGORY_ZS:
case UTF8PROC_CATEGORY_ZL:
case UTF8PROC_CATEGORY_ZP:
case UTF8PROC_CATEGORY_CC:
case UTF8PROC_CATEGORY_CF:
case UTF8PROC_CATEGORY_CO:
wantHex = true;
break;
default:
break;
}
if (wantHex)
{
if (beefString)
{
char tempStr[32];
sprintf(tempStr, "\\u{%x}", c32);
outStr += tempStr;
idx += seqLen - 1;
continue;
}
else
{
char tempStr[32];
sprintf(tempStr, "\\u%04x", c32);
outStr += tempStr;
idx += seqLen - 1;
noNextHex = true;
continue;
}
}
bool passed = true;
for (int checkOfs = 0; checkOfs < seqLen - 1; checkOfs++)
{
char checkC = str[idx + checkOfs + 1];
if ((checkC & 0xC0) != 0x80) // Has trailing signature (0x10xxxxxx)?
passed = false;
}
if (passed)
{
outStr += c;
for (int checkOfs = 0; checkOfs < seqLen - 1; checkOfs++)
{
char checkC = str[idx + checkOfs + 1];
outStr += checkC;
}
idx += seqLen - 1;
continue;
}
}
}
}
if (((uint8)c >= 0x80) && (utf8encode))
{
outStr += (char)(0xC0 | (((uint8)c & 0xFF) >> 6));
outStr += (char)(0x80 | ((uint8)c & 0x3F));
isVisibleChar = true;
continue;
}
if (((uint8)c >= 32) && ((uint8)c < 0x80))
{
outStr += c;
isVisibleChar = true;
}
else
{
char tempStr[32];
sprintf(tempStr, "\\x%02x", (uint8)c);
outStr += tempStr;
endingInSlashedNum = true;
if (!beefString)
noNextHex = true;
}
}
break;
}
prevEndedInSlashedNum = endingInSlashedNum;
lastWasVisibleChar = isVisibleChar;
}
return outStr;
}
#endif
UTF16String Beefy::UTF8Decode(const StringImpl& theString)
{
UTF16String strOut;
int strLen = 0;
char* cPtr = (char*)theString.c_str();
int lenLeft = (int)theString.length();
while (lenLeft > 0)
{
int seqLen = 0;
uint32 c32 = u8_toucs(cPtr, lenLeft, &seqLen);
if ((c32 >= 0x10000) && (sizeof(wchar_t) == 2))
strLen += 2;
else
strLen += 1;
cPtr += seqLen;
lenLeft -= seqLen;
}
strOut.ResizeRaw(strLen + 1);
strOut[strLen] = 0;
cPtr = (char*)theString.c_str();
uint16* wcPtr = strOut.mVals;
int wcLenLeft = (int)strOut.length() + 1;
lenLeft = (int)theString.length();
while (lenLeft > 0)
{
int seqLen = 0;
uint32 c32 = u8_toucs(cPtr, lenLeft, &seqLen);
if ((c32 >= 0x10000) && (sizeof(wchar_t) == 2))
{
*(wcPtr++) = (wchar_t)(((c32 - 0x10000) >> 10) + 0xD800);
*(wcPtr++) = (wchar_t)(((c32 - 0x10000) & 0x3FF) + 0xDC00);
wcLenLeft -= 2;
}
else
{
*(wcPtr++) = (wchar_t)c32;
wcLenLeft -= 1;
}
cPtr += seqLen;
lenLeft -= seqLen;
}
return strOut;
}
String Beefy::UTF8Encode(const uint16* theString, int length)
{
if (length == 0)
return String();
String strOut;
int utf8Len = 0;
uint16 utf16hi = 0;
for (int i = 0; i < length; i++)
{
uint16 c = theString[i];
uint32 c32 = c;
if ((c >= 0xD800) && (c < 0xDC00))
{
utf16hi = (uint16)c;
continue;
}
else if ((c >= 0xDC00) && (c < 0xE000))
{
uint16 utf16lo = c;
c32 = 0x10000 + ((uint32)(utf16hi - 0xD800) << 10) | (uint32)(utf16lo - 0xDC00);
}
utf8Len += u8_seqlen(c32);
}
strOut.Append('?', utf8Len);
char* cPtr = (char*)&strOut[0];
int lenLeft = (int)strOut.length() + 1;
for (int i = 0; i < length; i++)
{
uint16 c = theString[i];
uint32 c32 = c;
if ((c >= 0xD800) && (c < 0xDC00))
{
utf16hi = (uint16)c;
continue;
}
else if ((c >= 0xDC00) && (c < 0xE000))
{
uint16 utf16lo = c;
c32 = 0x10000 + ((uint32)(utf16hi - 0xD800) << 10) | (uint32)(utf16lo - 0xDC00);
}
int len = u8_toutf8(cPtr, lenLeft, c32);
cPtr += len;
lenLeft -= len;
}
return strOut;
}
String Beefy::UTF8Encode(const UTF16String& theString)
{
return UTF8Encode((uint16*)theString.c_str(), (int)theString.length());
}
UTF16String Beefy::UTF16Decode(const uint16* theString)
{
if (sizeof(wchar_t) == 2)
{
UTF16String str;
str.Set((wchar_t*)theString);
return str;
}
else
{
UTF16String str;
int len = 0;
while (theString[len] != 0)
len++;
str.ResizeRaw(len + 1);
str[len] = 0;
for (int pos = 0; pos < len; pos++)
str[pos] = (wchar_t)theString[pos];
return str;
}
}
static const char gHexChar[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
String Beefy::FileNameToURI(const StringImpl& fileName)
{
#ifdef _WIN32
String out = "file:///";
#else
String out = "file://";
#endif
for (int i = 0; i < (int)fileName.length(); i++)
{
char c = out[i];
bool isValid =
((c >= '@' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c >= '&' && c < 0x3b) ||
(c == '!') || (c == '$') || (c == '_') || (c == '=') || (c == '~'));
if ((((unsigned char)c) >= 0x80) || (!isValid))
{
out += '%';
out += gHexChar[((unsigned char)(c)) >> 4];
out += gHexChar[((unsigned char)(c)) & 0xf];
}
else if (c == '\\')
out += '/';
else
out += c;
}
return out;
}
static const uint8 cCharTo64b[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
0, 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, 0, 0, 0, 0, 63,
0, 0, 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, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int64 Beefy::DecodeULEB32(const char*& p)
{
uint64 val = 0;
unsigned shift = 0;
uint8 charVal;
do
{
charVal = cCharTo64b[(uint8)*(p++)];
val += uint64(charVal & 0x1f) << shift;
shift += 5;
} while ((charVal & 0x20) != 0);
return val;
}
static const char c64bToChar[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_'};
void Beefy::EncodeULEB32(uint64 value, StringImpl& buffer)
{
do
{
uint8 byteVal = value & 0x1f;
value >>= 5;
if (value != 0)
byteVal |= 0x20; // Mark this byte to show that more bytes will follow
buffer.Append(c64bToChar[byteVal]);
}
while (value != 0);
}
#ifndef BF_SMALL
void Beefy::ExactMinimalFloatToStr(float f, char* str)
{
sprintf(str, "%1.9g", f);
char* cPtrLast = str;
while (true)
{
char c = cPtrLast[1];
if ((c == 0) || (c == 'e'))
break;
cPtrLast++;
}
char* cStrEnd = cPtrLast + 1;
int baseLen = (int)(cPtrLast - str) + 1;
if (baseLen < 9)
return; // Isn't "full" precision
char removeChar;
char lastChar = *cPtrLast;
char secondToLast = cPtrLast[-1];
if (secondToLast == '9')
{
removeChar = '9';
}
else if (secondToLast == '0')
{
removeChar = '0';
}
else
{
return; // Not a 'transitional' representation
}
do
{
*cPtrLast = 0;
cPtrLast--;
} while (*cPtrLast == removeChar);
strcpy(cPtrLast + 1, cStrEnd);
if (removeChar == '9')
(*cPtrLast)++;
// Verify that our pretty representation is equivalent
float checkF = 0;
sscanf(str, "%f", &checkF);
if (f == checkF)
return;
// Rollback
int endLen = (int)strlen(cPtrLast + 1);
memmove(cStrEnd, cPtrLast + 1, endLen);
if (removeChar == '9')
(*cPtrLast)--;
cPtrLast++;
while (cPtrLast < cStrEnd - 1)
{
*cPtrLast = removeChar;
cPtrLast++;
}
*cPtrLast = lastChar;
}
void Beefy::ExactMinimalDoubleToStr(double d, char* str)
{
sprintf(str, "%1.17g", d);
char* cPtrLast = str;
while (true)
{
char c = cPtrLast[1];
if ((c == 0) || (c == 'e'))
break;
cPtrLast++;
}
char* cStrEnd = cPtrLast + 1;
int baseLen = (int)(cPtrLast - str) + 1;
if (baseLen < 18)
return; // Isn't "full" precision
char removeChar;
char lastChar = *cPtrLast;
if ((lastChar == '9') || (lastChar == '8'))
{
removeChar = '9';
}
else if ((lastChar == '1') || (lastChar == '2'))
{
removeChar = '0';
}
else
{
return; // Not a 'transitional' representation
}
do
{
*cPtrLast = 0;
cPtrLast--;
}
while (*cPtrLast == removeChar);
strcpy(cPtrLast + 1, cStrEnd);
if (removeChar == '9')
(*cPtrLast)++;
// Verify that our pretty representation is equivalent
double checkD = 0;
sscanf(str, "%lf", &checkD);
if (d == checkD)
return;
// Rollback
int endLen = (int)strlen(cPtrLast + 1);
memmove(cStrEnd, cPtrLast + 1, endLen);
if (removeChar == '9')
(*cPtrLast)--;
cPtrLast++;
while (cPtrLast < cStrEnd - 1)
{
*cPtrLast = removeChar;
cPtrLast++;
}
*cPtrLast = lastChar;
}
#endif
static char* StbspCallback(char *buf, void *user, int len)
{
((String*)user)->Append(buf, len);
return buf;
}
//extern "C" int _vsnprintf(char* const _Buffer, size_t const _BufferCount, char const* const _Format, va_list _ArgList, int zz);
#ifdef BF_SMALL
String Beefy::vformat(const char* fmt, va_list argPtr)
{
// We draw the line at a 1MB string.
const int maxSize = 1000000;
//va_list checkArgPtr = argPtr;
//va_start(checkArgPtr, fmt);
int argIdx = 0;
char* newFmt = NULL;
char tempBuff[2048];
char* tempBuffPtr = tempBuff;
for (int i = 0; fmt[i] != 0; i++)
{
if (fmt[i] == '%')
{
if (fmt[i + 1] == '%')
{
i++;
continue;
}
#ifdef BF32
bool isLongAddr = false;
#else
bool isLongAddr = true;
#endif
if ((fmt[i + 1] == 'l') && (fmt[i + 2] == '@'))
{
isLongAddr = true;
i++;
}
if (fmt[i + 1] == '@')
{
if (newFmt == NULL)
{
newFmt = (char*)malloc(strlen(fmt) + 1);
strcpy(newFmt, fmt);
//newFmt = strdup(fmt);
}
newFmt[i + 1] = 's';
const char*& argValPtr = *(const char**)((intptr*)argPtr + argIdx);
if (isLongAddr)
{
int64 iVal = *(int64*)((intptr*)argPtr + argIdx);
#ifdef BF32
argIdx++; // Takes two spots
#endif
argValPtr = tempBuffPtr;
int leftVal = (int)(iVal >> 32);
if (leftVal != 0)
{
sprintf(tempBuffPtr, "%x", leftVal);
tempBuffPtr += strlen(tempBuffPtr);
}
tempBuffPtr[0] = '\'';
tempBuffPtr++;
sprintf(tempBuffPtr, "%08x", (int)iVal);
tempBuffPtr += strlen(tempBuffPtr) + 1;
}
else
{
int32 iVal = (int32)(intptr)argValPtr;
argValPtr = tempBuffPtr;
sprintf(tempBuffPtr, "%08x", iVal);
tempBuffPtr += strlen(tempBuffPtr) + 1;
}
if (newFmt[i] == 'l')
newFmt[i] = '+';
}
else
{
//const char*& argValPtr = va_arg(checkArgPtr, const char*);
}
argIdx++;
}
}
if (newFmt != NULL)
{
Beefy::String retVal = vformat(newFmt, argPtr);
free(newFmt);
return retVal;
}
// If the string is less than 161 characters,
// allocate it on the stack because this saves
// the malloc/free time.
const int bufSize = 161;
char stackBuffer[bufSize];
int attemptedSize = bufSize - 1;
int numChars = 0;
#ifdef _WIN32
numChars = _vsnprintf(stackBuffer, attemptedSize, fmt, argPtr);
#else
numChars = vsnprintf(stackBuffer, attemptedSize, fmt, argPtr);
#endif
if ((numChars >= 0) && (numChars <= attemptedSize))
{
// Needed for case of 160-character printf thing
stackBuffer[numChars] = '\0';
// Got it on the first try.
return String(stackBuffer);
}
// Now use the heap.
char* heapBuffer = NULL;
while (((numChars == -1) || (numChars > attemptedSize)) &&
(attemptedSize < maxSize))
{
// Try a bigger size
attemptedSize *= 2;
heapBuffer = (char*)realloc(heapBuffer, (attemptedSize + 1));
#ifdef _WIN32
numChars = _vsnprintf(heapBuffer, attemptedSize, fmt, argPtr);
#else
numChars = vsnprintf(heapBuffer, attemptedSize, fmt, argPtr);
#endif
}
if (numChars == -1)
{
free(heapBuffer);
return "";
}
heapBuffer[numChars] = 0;
Beefy::String aResult = Beefy::String(heapBuffer);
free(heapBuffer);
return aResult;
}
#else
String Beefy::vformat(const char* fmt, va_list argPtr)
{
String str;
char buf[STB_SPRINTF_MIN];
BF_stbsp_vsprintfcb(StbspCallback, (void*)&str, buf, fmt, argPtr);
return str;
}
#endif
String Beefy::StrFormat(const char* fmt ...)
{
va_list argList;
va_start(argList, fmt);
String aResult = vformat(fmt, argList);
va_end(argList);
return aResult;
}
String Beefy::IntPtrDynAddrFormat(intptr addr)
{
if ((addr & 0xFFFFFFFF00000000LL) == 0)
return StrFormat("%08X", addr);
return StrFormat("%p", addr);
}
void Beefy::OutputDebugStr(const StringImpl& theString)
{
const int maxLen = 0xFFFE;
if (theString.length() > maxLen)
{
OutputDebugStr(theString.Substring(0, maxLen));
OutputDebugStr(theString.Substring(maxLen));
return;
}
BfpOutput_DebugString(theString.c_str());
}
void Beefy::OutputDebugStrF(const char* fmt ...)
{
va_list argList;
va_start(argList, fmt);
String aResult = vformat(fmt, argList);
va_end(argList);
OutputDebugStr(aResult);
}
uint8* Beefy::LoadBinaryData(const StringImpl& path, int* size)
{
FILE* fP = fopen(path.c_str(), "rb");
if (fP == NULL)
return NULL;
fseek(fP, 0, SEEK_END);
int aSize = (int32)ftell(fP);
fseek(fP, 0, SEEK_SET);
uint8* data = new uint8[aSize];
int readSize = (int)fread(data, 1, aSize, fP);
(void)readSize;
fclose(fP);
if (size)
*size = aSize;
return data;
}
char* Beefy::LoadTextData(const StringImpl& path, int* size)
{
FILE* fP = fopen(path.c_str(), "rb");
if (fP == NULL)
return NULL;
fseek(fP, 0, SEEK_END);
int fileSize = (int32)ftell(fP);
int strLen = fileSize;
fseek(fP, 0, SEEK_SET);
uint8 charHeader[3] = {0};
int readSize = (int)fread(charHeader, 1, 3, fP);
if ((charHeader[0] == 0xFF) && (charHeader[1] == 0xFE))
{
//UTF16 LE
int dataLen = fileSize - 2;
char* data = new char[dataLen + 2];
data[0] = (char)charHeader[2];
data[dataLen] = 0;
data[dataLen + 1] = 0;
int readSize = (int)fread(data + 1, 1, dataLen - 1, fP);
(void)readSize;
fclose(fP);
// UTF16
UTF16String str;
str.Set((wchar_t*)data);
delete [] data;
String utf8Str = UTF8Encode(str);
char* utf8Data = new char[utf8Str.length() + 1];
strLen = (int)utf8Str.length();
if (size != NULL)
*size = strLen;
memcpy(utf8Data, utf8Str.c_str(), strLen);
return utf8Data;
}
else if ((charHeader[0] == 0xEF) && (charHeader[1] == 0xBB) && (charHeader[2] == 0xBF))
{
strLen = fileSize - 3;
char* data = new char[strLen + 1];
data[strLen] = 0;
if (size != NULL)
*size = strLen;
int readSize = (int)fread(data, 1, strLen, fP);
(void)readSize;
fclose(fP);
return data;
}
if (size != NULL)
*size = strLen;
char* data = new char[strLen + 1];
data[strLen] = 0;
for (int i = 0; i < BF_MIN(3, strLen); i++)
data[i] = charHeader[i];
if (strLen > 3)
{
int readSize = (int)fread(data + 3, 1, strLen - 3, fP);
(void)readSize;
}
fclose(fP);
return data;
}
bool Beefy::LoadTextData(const StringImpl& path, StringImpl& str)
{
int size = 0;
char* data = LoadTextData(path, &size);
if (data == NULL)
return false;
if ((str.mAllocSizeAndFlags & StringImpl::DynAllocFlag) != 0)
str.Release();
str.mPtr = data;
str.mAllocSizeAndFlags = size | StringImpl::DynAllocFlag | StringImpl::StrPtrFlag;
str.mLength = size;
return true;
}
#ifdef BF_MINGW
unsigned long long __cdecl _byteswap_uint64(unsigned long long _Int64)
{
#ifdef _WIN64
unsigned long long retval;
__asm__ __volatile__ ("bswapq %[retval]" : [retval] "=rm" (retval) : "[retval]" (_Int64));
return retval;
#else
union {
long long int64part;
struct {
unsigned long lowpart;
unsigned long hipart;
};
} retval;
retval.int64part = _Int64;
__asm__ __volatile__ ("bswapl %[lowpart]\n"
"bswapl %[hipart]\n"
: [lowpart] "=rm" (retval.hipart), [hipart] "=rm" (retval.lowpart) : "[lowpart]" (retval.lowpart), "[hipart]" (retval.hipart));
return retval.int64part;
#endif
}
#endif
#ifdef _WIN32
int64 Beefy::EndianSwap(int64 val)
{
return _byteswap_uint64(val);
}
#endif
int32 Beefy::EndianSwap(int val)
{
return ((val & 0x000000FF) << 24) | ((val & 0x0000FF00) << 8) |
((val & 0x00FF0000) >> 8) | ((val & 0xFF000000) >> 24);
}
int16 Beefy::EndianSwap(int16 val)
{