diff --git a/headers/ascii.h b/headers/ascii.h index 1c6ebab..f834458 100644 --- a/headers/ascii.h +++ b/headers/ascii.h @@ -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 diff --git a/src/ascii.c b/src/ascii.c index d08ea2f..576046c 100644 --- a/src/ascii.c +++ b/src/ascii.c @@ -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; @@ -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; } diff --git a/src/ascii.h b/src/ascii.h index 1c6ebab..6e9a236 100644 --- a/src/ascii.h +++ b/src/ascii.h @@ -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 diff --git a/src/main.c b/src/main.c index de2fd49..0028deb 100644 --- a/src/main.c +++ b/src/main.c @@ -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]); @@ -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){ @@ -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("+--"); @@ -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("+--");