diff options
| author | David M. Syzdek <david@syzdek.net> | 2021-10-11 17:30:47 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2021-10-11 17:30:47 -0800 |
| commit | be4cec7dae9232faf560f054ba36cb8826c61e9f (patch) | |
| tree | fa53881ff009d4ebe1f0b57c9d4d1dea5400ca87 /src | |
| parent | 7231f8cd4536d275029d44b06c7864540f67135b (diff) | |
| download | dmstools-be4cec7dae9232faf560f054ba36cb8826c61e9f.tar.gz dmstools-be4cec7dae9232faf560f054ba36cb8826c61e9f.tar.bz2 dmstools-be4cec7dae9232faf560f054ba36cb8826c61e9f.tar.xz dmstools-be4cec7dae9232faf560f054ba36cb8826c61e9f.zip | |
numconvert: updating output
Diffstat (limited to 'src')
| -rw-r--r-- | src/numconvert.c | 60 |
1 files changed, 54 insertions, 6 deletions
diff --git a/src/numconvert.c b/src/numconvert.c index f2906d4..23669ee 100644 --- a/src/numconvert.c +++ b/src/numconvert.c | |||
| @@ -99,6 +99,7 @@ | |||
| 99 | #define MY_OPT_LEBIT 0x0040 ///< print binary in little endian bit order | 99 | #define MY_OPT_LEBIT 0x0040 ///< print binary in little endian bit order |
| 100 | #define MY_OPT_LIMIT 0x0080 ///< print hex and binary with least amount of digits | 100 | #define MY_OPT_LIMIT 0x0080 ///< print hex and binary with least amount of digits |
| 101 | #define MY_OPT_ASCII 0x0100 | 101 | #define MY_OPT_ASCII 0x0100 |
| 102 | #define MY_OPT_HEADER 0x0200 | ||
| 102 | 103 | ||
| 103 | #define MY_TOGGLE(value, bit) ( (value&bit) ? (value&(~bit)) : (value|bit)) | 104 | #define MY_TOGGLE(value, bit) ( (value&bit) ? (value&(~bit)) : (value|bit)) |
| 104 | 105 | ||
| @@ -137,6 +138,7 @@ int main(int argc, char * argv[]) | |||
| 137 | int base; | 138 | int base; |
| 138 | int optbase; | 139 | int optbase; |
| 139 | int opt_index; | 140 | int opt_index; |
| 141 | int bases; | ||
| 140 | int32_t opt; | 142 | int32_t opt; |
| 141 | intmax_t x; | 143 | intmax_t x; |
| 142 | intmax_t y; | 144 | intmax_t y; |
| @@ -156,6 +158,7 @@ int main(int argc, char * argv[]) | |||
| 156 | opt = MY_OPT_SPACE; | 158 | opt = MY_OPT_SPACE; |
| 157 | base = 0; | 159 | base = 0; |
| 158 | order = "abxod"; | 160 | order = "abxod"; |
| 161 | bases = 0; | ||
| 159 | 162 | ||
| 160 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) | 163 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) |
| 161 | { | 164 | { |
| @@ -167,6 +170,7 @@ int main(int argc, char * argv[]) | |||
| 167 | 170 | ||
| 168 | case 'A': | 171 | case 'A': |
| 169 | opt |= MY_OPT_ASCII; | 172 | opt |= MY_OPT_ASCII; |
| 173 | bases++; | ||
| 170 | break; | 174 | break; |
| 171 | 175 | ||
| 172 | case 'a': | 176 | case 'a': |
| @@ -175,6 +179,7 @@ int main(int argc, char * argv[]) | |||
| 175 | 179 | ||
| 176 | case 'B': | 180 | case 'B': |
| 177 | opt |= MY_OPT_BIN; | 181 | opt |= MY_OPT_BIN; |
| 182 | bases++; | ||
| 178 | break; | 183 | break; |
| 179 | 184 | ||
| 180 | case 'b': | 185 | case 'b': |
| @@ -183,6 +188,7 @@ int main(int argc, char * argv[]) | |||
| 183 | 188 | ||
| 184 | case 'D': | 189 | case 'D': |
| 185 | opt |= MY_OPT_DEC; | 190 | opt |= MY_OPT_DEC; |
| 191 | bases++; | ||
| 186 | break; | 192 | break; |
| 187 | 193 | ||
| 188 | case 'd': | 194 | case 'd': |
| @@ -203,6 +209,7 @@ int main(int argc, char * argv[]) | |||
| 203 | 209 | ||
| 204 | case 'O': | 210 | case 'O': |
| 205 | opt |= MY_OPT_OCT; | 211 | opt |= MY_OPT_OCT; |
| 212 | bases++; | ||
| 206 | break; | 213 | break; |
| 207 | 214 | ||
| 208 | case 'o': | 215 | case 'o': |
| @@ -224,6 +231,7 @@ int main(int argc, char * argv[]) | |||
| 224 | 231 | ||
| 225 | case 'X': | 232 | case 'X': |
| 226 | opt |= MY_OPT_HEX; | 233 | opt |= MY_OPT_HEX; |
| 234 | bases++; | ||
| 227 | break; | 235 | break; |
| 228 | 236 | ||
| 229 | case 'x': | 237 | case 'x': |
| @@ -246,7 +254,10 @@ int main(int argc, char * argv[]) | |||
| 246 | }; | 254 | }; |
| 247 | 255 | ||
| 248 | if (!(opt & (MY_OPT_BIN|MY_OPT_DEC|MY_OPT_HEX|MY_OPT_OCT|MY_OPT_ASCII))) | 256 | if (!(opt & (MY_OPT_BIN|MY_OPT_DEC|MY_OPT_HEX|MY_OPT_OCT|MY_OPT_ASCII))) |
| 257 | { | ||
| 249 | opt |= (MY_OPT_BIN|MY_OPT_DEC|MY_OPT_HEX|MY_OPT_OCT|MY_OPT_ASCII); | 258 | opt |= (MY_OPT_BIN|MY_OPT_DEC|MY_OPT_HEX|MY_OPT_OCT|MY_OPT_ASCII); |
| 259 | bases = 5; | ||
| 260 | }; | ||
| 250 | 261 | ||
| 251 | if ((argc - optind) < 1) | 262 | if ((argc - optind) < 1) |
| 252 | { | 263 | { |
| @@ -255,6 +266,9 @@ int main(int argc, char * argv[]) | |||
| 255 | return(1); | 266 | return(1); |
| 256 | }; | 267 | }; |
| 257 | 268 | ||
| 269 | if ( ((argc - optind) > 1) || (bases > 1) ) | ||
| 270 | my_print(opt|MY_OPT_HEADER, 0, order); | ||
| 271 | |||
| 258 | for(x = optind; x < argc; x++) | 272 | for(x = optind; x < argc; x++) |
| 259 | { | 273 | { |
| 260 | // attempts to detect base | 274 | // attempts to detect base |
| @@ -316,7 +330,7 @@ int main(int argc, char * argv[]) | |||
| 316 | // print value | 330 | // print value |
| 317 | void my_print(int32_t opt, uintmax_t num, const char * order) | 331 | void my_print(int32_t opt, uintmax_t num, const char * order) |
| 318 | { | 332 | { |
| 319 | char buff[9]; | 333 | char buff[56]; |
| 320 | unsigned pos; | 334 | unsigned pos; |
| 321 | intmax_t y; | 335 | intmax_t y; |
| 322 | intmax_t z; | 336 | intmax_t z; |
| @@ -345,18 +359,30 @@ void my_print(int32_t opt, uintmax_t num, const char * order) | |||
| 345 | // print ASCII value | 359 | // print ASCII value |
| 346 | case 'A': | 360 | case 'A': |
| 347 | case 'a': | 361 | case 'a': |
| 348 | if ((opt & MY_OPT_ASCII) == 0) | 362 | if (!(opt & MY_OPT_ASCII)) |
| 363 | break; | ||
| 364 | if (len) | ||
| 365 | printf((opt & MY_OPT_SPACE) ? ", " : ","); | ||
| 366 | if ((opt & MY_OPT_HEADER)) | ||
| 367 | { | ||
| 368 | len = printf("%s", "TXT"); | ||
| 349 | break; | 369 | break; |
| 370 | }; | ||
| 350 | len = printf((len ? ((opt & MY_OPT_SPACE) ? ", '%c'" : ",'%c'") : "'%c'"), ((((num & 0xFF) >= 32) && ((num & 0xFF) <= 126)) ? (char)(num & 0xFF) : '.')); | 371 | len = printf((len ? ((opt & MY_OPT_SPACE) ? ", '%c'" : ",'%c'") : "'%c'"), ((((num & 0xFF) >= 32) && ((num & 0xFF) <= 126)) ? (char)(num & 0xFF) : '.')); |
| 351 | break; | 372 | break; |
| 352 | 373 | ||
| 353 | // print binary value | 374 | // print binary value |
| 354 | case 'B': | 375 | case 'B': |
| 355 | case 'b': | 376 | case 'b': |
| 356 | if ((opt & MY_OPT_BIN) == 0) | 377 | if (!(opt & MY_OPT_BIN)) |
| 357 | break; | 378 | break; |
| 358 | if (len) | 379 | if (len) |
| 359 | printf((opt & MY_OPT_SPACE) ? ", " : ","); | 380 | printf((opt & MY_OPT_SPACE) ? ", " : ","); |
| 381 | if ((opt & MY_OPT_HEADER)) | ||
| 382 | { | ||
| 383 | len = printf("%*s", ((!(opt & MY_OPT_SPACE)) ? 0 : (((int)max*8) + ((int)max-1))), "Binary"); | ||
| 384 | break; | ||
| 385 | }; | ||
| 360 | for(y = 0; ((uint32_t)y) < max; y++) | 386 | for(y = 0; ((uint32_t)y) < max; y++) |
| 361 | { | 387 | { |
| 362 | if (opt & MY_OPT_LEBYTE) | 388 | if (opt & MY_OPT_LEBYTE) |
| @@ -382,7 +408,14 @@ void my_print(int32_t opt, uintmax_t num, const char * order) | |||
| 382 | case 'x': | 408 | case 'x': |
| 383 | if ((opt & MY_OPT_HEX) == 0) | 409 | if ((opt & MY_OPT_HEX) == 0) |
| 384 | break; | 410 | break; |
| 385 | len = printf((len ? ((opt & MY_OPT_SPACE) ? ", 0x%0*jX" : ",0x%0*jX") : "0x%0*jX"), (unsigned)(max*2), num); | 411 | if (len) |
| 412 | printf((opt & MY_OPT_SPACE) ? ", " : ","); | ||
| 413 | if ((opt & MY_OPT_HEADER)) | ||
| 414 | { | ||
| 415 | len = printf("%*s", (unsigned)(max*2)+2, "Hex"); | ||
| 416 | break; | ||
| 417 | }; | ||
| 418 | len = printf("0x%0*jX", (unsigned)(max*2), num); | ||
| 386 | break; | 419 | break; |
| 387 | 420 | ||
| 388 | // print octal value | 421 | // print octal value |
| @@ -390,7 +423,15 @@ void my_print(int32_t opt, uintmax_t num, const char * order) | |||
| 390 | case 'o': | 423 | case 'o': |
| 391 | if ((opt & MY_OPT_OCT) == 0) | 424 | if ((opt & MY_OPT_OCT) == 0) |
| 392 | break; | 425 | break; |
| 393 | len = printf((len ? ((opt & MY_OPT_SPACE) ? ", 0%jo" : ",0%jo") : "0%jo"), num); | 426 | if (len) |
| 427 | printf((opt & MY_OPT_SPACE) ? ", " : ","); | ||
| 428 | if ((opt & MY_OPT_HEADER)) | ||
| 429 | { | ||
| 430 | len = printf("%*s", ((int)max*3), "Octal"); | ||
| 431 | break; | ||
| 432 | }; | ||
| 433 | snprintf(buff, sizeof(buff), "0%jo", num); | ||
| 434 | len = printf("%*s", ((3*(int)max)), buff); | ||
| 394 | break; | 435 | break; |
| 395 | 436 | ||
| 396 | // print decimal value | 437 | // print decimal value |
| @@ -398,7 +439,14 @@ void my_print(int32_t opt, uintmax_t num, const char * order) | |||
| 398 | case 'd': | 439 | case 'd': |
| 399 | if ((opt & MY_OPT_DEC) == 0) | 440 | if ((opt & MY_OPT_DEC) == 0) |
| 400 | break; | 441 | break; |
| 401 | len = printf((len ? ((opt & MY_OPT_SPACE) ? ", %ju" : ",%ju") : "%ju"), num); | 442 | if (len) |
| 443 | printf((opt & MY_OPT_SPACE) ? ", " : ","); | ||
| 444 | if ((opt & MY_OPT_HEADER)) | ||
| 445 | { | ||
| 446 | len = printf("%*s", (((int)max/4)*10), "Decimal"); | ||
| 447 | break; | ||
| 448 | }; | ||
| 449 | len = printf("%*ju", (((int)max/4)*10), num); | ||
| 402 | break; | 450 | break; |
| 403 | 451 | ||
| 404 | default: | 452 | default: |
