33#include < iostream>
44#include < iomanip>
55#include < stdint.h>
6+ #include < stdio.h>
67
78#include < io.h>
89#include < fcntl.h>
@@ -24,7 +25,167 @@ enum _BR{
2425 _E = L' '
2526};
2627
28+ bool isPrintable (wchar_t character){
29+ return !(character >= 0 && character <= 32 || character == 127 );
30+ }
31+
32+ wchar_t * getPrintable (wchar_t content){
33+ if (!(content >= 0 && content <= 32 || content == 127 )) return L" " ;
34+
35+ switch (content){
36+ case 0 : return L" NUL (null)" ;
37+ case 1 : return L" SOH (start of heading)" ;
38+ case 2 : return L" STX (start of text)" ;
39+ case 3 : return L" ETX (end of text)" ;
40+ case 4 : return L" EOT (end of transmission)" ;
41+ case 5 : return L" ENQ (enquiry)" ;
42+ case 6 : return L" ACK (acknowledge)" ;
43+ case 7 : return L" BEL (bell)" ;
44+ case 8 : return L" BS (backspace)" ;
45+ case 9 : return L" TAB (horizontal tab)" ;
46+ case 10 : return L" LF (NL line feed, new line)" ;
47+ case 11 : return L" VT (vertical tab)" ;
48+ case 12 : return L" FF (NP form feed, new page)" ;
49+ case 13 : return L" CR (carriage return)" ;
50+ case 14 : return L" SO (shift out)" ;
51+ case 15 : return L" SI (shift in)" ;
52+ case 16 : return L" DLE (data link escape)" ;
53+ case 17 : return L" DC1 (device control 1)" ;
54+ case 18 : return L" DC2 (device control 2)" ;
55+ case 19 : return L" DC3 (device control 3)" ;
56+ case 20 : return L" DC4 (device control 4)" ;
57+ case 21 : return L" NAK (negative acknowledge)" ;
58+ case 22 : return L" SYN (synchronous idle)" ;
59+ case 23 : return L" ETB (end of trans. block)" ;
60+ case 24 : return L" CAN (cancel)" ;
61+ case 25 : return L" EM (end of medium)" ;
62+ case 26 : return L" SUB (substitute)" ;
63+ case 27 : return L" ESC (escape)" ;
64+ case 28 : return L" FS (file separator)" ;
65+ case 29 : return L" GS (group separator)" ;
66+ case 30 : return L" RS (record separator)" ;
67+ case 31 : return L" US (unit separator)" ;
68+ case 32 : return L" SPACE" ;
69+ case 127 :return L" DEL (delete control)" ;
70+ default : return L" " ;
71+ }
72+ }
73+
2774void _renderTable (asciiParams params){
2875 // _setmode(_fileno(stdin), _O_U16TEXT);
29- // _setmode(_fileno(stdout), _O_U16TEXT);
76+ _setmode (_fileno (stdout), _O_U16TEXT);
77+
78+ uint8_t col = (params.contentSize %MAX_ROW) == 0 ?(params.contentSize /MAX_ROW):(params.contentSize /MAX_ROW)+1 ;
79+ uint8_t row = col > 1 ?MAX_ROW:params.contentSize ;
80+
81+ uint8_t * maxLength = (uint8_t *) calloc (col,sizeof (uint8_t ));
82+ wstring* lines = new wstring[row];
83+ size_t s = 0 ;
84+
85+ uint8_t currRow = s%row;
86+ uint8_t currCol = (s/row)+1 ;
87+ uint8_t procCol = 0 ;
88+ uint8_t currLineLength = 0 ;
89+
90+ while (params.contentSize - s){
91+ wchar_t tmp[100 ];
92+ currRow = s%row;
93+ currCol = (s/row)+1 ;
94+
95+ if (currCol != procCol){
96+ for (uint8_t i = 0 ; i < row && s+i < params.contentSize ; i++){
97+ currLineLength = wcslen ( getPrintable (params.content [s+i]) ) - 3 ;
98+ maxLength[currCol-1 ] = maxLength[currCol-1 ] < currLineLength?currLineLength:maxLength[currCol-1 ];
99+ }
100+ procCol = currCol;
101+ }
102+
103+ if (params.onlyOct || params._onlyAll ){
104+ snwprintf (tmp, sizeof (tmp), L" %03o " , params.content [s]);
105+ lines[currRow] += tmp;
106+ }
107+
108+ if (params.onlyDec || params._onlyAll ){
109+ snwprintf (tmp, sizeof (tmp), L" %3d " , params.content [s]);
110+ lines[currRow] += tmp;
111+ }
112+
113+ if (params.onlyHex || params._onlyAll ){
114+ snwprintf (tmp, sizeof (tmp), L" %2X " , params.content [s]);
115+ lines[currRow] += tmp;
116+ }
117+
118+ if (params.onlyBin /* || params._onlyAll*/ ){
119+ uint8_t index = 0 ; wchar_t content = params.content [s];
120+ wstring binStr = L" 0000 0000\0 " ;
121+
122+ for (uint8_t i = 128 ; i > 0 && content != 0 ; ){
123+ if (binStr[index] == L' ' ){
124+ index++;
125+ continue ;
126+ }
127+
128+ if (content >= i){
129+ binStr[index] = L' 1' ;
130+ content -= i;
131+ }
132+
133+ index++;
134+ i=(i/2 );
135+ }
136+
137+ snwprintf (tmp, sizeof (tmp), L" %ls " , binStr);
138+ lines[currRow] += tmp;
139+ }
140+
141+ if (isPrintable (params.content [s])){
142+ snwprintf (tmp, sizeof (tmp),L" " YEL " %c " RESET, params.content [s]);
143+ currLineLength = 0 ;
144+ }else {
145+ wchar_t * print = getPrintable (params.content [s]);
146+ currLineLength = wcslen (print) - 3 ;
147+ snwprintf (tmp, sizeof (tmp),L" " YEL " %ls" RESET, print);
148+ }
149+ if (params.onlyChar || params._onlyAll ) lines[currRow] += tmp;
150+
151+ if (currCol != col){
152+ wchar_t * spaces = (wchar_t *)calloc ( (size_t )maxLength[currCol-1 ] - currLineLength +1 ,sizeof (wchar_t ));
153+ wmemset (spaces, (wchar_t )32 , (size_t )maxLength[currCol-1 ] - currLineLength);
154+
155+ lines[currRow] += spaces;
156+ lines[currRow] += (currLineLength == 0 ?L" " :L" " );
157+ lines[currRow] += (wchar_t )_VL_1;lines[currRow] += L" " ;
158+
159+ free (spaces);
160+ }
161+ s++;
162+ }
163+
164+ uint16_t colLineLength = (params.onlyOct || params._onlyAll ?5 :0 ) + (params.onlyDec || params._onlyAll ?5 :0 ) + (params.onlyHex || params._onlyAll ?5 :0 ) + (params.onlyBin /* || params._onlyAll*/ ?11 :0 ) + (params.onlyChar || params._onlyAll ?5 :0 );
165+
166+ for (uint8_t i = 0 ; i < col; i++){
167+ if (params.onlyOct || params._onlyAll ) wcout<<" Oct " ;
168+ if (params.onlyDec || params._onlyAll ) wcout<<" Dec " ;
169+ if (params.onlyHex || params._onlyAll ) wcout<<" Hex " ;
170+ if (params.onlyBin /* || params._onlyAll*/ ) wcout<<" Binary " ;
171+ if (params.onlyChar || params._onlyAll ) wcout<<YEL " Chr" RESET;
172+
173+ if (col != i+1 ){
174+ wcout<<(wchar_t )_E<<wstring (maxLength[i],(wchar_t )_E)<<(wchar_t )_VL_1<<(wchar_t )_E<<(wchar_t )_E;
175+ }
176+ }wcout<<" \n " RESET;
177+
178+ for (uint8_t i = 0 ; i < col; i++){
179+ wcout<<wstring (maxLength[i],(wchar_t )_HL_1)<<wstring ( (params._onlyAll ?(params.onlyBin ?19 +11 :19 ):(params.onlyDec ?5 :0 ) + (params.onlyOct ?5 :0 ) + (params.onlyHex ?5 :0 ) + (params.onlyBin ?10 :0 ) + (params.onlyChar ?4 :0 )),(wchar_t )_HL_1 );
180+ if (col-1 != i) wcout<<(wchar_t )_IC_1<<(wchar_t )_HL_1<<(wchar_t )_HL_1;
181+ }wcout<<" \n " RESET;
182+
183+ for (uint8_t i = 0 ; i < row; i++){
184+ wcout<<lines[i]<<endl<<RESET;
185+ }wcout<<RESET;
186+
187+ for (uint8_t i = 0 ; i < col; i++){
188+ wcout<<wstring (maxLength[i],(wchar_t )_HL_1)<<wstring ( (params._onlyAll ?(params.onlyBin ?19 +11 :19 ):(params.onlyDec ?5 :0 ) + (params.onlyOct ?5 :0 ) + (params.onlyHex ?5 :0 ) + (params.onlyBin ?10 :0 ) + (params.onlyChar ?4 :0 )),(wchar_t )_HL_1 );
189+ if (col-1 != i) wcout<<(wchar_t )_IB_1<<(wchar_t )_HL_1<<(wchar_t )_HL_1;
190+ }wcout<<" \n " RESET;
30191}
0 commit comments