Skip to content

Commit

Permalink
update add binary
Browse files Browse the repository at this point in the history
added binary representation to the output table
fixed large argument crash
  • Loading branch information
ManojTGN committed Apr 13, 2024
1 parent 8e91bb1 commit 82c948c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions headers/ascii.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct parameter{
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 onlyBin; //--bin shows Binary 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
Expand Down
3 changes: 2 additions & 1 deletion src/ascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ asciiParams parseParameter(int argv, char** args){
else if(strcmp(args[i],"--octa") == 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],"--bin") == 0) params.onlyBin = true;
// else if(strcmp(args[i],"--char") == 0) params.onlyChar = true;

else if(strcmp(args[i],"--asc")==0) params.order = 1;
Expand Down Expand Up @@ -157,7 +158,7 @@ void removeDuplicateChars(asciiParams *params){
params->contentSize = idx;

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

Expand Down
1 change: 1 addition & 0 deletions src/ascii.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ typedef struct parameter{

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
bool onlyBin; //--bin shows Binary in output table

uint8_t* content; // " " content / data (user input)
uint16_t contentSize; // content size
Expand Down
45 changes: 34 additions & 11 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,36 @@ void printData(asciiParams params){
procCol = currCol;
}

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

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

snprintf(tmp, sizeof(tmp), "%3d ", params.content[s]);
if(params.onlyDec || params._onlyAll) strcat(lines[currRow],tmp);
if(params.onlyHex || params._onlyAll){
snprintf(tmp, sizeof(tmp), " %2X ", params.content[s]);
strcat(lines[currRow],tmp);
}

snprintf(tmp, sizeof(tmp), " %2X ", params.content[s]);
if(params.onlyHex || params._onlyAll) strcat(lines[currRow],tmp);
if(params.onlyBin /*|| params._onlyAll*/){
uint8_t index = 0; uint8_t content = params.content[s];
uint8_t binStr[9] = "00000000\0";

for(uint8_t i = 128; i > 0 && content != 0; i=(i/2)){
if(content >= i){
binStr[index] = '1';
content -= i;
}
index++;
}

snprintf(tmp, sizeof(tmp), "%s ", binStr);
strcat(lines[currRow],tmp);
}

if(isPrintable(params.content[s])){
snprintf(tmp, sizeof(tmp),YEL"%c "RESET, params.content[s]);
Expand All @@ -64,12 +86,13 @@ void printData(asciiParams params){
s++;
}

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);
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*/?10: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.onlyBin /*|| params._onlyAll*/) printf("Binary ");
if(params.onlyChar|| params._onlyAll) printf(YEL"Chr"RESET);

if(col != i+1){
Expand All @@ -85,8 +108,8 @@ void printData(asciiParams params){
uint8_t* hyphens = (uint8_t*)calloc( (size_t)maxLength[i],sizeof(uint8_t));
memset(hyphens, (uint8_t)45, sizeof(uint8_t) * (size_t)maxLength[i]);

uint8_t* fHyphens = (uint8_t*)calloc( params._onlyAll?19:(params.onlyDec?5:0) + (params.onlyOct?5:0) + (params.onlyHex?5:0) + (params.onlyChar?4:0),sizeof(uint8_t));
memset(fHyphens, (uint8_t)45, sizeof(uint8_t) * (params._onlyAll?19:(params.onlyDec?5:0) + (params.onlyOct?5:0) + (params.onlyHex?5:0) + (params.onlyChar?4:0)));
uint8_t* fHyphens = (uint8_t*)calloc( params._onlyAll?(params.onlyBin?19+10:19):(params.onlyDec?5:0) + (params.onlyOct?5:0) + (params.onlyHex?5:0) + (params.onlyBin?10:0) + (params.onlyChar?4:0),sizeof(uint8_t));
memset(fHyphens, (uint8_t)45, sizeof(uint8_t) * (params._onlyAll?(params.onlyBin?19+10:19):(params.onlyDec?5:0) + (params.onlyOct?5:0) + (params.onlyHex?5:0) + (params.onlyBin?10:0) + (params.onlyChar?4:0)));

printf("%s%s",fHyphens,hyphens);
if(col-1 != i) printf("+--");
Expand All @@ -101,8 +124,8 @@ void printData(asciiParams params){
uint8_t* hyphens = (uint8_t*)calloc( (size_t)maxLength[i],sizeof(uint8_t));
memset(hyphens, (uint8_t)45, sizeof(uint8_t) * (size_t)maxLength[i]);

uint8_t* fHyphens = (uint8_t*)calloc( params._onlyAll?19:(params.onlyDec?5:0) + (params.onlyOct?5:0) + (params.onlyHex?5:0) + (params.onlyChar?4:0),sizeof(uint8_t));
memset(fHyphens, (uint8_t)45, sizeof(uint8_t) * (params._onlyAll?19:(params.onlyDec?5:0) + (params.onlyOct?5:0) + (params.onlyHex?5:0) + (params.onlyChar?4:0)));
uint8_t* fHyphens = (uint8_t*)calloc( params._onlyAll?(params.onlyBin?19+10:19):(params.onlyDec?5:0) + (params.onlyOct?5:0) + (params.onlyHex?5:0) + (params.onlyBin?10:0) + (params.onlyChar?4:0),sizeof(uint8_t));
memset(fHyphens, (uint8_t)45, sizeof(uint8_t) * (params._onlyAll?(params.onlyBin?19+10:19):(params.onlyDec?5:0) + (params.onlyOct?5:0) + (params.onlyHex?5:0) + (params.onlyBin?10:0) + (params.onlyChar?4:0)));

printf("%s%s",fHyphens,hyphens);
if(col-1 != i) printf("+--");
Expand Down

0 comments on commit 82c948c

Please sign in to comment.