-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmain.cpp
More file actions
1582 lines (1535 loc) · 56.9 KB
/
main.cpp
File metadata and controls
1582 lines (1535 loc) · 56.9 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
#include "VietnameseTextNormalizer.h"
#include <iostream>
#include <fstream>
#include <list>
//#undef WIN32_NORMALIZER_TOOL
#ifndef WIN32_NORMALIZER_TOOL
extern "C"
{
#include <Python.h>
}
#endif
/************************************************************************/
/* Utf8 - QuangBT Code */
/************************************************************************/
static void QuangBTConvertUtf8toUnicode(const unsigned char* utf8str, int utf8strlength, qwchar* ucs2buffer)
{
if (utf8str && ucs2buffer)
{
for (int ichar = 0; ichar < utf8strlength && utf8str[0] != 0; ichar++)
{
if (/*safe*/ichar + 1 < utf8strlength && utf8str[1] != 0 &&
/*condition*/ (utf8str[0] & 0xE0) == 0xC0 && (utf8str[1] & 0xC0) == 0x80)
{
/* 2 bytes UTF-8 Character.*/
*ucs2buffer = (((qwchar)(utf8str[0] & 0x001F)) << 6) | (utf8str[1] & 0x3F);
ucs2buffer++;
utf8str += 2;
ichar++;
}
else if (/*safe*/ichar + 2 < utf8strlength && utf8str[1] != 0 && utf8str[2] != 0 &&
/*condition*/ (utf8str[0] & 0xF0) == 0xE0 && (utf8str[1] & 0xC0) == 0x80 && (utf8str[2] & 0xC0) == 0x80)
{
/* 3bytes UTF-8 Character.*/
*ucs2buffer = (qwchar)((utf8str[0] & 0x000F) << 12) | ((utf8str[1] & 0x3F) << 6) | (utf8str[2] & 0x3F);
ucs2buffer++;
utf8str += 3;
ichar += 2;
}
else if (/*safe*/ichar + 3 < utf8strlength && utf8str[1] != 0 && utf8str[2] != 0 &&
/*condition*/ (utf8str[0] & 0xF0) == 0xE0 && (utf8str[1] & 0xC0) == 0x80 && (utf8str[2] & 0xC0) == 0x80 && (utf8str[3] & 0xC0) == 0x80)
{
/* 4bytes UTF-8 Character.*/
*ucs2buffer = (qwchar)((utf8str[0] & 0x000F) << 18) | ((utf8str[1] & 0x3F) << 12) | ((utf8str[2] & 0x3F) << 6) | (utf8str[3] & 0x3F);
ucs2buffer++;
utf8str += 4;
ichar += 3;
}
else
{
/* 1 byte UTF-8 Character.*/
*ucs2buffer = *utf8str;
ucs2buffer++;
utf8str++;
}
}
}
}
static int QuangBTConvertUnicodetoUtf8(const qwchar* ucs2str, int ucs2length, unsigned char* utf8buffer)
{
int convertlength = 0;
if (ucs2str && ucs2length > 0 && utf8buffer)
{
for (int iwchar = 0; iwchar < ucs2length; iwchar++)
{
if (0x0080 > *ucs2str)
{
/* 1 byte UTF-8 Character.*/
*utf8buffer = (unsigned char)(*ucs2str);
convertlength++;
ucs2str++;
utf8buffer++;
}
else if (0x0800 > *ucs2str)
{
if (*(utf8buffer - 1) == 20)
{
*utf8buffer = (unsigned char)(*ucs2str);
convertlength++;
ucs2str++;
utf8buffer++;
continue;
}
/*2 bytes UTF-8 Character.*/
*utf8buffer = ((unsigned char)((*ucs2str) >> 6)) | 0xc0;
*(utf8buffer + 1) = ((unsigned char)((*ucs2str) & 0x003F)) | 0x80;
convertlength += 2;
ucs2str++;
utf8buffer += 2;
}
else
{
/* 3 bytes UTF-8 Character .*/
*utf8buffer = ((unsigned char)((*ucs2str) >> 12)) | 0xE0;
*(utf8buffer + 1) = ((unsigned char)(((*ucs2str) & 0x0FC0) >> 6)) | 0x80;
*(utf8buffer + 2) = ((unsigned char)((*ucs2str) & 0x003F)) | 0x80;
convertlength += 3;
ucs2str++;
utf8buffer += 3;
}
}
*utf8buffer = 0;
}
return convertlength;
}
/************************************************************************/
/* Main */
/************************************************************************/
#ifdef WIN32_NORMALIZER_TOOL
#ifndef QBT_VALIDATE_TOOL
#include <Windows.h>
#include <string>
namespace std
{
/************************************************************************/
/* */
/************************************************************************/
void GoToXY(int x, int y)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
COORD Cursor_an_Pos = { (short)x, (short)y };
SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
#else
printf("%c[%d;%df", 0x1B, y, x);
#endif
}
void SetTitle(const std::wstring& title)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
SetConsoleTitleW(title.c_str());
#endif
}
void ClearScreen(void)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
HANDLE hConsoleOut;
COORD Home = { 0, 0 };
DWORD dummy;
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut, &csbiInfo);
FillConsoleOutputCharacter(hConsoleOut, ' ', csbiInfo.dwSize.X * csbiInfo.dwSize.Y, Home, &dummy);
csbiInfo.dwCursorPosition.X = 0;
csbiInfo.dwCursorPosition.Y = 0;
SetConsoleCursorPosition(hConsoleOut, csbiInfo.dwCursorPosition);
GoToXY(0, 0);
for (int i = 0; i < 50; i++) printf(" ");
GoToXY(0, 0);
#else
system("clear");
#endif
}
void HideCursor(void)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
HANDLE hOut;
CONSOLE_CURSOR_INFO ConCurInf = { 0 };
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
ConCurInf.dwSize = 10;
ConCurInf.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &ConCurInf);
#endif
}
std::wstring GetLower(const std::wstring& wstr);
void Show(const std::wstring& title, const wchar_t* format, ...)
{
if (format)
{
wchar_t* buffer = (wchar_t*)calloc(10250, sizeof(wchar_t));
if (buffer)
{
va_list args;
va_start(args, format);
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
vswprintf_s(buffer, 10240, format, args);
va_end(args);
int supportFlag = 0;
if (title.find(L"Lỗi") != std::wstring::npos) supportFlag = MB_ICONERROR;
else if (title.find(L"Cảnh báo") != std::wstring::npos) supportFlag = MB_ICONWARNING;
::MessageBoxW(0, (LPCWSTR)buffer, title.c_str(), MB_OK | supportFlag);
#else
vswprintf(buffer, 10240, format, args);
va_end(args);
gui::SetTextColor(CONSOLE_COLOR_BOLD);
fprintf(stderr, "%s\n", GetString(title).c_str());
gui::SetTextColor(CONSOLE_COLOR_DISABLE_BOLD);
fprintf(stderr, "%s\n", GetString(buffer).c_str());
#endif
free(buffer);
}
}
}
void SetTextColor(int color)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
HANDLE hConsoleOutput;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
GetConsoleScreenBufferInfo(hConsoleOutput, &screen_buffer_info);
WORD wAttributes = screen_buffer_info.wAttributes;
DWORD dcolor = color;
dcolor &= 0x000f;
wAttributes &= 0xfff0;
wAttributes |= dcolor;
SetConsoleTextAttribute(hConsoleOutput, wAttributes);
#else
#ifndef ON_REMOTE_LINUX
std::cout << "\033[" << color << "m";
#endif
#endif
}
void SetBackgroundColor(int color)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
HANDLE hConsoleOutput;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
GetConsoleScreenBufferInfo(hConsoleOutput, &screen_buffer_info);
WORD wAttributes = screen_buffer_info.wAttributes;
DWORD dcolor = color;
dcolor &= 0x000f;
dcolor <<= 4;
wAttributes &= 0xff0f;
wAttributes |= dcolor;
SetConsoleTextAttribute(hConsoleOutput, wAttributes);
#else
std::cout << "\033[" << color << "m";
#endif
}
static void guiWrite(int xsource, int ysource, int xdest, int ydest, const wchar_t* wstr/*=L"Quang"*/, int delay/*=15*/, int color/*=CONSOLE_COLOR_GREEN*/)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
SetTextColor(color);
if (xsource == xdest && ysource > ydest)
{
for (; wstr[0]; wstr++)
{
if (wstr[0] != 32)
{
for (int i = ysource; i >= ydest; i--)
{
GoToXY(xsource, i);
wprintf(L"%c", wstr[0]);
GoToXY(xsource, i + 1);
wprintf(L" ");
Sleep(delay);
}
}
else
{
GoToXY(xsource, ydest);
wprintf(L" ");
}
xsource++;
}
}
if (ydest == ysource && xsource > xdest)
{
for (; wstr[0]; wstr++)
{
if (wstr[0] != 32)
{
for (int i = xsource; i >= xdest; i--)
{
GoToXY(i, ysource);
wprintf(L"%c ", wstr[0]);
Sleep(delay);
}
}
else
{
GoToXY(xsource, ydest);
wprintf(L" ");
}
xdest++;
}
}
#endif
}
void Copyright(void)
{
ClearScreen();
HideCursor();
int x = 13;
int y = 5;
GoToXY(x, y + 1); printf(" / _` | | | |/ _` | '_ \\ / _` | \\ \\/ / _ \\ '_ \\ / _` |");
GoToXY(x, y + 2); printf("| (_| | |_| | (_| | | | | (_| | > < __/ | | | (_| |");
GoToXY(x, y + 3); printf(" \\__, |\\__,_|\\__,_|_| |_|\\__, | /_/\\_\\___|_| |_|\\__, |");
GoToXY(x, y + 4); printf(" | | __/ | __/ |");
GoToXY(x, y + 5); printf(" |_| |___/ |___/");
guiWrite(26, 15, 26, 13, L"Duoc viet boi Bui Tan Quang", 15, 2);
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
Sleep(2000);
#endif
}
/************************************************************************/
/* w-string */
/************************************************************************/
class wcslesscmp
{
public:
bool operator()(const std::wstring& lhs, const std::wstring& rhs)const
{
return lhs.compare(rhs) < 0;
}
};
typedef std::set<std::wstring, std::wcslesscmp> wstringset;
bool operator !=(const std::wstring& a, const std::wstring& b)
{
return wcscmp(a.c_str(), b.c_str()) != 0;
}
bool operator !=(const std::wstring& a, const wchar_t* b)
{
return wcscmp(a.c_str(), b) != 0;
}
wchar_t GetBase(wchar_t wch)
{
switch (wch)
{
case L'A':case L'à':case L'À':case L'á':case L'Á':case L'ả':case L'Ả':case L'ã':case L'Ã':case L'ạ':case L'Ạ':return L'a';
case L'E':case L'è':case L'È':case L'é':case L'É':case L'ẻ':case L'Ẻ':case L'ẽ':case L'Ẽ':case L'ẹ':case L'Ẹ':return L'e';
case L'I':case L'ì':case L'Ì':case L'í':case L'Í':case L'ỉ':case L'Ỉ':case L'ĩ':case L'Ĩ':case L'ị':case L'Ị':return L'i';
case L'O':case L'ò':case L'Ò':case L'ó':case L'Ó':case L'ỏ':case L'Ỏ':case L'õ':case L'Õ':case L'ọ':case L'Ọ':return L'o';
case L'U':case L'ù':case L'Ù':case L'ú':case L'Ú':case L'ủ':case L'Ủ':case L'ũ':case L'Ũ':case L'ụ':case L'Ụ':return L'u';
case L'Y':case L'ỳ':case L'Ỳ':case L'ý':case L'Ý':case L'ỷ':case L'Ỷ':case L'ỹ':case L'Ỹ':case L'ỵ':case L'Ỵ':return /*L'y'*/L'i';
case L'Â':case L'ầ':case L'Ầ':case L'ấ':case L'Ấ':case L'ẩ':case L'Ẩ':case L'ẫ':case L'Ẫ':case L'ậ':case L'Ậ':return L'â';
case L'Ê':case L'ề':case L'Ề':case L'ế':case L'Ế':case L'ể':case L'Ể':case L'ễ':case L'Ễ':case L'ệ':case L'Ệ':return L'ê';
case L'Ô':case L'ồ':case L'Ồ':case L'ố':case L'Ố':case L'ổ':case L'Ổ':case L'ỗ':case L'Ỗ':case L'ộ':case L'Ộ':return L'ô';
case L'Ă':case L'ằ':case L'Ằ':case L'ắ':case L'Ắ':case L'ẳ':case L'Ẳ':case L'ẵ':case L'Ẵ':case L'ặ':case L'Ặ':return L'ă';
case L'Ơ':case L'ờ':case L'Ờ':case L'ớ':case L'Ớ':case L'ở':case L'Ở':case L'ỡ':case L'Ỡ':case L'ợ':case L'Ợ':return L'ơ';
case L'Ư':case L'ừ':case L'Ừ':case L'ứ':case L'Ứ':case L'ử':case L'Ử':case L'ữ':case L'Ữ':case L'ự':case L'Ự':return L'ư';
case L'Đ':return L'đ';
case L'B':return L'b';
case L'C':return L'c';
case L'D':return L'd';
case L'F':return L'f';
case L'G':return L'g';
case L'H':return L'h';
case L'J':return L'j';
case L'K':return L'k';
case L'L':return L'l';
case L'M':return L'm';
case L'N':return L'n';
case L'P':return L'p';
case L'Q':return L'q';
case L'R':return L'r';
case L'S':return L's';
case L'T':return L't';
case L'V':return L'v';
case L'W':return L'w';
case L'X':return L'x';
case L'Z':return L'z';
}
if (wch >= 'a' && wch <= 'z') return wch;
if (wch >= 'A' && wch <= 'Z') return wch;
if (wch >= '0' && wch <= '9') return wch;
return L' ';
}
std::wstring GetBase(const std::wstring& wstr)
{
std::wstring base;
for (unsigned i = 0; i < wstr.size(); i++)
{
wchar_t baseChar = GetBase(wstr[i]);
if (baseChar != L' ') base += baseChar;
}
return base;
}
wchar_t GetLower(wchar_t wch)
{
switch (wch)
{
case 0xC0:/*AF*/
return 0xE0/*af*/;
case 0xC1:/*AS*/
return 0xE1/*as*/;
case 0x1EA2:/*AR*/
return 0x1EA3/*ar*/;
case 0xC3:/*AX*/
return 0xE3/*ax*/;
case 0x1EA0:/*AJ*/
return 0x1EA1/*aj*/;
case 0x102:/*AW*/
return 0x103/*aw*/;
case 0x1EB0:/*AWF*/
return 0x1EB1/*awf*/;
case 0x1EAE:/*AWS*/
return 0x1EAF/*aws*/;
case 0x1EB2:/*AWR*/
return 0x1EB3/*awr*/;
case 0x1EB4:/*AWX*/
return 0x1EB5/*awx*/;
case 0x1EB6:/*AWJ*/
return 0x1EB7/*awj*/;
case 0xC2:/*AA*/
return 0xE2/*aa*/;
case 0x1EA6:/*AAF*/
return 0x1EA7/*aaf*/;
case 0x1EA4:/*AAS*/
return 0x1EA5/*aas*/;
case 0x1EA8:/*AAR*/
return 0x1EA9/*aar*/;
case 0x1EAA:/*AAX*/
return 0x1EAB/*aax*/;
case 0x1EAC:/*AAJ*/
return 0x1EAD/*aaj*/;
case 0x110:/*DD*/
return 0x111/*dd*/;
case 0xC8:/*EF*/
return 0xE8/*ef*/;
case 0xC9:/*ES*/
return 0xE9/*es*/;
case 0x1EBA:/*ER*/
return 0x1EBB/*er*/;
case 0x1EBC:/*EX*/
return 0x1EBD/*ex*/;
case 0x1EB8:/*EJ*/
return 0x1EB9/*ej*/;
case 0xCA:/*EE*/
return 0xEA/*ee*/;
case 0x1EC0:/*EEF*/
return 0x1EC1/*eef*/;
case 0x1EBE:/*EES*/
return 0x1EBF/*ees*/;
case 0x1EC2:/*EER*/
return 0x1EC3/*eer*/;
case 0x1EC4:/*EEX*/
return 0x1EC5/*eex*/;
case 0x1EC6:/*EEJ*/
return 0x1EC7/*eej*/;
case 0xCC:/*IF*/
return 0xEC/*if*/;
case 0xCD:/*IS*/
return 0xED/*is*/;
case 0x1EC8:/*IR*/
return 0x1EC9/*ir*/;
case 0x128:/*IX*/
return 0x129/*ix*/;
case 0x1ECA:/*IJ*/
return 0x1ECB/*ij*/;
case 0xD2:/*OF*/
return 0xF2/*of*/;
case 0xD3:/*OS*/
return 0xF3/*os*/;
case 0x1ECE:/*OR*/
return 0x1ECF/*or*/;
case 0xD5:/*OX*/
return 0xF5/*ox*/;
case 0x1ECC:/*OJ*/
return 0x1ECD/*oj*/;
case 0xD4:/*OO*/
return 0xF4/*oo*/;
case 0x1ED2:/*OOF*/
return 0x1ED3/*oof*/;
case 0x1ED0:/*OOS*/
return 0x1ED1/*oos*/;
case 0x1ED4:/*OOR*/
return 0x1ED5/*oor*/;
case 0x1ED6:/*OOX*/
return 0x1ED7/*oox*/;
case 0x1ED8:/*OOJ*/
return 0x1ED9/*ooj*/;
case 0x1A0:/*OW*/
return 0x1A1/*ow*/;
case 0x1EDC:/*OWF*/
return 0x1EDD/*owf*/;
case 0x1EDA:/*OWS*/
return 0x1EDB/*ows*/;
case 0x1EDE:/*OWR*/
return 0x1EDF/*owr*/;
case 0x1EE0:/*OWX*/
return 0x1EE1/*owx*/;
case 0x1EE2:/*OWJ*/
return 0x1EE3/*owj*/;
case 0xD9:/*UF*/
return 0xF9/*uf*/;
case 0xDA:/*US*/
return 0xFA/*us*/;
case 0x1EE6:/*UR*/
return 0x1EE7/*ur*/;
case 0x168:/*UX*/
return 0x169/*ux*/;
case 0x1EE4:/*UJ*/
return 0x1EE5/*uj*/;
case 0x1AF:/*UW*/
return 0x1B0/*uw*/;
case 0x1EEA:/*UWF*/
return 0x1EEB/*uwf*/;
case 0x1EE8:/*UWS*/
return 0x1EE9/*uws*/;
case 0x1EEC:/*UWR*/
return 0x1EED/*uwr*/;
case 0x1EEE:/*UWX*/
return 0x1EEF/*uwx*/;
case 0x1EF0:/*UWJ*/
return 0x1EF1/*uwj*/;
case 0x1EF2:/*YF*/
return 0x1EF3/*yf*/;
case 0xDD:/*YS*/
return 0xFD/*ys*/;
case 0x1EF6:/*YR*/
return 0x1EF7/*yr*/;
case 0x1EF8:/*YX*/
return 0x1EF9/*yx*/;
case 0x1EF4:/*YJ*/
return 0x1EF5/*yj*/;
case 0xD0/*Kí tự đặc biệt Ð*/:
return 0x111/*đ*/;
}
if (wch >= 'A' && wch <= 'Z') return wch + 'a' - 'A';
return wch;
}
std::wstring GetLower(const std::wstring& wstr)
{
std::wstring bufferLower;
for (unsigned int i = 0; i < wstr.size(); i++)
{
bufferLower += GetLower(wstr[i]);
}
return bufferLower;
}
int StartWith(const std::wstring& str, const std::wstring& subStr)
{
if (str.size() >= subStr.size() && subStr.size() > 0)
{
for (auto i = 0u; i < subStr.size(); i++)
{
if (str[i] != subStr[i]) return 0;
}
return 1;
}
return 0;
}
int EndWith(const std::wstring& str, const std::wstring& subStr)
{
return (str.size() >= subStr.size() && subStr.size() > 0 && str.substr(str.size() - subStr.size()) == subStr);
}
std::wstring GetWString(const std::string& str)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
// std::wstring buffer;
// int bufferWideCharLength = str.size() + 10;
// wchar_t * bufferWideChar = (wchar_t*)calloc(bufferWideCharLength + 100/*safe*/, sizeof(wchar_t));
// if (bufferWideChar)
// {
// MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, bufferWideChar, bufferWideCharLength);
// buffer += bufferWideChar;
// free(bufferWideChar);
// }
// return buffer;
qwchar* ucs2buffer = (qwchar*)qcalloc(str.size() * 3 + 10/*safe*/, sizeof(qwchar));
if (ucs2buffer)
{
QuangBTConvertUtf8toUnicode((const unsigned char*)(str.c_str()), str.size(), ucs2buffer);
std::wstring bufferW = ucs2buffer;
qfree(ucs2buffer);
return bufferW;
}
//std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
//return myconv.from_bytes(str);
#else
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
return myconv.from_bytes(str);
#endif
}
std::wstring GetWString(const wchar_t* wstr, int length)
{
std::wstring buffer;
if (wstr)
{
for (int ichar = 0; ichar < length && wstr[ichar]; ichar++) buffer += wstr[ichar];
}
return buffer;
}
std::wstring GetWString(const char* str, int length)
{
if (str && length > 0)
{
std::string bufferA;
for (int ichar = 0; ichar < length && str[ichar]; ichar++) bufferA += str[ichar];
qwchar* ucs2buffer = (qwchar*)qcalloc(bufferA.size() * 3 + 10/*safe*/, sizeof(qwchar));
if (ucs2buffer)
{
QuangBTConvertUtf8toUnicode((const unsigned char*)(bufferA.c_str()), bufferA.size(), ucs2buffer);
std::wstring bufferW = ucs2buffer;
qfree(ucs2buffer);
return bufferW;
}
}
return L"";
}
std::wstring GetWString(const char* str, long long int length)
{
if (str && length > 0)
{
std::string bufferA;
for (int ichar = 0; ichar < length && str[ichar]; ichar++) bufferA += str[ichar];
qwchar* ucs2buffer = (qwchar*)qcalloc(bufferA.size() * 3 + 10/*safe*/, sizeof(qwchar));
if (ucs2buffer)
{
QuangBTConvertUtf8toUnicode((const unsigned char*)(bufferA.c_str()), bufferA.size(), ucs2buffer);
std::wstring bufferW = ucs2buffer;
qfree(ucs2buffer);
return bufferW;
}
}
return L"";
}
std::string GetString(const std::wstring& wstr)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
std::string buffer;
int bufferUtf8Length = int(wstr.size() * 4 + 10);
char* bufferUtf8 = (char*)calloc(bufferUtf8Length, sizeof(char));
if (bufferUtf8)
{
QuangBTConvertUnicodetoUtf8(wstr.c_str(), (int)wstr.size(), (unsigned char*)bufferUtf8);
buffer += bufferUtf8;
free(bufferUtf8);
}
return buffer;
#else
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
return myconv.to_bytes(wstr);
#endif
}
/************************************************************************/
/* file */
/************************************************************************/
void DeleteFile(const std::wstring& fileName)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
::DeleteFileW(fileName.c_str());
#else
remove(GetString(fileName).c_str());
#endif
}
void ScanFile(const std::wstring& path, const std::wstring& extension, std::wstringset& fileSet)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
std::wstring wFolder = path;
if (wFolder.size() && wFolder[wFolder.size() - 1] != L'\\') wFolder += L'\\';
wFolder += extension;
WIN32_FIND_DATAW fd;
HANDLE hFind = FindFirstFileW(wFolder.c_str(), &fd);
if (hFind == INVALID_HANDLE_VALUE) return;
do
{
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{//là thư mục
if (wcscmp(fd.cFileName, L".") && wcscmp(fd.cFileName, L".."))
{//không phải là thư mục đặc biệt
std::wstring child = path;
if (child.size() && child[child.size() - 1] != L'\\') child += L'\\';
child += fd.cFileName;
ScanFile(child, extension, fileSet);
}
}
else
{//là file
std::wstring filePath = path;
if (filePath.size() && filePath[filePath.size() - 1] != L'\\') filePath += L'\\';
filePath += fd.cFileName;
fileSet.insert(filePath);
}
} while (FindNextFileW(hFind, &fd));
FindClose(hFind);
#else
DIR* dir = opendir(GetString(path).c_str());
class dirent* ent;
class stat st;
while ((ent = readdir(dir)) != NULL)
{
const std::string file_name = ent->d_name;
const std::wstring full_file_name = path + L"/" + file_name;
if (file_name[0] == '.')
{
/*do nothing*/
}
else if (stat(GetString(full_file_name).c_str(), &st) == -1)
{
/*do nothing*/
}
else
{
const bool is_directory = (st.st_mode & S_IFDIR) != 0;
if (is_directory)
{
ScanFile(full_file_name, extension, fileSet);
}
else
{
fileSet.insert(full_file_name);
}
}
}
closedir(dir);
#endif
}
void GetRealFileName(const std::wstring& filePath, std::wstring& rFilename)
{
rFilename.clear();
for (auto i = 0u; i < filePath.size(); i++)
{
if (filePath[i] == L'\\' || filePath[i] == L'/') rFilename.clear();
else rFilename += filePath[i];
}
auto pos = rFilename.find(L'.');
if (pos != std::wstring::npos) rFilename.erase(pos, rFilename.size() - pos);
}
std::wstring GetRealFileName(const std::wstring& filePath)
{
std::wstring rFilename;
GetRealFileName(filePath, rFilename);
return rFilename;
}
bool ReadFile(const std::wstring& fileName, std::wstring& buffer)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
std::ifstream fileInputStream(fileName, std::ifstream::in | std::ifstream::binary);
#else
std::ifstream fileInputStream(GetString(fileName), std::ifstream::in | std::ifstream::binary);
#endif
if (fileInputStream.is_open())
{
fileInputStream.seekg(0, std::ios_base::end);
long long fileSize = fileInputStream.tellg();
fileInputStream.seekg(0, std::ios_base::beg);
if (fileSize <= 4)
{
char* utf8data = new char[fileSize + 10];
memset(utf8data, 0, fileSize + 10);
fileInputStream.seekg(0, std::ios_base::beg);
fileInputStream.read(utf8data, fileSize);
buffer += GetWString(utf8data, fileSize);
delete[] utf8data;
fileInputStream.close();
return true;
}
bool flagValidateToRead = true;
char byteOrderMark1 = 0;
char byteOrderMark2 = 0;
char byteOrderMark3 = 0;
char byteOrderMark4 = 0;
fileInputStream.read(&byteOrderMark1, 1);
fileInputStream.read(&byteOrderMark2, 1);
fileInputStream.read(&byteOrderMark3, 1);
fileInputStream.read(&byteOrderMark4, 1);
fileInputStream.seekg(0, std::ios_base::beg);
#pragma warning(push)
#pragma warning(disable : 4310)
if (byteOrderMark1 == char(0xFE) && byteOrderMark2 == char(0xFF))
{//Unicode Big Endian
std::Show(std::wstring(L"Lỗi"), L"Error: Unsupported encode (Unicode Big Endian) with %ls \n", fileName.c_str());
flagValidateToRead = false;
}
else if (byteOrderMark1 == (char)0x00 && byteOrderMark2 == (char)0x00 && byteOrderMark3 == (char)0xFE && byteOrderMark4 == (char)0xFF)
{//UTF-32 Big Endian
std::Show(std::wstring(L"Lỗi"), L"Error: Unsupported encode (UTF-32 Big Endian) with %ls \n", fileName.c_str());
flagValidateToRead = false;
}
else if (byteOrderMark1 == (char)0xFF && byteOrderMark2 == (char)0xFE && byteOrderMark3 == (char)0x00 && byteOrderMark4 == (char)0x00)
{//UTF-32 Little Endian
std::Show(std::wstring(L"Lỗi"), L"Error: Unsupported encode (UTF-32 Little Endian) with %ls \n", fileName.c_str());
flagValidateToRead = false;
}
else if ((byteOrderMark1 == (char)0x2B && byteOrderMark2 == (char)0x2F && byteOrderMark3 == (char)0x76))
{//UTF-7
std::Show(std::wstring(L"Lỗi"), L"Error: Unsupported encode (UTF-7) with %ls \n", fileName.c_str());
flagValidateToRead = false;
}
else if (byteOrderMark1 == (char)0xFF && byteOrderMark2 == (char)0xFE)
{//Unicode Little Endian
wchar_t* unicodeData = new wchar_t[fileSize * 2 + 10];
memset(unicodeData, 0, fileSize * 2 + 10);
fileInputStream.seekg(2, std::ios_base::beg);
fileInputStream.read((char*)unicodeData, fileSize);
buffer += unicodeData;
delete[] unicodeData;
}
else if (byteOrderMark1 == (char)0xEF && byteOrderMark2 == (char)0xBB && byteOrderMark3 == (char)0xBF)
{//utf8
char* utf8data = new char[fileSize + 10];
memset(utf8data, 0, fileSize + 10);
fileInputStream.seekg(3, std::ios_base::beg);
fileInputStream.read(utf8data, fileSize);
buffer += GetWString(utf8data, fileSize);
delete[] utf8data;
}
else
{//utf-8 without BOM
char* utf8data = (char*)calloc(fileSize + 10, sizeof(char));
if (utf8data)
{
fileInputStream.seekg(0, std::ios_base::beg);
fileInputStream.read(utf8data, fileSize);
buffer += GetWString(utf8data, fileSize);
free(utf8data);
}
else
{
std::Show(std::wstring(L"Lỗi"), L"Can not malloc to open file %ls \n", fileName.c_str());
}
}
#pragma warning(pop)
fileInputStream.close();
return flagValidateToRead;
}
else
{
std::Show(std::wstring(L"Lỗi"), L"Error: Can not open file %ls \n", fileName.c_str());
return false;
}
}
std::wstring ReadFile(const std::wstring& fileName)
{
std::wstring bufferContent;
ReadFile(fileName, bufferContent);
return bufferContent;
}
void WriteFile(const std::wstring& fileName, const std::wstring& wstr, bool truncate)
{
for (auto i = 0u; i < fileName.size(); i++)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
if (fileName[i] == L'\\') ::CreateDirectoryW(fileName.substr(0, i).c_str(), 0);
#else
int mkdir(const char* path, mode_t mode);
if (fileName[i] == L'/') mkdir(GetString(fileName.substr(0, i)).c_str(), 0777);
#endif
}
if (truncate) DeleteFile(fileName);
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
std::ofstream fileHandle(fileName, std::ios_base::out | std::ios_base::binary | (truncate ? std::ios_base::trunc : (std::ios_base::app | std::ios_base::ate)));
#else
std::ofstream fileHandle(GetString(fileName), std::ios_base::out | std::ios_base::binary | (truncate ? std::ios_base::trunc : (std::ios_base::app | std::ios_base::ate)));
#endif
if (fileHandle.is_open())
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
int buffUtf8Size = int(wstr.size() * 2 + 10);
char* buffUtf8 = new char[buffUtf8Size];
memset(buffUtf8, 0, buffUtf8Size);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), buffUtf8, buffUtf8Size, 0, 0);
fileHandle.write(buffUtf8, strlen(buffUtf8));
fileHandle.close();
delete[] buffUtf8;
#else
std::string bufferToWrite = GetString(wstr);
int writeSize = bufferToWrite.size();
unsigned int beforeWriteOffset = fileHandle.tellp();
fileHandle.write(bufferToWrite.c_str(), writeSize);
unsigned int affterWriteOffset = fileHandle.tellp();
unsigned int writtenSize = affterWriteOffset - beforeWriteOffset;
fileHandle.close();
if (writtenSize != writeSize)
{
std::Show(std::wstring(L"Lỗi"), L"Error: write file %ls (utf8 size == %d bytes, write == %d bytes)\n", fileName.c_str(), writeSize, writtenSize);
//return false;
}
//else return true;
#endif
}
else
{
std::Show(std::wstring(L"Lỗi"), L"Error: Can not open to write file %ls \n", fileName.c_str());
}
}
}
void main(void)
{
// wchar_t testStr[] = L"Tôi làm việ ở ban công ngệ FPT, tôi là người viêt nam. hôm nay tôi ko thích ăn mì tôm. tôi làm đc 2 bài tập.";
// VietnameseTextNormalizer vntObject;
// vntObject.Input(testStr);
// vntObject.Normalize();
// vntObject.GenStandardText();
// ::MessageBoxW(0, vntObject.standardText, L"Output", MB_OK);
std::SetTitle(L"Vietnamese Text Normalizer");
std::HideCursor();
std::SetBackgroundColor(15);
std::SetTextColor(2);
std::ClearScreen();
#ifdef _DEBUG
std::DeleteFile(L"log.txt");
#endif
wprintf(L"Huong dan su dung :\n");
wprintf(L" Dat toan bo tap tin van ban (*.txt) vao thu muc txt\n");
wprintf(L" Dat tool nay ngang hang voi thu muc txt va chay tool\n");
wprintf(L"==>xem thu muc fix de lay ket qua\n\n");
wprintf(L"...An phim bat ki de bat dau...\n");
#ifndef _DEBUG
_getch();
#endif // !_DEBUG
std::wstringset textFileSet;
std::ScanFile(L"", L"*.*", textFileSet);
int countTotalChange = 0;
for (auto itxt = textFileSet.begin(); itxt != textFileSet.end(); itxt++)
{
if ((std::EndWith(std::GetLower(*itxt), L".txt")
|| std::EndWith(std::GetLower(*itxt), L".ftext")
|| std::EndWith(std::GetLower(*itxt), L".info")
|| std::EndWith(std::GetLower(*itxt), L".xml")
|| std::EndWith(std::GetLower(*itxt), L".html")
|| std::EndWith(std::GetLower(*itxt), L".xhtml")
)
&& std::GetRealFileName(*itxt) != L"info"
&& std::GetRealFileName(*itxt) != L"log"
&& (!StartWith(*itxt, L"fix\\")))
{
std::GoToXY(0, 15);
wprintf(L" ");
std::GoToXY(0, 15);
wprintf(L"Read file %s..", itxt->c_str());
std::wstring fileContent = std::ReadFile(*itxt);
wprintf(L"fix..");
VietnameseTextNormalizer vntObject;
//vntObject.flagStandardTextForTTS = true;
#ifdef _DEBUG
vntObject.logFile = fopen("log.txt", "a");
#endif
vntObject.Input(fileContent.c_str());
vntObject.Normalize();
vntObject.GenStandardText();
wprintf(L"save..");
if (vntObject.standardText && vntObject.standardTextChange > 0)
{
std::WriteFile(L"fix\\" + (*itxt), std::wstring(vntObject.standardText), true);
countTotalChange += vntObject.standardTextChange;
#ifdef _DEBUG
//vntObject.logFile = fopen("log.txt", "a");
fseek(vntObject.logFile, 0, SEEK_END);
vntObject.Log("\n%ls :\n", std::GetRealFileName(*itxt).c_str());
std::wstringset listedSet;
for (auto textNode = vntObject.head; textNode != NULL; textNode = textNode->next)
{
if (textNode->originalText)
{
bool flagCurrentNodeIsChange = (textNode->textLength != textNode->originalTextLength);
if (flagCurrentNodeIsChange == false)
{
for (int ichar = 0; ichar < textNode->textLength; ichar++)
{
if (textNode->text[ichar] != textNode->originalText[ichar])
{
flagCurrentNodeIsChange = true;
ichar = textNode->textLength/*soft break*/;
}
}
}
if (flagCurrentNodeIsChange)
{
std::wstring currentList = std::GetWString(textNode->originalText, textNode->originalTextLength) + L" " + std::GetWString(textNode->text, textNode->textLength);
if (listedSet.insert(currentList).second == true)
{
std::wstring originalSyllable = std::GetWString(textNode->originalText, textNode->originalTextLength);
std::wstring newSyllable = std::GetWString(textNode->text, textNode->textLength);
std::wstring originalBase = GetBase(originalSyllable);
std::wstring newBase = GetBase(newSyllable);
if (originalBase != newBase)
{
vntObject.Log("\t\t+Fix \"");
vntObject.Log(textNode->originalText, textNode->originalTextLength);
vntObject.Log("\" - %d character ----> \"", textNode->originalTextLength);
vntObject.Log(textNode->text, textNode->textLength);
vntObject.Log("\" - %d character\n", textNode->textLength);
}
}
}
}
}