5
5
#include <stdlib.h>
6
6
7
7
#define MAX_COL 4
8
- #define MAX_ROW 32
8
+ #define MAX_ROW 20
9
9
#define YEL "\x1b[33m"
10
10
#define RESET "\x1b[0m"
11
11
12
12
typedef struct parameter {
13
13
14
14
bool showAll ; //--all shows all available ascii codes
15
- bool showAllDigits ; //--digits shows all digits ascii codes
16
- bool showAllAlphas ; //--alphas shows all alphabets ascii codes
15
+ uint8_t showAllDigits ; //--digits shows all digits ascii codes
16
+ uint8_t showAllAlphas ; //--alphas shows all alphabets ascii codes
17
17
18
- bool onlyOct ; //--octa shows only octa in output table
18
+ uint8_t showControlChars ;//--controls shows all control ascii codes
19
+ uint8_t showSpecialChars ;//--specials shows all special ascii codes
20
+
21
+ bool onlyOct ; //--oct shows only octa in output table
19
22
bool onlyDec ; //--dec shows only dec in output table
20
23
bool onlyHex ; //--hex shows only hex in output table
21
24
22
25
bool onlyChar ; // hardcoded and set to true(1) always shows `chr` column
23
26
bool _onlyAll ; // if all only* 3 are false this will be set to true
24
27
25
- char * content ; // " " content / data (user input)
28
+ uint8_t * content ; // " " content / data (user input)
29
+ uint16_t contentSize ; // content size
26
30
27
31
uint8_t order ; //0 - default , (--asc)1 - ascending, (--des)2 - desending ordered output table
28
32
bool color ; //--vt100 register ansi vt100 escape sequence color to the terminal
@@ -66,7 +70,41 @@ void splashScreen(){
66
70
"035 29 1D " YEL "GS (group separator)" RESET " | 075 61 3D " YEL "=" RESET " | 135 93 5D " YEL "]" RESET " | 175 125 7D " YEL "}" RESET "\n"
67
71
"036 30 1E " YEL "RS (record separator)" RESET " | 076 62 3E " YEL ">" RESET " | 136 94 5E " YEL "^" RESET " | 176 126 7E " YEL "~" RESET "\n"
68
72
"037 31 1F " YEL "US (unit separator)" RESET " | 077 63 3F " YEL "?" RESET " | 137 95 5F " YEL "_" RESET " | 177 127 7F " YEL "DEL" RESET "\n"
69
- "---------------------------------------------------------------------------------------------------------------\n" );
73
+ "--------------------------------------------+-----------------------+---------------------+--------------------\n" );
74
+ }
75
+
76
+ uint8_t isHex ( char * _hexadecimal ){
77
+ return strlen (_hexadecimal ) >= 3 && _hexadecimal [0 ]== '0' && (_hexadecimal [1 ]== 'x' || _hexadecimal [1 ]== 'X' );
78
+ }
79
+
80
+ uint8_t isOct ( char * _octal ){
81
+ if (strlen (_octal ) <= 1 || _octal [strlen (_octal )- 1 ] != 'o' ) return 0 ;
82
+
83
+ for (uint8_t i = 0 ; i < strlen (_octal )- 1 ; i ++ ){
84
+ if (!(_octal [i ] >= 48 && _octal [i ] <= 55 )) return 0 ;
85
+ }
86
+
87
+ return 8 ;
88
+ }
89
+
90
+ uint8_t isBin ( char * _binary ){
91
+ if (strlen (_binary ) <= 1 || _binary [strlen (_binary )- 1 ] != 'b' ) return 0 ;
92
+
93
+ for (uint8_t i = 0 ; i < strlen (_binary )- 1 ; i ++ ){
94
+ if (!(_binary [i ] == 48 || _binary [i ] == 49 )) return 0 ;
95
+ }
96
+
97
+ return 2 ;
98
+ }
99
+
100
+ uint8_t isDec ( char * _decimal ){
101
+ if (strlen (_decimal ) <= 1 || _decimal [strlen (_decimal )- 1 ] != 'd' ) return 0 ;
102
+
103
+ for (uint8_t i = 0 ; i < strlen (_decimal )- 1 ; i ++ ){
104
+ if (!(_decimal [i ] >= 48 && _decimal [i ] <= 57 )) return 0 ;
105
+ }
106
+
107
+ return 10 ;
70
108
}
71
109
72
110
asciiParams parseParameter (int argv , char * * args ){
@@ -76,30 +114,49 @@ asciiParams parseParameter(int argv, char** args){
76
114
for (uint8_t i = 1 ; i < argv ; i ++ ){
77
115
78
116
if (!(strlen (args [i ]) >= 2 && args [i ][0 ] == '-' && args [i ][1 ] == '-' )){
79
- if (params .content == NULL ) params .content = args [i ];
80
- else strcat (params .content , args [i ]);
117
+
118
+ uint8_t base = 0 ;
119
+ if ( (base = isHex (args [i ])) || (base = isOct (args [i ])) || (base = isBin ( args [i ])) || (base = isDec ( args [i ])) ){
120
+ uint8_t number = (uint8_t ) strtol (args [i ],NULL ,isHex (args [i ])?0 :base );
121
+
122
+ free (args [i ]);
123
+ args [i ] = (uint8_t * ) calloc (2 ,sizeof (uint8_t ));
124
+
125
+ args [i ][0 ] = number ;
126
+ args [i ][1 ] = '\0' ;
127
+ }
81
128
129
+ if (params .content == NULL ) params .content = (uint8_t * )args [i ];
130
+ else strcat (params .content ,(uint8_t * ) args [i ]);
131
+
132
+ params .contentSize ++ ;
82
133
continue ;
83
134
}
84
135
85
136
if (strcmp (args [i ],"--all" ) == 0 ) params .showAll = true;
86
- else if (strcmp (args [i ],"--digits" ) == 0 ) params .showAllDigits = true;
87
- else if (strcmp (args [i ],"--alphas" ) == 0 ) params .showAllAlphas = true;
137
+ else if (strcmp (args [i ],"--digits" ) == 0 ) params .showAllDigits = 10 ;
138
+ else if (strcmp (args [i ],"--alphas" ) == 0 ) params .showAllAlphas = 52 ;
139
+
140
+ else if (strcmp (args [i ],"--controls" ) == 0 ) params .showControlChars = 0 ;
141
+ else if (strcmp (args [i ],"--specials" ) == 0 ) params .showSpecialChars = 35 ;
88
142
89
- else if (strcmp (args [i ],"--octa " ) == 0 ) params .onlyOct = true;
143
+ else if (strcmp (args [i ],"--oct " ) == 0 ) params .onlyOct = true;
90
144
else if (strcmp (args [i ],"--dec" ) == 0 ) params .onlyDec = true;
91
145
else if (strcmp (args [i ],"--hex" ) == 0 ) params .onlyHex = true;
92
146
// else if(strcmp(args[i],"--char") == 0) params.onlyChar = true;
93
147
94
- else if (strcmp (args [i ],"--asc" )== 0 ) params .order = 1 ;
95
- else if (strcmp (args [i ],"--des " )== 0 ) params .order = 2 ;
148
+ else if (strcmp (args [i ],"--asc" )== 0 ) params .order = 1 ;
149
+ else if (strcmp (args [i ],"--desc " )== 0 ) params .order = 2 ;
96
150
97
151
else if (strcmp (args [i ],"--vt100" )== 0 ) params .color = true;
98
152
99
153
}
100
154
101
155
params .onlyChar = 1 ; //hardcoded;
102
156
params ._onlyAll = !(params .onlyDec || params .onlyHex || params .onlyOct );
157
+
158
+ params .content = params .content == NULL ?(uint8_t * )calloc (1 ,sizeof (uint8_t )):params .content ;
159
+ // params.contentSize = strlen(params.content);
103
160
104
161
return params ;
105
162
@@ -110,8 +167,7 @@ static int desCmp(const void* a, const void* b){return *(char *)a < *(char *)b;}
110
167
void manipulateData (asciiParams * params ){
111
168
112
169
char occur [256 ] = {0 };int i ;int idx = 0 ;
113
-
114
- for (i = 0 ; i < strlen (params -> content ); i ++ ){
170
+ for (i = 0 ; i < params -> contentSize ; i ++ ){
115
171
if (!occur [params -> content [i ]]){
116
172
if (idx != i ){
117
173
params -> content [idx ] = params -> content [i ];
@@ -123,41 +179,71 @@ void manipulateData(asciiParams *params){
123
179
}
124
180
125
181
char * tmp = (char * )malloc ((idx ) * sizeof (char ));
182
+ params -> contentSize = idx ;
183
+
126
184
strncpy (tmp , params -> content , idx );tmp [idx ] = '\0' ;
127
185
params -> content = tmp ;
128
186
129
187
if (params -> order ){
130
- if (params -> order == 1 ) qsort (params -> content , strlen ( params -> content ) , sizeof (char ), ascCmp );
131
- else qsort (params -> content , strlen ( params -> content ) , sizeof (char ), desCmp );
188
+ if (params -> order == 1 ) qsort (params -> content , params -> contentSize , sizeof (char ), ascCmp );
189
+ else qsort (params -> content , params -> contentSize , sizeof (char ), desCmp );
132
190
}
133
191
}
134
192
135
193
void printData (asciiParams params ){
136
194
137
- size_t s = 0 ;char se_line [20 ] = "" ;
138
- if (params ._onlyAll ) printf ("Oct Dec Hex " YEL "Chr" RESET "\n------------------\n" );
139
- else {
140
- if (params .onlyOct ){ printf ("Oct " ); strcat (se_line ,"-----" );}
141
- if (params .onlyDec ){ printf ("Dec " ); strcat (se_line ,"-----" );}
142
- if (params .onlyHex ){ printf ("Hex " ); strcat (se_line ,"-----" );}
143
- if (params .onlyChar ){ printf (YEL "Chr" RESET ); strcat (se_line ,"---" );}
144
- printf ("\n%s\n" ,se_line );
145
- }
195
+ uint8_t col = (params .contentSize /MAX_ROW ) + 1 ;
196
+ uint8_t row = col > 1 ?MAX_ROW :params .contentSize ;
197
+
198
+ char * * lines = (char * * )calloc (row ,sizeof (char * ));
199
+ size_t s = 0 ;
146
200
147
- while (strlen (params .content ) - s ){
148
- if (params ._onlyAll ) printf ("%3o %3d %2X " YEL "%c" RESET "\n" , params .content [s ], params .content [s ], params .content [s ], params .content [s ]);
149
- else {
150
- if (params .onlyOct ) printf ("%3o " , params .content [s ]);
151
- if (params .onlyDec ) printf ("%3d " , params .content [s ]);
152
- if (params .onlyHex ) printf (" %2X " , params .content [s ]);
153
- if (params .onlyChar ) printf (YEL "%c" RESET , params .content [s ]);
154
- printf ("\n" );
155
- }
201
+ for (uint8_t i = 0 ; i < row ; i ++ )
202
+ lines [i ] = (char * )calloc (200 ,sizeof (char ));
203
+
204
+ while (params .contentSize - s ){
205
+ char tmp [20 ];
206
+
207
+ snprintf (tmp , sizeof (tmp ), "%03o " , params .content [s ]);
208
+ if (params .onlyOct || params ._onlyAll ) strcat (lines [s %row ],tmp );
209
+
210
+ snprintf (tmp , sizeof (tmp ), "%3d " , params .content [s ]);
211
+ if (params .onlyDec || params ._onlyAll ) strcat (lines [s %row ],tmp );
212
+
213
+ snprintf (tmp , sizeof (tmp ), " %2X " , params .content [s ]);
214
+ if (params .onlyHex || params ._onlyAll ) strcat (lines [s %row ],tmp );
215
+
216
+ snprintf (tmp , sizeof (tmp ),YEL "%c " RESET , params .content [s ]);
217
+ if (params .onlyChar || params ._onlyAll ) strcat (lines [s %row ],tmp );
218
+
219
+ if ((s /row )+ 1 != col ) strcat (lines [s %row ]," | " );
156
220
s ++ ;
157
221
}
158
222
159
- if (params ._onlyAll ) printf ("------------------\n" );
160
- else printf ("%s" ,se_line );
223
+ uint16_t colLineLength = (params .onlyOct || params ._onlyAll ?5 :0 ) + (params .onlyDec || params ._onlyAll ?5 :0 ) + (params .onlyHex || params ._onlyAll ?5 :0 ) + (params .onlyChar || params ._onlyAll ?5 :0 );
224
+
225
+ for (uint8_t i = 0 ; i < col ; i ++ ){
226
+ if (params .onlyOct || params ._onlyAll ) printf ("Oct " );
227
+ if (params .onlyDec || params ._onlyAll ) printf ("Dec " );
228
+ if (params .onlyHex || params ._onlyAll ) printf ("Hex " );
229
+ if (params .onlyChar || params ._onlyAll ) printf (YEL "Chr" RESET );
230
+ printf (" " );
231
+ }printf ("\n" RESET );
232
+
233
+ for (uint8_t i = 1 ; i <= colLineLength * col ; i ++ ){
234
+ if (i %colLineLength == 0 && i != colLineLength * col ) printf ("+--" );
235
+ else printf ("-" );
236
+ }printf ("\n" RESET );
237
+
238
+ for (uint8_t i = 0 ; i < row ; i ++ ){
239
+ printf ("%s\n" ,lines [i ]);
240
+ }printf (RESET );
241
+
242
+ for (uint8_t i = 1 ; i <= colLineLength * col ; i ++ ){
243
+ if (i %colLineLength == 0 && i != colLineLength * col ) printf ("+--" );
244
+ else printf ("-" );
245
+ }printf ("\n" RESET );
246
+
161
247
}
162
248
163
249
@@ -178,12 +264,22 @@ int main(int argv, char** args){
178
264
if (params .showAll ){
179
265
splashScreen ();
180
266
return 0 ;
181
- }else if (params .showAllAlphas && params .showAllDigits ){
182
- params .content = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ;
183
- }else if (params .showAllAlphas ){
184
- params .content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ;
185
- }else if (params .showAllDigits ){
186
- params .content = "0123456789" ;
267
+ }
268
+
269
+ uint16_t mem = params .showAllAlphas + params .showAllDigits + params .showSpecialChars + params .showControlChars ;
270
+
271
+ if (mem ){
272
+ uint8_t * cpy = (uint8_t * ) calloc (strlen (params .content ),sizeof (uint8_t ));
273
+ strcpy (cpy ,params .content );
274
+
275
+ params .content = (uint8_t * ) calloc (mem + strlen (cpy )+ 1 , sizeof (uint8_t ));
276
+ strcat (params .content , cpy );
277
+
278
+ if (params .showAllAlphas ) strcat (params .content ,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" );
279
+ if (params .showAllDigits ) strcat (params .content ,"0123456789" );
280
+ if (params .showSpecialChars ) strcat (params .content ," !\"#$%%&\'()*+,-./:;<=>?@[\\]^_`{|}~" );
281
+ if (params .showControlChars ) ;
282
+
187
283
}
188
284
189
285
//@todo: print the data first in the center
0 commit comments