Skip to content

Commit

Permalink
add commands, update table
Browse files Browse the repository at this point in the history
  • Loading branch information
ManojTGN committed Apr 8, 2024
1 parent d6d8265 commit e8e451b
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 44 deletions.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,46 @@ ascii [characters...] [..options]
`--all` shows all available ascii codes
`--digits` shows all digits ascii codes
`--alphas` shows all alphabets ascii codes
`--specials`shows all special char ascii codes
`--controls`shows all control ascii codes (non printables)

`--octa` shows only octa in output table
`--dec` shows only dec in output table
`--hex` shows only hex in output table

`--asc` shows output table in ascending order
`--des` shows output table in desending order
`--desc` shows output table in desending order

`--vt100` register ansi vt100 escape sequence color to the terminal & terminate

#### Input Types:
`0000b` represents 0 in binary (add b at last)
`0d` represents 0 in decimal (add d at last)
`0o` represents 0 in octal (add o at last)
`0x00` represents 0 in hexa (add 0x at first)

#### Examples
```bash
ascii abcd
ascii "manojtgn"
ascii "helloworld"

ascii "make it ascending" --asc
ascii "make it descending" --desc

ascii 0x78
ascii 0x78 69d
ascii 0x78 15c 69d 01001001b

ascii --alphas
ascii --digits --specials
ascii --digits --alphas --specials --controls
ascii --all

ascii "show octal & decimal" --oct --dec
ascii "show only hex" --hex

ascii --vt100

ascii
```
1 change: 1 addition & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ BINS = main
all: $(BINS)

main:
if not exist build mkdir build
$(CC) -o build/ascii.exe src/main.c
182 changes: 139 additions & 43 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@
#include <stdlib.h>

#define MAX_COL 4
#define MAX_ROW 32
#define MAX_ROW 20
#define YEL "\x1b[33m"
#define RESET "\x1b[0m"

typedef struct parameter{

bool showAll; //--all shows all available ascii codes
bool showAllDigits; //--digits shows all digits ascii codes
bool showAllAlphas; //--alphas shows all alphabets ascii codes
uint8_t showAllDigits; //--digits shows all digits ascii codes
uint8_t showAllAlphas; //--alphas shows all alphabets ascii codes

bool onlyOct; //--octa shows only octa in output table
uint8_t showControlChars;//--controls shows all control ascii codes
uint8_t showSpecialChars;//--specials shows all special ascii codes

bool onlyOct; //--oct shows only octa in output table
bool onlyDec; //--dec shows only dec in output table
bool onlyHex; //--hex shows only hex in output table

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

char* content; // " " content / data (user input)
uint8_t* content; // " " content / data (user input)
uint16_t contentSize; // content size

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

uint8_t isHex( char* _hexadecimal ){
return strlen(_hexadecimal) >= 3 && _hexadecimal[0]=='0' && (_hexadecimal[1]=='x'||_hexadecimal[1]=='X');
}

uint8_t isOct( char* _octal ){
if(strlen(_octal) <= 1 || _octal[strlen(_octal)-1] != 'o') return 0;

for(uint8_t i = 0; i < strlen(_octal)-1; i++){
if(!(_octal[i] >= 48 && _octal[i] <= 55)) return 0;
}

return 8;
}

uint8_t isBin( char* _binary ){
if(strlen(_binary) <= 1 || _binary[strlen(_binary)-1] != 'b') return 0;

for(uint8_t i = 0; i < strlen(_binary)-1; i++){
if(!(_binary[i] == 48 || _binary[i] == 49)) return 0;
}

return 2;
}

uint8_t isDec( char* _decimal ){
if(strlen(_decimal) <= 1 || _decimal[strlen(_decimal)-1] != 'd') return 0;

for(uint8_t i = 0; i < strlen(_decimal)-1; i++){
if(!(_decimal[i] >= 48 && _decimal[i] <= 57)) return 0;
}

return 10;
}

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

if(!(strlen(args[i]) >= 2 && args[i][0] == '-' && args[i][1] == '-')){
if(params.content == NULL) params.content = args[i];
else strcat(params.content, args[i]);

uint8_t base = 0;
if( (base = isHex(args[i])) || (base = isOct(args[i])) || (base = isBin( args[i])) || (base = isDec( args[i])) ){
uint8_t number = (uint8_t) strtol(args[i],NULL,isHex(args[i])?0:base);

free(args[i]);
args[i] = (uint8_t*) calloc(2,sizeof(uint8_t));

args[i][0] = number;
args[i][1] = '\0';
}

if(params.content == NULL) params.content = (uint8_t*)args[i];
else strcat(params.content,(uint8_t*) args[i]);

params.contentSize++;
continue;
}

if (strcmp(args[i],"--all") == 0) params.showAll = true;
else if(strcmp(args[i],"--digits") == 0) params.showAllDigits = true;
else if(strcmp(args[i],"--alphas") == 0) params.showAllAlphas = true;
else if(strcmp(args[i],"--digits") == 0) params.showAllDigits = 10;
else if(strcmp(args[i],"--alphas") == 0) params.showAllAlphas = 52;

else if(strcmp(args[i],"--controls") == 0) params.showControlChars = 0;
else if(strcmp(args[i],"--specials") == 0) params.showSpecialChars = 35;

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

else if(strcmp(args[i],"--asc")==0) params.order = 1;
else if(strcmp(args[i],"--des")==0) params.order = 2;
else if(strcmp(args[i],"--asc")==0) params.order = 1;
else if(strcmp(args[i],"--desc")==0) params.order = 2;

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

}

params.onlyChar = 1; //hardcoded;
params._onlyAll = !(params.onlyDec || params.onlyHex || params.onlyOct);

params.content = params.content == NULL?(uint8_t*)calloc(1,sizeof(uint8_t)):params.content;
// params.contentSize = strlen(params.content);

return params;

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

char occur[256] = {0};int i;int idx = 0;

for(i = 0; i < strlen(params->content); i++){
for(i = 0; i < params->contentSize; i++){
if(!occur[params->content[i]]){
if(idx != i){
params->content[idx] = params->content[i];
Expand All @@ -123,41 +179,71 @@ void manipulateData(asciiParams *params){
}

char* tmp = (char *)malloc((idx) * sizeof(char));
params->contentSize = idx;

strncpy(tmp, params->content, idx);tmp[idx] = '\0';
params->content = tmp;

if(params->order){
if(params->order == 1) qsort(params->content, strlen(params->content), sizeof(char), ascCmp);
else qsort(params->content, strlen(params->content), sizeof(char), desCmp);
if(params->order == 1) qsort(params->content, params->contentSize, sizeof(char), ascCmp);
else qsort(params->content, params->contentSize, sizeof(char), desCmp);
}
}

void printData(asciiParams params){

size_t s = 0;char se_line[20] = "";
if(params._onlyAll) printf("Oct Dec Hex "YEL"Chr"RESET"\n------------------\n");
else{
if(params.onlyOct){ printf("Oct "); strcat(se_line,"-----");}
if(params.onlyDec){ printf("Dec "); strcat(se_line,"-----");}
if(params.onlyHex){ printf("Hex "); strcat(se_line,"-----");}
if(params.onlyChar){ printf(YEL"Chr"RESET); strcat(se_line,"---");}
printf("\n%s\n",se_line);
}
uint8_t col = (params.contentSize/MAX_ROW) + 1;
uint8_t row = col > 1?MAX_ROW:params.contentSize;

char** lines = (char**)calloc(row,sizeof(char*));
size_t s = 0;

while(strlen(params.content) - s){
if(params._onlyAll) printf("%3o %3d %2X "YEL"%c"RESET"\n", params.content[s], params.content[s], params.content[s], params.content[s]);
else{
if(params.onlyOct) printf("%3o ", params.content[s]);
if(params.onlyDec) printf("%3d ", params.content[s]);
if(params.onlyHex) printf(" %2X ", params.content[s]);
if(params.onlyChar) printf(YEL"%c"RESET, params.content[s]);
printf("\n");
}
for(uint8_t i = 0; i < row; i++)
lines[i] = (char*)calloc(200,sizeof(char));

while(params.contentSize - s){
char tmp[20];

snprintf(tmp, sizeof(tmp), "%03o ", params.content[s]);
if(params.onlyOct || params._onlyAll) strcat(lines[s%row],tmp);

snprintf(tmp, sizeof(tmp), "%3d ", params.content[s]);
if(params.onlyDec || params._onlyAll) strcat(lines[s%row],tmp);

snprintf(tmp, sizeof(tmp), " %2X ", params.content[s]);
if(params.onlyHex || params._onlyAll) strcat(lines[s%row],tmp);

snprintf(tmp, sizeof(tmp),YEL"%c "RESET, params.content[s]);
if(params.onlyChar || params._onlyAll) strcat(lines[s%row],tmp);

if((s/row)+1 != col) strcat(lines[s%row]," | ");
s++;
}

if(params._onlyAll) printf("------------------\n");
else printf("%s",se_line);
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);

for(uint8_t i = 0; i < col; i++){
if(params.onlyOct || params._onlyAll) printf("Oct ");
if(params.onlyDec || params._onlyAll) printf("Dec ");
if(params.onlyHex || params._onlyAll) printf("Hex ");
if(params.onlyChar|| params._onlyAll) printf(YEL"Chr"RESET);
printf(" ");
}printf("\n"RESET);

for(uint8_t i = 1; i <= colLineLength*col; i++){
if(i%colLineLength == 0 && i != colLineLength*col) printf("+--");
else printf("-");
}printf("\n"RESET);

for(uint8_t i = 0; i < row; i++){
printf("%s\n",lines[i]);
}printf(RESET);

for(uint8_t i = 1; i <= colLineLength*col; i++){
if(i%colLineLength == 0 && i != colLineLength*col) printf("+--");
else printf("-");
}printf("\n"RESET);

}


Expand All @@ -178,12 +264,22 @@ int main(int argv, char** args){
if(params.showAll){
splashScreen();
return 0;
}else if(params.showAllAlphas && params.showAllDigits){
params.content = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
}else if(params.showAllAlphas){
params.content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
}else if(params.showAllDigits){
params.content = "0123456789";
}

uint16_t mem = params.showAllAlphas + params.showAllDigits + params.showSpecialChars + params.showControlChars;

if(mem){
uint8_t* cpy = (uint8_t*) calloc(strlen(params.content),sizeof(uint8_t));
strcpy(cpy,params.content);

params.content = (uint8_t*) calloc(mem+strlen(cpy)+1, sizeof(uint8_t));
strcat(params.content, cpy);

if(params.showAllAlphas) strcat(params.content,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
if(params.showAllDigits) strcat(params.content,"0123456789");
if(params.showSpecialChars) strcat(params.content," !\"#$%%&\'()*+,-./:;<=>?@[\\]^_`{|}~");
if(params.showControlChars) ;

}

//@todo: print the data first in the center
Expand Down

0 comments on commit e8e451b

Please sign in to comment.