Skip to content

Commit e8e451b

Browse files
committed
add commands, update table
1 parent d6d8265 commit e8e451b

File tree

3 files changed

+175
-44
lines changed

3 files changed

+175
-44
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,46 @@ ascii [characters...] [..options]
3131
`--all` shows all available ascii codes
3232
`--digits` shows all digits ascii codes
3333
`--alphas` shows all alphabets ascii codes
34+
`--specials`shows all special char ascii codes
35+
`--controls`shows all control ascii codes (non printables)
3436

3537
`--octa` shows only octa in output table
3638
`--dec` shows only dec in output table
3739
`--hex` shows only hex in output table
3840

3941
`--asc` shows output table in ascending order
40-
`--des` shows output table in desending order
42+
`--desc` shows output table in desending order
4143

4244
`--vt100` register ansi vt100 escape sequence color to the terminal & terminate
45+
46+
#### Input Types:
47+
`0000b` represents 0 in binary (add b at last)
48+
`0d` represents 0 in decimal (add d at last)
49+
`0o` represents 0 in octal (add o at last)
50+
`0x00` represents 0 in hexa (add 0x at first)
51+
52+
#### Examples
53+
```bash
54+
ascii abcd
55+
ascii "manojtgn"
56+
ascii "helloworld"
57+
58+
ascii "make it ascending" --asc
59+
ascii "make it descending" --desc
60+
61+
ascii 0x78
62+
ascii 0x78 69d
63+
ascii 0x78 15c 69d 01001001b
64+
65+
ascii --alphas
66+
ascii --digits --specials
67+
ascii --digits --alphas --specials --controls
68+
ascii --all
69+
70+
ascii "show octal & decimal" --oct --dec
71+
ascii "show only hex" --hex
72+
73+
ascii --vt100
74+
75+
ascii
76+
```

makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ BINS = main
44
all: $(BINS)
55

66
main:
7+
if not exist build mkdir build
78
$(CC) -o build/ascii.exe src/main.c

src/main.c

Lines changed: 139 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@
55
#include <stdlib.h>
66

77
#define MAX_COL 4
8-
#define MAX_ROW 32
8+
#define MAX_ROW 20
99
#define YEL "\x1b[33m"
1010
#define RESET "\x1b[0m"
1111

1212
typedef struct parameter{
1313

1414
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
1717

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
1922
bool onlyDec; //--dec shows only dec in output table
2023
bool onlyHex; //--hex shows only hex in output table
2124

2225
bool onlyChar; // hardcoded and set to true(1) always shows `chr` column
2326
bool _onlyAll; // if all only* 3 are false this will be set to true
2427

25-
char* content; // " " content / data (user input)
28+
uint8_t* content; // " " content / data (user input)
29+
uint16_t contentSize; // content size
2630

2731
uint8_t order; //0 - default , (--asc)1 - ascending, (--des)2 - desending ordered output table
2832
bool color; //--vt100 register ansi vt100 escape sequence color to the terminal
@@ -66,7 +70,41 @@ void splashScreen(){
6670
"035 29 1D "YEL"GS (group separator)"RESET" | 075 61 3D "YEL"="RESET" | 135 93 5D "YEL"]"RESET" | 175 125 7D "YEL"}"RESET"\n"
6771
"036 30 1E "YEL"RS (record separator)"RESET" | 076 62 3E "YEL">"RESET" | 136 94 5E "YEL"^"RESET" | 176 126 7E "YEL"~"RESET"\n"
6872
"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;
70108
}
71109

72110
asciiParams parseParameter(int argv, char** args){
@@ -76,30 +114,49 @@ asciiParams parseParameter(int argv, char** args){
76114
for(uint8_t i = 1; i < argv; i++){
77115

78116
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+
}
81128

129+
if(params.content == NULL) params.content = (uint8_t*)args[i];
130+
else strcat(params.content,(uint8_t*) args[i]);
131+
132+
params.contentSize++;
82133
continue;
83134
}
84135

85136
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;
88142

89-
else if(strcmp(args[i],"--octa") == 0) params.onlyOct = true;
143+
else if(strcmp(args[i],"--oct") == 0) params.onlyOct = true;
90144
else if(strcmp(args[i],"--dec") == 0) params.onlyDec = true;
91145
else if(strcmp(args[i],"--hex") == 0) params.onlyHex = true;
92146
// else if(strcmp(args[i],"--char") == 0) params.onlyChar = true;
93147

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;
96150

97151
else if(strcmp(args[i],"--vt100")==0) params.color = true;
98152

99153
}
100154

101155
params.onlyChar = 1; //hardcoded;
102156
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);
103160

104161
return params;
105162

@@ -110,8 +167,7 @@ static int desCmp(const void* a, const void* b){return *(char *)a < *(char *)b;}
110167
void manipulateData(asciiParams *params){
111168

112169
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++){
115171
if(!occur[params->content[i]]){
116172
if(idx != i){
117173
params->content[idx] = params->content[i];
@@ -123,41 +179,71 @@ void manipulateData(asciiParams *params){
123179
}
124180

125181
char* tmp = (char *)malloc((idx) * sizeof(char));
182+
params->contentSize = idx;
183+
126184
strncpy(tmp, params->content, idx);tmp[idx] = '\0';
127185
params->content = tmp;
128186

129187
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);
132190
}
133191
}
134192

135193
void printData(asciiParams params){
136194

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;
146200

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]," | ");
156220
s++;
157221
}
158222

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+
161247
}
162248

163249

@@ -178,12 +264,22 @@ int main(int argv, char** args){
178264
if(params.showAll){
179265
splashScreen();
180266
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+
187283
}
188284

189285
//@todo: print the data first in the center

0 commit comments

Comments
 (0)