-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
1618 lines (1491 loc) · 37.3 KB
/
Copy pathcommon.cpp
File metadata and controls
1618 lines (1491 loc) · 37.3 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
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
/*
Skelton for retropc emulator
Author : Takeda.Toshiya
Date : 2013.01.17-
[ common ]
*/
#if defined(_USE_QT)
#include <string.h>
#include <fcntl.h>
#if !defined(__WIN32) && !defined(__WIN64)
#include <unistd.h>
#else
#include <io.h>
#include <direct.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include "csp_logger.h"
#include <string>
#include <algorithm>
#include <cctype>
#include <QDir>
#include <QFileInfo>
#elif defined(_WIN32)
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#else
#include <time.h>
#endif
#include <math.h>
#include "common.h"
#include "fileio.h"
#if defined(__MINGW32__) || defined(__MINGW64__)
extern DWORD GetLongPathName(LPCTSTR lpszShortPath, LPTSTR lpszLongPath, DWORD cchBuffer);
#endif
#if defined(_USE_QT)
std::string DLL_PREFIX cpp_homedir;
std::string DLL_PREFIX my_procname;
std::string DLL_PREFIX sRssDir;
#endif
void DLL_PREFIX common_initialize()
{
// get the initial current path when the software starts
get_initial_current_path();
}
uint32_t DLL_PREFIX EndianToLittle_DWORD(uint32_t x)
{
#if defined(__LITTLE_ENDIAN__)
return x;
#else
uint32_t y;
y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
((x & 0x00ff0000) >> 8) | ((x & 0xff000000) >> 24);
return y;
#endif
}
uint16_t DLL_PREFIX EndianToLittle_WORD(uint16_t x)
{
#if defined(__LITTLE_ENDIAN__)
return x;
#else
uint16_t y;
y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
return y;
#endif
}
uint32_t DLL_PREFIX EndianFromLittle_DWORD(uint32_t x)
{
#if defined(__LITTLE_ENDIAN__)
return x;
#else
uint32_t y;
y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
((x & 0x00ff0000) >> 8) | ((x & 0xff000000) >> 24);
return y;
#endif
}
uint16_t DLL_PREFIX EndianFromLittle_WORD(uint16_t x)
{
#if defined(__LITTLE_ENDIAN__)
return x;
#else
uint16_t y;
y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
return y;
#endif
}
uint32_t DLL_PREFIX EndianToBig_DWORD(uint32_t x)
{
#if defined(__BIG_ENDIAN__)
return x;
#else
uint32_t y;
y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
((x & 0x00ff0000) >> 8) | ((x & 0xff000000) >> 24);
return y;
#endif
}
uint16_t DLL_PREFIX EndianToBig_WORD(uint16_t x)
{
#if defined(__BIG_ENDIAN__)
return x;
#else
uint16_t y;
y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
return y;
#endif
}
uint32_t DLL_PREFIX EndianFromBig_DWORD(uint32_t x)
{
#if defined(__BIG_ENDIAN__)
return x;
#else
uint32_t y;
y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
((x & 0x00ff0000) >> 8) | ((x & 0xff000000) >> 24);
return y;
#endif
}
uint16_t DLL_PREFIX EndianFromBig_WORD(uint16_t x)
{
#if defined(__BIG_ENDIAN__)
return x;
#else
uint16_t y;
y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
return y;
#endif
}
uint64_t DLL_PREFIX ExchangeEndianU64(uint64_t x)
{
pair64_t __i, __o;
__i.q = x;
__o.b.h7 = __i.b.l;
__o.b.h6 = __i.b.h;
__o.b.h5 = __i.b.h2;
__o.b.h4 = __i.b.h3;
__o.b.h3 = __i.b.h4;
__o.b.h2 = __i.b.h5;
__o.b.h = __i.b.h6;
__o.b.l = __i.b.h7;
return __o.q;
}
int64_t DLL_PREFIX ExchangeEndianS64(uint64_t x)
{
pair64_t __i, __o;
__i.q = x;
__o.b.h7 = __i.b.l;
__o.b.h6 = __i.b.h;
__o.b.h5 = __i.b.h2;
__o.b.h4 = __i.b.h3;
__o.b.h3 = __i.b.h4;
__o.b.h2 = __i.b.h5;
__o.b.h = __i.b.h6;
__o.b.l = __i.b.h7;
return __o.sq;
}
uint32_t DLL_PREFIX ExchangeEndianU32(uint32_t x)
{
pair32_t __i, __o;
__i.d = x;
__o.b.h3 = __i.b.l;
__o.b.h2 = __i.b.h;
__o.b.h = __i.b.h2;
__o.b.l = __i.b.h3;
return __o.d;
}
int32_t DLL_PREFIX ExchangeEndianS32(uint32_t x)
{
pair32_t __i, __o;
__i.d = x;
__o.b.h3 = __i.b.l;
__o.b.h2 = __i.b.h;
__o.b.h = __i.b.h2;
__o.b.l = __i.b.h3;
return __o.sd;
}
uint16_t DLL_PREFIX ExchangeEndianU16(uint16_t x)
{
pair16_t __i, __o;
__i.w = x;
__o.b.h = __i.b.l;
__o.b.l = __i.b.h;
return __o.w;
}
int16_t DLL_PREFIX ExchangeEndianS16(uint16_t x)
{
pair16_t __i, __o;
__i.w = x;
__o.b.h = __i.b.l;
__o.b.l = __i.b.h;
return __o.sw;
}
#ifndef _MSC_VER
int DLL_PREFIX max(int a, int b)
{
if(a > b) {
return a;
} else {
return b;
}
}
unsigned DLL_PREFIX int max(unsigned int a, unsigned int b)
{
if(a > b) {
return a;
} else {
return b;
}
}
unsigned DLL_PREFIX int max(unsigned int a, int b)
{
if(b < 0) return a;
if(a > (unsigned int)b) {
return a;
} else {
return b;
}
}
unsigned DLL_PREFIX int max(int a, unsigned int b)
{
if(a < 0) return b;
if((unsigned int)a > b) {
return a;
} else {
return b;
}
}
int DLL_PREFIX min(int a, int b)
{
if(a < b) {
return a;
} else {
return b;
}
}
int DLL_PREFIX min(unsigned int a, int b)
{
if(b < 0) return b;
if(a > INT_MAX) return b;
if((int)a < b) {
return (int)a;
} else {
return b;
}
}
int DLL_PREFIX min(int a, unsigned int b)
{
if(a < 0) return a;
if(b > INT_MAX) return a;
if(a < (int)b) {
return a;
} else {
return (int)b;
}
}
unsigned int DLL_PREFIX min(unsigned int a, unsigned int b)
{
if(a < b) {
return a;
} else {
return b;
}
}
#endif
#ifndef SUPPORT_SECURE_FUNCTIONS
//errno_t my_tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode)
//{
// if((*pFile = _tfopen(filename, mode)) != NULL) {
// return 0;
// } else {
// return errno;
// }
//}
errno_t DLL_PREFIX my_tcscat_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource)
{
_tcscat(strDestination, strSource);
return 0;
}
errno_t DLL_PREFIX my_strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource)
{
strcpy(strDestination, strSource);
return 0;
}
errno_t DLL_PREFIX my_tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource)
{
_tcscpy(strDestination, strSource);
return 0;
}
errno_t DLL_PREFIX my_tcscpy_s(_TCHAR *strDestination, const _TCHAR *strSource)
{
_tcscpy(strDestination, strSource);
return 0;
}
errno_t DLL_PREFIX my_strncpy_s(char *strDestination, size_t numberOfElements, const char *strSource, size_t count)
{
strncpy(strDestination, strSource, count);
return 0;
}
errno_t DLL_PREFIX my_tcsncpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource, size_t count)
{
_tcsncpy(strDestination, strSource, count);
return 0;
}
char *DLL_PREFIX my_strtok_s(char *strToken, const char *strDelimit, char **context)
{
return strtok(strToken, strDelimit);
}
_TCHAR *DLL_PREFIX my_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context)
{
return _tcstok(strToken, strDelimit);
}
int DLL_PREFIX my_sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...)
{
va_list ap;
va_start(ap, format);
int result = vsprintf(buffer, format, ap);
va_end(ap);
return result;
}
int DLL_PREFIX my_swprintf_s(wchar_t *buffer, size_t sizeOfBuffer, const wchar_t *format, ...)
{
va_list ap;
va_start(ap, format);
int result = vswprintf(buffer, format, ap);
va_end(ap);
return result;
}
int DLL_PREFIX my_stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...)
{
va_list ap;
va_start(ap, format);
int result = _vstprintf(buffer, format, ap);
va_end(ap);
return result;
}
int DLL_PREFIX my_vsprintf_s(char *buffer, size_t numberOfElements, const char *format, va_list argptr)
{
return vsprintf(buffer, format, argptr);
}
int DLL_PREFIX my_vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr)
{
return _vstprintf(buffer, format, argptr);
}
#endif
#ifndef _MSC_VER
void DLL_PREFIX *my_memcpy(void *dst, void *src, size_t len)
{
size_t len1;
register size_t len2;
register uint32_t s_align = (uint32_t)(((size_t)src) & 0x1f);
register uint32_t d_align = (uint32_t)(((size_t)dst) & 0x1f);
int i;
if(len == 0) return dst;
if(len < 8) {
return memcpy(dst, src, len);
}
len1 = len;
#if defined(WITHOUT_UNALIGNED_SIMD)
// Using SIMD without un-aligned instructions.
switch(s_align) {
case 0: // Align 256
{
uint64_t b64[4];
register uint64_t *s64 = (uint64_t *)src;
register uint64_t *d64 = (uint64_t *)dst;
switch(d_align) {
case 0: // 256 vs 256
{
len2 = len1 >> 5;
while(len2 > 0) {
for(i = 0; i < 4; i++) b64[i] = s64[i];
for(i = 0; i < 4; i++) d64[i] = b64[i];
s64 += 4;
d64 += 4;
--len2;
}
len1 = len1 & 0x1f;
if(len1 != 0) return memcpy(d64, s64, len1);
return dst;
}
break;
case 0x10: // 256 vs 128
{
len2 = len1 >> 5;
while(len2 > 0) {
for(i = 0; i < 4; i++) b64[i] = s64[i];
for(i = 0; i < 2; i++) d64[i] = b64[i];
d64 += 2;
for(i = 2; i < 4; i++) d64[i - 2] = b64[i];
d64 += 2;
s64 += 4;
--len2;
}
len1 = len1 & 0x1f;
if(len1 != 0) return memcpy(d64, s64, len1);
return dst;
}
break;
case 0x08:
case 0x18: // 256 vs 64
{
len2 = len1 >> 5;
while(len2 > 0) {
for(i = 0; i < 4; ++i) b64[i] = s64[i];
for(i = 0; i < 4; ++i) {
*d64 = b64[i];
++d64;
}
s64 += 4;
--len2;
}
len1 = len1 & 0x1f;
if(len1 != 0) return memcpy(d64, s64, len1);
return dst;
}
break;
case 0x04:
case 0x0c:
case 0x14:
case 0x1c: // 256 vs 32
{
uint32_t b32[8];
register uint32_t *s32 = (uint32_t *)src;
register uint32_t *d32 = (uint32_t *)dst;
len2 = len1 >> 5;
while(len2 > 0) {
for(i = 0; i < 8; ++i) b32[i] = s32[i];
*d32 = b32[0];
++d32;
*d32 = b32[1];
++d32;
*d32 = b32[2];
++d32;
*d32 = b32[3];
++d32;
*d32 = b32[4];
++d32;
*d32 = b32[5];
++d32;
*d32 = b32[6];
++d32;
*d32 = b32[7];
++d32;
s32 += 8;
--len2;
}
len1 = len1 & 0x1f;
if(len1 != 0) return memcpy(d32, s32, len1);
return dst;
}
break;
default:
return memcpy(dst, src, len1);
break;
}
}
break;
case 0x10: // Src alignn to 16.
{
uint32_t b32[4];
register uint32_t *s32 = (uint32_t *)src;
register uint32_t *d32 = (uint32_t *)dst;
switch(d_align) {
case 0: // 128 vs 256/128
case 0x10:
{
len2 = len1 >> 4;
while(len2 > 0) {
for(i = 0; i < 4; i++) b32[i] = s32[i];
for(i = 0; i < 4; i++) d32[i] = b32[i];
s32 += 4;
d32 += 4;
--len2;
}
len1 = len1 & 0x0f;
if(len1 != 0) return memcpy(d32, s32, len1);
return dst;
}
break;
case 0x08:
case 0x18: // 128 vs 64
{
len2 = len1 >> 4;
while(len2 > 0) {
for(i = 0; i < 4; ++i) b32[i] = s32[i];
for(i = 0; i < 2; ++i) {
d32[i] = b32[i];
}
d32 += 2;
for(i = 2; i < 4; ++i) {
d32[i - 2] = b32[i];
}
d32 += 2;
s32 += 4;
--len2;
}
len1 = len1 & 0x0f;
if(len1 != 0) return memcpy(d32, s32, len1);
return dst;
}
break;
case 0x04:
case 0x0c:
case 0x14:
case 0x1c: // 128 vs 32
{
len2 = len1 >> 4;
while(len2 > 0) {
for(i = 0; i < 4; ++i) b32[i] = s32[i];
*d32 = b32[0];
++d32;
*d32 = b32[1];
++d32;
*d32 = b32[2];
++d32;
*d32 = b32[3];
++d32;
s32 += 4;
--len2;
}
len1 = len1 & 0x0f;
if(len1 != 0) return memcpy(d32, s32, len1);
return dst;
}
break;
default:
return memcpy(dst, src, len1);
break;
}
}
break;
case 0x08:
case 0x18: // Src alignn to 64.
{
register uint32_t *s32 = (uint32_t *)src;
register uint32_t *d32 = (uint32_t *)dst;
register uint64_t *s64 = (uint64_t *)src;
register uint64_t *d64 = (uint64_t *)dst;
switch(d_align) {
case 0:
case 0x10: // 64 vs 128
{
uint64_t b128[2];
len2 = len1 >> 4;
while(len2 > 0) {
b128[0] = *s64;
++s64;
b128[1] = *s64;
++s64;
for(i = 0; i < 2; i++) d64[i] = b128[i];
d64 += 2;
--len2;
}
len1 = len1 & 0x0f;
if(len1 != 0) return memcpy(d64, s64, len1);
return dst;
}
break;
case 0x08:
case 0x18: // 64 vs 64
{
len2 = len1 >> 3;
while(len2 > 0) {
*d64 = *s64;
++s64;
++d64;
--len2;
}
len1 = len1 & 0x07;
if(len1 != 0) return memcpy(d64, s64, len1);
return dst;
}
break;
case 0x04:
case 0x0c: // 64 vs 32
case 0x14:
case 0x1c: // 64 vs 32
{
uint32_t b32[2];
len2 = len1 >> 3;
while(len2 > 0) {
for(i = 0; i < 2; ++i) b32[i] = s32[i];
d32[0] = b32[0];
d32[1] = b32[1];
s32 += 2;
d32 += 2;
--len2;
}
len1 = len1 & 0x07;
if(len1 != 0) return memcpy(d32, s32, len1);
return dst;
}
break;
default:
return memcpy(dst, src, len1);
break;
}
}
case 0x04:
case 0x0c:
case 0x14:
case 0x1c: // Src align 32
{
register uint32_t *s32 = (uint32_t *)src;
register uint32_t *d32 = (uint32_t *)dst;
register uint64_t *d64 = (uint64_t *)dst;
switch(d_align) {
case 0:
case 0x10: // 32 vs 128
{
uint32_t b128[4];
len2 = len1 >> 4;
while(len2 > 0) {
b128[0] = *s32;
++s32;
b128[1] = *s32;
++s32;
b128[3] = *s32;
++s32;
b128[4] = *s32;
++s32;
for(i = 0; i < 4; i++) d32[i] = b128[i];
d32 += 4;
--len2;
}
len1 = len1 & 0x0f;
if(len1 != 0) return memcpy(d32, s32, len1);
return dst;
}
break;
case 0x08:
case 0x18: // 32 vs 64
{
uint32_t b64[2];
len2 = len1 >> 3;
while(len2 > 0) {
b64[0] = *s32;
++s32;
b64[1] = *s32;
++s32;
for(i = 0; i < 2; i++) d32[i] = b64[i];
d32 += 2;
--len2;
}
len1 = len1 & 0x07;
if(len1 != 0) return memcpy(d32, s32, len1);
return dst;
}
break;
case 0x04:
case 0x0c:
case 0x14:
case 0x1c: // 32 vs 32
{
len2 = len1 >> 2;
while(len2 > 0) {
*d32 = *s32;
++s32;
++d32;
--len2;
}
len1 = len1 & 0x03;
if(len1 != 0) return memcpy(d32, s32, len1);
return dst;
}
break;
default:
return memcpy(dst, src, len1);
break;
}
}
break;
default:
if(len1 != 0) return memcpy(dst, src, len1);
break;
}
#else
// Using SIMD *with* un-aligned instructions.
register uint32_t *s32 = (uint32_t *)src;
register uint32_t *d32 = (uint32_t *)dst;
if(((s_align & 0x07) != 0x0) && ((d_align & 0x07) != 0x0)) { // None align.
return memcpy(dst, src, len);
}
if((s_align == 0x0) || (d_align == 0x0)) { // Align to 256bit
uint32_t b256[8];
len2 = len1 >> 5;
while(len2 > 0) {
for(i = 0; i < 8; i++) b256[i] = s32[i];
for(i = 0; i < 8; i++) d32[i] = b256[i];
s32 += 8;
d32 += 8;
--len2;
}
len1 = len1 & 0x1f;
if(len1 != 0) return memcpy(d32, s32, len1);
return dst;
}
if(((s_align & 0x0f) == 0x0) || ((d_align & 0x0f) == 0x0)) { // Align to 128bit
uint32_t b128[4];
len2 = len1 >> 4;
while(len2 > 0) {
for(i = 0; i < 4; i++) b128[i] = s32[i];
for(i = 0; i < 4; i++) d32[i] = b128[i];
s32 += 4;
d32 += 4;
--len2;
}
len1 = len1 & 0x0f;
if(len1 != 0) return memcpy(d32, s32, len1);
return dst;
}
if(((s_align & 0x07) == 0x0) || ((d_align & 0x07) == 0x0)) { // Align to 64bit
uint32_t b64[2];
len2 = len1 >> 3;
while(len2 > 0) {
for(i = 0; i < 2; i++) b64[i] = s32[i];
for(i = 0; i < 2; i++) d32[i] = b64[i];
s32 += 2;
d32 += 2;
--len2;
}
len1 = len1 & 0x07;
if(len1 != 0) return memcpy(d32, s32, len1);
return dst;
}
//if(len1 != 0) return memcpy(dst, src, len1);
#endif
// Trap
return dst;
}
#endif
#ifndef _WIN32
BOOL DLL_PREFIX MyWritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName)
{
BOOL result = FALSE;
FILEIO* fio_i = new FILEIO();
if(fio_i->Fopen(lpFileName, FILEIO_READ_ASCII)) {
char tmp_path[_MAX_PATH];
my_sprintf_s(tmp_path, _MAX_PATH, "%s.$$$", lpFileName);
FILEIO* fio_o = new FILEIO();
if(fio_o->Fopen(tmp_path, FILEIO_WRITE_ASCII)) {
bool in_section = false;
char section[1024], line[1024], *equal;
my_sprintf_s(section, 1024, "[%s]", lpAppName);
while(fio_i->Fgets(line, 1024) != NULL && strlen(line) > 0) {
if(line[strlen(line) - 1] == '\n') {
line[strlen(line) - 1] = '\0';
}
if(!result) {
if(line[0] == '[') {
if(in_section) {
fio_o->Fprintf("%s=%s\n", lpKeyName, lpString);
result = TRUE;
} else if(strcmp(line, section) == 0) {
in_section = true;
}
} else if(in_section && (equal = strstr(line, "=")) != NULL) {
*equal = '\0';
if(strcmp(line, lpKeyName) == 0) {
fio_o->Fprintf("%s=%s\n", lpKeyName, lpString);
result = TRUE;
continue;
}
*equal = '=';
}
}
fio_o->Fprintf("%s\n", line);
}
if(!result) {
if(!in_section) {
fio_o->Fprintf("[%s]\n", lpAppName);
}
fio_o->Fprintf("%s=%s\n", lpKeyName, lpString);
result = TRUE;
}
fio_o->Fclose();
}
delete fio_o;
fio_i->Fclose();
if(result) {
if(!(FILEIO::RemoveFile(lpFileName) && FILEIO::RenameFile(tmp_path, lpFileName))) {
result = FALSE;
}
}
} else {
FILEIO* fio_o = new FILEIO();
if(fio_o->Fopen(lpFileName, FILEIO_WRITE_ASCII)) {
fio_o->Fprintf("[%s]\n", lpAppName);
fio_o->Fprintf("%s=%s\n", lpKeyName, lpString);
fio_o->Fclose();
}
delete fio_o;
}
delete fio_i;
return result;
}
DWORD DLL_PREFIX MyGetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName)
{
_TCHAR *lpp = (_TCHAR *)lpReturnedString;
if(lpDefault != NULL) {
my_strcpy_s(lpp, nSize, lpDefault);
} else {
lpp[0] = '\0';
}
FILEIO* fio = new FILEIO();
if(fio->Fopen(lpFileName, FILEIO_READ_ASCII)) {
bool in_section = false;
char section[1024], line[1024], *equal;
my_sprintf_s(section, 1024, "[%s]", lpAppName);
while(fio->Fgets(line, 1024) != NULL && strlen(line) > 0) {
if(line[strlen(line) - 1] == '\n') {
line[strlen(line) - 1] = '\0';
}
if(line[0] == '[') {
if(in_section) {
break;
} else if(strcmp(line, section) == 0) {
in_section = true;
}
} else if(in_section && (equal = strstr(line, "=")) != NULL) {
*equal = '\0';
if(strcmp(line, lpKeyName) == 0) {
my_strcpy_s(lpp, nSize, equal + 1);
break;
}
}
}
fio->Fclose();
}
delete fio;
return strlen(lpp);
}
UINT DLL_PREFIX MyGetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName)
{
int i;
char sstr[128];
char sval[128];
std::string s;
memset(sstr, 0x00, sizeof(sstr));
memset(sval, 0x00, sizeof(sval));
snprintf(sval, 128, "%d", nDefault);
MyGetPrivateProfileString(lpAppName,lpKeyName, sval, sstr, 128, lpFileName);
s = sstr;
if(s.empty()) {
i = nDefault;
} else {
i = strtol(s.c_str(), NULL, 10);
}
return i;
}
#endif
#if defined(_RGB555)
scrntype_t DLL_PREFIX RGB_COLOR(uint32_t r, uint32_t g, uint32_t b)
{
scrntype_t rr = ((scrntype_t)r * 0x1f) / 0xff;
scrntype_t gg = ((scrntype_t)g * 0x1f) / 0xff;
scrntype_t bb = ((scrntype_t)b * 0x1f) / 0xff;
return (rr << 10) | (gg << 5) | bb;
}
scrntype_t DLL_PREFIX RGBA_COLOR(uint32_t r, uint32_t g, uint b, uint32_t a)
{
return RGB_COLOR(r, g, b);
}
uint8_t DLL_PREFIX R_OF_COLOR(scrntype_t c)
{
c = (c >> 10) & 0x1f;
c = (c * 0xff) / 0x1f;
return (uint8_t)c;
}
uint8_t DLL_PREFIX G_OF_COLOR(scrntype_t c)
{
c = (c >> 5) & 0x1f;
c = (c * 0xff) / 0x1f;
return (uint8_t)c;
}
uint8_t DLL_PREFIX B_OF_COLOR(scrntype_t c)
{
c = (c >> 0) & 0x1f;
c = (c * 0xff) / 0x1f;
return (uint8_t)c;
}
uint8_t DLL_PREFIX A_OF_COLOR(scrntype_t c)
{
return 0;
}
#elif defined(_RGB565)
scrntype_t DLL_PREFIX RGB_COLOR(uint32_t r, uint32_t g, uint32_t b)
{
scrntype_t rr = ((scrntype_t)r * 0x1f) / 0xff;
scrntype_t gg = ((scrntype_t)g * 0x3f) / 0xff;
scrntype_t bb = ((scrntype_t)b * 0x1f) / 0xff;
return (rr << 11) | (gg << 5) | bb;
}
scrntype_t DLL_PREFIX RGBA_COLOR(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
{
return RGB_COLOR(r, g, b);
}
uint8_t DLL_PREFIX R_OF_COLOR(scrntype_t c)
{
c = (c >> 11) & 0x1f;
c = (c * 0xff) / 0x1f;
return (uint8_t)c;
}
uint8_t DLL_PREFIX G_OF_COLOR(scrntype_t c)
{
c = (c >> 5) & 0x3f;
c = (c * 0xff) / 0x3f;
return (uint8_t)c;
}
uint8_t DLL_PREFIX B_OF_COLOR(scrntype_t c)
{
c = (c >> 0) & 0x1f;
c = (c * 0xff) / 0x1f;
return (uint8_t)c;
}
uint8_t DLL_PREFIX A_OF_COLOR(scrntype_t c)
{
return 0;
}
#endif
#ifndef _MSC_VER
struct to_upper { // Refer from documentation of libstdc++, GCC5.
char operator() (char c) const { return std::toupper(c); }
};
#endif
#if defined(_USE_QT)
static void _my_mkdir(std::string t_dir)
{
struct stat st;
//#if !defined(__WIN32) && !defined(__WIN64)
// if(fstatat(AT_FDCWD, csppath.c_str(), &st, 0) != 0) {
// mkdirat(AT_FDCWD, t_dir.c_str(), 0700); // Not found
// }
#if defined(_USE_QT)
if(stat(t_dir.c_str(), &st) != 0) {