-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyUtils.h
More file actions
1593 lines (1252 loc) · 41.5 KB
/
MyUtils.h
File metadata and controls
1593 lines (1252 loc) · 41.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
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
#ifndef _MYUTILS_
#define _MYUTILS_
#include "time.h"
#include "ctype.h"
#include <limits>
#include "xVect.h"
#include <algorithm>
#ifdef SIMULATE_NO_WIN32
#define _WIN32_BKP _WIN32
#define _MSC_VER_BKP _MSC_VER
#undef _WIN32
#undef _MSC_VER
#endif
#define NUMEL(a) (sizeof(a)/sizeof((a)[0]))
#define BIT(n) ((long long int)1<<((n)-1))
CharPointer MyGetLine(FILE * f);
void RemoveUnneededExposantChars(char * ptr1);
void GetDateStr(aVect<char> & str, char * formatString = "%.2d/%.2d/%.4d");
aVect<char> GetDateStr(char * formatString = "%.2d/%.2d/%.4d");
void GetTimeStr(aVect<char> & str, char * formatString = "%.2d:%.2d:%.2d");
aVect<char> GetTimeStr(char * formatString = "%.2d:%.2d:%.2d");
aVect<char> NiceTime(double secondes, bool noDays = false, bool integerSeconds = true, bool noSeconds = false);
void NiceFloat(aVect<char> & str, double tol = 1e-6);
template <class T>
void NiceFloat(aVect<char> & str, T&& val, double tol = 1e-6) {
RemoveDecimalsWithPrec((double)val, str, tol, true);
//RemoveUnneededExposantChars(str);
}
template <class T>
aVect<char> NiceFloat(T&& val, double tol = 1e-6) {
aVect<char> str;
NiceFloat(str, (double)val, tol);
return std::move(str);
}
//aVect<char> DoubleToString(double val, double tol = 1e-6);
//void DoubleToString(aVect<char> & str, double val, double tol = 1e-6);
void RemoveDecimalsWithPrec(aVect<char> & str, double tol = 1e-6, bool removeUnneededExposantChars = true);
void RemoveDecimalsWithPrec(double val, aVect<char> & str, double tol = 1e-6, bool removeUnneededExposantChars = true);
//int strcmp_caseInsensitive(const char * str1, const char * str2);
int strcmp_caseInsensitive(const wchar_t * str1, const wchar_t * str2);
//char * strstr_caseInsensitive(const char * str, const char * subStr);
char * reverse_strstr(char * str, char * subStr, bool caseSensitive = true);
char * reverse_strstr_caseInsensitive(char * str, char * subStr);
//size_t ReplaceStr(aVect<char> & str, char * subStr, char * replaceStr, bool onlyFirstOccurence = false, bool caseSensitive = true, bool reverse = false, size_t startFrom = 0);
//
//size_t ReplaceStr_CaseInsensitive(aVect<char> & str, char * subStr, char * replaceStr, bool onlyFirstOccurence = false, size_t startFrom = 0);
//size_t ReplaceStr_FirstOccurence(aVect<char> & str, char * subStr, char * replaceStr, bool caseSensitive = true, size_t startFrom = 0);
//size_t ReplaceStr_LastOccurence(aVect<char> & str, char * subStr, char * replaceStr, bool caseSensitive = true, size_t startFrom = 0);
//size_t ReplaceStr_FirstOccurence_CaseInsensitive(aVect<char> & str, char * subStr, char * replaceStr, size_t startFrom = 0);
//size_t ReplaceStr_LastOccurence_CaseInsensitive(aVect<char> & str, char * subStr, char * replaceStr, size_t startFrom = 0);
//
//aVect<char> ReplaceStr(char * str, char * subStr, char * replaceStr, bool onlyFirstOccurence = false, bool caseSensitive = true, bool reverse = false, size_t startFrom = 0);
//
//aVect<char> ReplaceStr_CaseInsensitive(char * str, char * subStr, char * replaceStr, bool onlyFirstOccurence = false, bool reverse = false, size_t startFrom = 0);
//aVect<char> ReplaceStr_FirstOccurence(char * str, char * subStr, char * replaceStr, bool caseSensitive = true, size_t startFrom = 0);
//aVect<char> ReplaceStr_LastOccurence(char * str, char * subStr, char * replaceStr, bool caseSensitive = true, size_t startFrom = 0);
//aVect<char> ReplaceStr_FirstOccurence_CaseInsensitive(char * str, char * subStr, char * replaceStr, size_t startFrom = 0);
//aVect<char> ReplaceStr_LastOccurence_CaseInsensitive(char * str, char * subStr, char * replaceStr, size_t startFrom = 0);
#define CHECKNUM_ISNUMBER BIT(1)
#define CHECKNUM_CONTAINSNUMBERFIRST BIT(2)
#define CHECKNUM_ISINF BIT(3)
bool SeparateNumber(aVect<char> & str);
bool IsNumber(const char * str, bool strict = true);
bool IsInf(const char * str);
char CheckNumber(aVect<char> & str, bool separate, bool strict);
bool SeparateNumber(aVect<wchar_t> & str);
bool IsNumber(const wchar_t * str, bool strict = true);
bool IsInf(const wchar_t * str);
wchar_t CheckNumber(aVect<wchar_t> & str, bool separate, bool strict);
void SetPromptBeforeExit();
template <class T> T * AlloCopy(T * el, unsigned int count) {
unsigned int n = count*sizeof(T);
T * retVal = (T*)malloc(n);
if (!retVal) MY_ERROR("malloc = NULL");
memcpy(retVal, el, n);
return retVal;
}
template <class T> T * AlloCopy(T * el) {
return AlloCopy(el, 1);
}
template<bool checkType, class U>
struct Strlen_Check {
static_assert(!checkType, "wrong type");
};
template<bool checkType>
struct Strlen_Check<checkType, wchar_t> {
};
template<bool checkType>
struct Strlen_Check<checkType, char> {
};
template<bool checkType = true, class U>
size_t Strlen(U * ptr) {
Strlen_Check<checkType, U>();
return Strlen_Core(ptr);
}
template<class U>
size_t Strlen_Core(U * ptr) {
MY_ERROR("wrong type");
return 0;
}
template<>
inline size_t Strlen_Core<wchar_t>(wchar_t * ptr) {
if (ptr) return wcslen(ptr);
return 0;
}
template<>
inline size_t Strlen_Core<char>(char * ptr) {
if (ptr) return strlen(ptr);
return 0;
}
template <int nSep, char sep1, char sep2, char sep3, char sep4, char sep5>
char * RetrieveStr(char *& str_orig, bool strict = false) {
//static CharPointer ptr, retVal;
CharPointer ptr, retVal;
ptr = str_orig;
retVal = RetrieveStr<CharPointer, char, nSep, sep1, sep2, sep3, sep4, sep5>(ptr, strict);
str_orig = *(char**)&ptr;
return *(char**)&retVal;
}
template <int nSep, wchar_t sep1, wchar_t sep2, wchar_t sep3, wchar_t sep4, wchar_t sep5>
wchar_t * RetrieveStr(wchar_t *& str_orig, bool strict = false) {
//static WCharPointer ptr, retVal;
WCharPointer ptr, retVal;
ptr = str_orig;
retVal = RetrieveStr<WCharPointer, wchar_t, nSep, sep1, sep2, sep3, sep4, sep5>(ptr, strict);
str_orig = *(wchar_t**)&ptr;
return *(wchar_t**)&retVal;
}
template <class CharPtrClass, class T, T nSep, T sep1, T sep2, T sep3, T sep4, T sep5>
CharPtrClass RetrieveStr(CharPtrClass & str_orig, bool strict = false) {
//static CharPtrClass emptyVect;
CharPtrClass emptyVect = 0;
if (!str_orig) return emptyVect;
//T * str_orig = str_orig_;
T * str = str_orig;
//size_t len_str = Strlen(str);
bool cond = false;
size_t i=0, j=0, k=0;
if (*str == 0) str_orig.Free();
if (strict) {
cond = true;
}
else {
for (i=0 ; /*i<len_str*/ ; ++i) {
T c = str[i];
if (!c) break;
switch (nSep) {
case 1: cond = (c != sep1);break;
case 2: cond = (c != sep1 && c != sep2);break;
case 3: cond = (c != sep1 && c != sep2 && c != sep3);break;
case 4: cond = (c != sep1 && c != sep2 && c != sep3 && c != sep4);break;
case 5: cond = (c != sep1 && c != sep2 && c != sep3 && c != sep4 && c != sep5);break;
default: MY_ERROR("5 separators max");
}
if (cond) break;
}
}
if (cond) {
cond = false;
str_orig.Free();
for (j=i ; /*j < len_str*/ ; ++j) {
T c = str[j];
if (!c) break;
switch (nSep) {
case 1: cond = (c == sep1);break;
case 2: cond = (c == sep1 || c == sep2);break;
case 3: cond = (c == sep1 || c == sep2 || c == sep3);break;
case 4: cond = (c == sep1 || c == sep2 || c == sep3 || c == sep4);break;
case 5: cond = (c == sep1 || c == sep2 || c == sep3 || c == sep4 || c == sep5);break;
default: MY_ERROR("5 separators max");
}
if (cond) {
if (strict) {
str_orig = &str[j+1]; // str_orig.Wrap(&str[j+1]);
}
else {
cond = false;
for (k=j+1 ; /*k<len_str*/ ; ++k) {
T c = str[k];
if (!c) break;
switch (nSep) {
case 1: cond = (c != sep1);break;
case 2: cond = (c != sep1 && c != sep2);break;
case 3: cond = (c != sep1 && c != sep2 && c != sep3);break;
case 4: cond = (c != sep1 && c != sep2 && c != sep3 && c != sep4);break;
case 5: cond = (c != sep1 && c != sep2 && c != sep3 && c != sep4 && c != sep5);break;
default: MY_ERROR("5 separators max");
}
if (cond) break;
}
str_orig = &str[k];
if (*str_orig == 0) str_orig.Free();
}
break;
}
}
((T *) &str[0])[j] = 0;
/*CharPointer retVal = */;
return &str[i];//retVal;
}
return emptyVect;
}
template <char sep1, char sep2, char sep3, char sep4, char sep5> CharPointer RetrieveStr(CharPointer & str_orig, bool strict = false) {
return RetrieveStr<5, sep1, sep2, sep3, sep4, sep5>(str_orig, strict);
}
template <char sep1, char sep2, char sep3, char sep4> CharPointer RetrieveStr(CharPointer & str_orig, bool strict = false) {
return RetrieveStr<4, sep1, sep2, sep3, sep4, 0>(str_orig, strict);
}
template <char sep1, char sep2, char sep3> CharPointer RetrieveStr(CharPointer & str_orig, bool strict = false) {
return RetrieveStr<3, sep1, sep2, sep3, 0, 0>(str_orig, strict);
}
template <char sep1, char sep2> CharPointer RetrieveStr(CharPointer & str_orig, bool strict = false) {
return RetrieveStr<2, sep1, sep2, 0, 0, 0>(str_orig, strict);
}
template <char sep1> CharPointer RetrieveStr(CharPointer & str_orig, bool strict = false) {
return RetrieveStr<1, sep1, 0, 0, 0, 0>(str_orig, strict);
}
CharPointer RetrieveStr(CharPointer & str_orig, bool strict = false);
template <char sep1, char sep2, char sep3, char sep4, char sep5> char * RetrieveStr(char * & str_orig, bool strict = false) {
return RetrieveStr<5, sep1, sep2, sep3, sep4, sep5>(str_orig, strict);
}
template <char sep1, char sep2, char sep3, char sep4> char * RetrieveStr(char * & str_orig, bool strict = false) {
return RetrieveStr<4, sep1, sep2, sep3, sep4, 0>(str_orig, strict);
}
template <char sep1, char sep2, char sep3> char * RetrieveStr(char * & str_orig, bool strict = false) {
return RetrieveStr<3, sep1, sep2, sep3, 0, 0>(str_orig, strict);
}
template <char sep1, char sep2> char * RetrieveStr(char * & str_orig, bool strict = false) {
return RetrieveStr<2, sep1, sep2, 0, 0, 0>(str_orig, strict);
}
template <char sep1> char * RetrieveStr(char * & str_orig, bool strict = false) {
return RetrieveStr<1, sep1, 0, 0, 0, 0>(str_orig, strict);
}
char * RetrieveStr(char * & str_orig, bool strict = false);
//Wide char versions
template <wchar_t sep1, wchar_t sep2, wchar_t sep3, wchar_t sep4, wchar_t sep5> WCharPointer RetrieveStr(WCharPointer & str_orig, bool strict = false) {
return RetrieveStr<5, sep1, sep2, sep3, sep4, sep5>(str_orig, strict);
}
template <wchar_t sep1, wchar_t sep2, wchar_t sep3, wchar_t sep4> WCharPointer RetrieveStr(WCharPointer & str_orig, bool strict = false) {
return RetrieveStr<4, sep1, sep2, sep3, sep4, 0>(str_orig, strict);
}
template <wchar_t sep1, wchar_t sep2, wchar_t sep3> WCharPointer RetrieveStr(WCharPointer & str_orig, bool strict = false) {
return RetrieveStr<3, sep1, sep2, sep3, 0, 0>(str_orig, strict);
}
template <wchar_t sep1, wchar_t sep2> WCharPointer RetrieveStr(WCharPointer & str_orig, bool strict = false) {
return RetrieveStr<2, sep1, sep2, 0, 0, 0>(str_orig, strict);
}
template <wchar_t sep1> WCharPointer RetrieveStr(WCharPointer & str_orig, bool strict = false) {
return RetrieveStr<1, sep1, 0, 0, 0, 0>(str_orig, strict);
}
WCharPointer RetrieveStr(WCharPointer & str_orig, bool strict = false);
template <wchar_t sep1, wchar_t sep2, wchar_t sep3, wchar_t sep4, wchar_t sep5> wchar_t * RetrieveStr(wchar_t * & str_orig, bool strict = false) {
return RetrieveStr<5, sep1, sep2, sep3, sep4, sep5>(str_orig, strict);
}
template <wchar_t sep1, wchar_t sep2, wchar_t sep3, wchar_t sep4> wchar_t * RetrieveStr(wchar_t * & str_orig, bool strict = false) {
return RetrieveStr<4, sep1, sep2, sep3, sep4, 0>(str_orig, strict);
}
template <wchar_t sep1, wchar_t sep2, wchar_t sep3> wchar_t * RetrieveStr(wchar_t * & str_orig, bool strict = false) {
return RetrieveStr<3, sep1, sep2, sep3, 0, 0>(str_orig, strict);
}
template <wchar_t sep1, wchar_t sep2> wchar_t * RetrieveStr(wchar_t * & str_orig, bool strict = false) {
return RetrieveStr<2, sep1, sep2, 0, 0, 0>(str_orig, strict);
}
template <wchar_t sep1> wchar_t * RetrieveStr(wchar_t * & str_orig, bool strict = false) {
return RetrieveStr<1, sep1, 0, 0, 0, 0>(str_orig, strict);
}
wchar_t * RetrieveStr(wchar_t * & str_orig, bool strict = false);
template <class T1, class T2> auto Max(T1 t1, T2 t2) -> typename std::remove_reference<decltype(t1 > t2 ? t1 : t2)>::type {
return t1 > t2 ? t1 : t2;
}
template <class T1, class T2> auto Min(T1 t1, T2 t2) -> typename std::remove_reference<decltype(t1 < t2 ? t1 : t2)>::type {
return t1 < t2 ? t1 : t2;
}
template <class T> void Swap(T& t1, T& t2) {
PlaceHolder<T> tmp;
tmp = *(PlaceHolder<T>*)&t1;
*(PlaceHolder<T>*)&t1 = *(PlaceHolder<T>*)&t2;
*(PlaceHolder<T>*)&t2 = *(PlaceHolder<T>*)&tmp;
}
//template <class T> void Swap(T& t1, T& t2) {
// if (std::is_trivial<T>::value) {
// T tmp;
// tmp = t1;// t2 = tmp;
// }
// t1 = t2;
// else {
// PlaceHolder<T> tmp;
// PlaceHolder<T> * p1 = (PlaceHolder<T> *)&t1;
// PlaceHolder<T> * p2 = (PlaceHolder<T> *)&t2;
// tmp = *p1;
// *p1 = *p2;
// *p2 = tmp;
// }
//}
bool IsEqualWithPrec(double a, double b, double prec);
int MyRound(double x);
#define SET_DWORD_BYTE_MASK(a, mask, b) {(a) = ((a) & ~(mask)) | ( ((b) ? 0xFFFFFFFF : 0) & (mask));}
#define SET_BYTE_MASK(a, mask, b) {(a) = (b) ? (a) | (mask) : (a) & ~(mask);}
#define ASSERT_ALWAYS(a, b) \
if (!(a)) { \
MY_ERROR(b); \
}
#define MyAssert(a) \
if (!(a)) { \
MY_ERROR(#a); \
}
#ifndef _XVECT_
#define UINT_OVERFLOW_CHECK_AFFECT(a) (a); \
{ \
if (((double)(a)) > ((double) UINT_MAX)) { \
MY_ERROR(aVect<char>("unsigned overflow affectation : %f > %u", \
((double)(a)), UINT_MAX)); \
} \
}
#define UINT_OVERFLOW_CHECK_MULT(a, b) { \
if ((a) > UINT_MAX/(b)) { \
MY_ERROR(aVect<char>("unsigned overflow : %u * %u", (a), (b))); \
} \
}
#define UINT_OVERFLOW_CHECK_ADD(a, b) { \
if (UINT_MAX - (a) < (b)) { \
MY_ERROR(aVect<char>("unsigned overflow : %u + %u", (a), (b))); \
} \
}
#define UINT_OVERFLOW_CHECK_SUBSTRACT(a, b) { \
if ((a) < (b)) { \
MY_ERROR(aVecct<char>("unsigned overflow : %u - %u", (a), (b))); \
} \
}
#define SIZE_T_OVERFLOW_CHECK_AFFECT(a) (a); \
{ \
if (((double)(a)) > ((double) SIZE_MAX)) { \
MY_ERROR(aVect<char>("unsigned overflow affectation : %f > %u", \
((double)(a)), SIZE_MAX)); \
} \
}
#define SIZE_T_OVERFLOW_CHECK_MULT(a, b) { \
if ((a) > SIZE_MAX/(b)) { \
MY_ERROR(aVect<char>("unsigned overflow : %u * %u", (a), (b))); \
} \
}
#define SIZE_T_OVERFLOW_CHECK_ADD(a, b) { \
if (SIZE_MAX - (a) < (b)) { \
MY_ERROR(aVect<char>("unsigned overflow : %u + %u", (a), (b))); \
} \
}
#define SIZE_T_OVERFLOW_CHECK_SUBSTRACT(a, b) { \
if ((a) < (b)) { \
MY_ERROR(aVecct<char>("unsigned overflow : %u - %u", (a), (b))); \
} \
}
#endif
#define SIZE_T_OVERFLOW_CHECK_AFFECT_(a) (a); \
{ \
if (((double)(a)) > ((double) SIZE_MAX)) { \
MY_ERROR("unsigned overflow affectation"); \
} \
}
#define SIZE_T_OVERFLOW_CHECK_MULT_(a, b) { \
if ((a) > SIZE_MAX/(b)) { \
MY_ERROR("unsigned overflow"); \
} \
}
#define SIZE_T_OVERFLOW_CHECK_ADD_(a, b) { \
if (SIZE_MAX - (a) < (b)) { \
MY_ERROR("unsigned overflow ")); \
} \
}
#define SIZE_T_OVERFLOW_CHECK_SUBSTRACT_(a, b) { \
if ((a) < (b)) { \
MY_ERROR("unsigned overflow", (a), (b))); \
} \
}
class File {
protected:
aVect<char> filePath, openMode;
FILE * file;
long long fileSize;
int buf_nEl;
char * buf;
char * ptrStart;
char * curPos;
char * ptrEndRead;
bool endOfFile;
bool cachedData;
bool writeMode;
clock_t lastFlush;
aVect<char> lineContinuationBuffer;
File & Init();
File & Reset();
File & SetOpenMode(const char * om);
virtual void CloseCore();
virtual void OpenCore(const char * fp, const char * om);
virtual void FlushCore();
virtual size_t ReadCore(void*, size_t, size_t);
virtual size_t WriteCore(void*, size_t, size_t);
virtual bool File::EofCore();
CharPointer GetLineCore(bool errorIfEndOfFile = false, bool nullEndLine = true);
File & operator =(const File & f) = delete;
File(const File & f) = delete;
public:
File();
File(const char * fn);
File(const char * fn, const char * om);
File(File && f);
File & operator =(File && f);
virtual ~File();
virtual operator bool();
long long GetSize();
long long GetCurrentPosition();
void Flush();
File & Close();
File & SetFile(const char * fp, const char * om = nullptr);
aVect<char> GetFilePath() const;
aVect<char> GetOpenMode();
bool IsWriteMode(const char * om);
bool IsReadMode(const char * om);
bool IsAppendMode(const char * om);
bool IsBinaryMode(const char * om);
bool Eof();
virtual File & Open(const char * fp = nullptr, const char * om = nullptr);
operator FILE*();
const FILE* GetFILEptr();
FILE* GetFILEptr_UNSAFE();
size_t Write(const char * str);
size_t Read(aVect<char> & data, size_t n);
template <char lineContinuation = 0, bool trim = false, char lineComment = 0, bool olgaStyle = false>
CharPointer GetLine(bool errorIfEndOfFile = false, bool nullEndLine = true); //nullEndLine = false => !Beware! No Final '\0' !!! For very specific use (mix of text/binary)
CharPointer GetLastLine();
void PosChanged();
template <class T, class... Args>
size_t Write(T* format, Args&&... args) {
static aVect<std::remove_const<T>::type> buffer;
static WinCriticalSection cs;
Critical_Section(cs) {
return this->Write(buffer.Format(format, std::forward<Args>(args)...));
}
//unreachable code, but this makes compiler happy
return -1;
}
template <class T>
size_t Write(T* str) {
static aVect<std::remove_const<T>::type> buffer;
static WinCriticalSection cs;
Critical_Section(cs) {
return this->Write(buffer.Copy(str));
}
}
template <bool useActualCount = false, class T>
size_t Write(aVect<T> & data) {
if (!*this) {
openMode.sprintf("w");
if (!Open()) MY_ERROR(aVect<char>("Unable to open \"%s\"", (char*)filePath));
}
if (!data) return 0;
size_t size;
static_assert(!useActualCount || (std::is_same<T, char>::value || std::is_same<T, wchar_t>::value), "useActualCount = true is only intended for aVect<char> or aVect<wchar_t>");
if (!useActualCount && (std::is_same<T, char>::value || std::is_same<T, wchar_t>::value)) size = (sizeof T) * (data.Last() == 0 ? data.Count() - 1 : data.Count());
else size = (sizeof T) * data.Count();
return WriteCore(&data[0], size, 1);
}
};
template <char lineContinuation, bool trim, char lineComment, bool olgaStyle>
CharPointer File::GetLine(bool errorIfEndOfFile, bool nullEndLine) {
if (lineContinuation) {
if (!nullEndLine) MY_ERROR("TODO");
CharPointer line = this->GetLineCore(errorIfEndOfFile, nullEndLine);
for (;;) {
if (trim) line.Trim();
//static_assert(!lineComment, "TODO");
//if (lineComment) {
// if (auto ptr = strchr(line, lineComment)) {
// *ptr = '\n';
// }
//}
if (!line || line.Last(-1) != lineContinuation) break;
this->lineContinuationBuffer.Copy(line).Grow(-1).Last() = 0;
auto newLine = this->GetLineCore(errorIfEndOfFile, nullEndLine);
if (newLine && lineComment) {
if (auto ptr = strchr(newLine, lineComment)) {
if (olgaStyle) {
if (ptr == newLine) {
this->lineContinuationBuffer.Last() = lineContinuation;
this->lineContinuationBuffer.Push(0);
}
}
*ptr = 0;
}
}
this->lineContinuationBuffer.Append(newLine);
line = this->lineContinuationBuffer;
}
return line;
}
else {
auto retVal = GetLineCore(errorIfEndOfFile, nullEndLine);
if (retVal && lineComment) {
if (!nullEndLine) MY_ERROR("TODO");
if (auto ptr = strchr(retVal, lineComment)) {
if (olgaStyle) MY_ERROR("TODO");
*ptr = 0;
}
}
if (trim) {
if (!nullEndLine) MY_ERROR("TODO");
retVal.Trim();
}
return retVal;
}
}
class LogFile {
private:
struct VerboseLvl {
int verboseLvl;
bool pendingNewLine;
};
aVect<FILE *> logFiles;
aVect< aVect<wchar_t> > filePaths;
aVect<aVect<wchar_t> > openModes;
aVect<int> verboseLvls;
int dispVerboseLvl, printLvl;
bool display, tmpDisplay;
bool printTime, tmpPrintTime;
bool printDate, tmpPrintDate;
bool write, tmpWrite;
aVect<VerboseLvl> knownVerboseLvls;
LogFile & Init(char * fName, bool erase = true);
LogFile & Init(wchar_t * fName = nullptr, bool erase = true);
LogFile & Open();
//LogFile & vPrintf(char * fs, va_list args);
LogFile & AddIfUnkownVerboseLvl(int verboseLvl, bool pendingLine = false);
LogFile & LogFile::ResetPendingLine(int verboseLvl);
bool HasPendingLine(int verboseLvl);
public:
LogFile();
~LogFile();
LogFile(char * fPath, bool erase = true);
LogFile(wchar_t * fPath, bool erase = true);
LogFile & Close();
LogFile & SetFile(char * fPath, int verboseLvl = 1000, bool erase = true);
LogFile & SetFile(wchar_t * fPath, int verboseLvl = 1000, bool erase = true);
LogFile & AddFile(char * fPath, int verboseLvl = 1000, bool erase = true);
LogFile & AddFile(wchar_t * fPath, int verboseLvl = 1000, bool erase = true);
LogFile & RemoveFile(size_t i, bool erase = true);
aVect<wchar_t> GetFilePath(int i=0);
size_t GetFilesCount();
FILE * GetFile(int i=0);
aVect< aVect<wchar_t> > GetFilePaths();
LogFile & SetVerboseLvl(int vLvl, int i=0);
LogFile & SetDisplayVerboseLvl(int vLvl);
template <class... Args>
LogFile & Printf(const wchar_t * fs, Args&&... args) {
this->RuntimeArgChecker(std::forward<Args>(args)...);
bool allTrue = true;
aVect_static_for(verboseLvls, i) {
allTrue = allTrue && printLvl > verboseLvls[i];
}
if (allTrue && printLvl > dispVerboseLvl) return *this;
//static aVect<wchar_t> buffer;
aVect<wchar_t> buffer;
if (tmpWrite) {
if (!filePaths) MY_ERROR("Aucun fichier n'a été spécifié");
Open();
}
if (tmpPrintDate) {
this->PrintDate();
}
buffer.sprintf(fs, std::forward<Args>(args)...);
if (tmpPrintTime) {
//static aVect<char> timeStr;
aVect<char> timeStr;
GetTimeStr(timeStr);
buffer.sprintf(L"%S%s%s", (char*)timeStr, buffer ? L" " : L"", (wchar_t*)buffer);
}
aVect_static_for(verboseLvls, i) {
if (printLvl <= verboseLvls[i] && tmpWrite) {
if (HasPendingLine(verboseLvls[i])) {
fwprintf(logFiles[i], L"\n");
}
fwprintf(logFiles[i], L"%s", (wchar_t*)buffer);
fflush(logFiles[i]);
}
}
if (printLvl <= dispVerboseLvl && tmpDisplay) {
if (HasPendingLine(dispVerboseLvl)) {
wprintf(L"\n");
}
wprintf(L"%s", (wchar_t*)buffer);
ResetPendingLine(dispVerboseLvl);
}
aVect_static_for(verboseLvls, i) {
if (printLvl <= verboseLvls[i] && tmpWrite) {
ResetPendingLine(verboseLvls[i]);
}
openModes[i] = "a";
}
tmpPrintTime = printTime;
tmpPrintDate = printDate;
tmpDisplay = display;
tmpWrite = write;
return *this;
}
template <class... Args>
LogFile & Printf(const char * fs, Args&&... args) {
//static aVect<char> buffer;
aVect<char> buffer;
this->RuntimeArgChecker(std::forward<Args>(args)...);
buffer.sprintf(fs, std::forward<Args>(args)...);
return this->Printf(L"%S", (char*)buffer);
return *this;
}
template <class... Args>
LogFile & xPrintf(const char * fs, Args&&... args) {
aVect<char> buffer;
buffer.Format(fs, std::forward<Args>(args)...);
return this->Printf(L"%S", (char*)buffer);
return *this;
}
template <class... Args>
LogFile & xPrintf(const wchar_t * fs, Args&&... args) {
aVect<wchar_t> buffer;
buffer.Format(fs, std::forward<Args>(args)...);
return this->Printf(L"%s", (wchar_t*)buffer);
return *this;
}
LogFile & PrintTime();
LogFile & PrintDate();
void RuntimeArgChecker() {}
template <class ToCheck, class... Args> void RuntimeArgChecker(ToCheck toCheck, Args&&... args) {
return;
//if (std::is_same<ToCheck, char*>::value || std::is_same<ToCheck, wchar_t*>::value) {
// if (!toCheck) MY_ERROR("null string pointer");
// if (toCheck == 0) MY_ERROR("empty string pointer");
//}
//this->RuntimeArgChecker(std::forward<Args>(args)...);
}
template <class... Args>
LogFile & Printf(int vLvl, const char * fs, Args&&... args) {
AddIfUnkownVerboseLvl(vLvl);
printLvl = vLvl;
this->Printf(fs, std::forward<Args>(args)...);
return *this;
}
template <class... Args>
LogFile & Printf(int vLvl, const wchar_t * fs, Args&&... args) {
AddIfUnkownVerboseLvl(vLvl);
printLvl = vLvl;
this->Printf(fs, std::forward<Args>(args)...);
return *this;
}
LogFile & NoTime();
LogFile & Time();
LogFile & NoDate();
LogFile & Date();
LogFile & NoDisp();
LogFile & Disp();
LogFile & NoWrite();
LogFile & Write();
LogFile & DisableDisplay();
LogFile & DisableTime();
LogFile & DisableDate();
LogFile & DisableWrite();
LogFile & EnableDisplay();
LogFile & EnableTime();
LogFile & EnableDate();
LogFile & EnableWrite();
LogFile & SetPendingNewLine();
LogFile & UnsetPendingNewLine();
LogFile & SetPendingNewLine(int vLvl);
LogFile & UnsetPendingNewLine(int vLvl);
operator bool();
};
bool IsStringInFile(File & f, const char * str);
template <class T = int, bool min = true> class BinaryHeap {
protected:
aVect<T> keys;
size_t LeftChild(size_t i) {
return 2 * i + 1;
}
size_t RightChild(size_t i) {
return 2 * i + 2;
}
size_t Parent(size_t i) {
return (i - 1) / 2;
}
void BubbleUp(size_t i) {
size_t p = this->Parent(i);
while (i > 0 && (min ? this->keys[i] < this->keys[p] : this->keys[i] > this->keys[p])) {
Swap(this->keys[i], this->keys[p]);
i = p;
p = this->Parent(i);
}
}
void TrickleDown(size_t i) {
size_t n = this->keys.Count();
while (true) {
size_t r = this->RightChild(i);
size_t l = this->LeftChild(i);
size_t j = i;
if (r < n && (min ? this->keys[r] < this->keys[j] : this->keys[r] > this->keys[j])) {
j = r;
}
if (l < n && (min ? this->keys[l] < this->keys[j] : this->keys[l] > this->keys[j])) {
j = l;
}
if (j == i) break;
Swap(this->keys[i], this->keys[j]);
i = j;
}
}
public:
T & operator[](size_t i) {
return this->keys[i];
}
BinaryHeap & Update(size_t i, T & newVal) {
T & oldVal = this->keys[i];
bool up = (min ? newVal < oldVal : newVal > oldVal);
if (std::is_trivial<T>::value) {
oldVal = newVal;
}
else {
T newValCopy(newVal);
Swap(newValCopy, oldVal);
}
up ? this->BubbleUp(i) : this->TrickleDown(i);
}
BinaryHeap & Delete(size_t i) {
size_t n = keys.Count();
if (!n) MY_ERROR("heap is empty");
if (n == 1) {
this->Reset();
return *this;
}
T & thisKey = this->keys[i];
T & lastKey = this->keys.Last();
Swap(thisKey, lastKey);
this->keys.Redim(n - 1);
this->TrickleDown(i);
}
size_t Count() {
return this->keys.Count();
}
BinaryHeap & Reset() {
this->keys.Redim(0);
return *this;
}
BinaryHeap & Free() {
this->keys.Free();
return *this;
}
operator bool() {
return (bool)keys;
}
BinaryHeap & Push(T & x) {
this->keys.Push(x);
this->BubbleUp(this->keys.Count()-1);
return *this;
}
T & PrePush() {
return this->keys.Grow(1).Last();
}
void PostPush() {
this->BubbleUp(this->keys.Count() - 1);
}
void Pop(T & x) {
if (this->keys.Count() > 2) {
this->Pop_Fast_bug_if_small(x);
}
else {
this->Pop_slow(x);
}
}
void Pop_slow(T & x) {
if (!this->keys) MY_ERROR("heap is empty");
Swap(x, this->keys[0]);
if (this->keys.Count() > 1) {
Swap(this->keys[0], this->keys[this->keys.Count() - 1]);
this->keys.Grow(-1);
this->TrickleDown(0);
}
else this->keys.Redim(0);
}
void Pop_Fast_bug_if_small(T & x) {
if (!this->keys) MY_ERROR("heap is empty");
size_t hole = 1;
size_t succ = 2;
size_t sz = this->keys.Count();
T * data = this->keys;