From f486fd76b92a5cfead72c84b72adc2fd575beff2 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sun, 8 Sep 2019 18:19:39 -0500 Subject: [PATCH] Feature: implement --decimal display in bit_dump --- level_3/bit_dump/c/bit_dump.c | 135 ++++++++++++++-------------------- level_3/bit_dump/c/bit_dump.h | 15 ++-- level_3/bit_dump/c/private-bit_dump.c | 14 ++-- 3 files changed, 73 insertions(+), 91 deletions(-) diff --git a/level_3/bit_dump/c/bit_dump.c b/level_3/bit_dump/c/bit_dump.c index 5cedaa9..60aa9ba 100644 --- a/level_3/bit_dump/c/bit_dump.c +++ b/level_3/bit_dump/c/bit_dump.c @@ -9,16 +9,18 @@ extern "C" { f_return_status bit_dump_print_help(const bit_dump_data data) { fll_program_print_help_header(data.context, bit_dump_name_long, bit_dump_version); - fll_program_print_help_option(data.context, f_console_standard_short_help, f_console_standard_long_help, f_console_symbol_short_enable, f_console_symbol_long_enable, " Print this help message."); - fll_program_print_help_option(data.context, f_console_standard_short_light, f_console_standard_long_light, f_console_symbol_short_disable, f_console_symbol_long_disable, " Output using colors that show up better on light backgrounds"); - fll_program_print_help_option(data.context, f_console_standard_short_dark, f_console_standard_long_dark, f_console_symbol_short_disable, f_console_symbol_long_disable, " Output using colors that show up better on dark backgrounds"); - fll_program_print_help_option(data.context, f_console_standard_short_no_color, f_console_standard_long_no_color, f_console_symbol_short_disable, f_console_symbol_long_disable, " Do not output in color."); - fll_program_print_help_option(data.context, f_console_standard_short_version, f_console_standard_long_version, f_console_symbol_short_disable, f_console_symbol_long_disable, " Print only the version number."); + fll_program_print_help_option(data.context, f_console_standard_short_help, f_console_standard_long_help, f_console_symbol_short_enable, f_console_symbol_long_enable, " Print this help message."); + fll_program_print_help_option(data.context, f_console_standard_short_light, f_console_standard_long_light, f_console_symbol_short_disable, f_console_symbol_long_disable, " Output using colors that show up better on light backgrounds"); + fll_program_print_help_option(data.context, f_console_standard_short_dark, f_console_standard_long_dark, f_console_symbol_short_disable, f_console_symbol_long_disable, " Output using colors that show up better on dark backgrounds"); + fll_program_print_help_option(data.context, f_console_standard_short_no_color, f_console_standard_long_no_color, f_console_symbol_short_disable, f_console_symbol_long_disable, " Do not output in color."); + fll_program_print_help_option(data.context, f_console_standard_short_version, f_console_standard_long_version, f_console_symbol_short_disable, f_console_symbol_long_disable, " Print only the version number."); printf("%c", f_string_eol); - fll_program_print_help_option(data.context, bit_dump_short_binary, bit_dump_long_binary, f_console_symbol_short_enable, f_console_symbol_long_enable, " Display binary representation."); - fll_program_print_help_option(data.context, bit_dump_short_hex, bit_dump_long_hex, f_console_symbol_short_enable, f_console_symbol_long_enable, " Display hexadecimal representation."); + fll_program_print_help_option(data.context, bit_dump_short_binary, bit_dump_long_binary, f_console_symbol_short_enable, f_console_symbol_long_enable, " Display binary representation."); + fll_program_print_help_option(data.context, bit_dump_short_decimal, bit_dump_long_decimal, f_console_symbol_short_enable, f_console_symbol_long_enable, " Display decimal representation."); + fll_program_print_help_option(data.context, bit_dump_short_hexdecimal, bit_dump_long_hexidecimal, f_console_symbol_short_enable, f_console_symbol_long_enable, "Display hexadecimal representation."); + fll_program_print_help_option(data.context, bit_dump_short_octal, bit_dump_long_octal, f_console_symbol_short_enable, f_console_symbol_long_enable, " Display octal representation."); fll_program_print_help_usage(data.context, bit_dump_name, "filename(s)"); @@ -44,88 +46,49 @@ extern "C" { { f_console_parameters parameters = { data->parameters, bit_dump_total_parameters }; - f_console_parameter_id ids[3] = { bit_dump_parameter_no_color, bit_dump_parameter_light, bit_dump_parameter_dark }; - f_console_parameter_ids choices = { ids, 3 }; + f_console_parameter_ids choices = f_console_parameter_ids_initialize; - status = fll_program_process_parameters(arguments, parameters, choices, &data->remaining, &data->context); - } + { + f_console_parameter_id ids[3] = { bit_dump_parameter_no_color, bit_dump_parameter_light, bit_dump_parameter_dark }; + choices.id = ids; + choices.used = 3; - if (f_status_is_error(status)) { - bit_dump_delete_data(data); - return f_status_set_error(status); - } + status = fll_program_process_parameters(arguments, parameters, choices, &data->remaining, &data->context); - if (data->parameters[bit_dump_parameter_binary].result == f_console_result_found) { - if (data->parameters[bit_dump_parameter_hexidecimal].result == f_console_result_none) { - if (data->parameters[bit_dump_parameter_octal].result == f_console_result_none || data->parameters[bit_dump_parameter_binary].location > data->parameters[bit_dump_parameter_octal].location) { - data->mode = bit_dump_mode_binary; - } - else if (data->parameters[bit_dump_parameter_binary].location == data->parameters[bit_dump_parameter_octal].location && data->parameters[bit_dump_parameter_binary].location_sub > data->parameters[bit_dump_parameter_octal].location_sub) { - data->mode = bit_dump_mode_binary; - } - else { - data->mode = bit_dump_mode_octal; + if (f_status_is_error(status)) { + bit_dump_delete_data(data); + return f_status_set_error(status); } } - else { - if (data->parameters[bit_dump_parameter_octal].result == f_console_result_none) { - if (data->parameters[bit_dump_parameter_binary].location > data->parameters[bit_dump_parameter_hexidecimal].location) { - data->mode = bit_dump_mode_binary; - } - else if (data->parameters[bit_dump_parameter_binary].location == data->parameters[bit_dump_parameter_hexidecimal].location && data->parameters[bit_dump_parameter_binary].location_sub > data->parameters[bit_dump_parameter_hexidecimal].location_sub) { - data->mode = bit_dump_mode_binary; - } - else { - data->mode = bit_dump_mode_hexidecimal; - } - } - else if (data->parameters[bit_dump_parameter_binary].location > data->parameters[bit_dump_parameter_octal].location) { - if (data->parameters[bit_dump_parameter_binary].location > data->parameters[bit_dump_parameter_hexidecimal].location) { - data->mode = bit_dump_mode_binary; - } - else if (data->parameters[bit_dump_parameter_binary].location == data->parameters[bit_dump_parameter_hexidecimal].location && data->parameters[bit_dump_parameter_binary].location_sub > data->parameters[bit_dump_parameter_hexidecimal].location_sub) { - data->mode = bit_dump_mode_binary; - } - else { - data->mode = bit_dump_mode_hexidecimal; - } - } - else if (data->parameters[bit_dump_parameter_binary].location == data->parameters[bit_dump_parameter_octal].location && data->parameters[bit_dump_parameter_binary].location_sub > data->parameters[bit_dump_parameter_octal].location_sub) { - if (data->parameters[bit_dump_parameter_binary].location > data->parameters[bit_dump_parameter_hexidecimal].location) { - data->mode = bit_dump_mode_binary; - } - else if (data->parameters[bit_dump_parameter_binary].location == data->parameters[bit_dump_parameter_hexidecimal].location && data->parameters[bit_dump_parameter_binary].location_sub > data->parameters[bit_dump_parameter_hexidecimal].location_sub) { - data->mode = bit_dump_mode_binary; - } - else { - data->mode = bit_dump_mode_hexidecimal; - } - } - else if (data->parameters[bit_dump_parameter_hexidecimal].location > data->parameters[bit_dump_parameter_octal].location) { - data->mode = bit_dump_mode_hexidecimal; + + { + f_console_parameter_id ids[4] = { bit_dump_parameter_hexidecimal, bit_dump_parameter_octal, bit_dump_parameter_binary, bit_dump_parameter_decimal }; + f_console_parameter_id choice = bit_dump_parameter_hexidecimal; + choices.id = ids; + choices.used = 4; + + status = fl_console_parameter_prioritize(parameters, choices, &choice); + + if (f_status_is_error(status)) { + bit_dump_delete_data(data); + return f_status_set_error(status); } - else if (data->parameters[bit_dump_parameter_hexidecimal].location == data->parameters[bit_dump_parameter_octal].location && data->parameters[bit_dump_parameter_hexidecimal].location_sub > data->parameters[bit_dump_parameter_octal].location_sub) { + + + if (choice == bit_dump_parameter_hexidecimal) { data->mode = bit_dump_mode_hexidecimal; } - else { + else if (choice == bit_dump_parameter_octal) { data->mode = bit_dump_mode_octal; } + else if (choice == bit_dump_parameter_binary) { + data->mode = bit_dump_mode_binary; + } + else if (choice == bit_dump_parameter_decimal) { + data->mode = bit_dump_mode_decimal; + } } } - else if (data->parameters[bit_dump_parameter_hexidecimal].result == f_console_result_found) { - if (data->parameters[bit_dump_parameter_octal].result == f_console_result_none || data->parameters[bit_dump_parameter_hexidecimal].location > data->parameters[bit_dump_parameter_octal].location) { - data->mode = bit_dump_mode_hexidecimal; - } - else if (data->parameters[bit_dump_parameter_hexidecimal].location == data->parameters[bit_dump_parameter_octal].location && data->parameters[bit_dump_parameter_hexidecimal].location_sub > data->parameters[bit_dump_parameter_octal].location_sub) { - data->mode = bit_dump_mode_hexidecimal; - } - else { - data->mode = bit_dump_mode_octal; - } - } - else if (data->parameters[bit_dump_parameter_octal].result == f_console_result_found) { - data->mode = bit_dump_mode_octal; - } status = f_none; @@ -242,7 +205,23 @@ extern "C" { printf("%c", f_string_eol); fl_color_print(f_standard_output, data->context.title, data->context.reset, "Byte Dump of: "); - fl_color_print_line(f_standard_output, data->context.notable, data->context.reset, "%s", arguments.argv[data->remaining.array[counter]]); + fl_color_print(f_standard_output, data->context.notable, data->context.reset, "%s", arguments.argv[data->remaining.array[counter]]); + fl_color_print(f_standard_output, data->context.title, data->context.reset, " (in "); + + if (data->mode == bit_dump_mode_hexidecimal) { + fl_color_print(f_standard_output, data->context.title, data->context.reset, "Hexidecimal"); + } + else if (data->mode == bit_dump_mode_octal) { + fl_color_print(f_standard_output, data->context.title, data->context.reset, "Octal"); + } + else if (data->mode == bit_dump_mode_binary) { + fl_color_print(f_standard_output, data->context.title, data->context.reset, "Binary"); + } + else if (data->mode == bit_dump_mode_decimal) { + fl_color_print(f_standard_output, data->context.title, data->context.reset, "Decimal"); + } + + fl_color_print_line(f_standard_output, data->context.title, data->context.reset, ")"); status = bit_dump_file(*data, arguments.argv[data->remaining.array[counter]], file); diff --git a/level_3/bit_dump/c/bit_dump.h b/level_3/bit_dump/c/bit_dump.h index f106b7e..816b68d 100644 --- a/level_3/bit_dump/c/bit_dump.h +++ b/level_3/bit_dump/c/bit_dump.h @@ -65,7 +65,7 @@ extern "C" { bit_dump_mode_hexidecimal, bit_dump_mode_octal, bit_dump_mode_binary, - bit_dump_mode_digit, + bit_dump_mode_decimal, }; #define bit_dump_sequence_acknowledge "␆" @@ -111,7 +111,8 @@ extern "C" { #define bit_dump_character_unused "�" #define bit_dump_short_binary "b" - #define bit_dump_short_hex "x" + #define bit_dump_short_decimal "d" + #define bit_dump_short_hexdecimal "x" #define bit_dump_short_octal "o" #define bit_dump_short_first "f" @@ -121,7 +122,8 @@ extern "C" { #define bit_dump_short_placeholder "p" #define bit_dump_long_binary "binary" - #define bit_dump_long_hex "hex" + #define bit_dump_long_decimal "decimal" + #define bit_dump_long_hexidecimal "hexidecimal" #define bit_dump_long_octal "octal" #define bit_dump_long_first "first" // first offset byte size. @@ -139,9 +141,9 @@ extern "C" { bit_dump_parameter_version, bit_dump_parameter_binary, + bit_dump_parameter_decimal, bit_dump_parameter_hexidecimal, bit_dump_parameter_octal, - // @todo: add digit print support bit_dump_parameter_digit, bit_dump_parameter_first, bit_dump_parameter_last, @@ -159,7 +161,8 @@ extern "C" { f_console_parameter_initialize(f_console_standard_short_no_color, f_console_standard_long_no_color, 0, 0, f_console_type_inverse), \ f_console_parameter_initialize(f_console_standard_short_version, f_console_standard_long_version, 0, 0, f_console_type_inverse), \ f_console_parameter_initialize(bit_dump_short_binary, bit_dump_long_binary, 0, 0, f_console_type_normal), \ - f_console_parameter_initialize(bit_dump_short_hex, bit_dump_long_hex, 0, 0, f_console_type_normal), \ + f_console_parameter_initialize(bit_dump_short_decimal, bit_dump_long_decimal, 0, 0, f_console_type_normal), \ + f_console_parameter_initialize(bit_dump_short_hexdecimal, bit_dump_long_hexidecimal, 0, 0, f_console_type_normal), \ f_console_parameter_initialize(bit_dump_short_octal, bit_dump_long_octal, 0, 0, f_console_type_normal), \ f_console_parameter_initialize(bit_dump_short_first, bit_dump_long_first, 0, 1, f_console_type_normal), \ f_console_parameter_initialize(bit_dump_short_last, bit_dump_long_last, 0, 1, f_console_type_normal), \ @@ -168,7 +171,7 @@ extern "C" { f_console_parameter_initialize(bit_dump_short_placeholder, bit_dump_long_placeholder, 0, 0, f_console_type_normal), \ } - #define bit_dump_total_parameters 13 + #define bit_dump_total_parameters 14 #endif // _di_bit_dump_defines_ #ifndef _di_bit_dump_data_ diff --git a/level_3/bit_dump/c/private-bit_dump.c b/level_3/bit_dump/c/private-bit_dump.c index 5eae239..26d3b42 100644 --- a/level_3/bit_dump/c/private-bit_dump.c +++ b/level_3/bit_dump/c/private-bit_dump.c @@ -185,8 +185,8 @@ else if (data.mode == bit_dump_mode_binary) { printf(" "); } - else if (data.mode == bit_dump_mode_digit) { - // @todo + else if (data.mode == bit_dump_mode_decimal) { + printf(" "); } column++; @@ -201,7 +201,7 @@ else if (data.mode == bit_dump_mode_binary && column % 4 == 0) { printf(" "); } - else if (data.mode == bit_dump_mode_digit && column % 4 == 0) { + else if (data.mode == bit_dump_mode_decimal && column % 6 == 0) { printf(" "); } } @@ -302,12 +302,12 @@ printf(" %s", binary_string); } } - else if (data.mode == bit_dump_mode_digit) { + else if (data.mode == bit_dump_mode_decimal) { if (invalid[character_current]) { - fl_color_print(f_standard_output, data.context.error, data.context.reset, " %02d", (uint8_t) byte); + fl_color_print(f_standard_output, data.context.error, data.context.reset, " %3d", (uint8_t) byte); } else { - printf(" %02d", (uint8_t) byte); + printf(" %3d", (uint8_t) byte); } } @@ -350,7 +350,7 @@ else if (data.mode == bit_dump_mode_binary && *column % 4 == 0) { printf(" "); } - else if (data.mode == bit_dump_mode_digit && *column % 4 == 0) { + else if (data.mode == bit_dump_mode_decimal && *column % 6 == 0) { printf(" "); } -- 1.8.3.1