From 129967b9623e529e537958234dc772ce382cddc6 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Wed, 26 Jul 2023 21:31:05 -0500 Subject: [PATCH] Update: Relocate several static stings into single location for Byte Dump program. --- level_3/byte_dump/c/main/common/string.c | 20 +++++++ level_3/byte_dump/c/main/common/string.h | 42 ++++++++++++++ level_3/byte_dump/c/main/print/data.c | 95 ++++++++------------------------ level_3/byte_dump/c/main/process.c | 36 +----------- 4 files changed, 88 insertions(+), 105 deletions(-) diff --git a/level_3/byte_dump/c/main/common/string.c b/level_3/byte_dump/c/main/common/string.c index ab6232a..2542d9c 100644 --- a/level_3/byte_dump/c/main/common/string.c +++ b/level_3/byte_dump/c/main/common/string.c @@ -24,8 +24,28 @@ extern "C" { const f_string_static_t byte_dump_character_placeholder_s = macro_f_string_static_t_initialize_1(BYTE_DUMP_character_placeholder_s, 0, BYTE_DUMP_character_placeholder_s_length); const f_string_static_t byte_dump_character_incomplete_s = macro_f_string_static_t_initialize_1(BYTE_DUMP_character_incomplete_s, 0, BYTE_DUMP_character_incomplete_s_length); const f_string_static_t byte_dump_character_unused_s = macro_f_string_static_t_initialize_1(BYTE_DUMP_character_unused_s, 0, BYTE_DUMP_character_unused_s_length); + + const f_string_static_t byte_dump_format_raw_context_s = macro_f_string_static_t_initialize_1(BYTE_DUMP_format_raw_context_s, 0, BYTE_DUMP_format_raw_context_s_length); #endif // _di_byte_dump_s_ +#ifndef _di_byte_dump_padding_s_ + const f_string_static_t byte_dump_padding_3_s = macro_f_string_static_t_initialize_1(BYTE_DUMP_padding_3_s, 0, BYTE_DUMP_padding_3_s_length); + const f_string_static_t byte_dump_padding_4_s = macro_f_string_static_t_initialize_1(BYTE_DUMP_padding_4_s, 0, BYTE_DUMP_padding_4_s_length); + const f_string_static_t byte_dump_padding_5_s = macro_f_string_static_t_initialize_1(BYTE_DUMP_padding_5_s, 0, BYTE_DUMP_padding_5_s_length); + const f_string_static_t byte_dump_padding_6_s = macro_f_string_static_t_initialize_1(BYTE_DUMP_padding_6_s, 0, BYTE_DUMP_padding_6_s_length); + const f_string_static_t byte_dump_padding_8_s = macro_f_string_static_t_initialize_1(BYTE_DUMP_padding_8_s, 0, BYTE_DUMP_padding_8_s_length); + const f_string_static_t byte_dump_padding_9_s = macro_f_string_static_t_initialize_1(BYTE_DUMP_padding_9_s, 0, BYTE_DUMP_padding_9_s_length); + + const f_string_static_t byte_dump_padding_s[] = { + byte_dump_padding_9_s, + byte_dump_padding_3_s, + byte_dump_padding_4_s, + byte_dump_padding_4_s, + byte_dump_padding_9_s, + byte_dump_padding_4_s, + }; +#endif // _di_byte_dump_padding_s_ + #ifndef _di_byte_dump_parameter_d_ const f_string_static_t byte_dump_short_binary_s = macro_f_string_static_t_initialize_1(BYTE_DUMP_short_binary_s, 0, BYTE_DUMP_short_binary_s_length); const f_string_static_t byte_dump_short_decimal_s = macro_f_string_static_t_initialize_1(BYTE_DUMP_short_decimal_s, 0, BYTE_DUMP_short_decimal_s_length); diff --git a/level_3/byte_dump/c/main/common/string.h b/level_3/byte_dump/c/main/common/string.h index 8029813..df467f3 100644 --- a/level_3/byte_dump/c/main/common/string.h +++ b/level_3/byte_dump/c/main/common/string.h @@ -74,6 +74,8 @@ extern "C" { #define BYTE_DUMP_character_incomplete_s "�" #define BYTE_DUMP_character_unused_s "�" + #define BYTE_DUMP_format_raw_context_s "%[%r%]" + #define BYTE_DUMP_hexidecimal_s_length 11 #define BYTE_DUMP_duodecimal_s_length 10 #define BYTE_DUMP_octal_s_length 5 @@ -85,6 +87,8 @@ extern "C" { #define BYTE_DUMP_character_incomplete_s_length 3 #define BYTE_DUMP_character_unused_s_length 3 + #define BYTE_DUMP_format_raw_context_s_length 6 + extern const f_string_static_t byte_dump_hexidecimal_s; extern const f_string_static_t byte_dump_duodecimal_s; extern const f_string_static_t byte_dump_octal_s; @@ -95,9 +99,47 @@ extern "C" { extern const f_string_static_t byte_dump_character_placeholder_s; extern const f_string_static_t byte_dump_character_incomplete_s; extern const f_string_static_t byte_dump_character_unused_s; + + extern const f_string_static_t byte_dump_format_raw_context_s; #endif // _di_byte_dump_s_ /** + * Paddings and an array containing the paddings for quick reference. + * + * The byte_dump_padding_s is an array with the following structure: + * - index 0: unicode. + * - index 1: hexidecimal. + * - index 2: duodecimal. + * - index 3: octal. + * - index 4: binary. + * - index 5: decimal. + */ +#ifndef _di_byte_dump_padding_s_ + #define BYTE_DUMP_padding_3_s " " + #define BYTE_DUMP_padding_4_s " " + #define BYTE_DUMP_padding_5_s " " + #define BYTE_DUMP_padding_6_s " " + #define BYTE_DUMP_padding_8_s " " + #define BYTE_DUMP_padding_9_s " " + + #define BYTE_DUMP_padding_3_s_length 3 + #define BYTE_DUMP_padding_4_s_length 4 + #define BYTE_DUMP_padding_5_s_length 5 + #define BYTE_DUMP_padding_6_s_length 6 + #define BYTE_DUMP_padding_8_s_length 8 + #define BYTE_DUMP_padding_9_s_length 9 + + extern const f_string_static_t byte_dump_padding_3_s; + extern const f_string_static_t byte_dump_padding_4_s; + extern const f_string_static_t byte_dump_padding_5_s; + extern const f_string_static_t byte_dump_padding_6_s; + extern const f_string_static_t byte_dump_padding_8_s; + extern const f_string_static_t byte_dump_padding_9_s; + + extern const f_string_static_t byte_dump_padding_s[]; +#endif // _di_byte_dump_padding_s_ + +/** * The main program parameters. */ #ifndef _di_byte_dump_parameter_d_ diff --git a/level_3/byte_dump/c/main/print/data.c b/level_3/byte_dump/c/main/print/data.c index c4f6381..9163771 100644 --- a/level_3/byte_dump/c/main/print/data.c +++ b/level_3/byte_dump/c/main/print/data.c @@ -38,30 +38,13 @@ extern "C" { // Pad the buffer with spaces to hide any skipped bytes (skipped via --first). while (offset_to_print && cell->column < main->setting.width) { - if (main->setting.flag & byte_dump_main_flag_unicode_e) { - f_print_terminated(" ", print->to); - } - else if (main->setting.mode == byte_dump_mode_hexidecimal_e) { - f_print_terminated(" ", print->to); - } - else if (main->setting.mode == byte_dump_mode_duodecimal_e) { - f_print_terminated(" ", print->to); - } - else if (main->setting.mode == byte_dump_mode_octal_e) { - f_print_terminated(" ", print->to); - } - else if (main->setting.mode == byte_dump_mode_binary_e) { - f_print_terminated(" ", print->to); - } - else if (main->setting.mode == byte_dump_mode_decimal_e) { - f_print_terminated(" ", print->to); - } + f_print_dynamic_raw(byte_dump_padding_s[(main->setting.flag & byte_dump_main_flag_unicode_e) ? 0 : main->setting.mode], print->to); --offset_to_print; ++cell->column; if (cell->column < main->setting.width) { - if (main->setting.flag & byte_dump_main_flag_unicode_e) { + if ((main->setting.flag & byte_dump_main_flag_unicode_e) || main->setting.mode == byte_dump_mode_binary_e) { if (!(cell->column % 4)) { f_print_dynamic_raw(f_string_space_s, print->to); } @@ -71,22 +54,7 @@ extern "C" { f_print_dynamic_raw(f_string_space_s, print->to); } } - else if (main->setting.mode == byte_dump_mode_duodecimal_e) { - if (!(cell->column % 6)) { - f_print_dynamic_raw(f_string_space_s, print->to); - } - } - else if (main->setting.mode == byte_dump_mode_octal_e) { - if (!(cell->column % 6)) { - f_print_dynamic_raw(f_string_space_s, print->to); - } - } - else if (main->setting.mode == byte_dump_mode_binary_e) { - if (!(cell->column % 4)) { - f_print_dynamic_raw(f_string_space_s, print->to); - } - } - else if (main->setting.mode == byte_dump_mode_decimal_e) { + else if (main->setting.mode == byte_dump_mode_duodecimal_e || main->setting.mode == byte_dump_mode_octal_e || main->setting.mode == byte_dump_mode_decimal_e) { if (!(cell->column % 6)) { f_print_dynamic_raw(f_string_space_s, print->to); } @@ -138,13 +106,13 @@ extern "C" { else { // Pad the sequence that are incomplete fragments of an already printed valid Unicode. - f_print_terminated(" ", print->to); + f_print_dynamic_raw(byte_dump_padding_9_s, print->to); } } else { if (main->setting.mode == byte_dump_mode_hexidecimal_e) { if (main->setting.flag & byte_dump_main_flag_unicode_e) { - f_print_terminated(" ", print->to); + f_print_dynamic_raw(byte_dump_padding_6_s, print->to); } if (invalid[current]) { @@ -156,7 +124,7 @@ extern "C" { } else if (main->setting.mode == byte_dump_mode_duodecimal_e) { if (main->setting.flag & byte_dump_main_flag_unicode_e) { - f_print_terminated(" ", print->to); + f_print_dynamic_raw(byte_dump_padding_5_s, print->to); } if (invalid[current]) { @@ -168,7 +136,7 @@ extern "C" { } else if (main->setting.mode == byte_dump_mode_octal_e) { if (main->setting.flag & byte_dump_main_flag_unicode_e) { - f_print_terminated(" ", print->to); + f_print_dynamic_raw(byte_dump_padding_5_s, print->to); } if (invalid[current]) { @@ -188,7 +156,7 @@ extern "C" { } else if (main->setting.mode == byte_dump_mode_decimal_e) { if (main->setting.flag & byte_dump_main_flag_unicode_e) { - f_print_terminated(" ", print->to); + f_print_dynamic_raw(byte_dump_padding_5_s, print->to); } if (invalid[current]) { @@ -232,7 +200,7 @@ extern "C" { } } else { - if (main->setting.flag & byte_dump_main_flag_unicode_e) { + if ((main->setting.flag & byte_dump_main_flag_unicode_e) || main->setting.mode == byte_dump_mode_binary_e) { if (!(cell->column % 4)) { f_print_dynamic_raw(f_string_space_s, print->to); } @@ -242,22 +210,7 @@ extern "C" { f_print_dynamic_raw(f_string_space_s, print->to); } } - else if (main->setting.mode == byte_dump_mode_duodecimal_e) { - if (!(cell->column % 6)) { - f_print_dynamic_raw(f_string_space_s, print->to); - } - } - else if (main->setting.mode == byte_dump_mode_octal_e) { - if (!(cell->column % 6)) { - f_print_dynamic_raw(f_string_space_s, print->to); - } - } - else if (main->setting.mode == byte_dump_mode_binary_e) { - if (!(cell->column % 4)) { - f_print_dynamic_raw(f_string_space_s, print->to); - } - } - else if (main->setting.mode == byte_dump_mode_decimal_e) { + else if (main->setting.mode == byte_dump_mode_duodecimal_e || main->setting.mode == byte_dump_mode_octal_e || main->setting.mode == byte_dump_mode_decimal_e) { if (!(cell->column % 6)) { f_print_dynamic_raw(f_string_space_s, print->to); } @@ -303,7 +256,7 @@ extern "C" { if (main->setting.flag & byte_dump_main_flag_placeholder_e) { for (; *offset && at < main->setting.width; --(*offset), ++at) { - fl_print_format("%[%r%]", print->to, print->set->warning, byte_dump_character_placeholder_s, print->set->warning); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->warning, byte_dump_character_placeholder_s, print->set->warning); if (main->setting.flag & byte_dump_main_flag_wide_e) { f_print_dynamic_raw(f_string_space_s, print->to); @@ -336,13 +289,13 @@ extern "C" { for (; at < previous->bytes && at < main->setting.width; ++at) { if (previous->invalid) { - fl_print_format("%[%r%]", print->to, print->set->error, byte_dump_character_placeholder_s, print->set->error); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->error, byte_dump_character_placeholder_s, print->set->error); } else if (main->setting.flag & byte_dump_main_flag_classic_e) { f_print_dynamic_raw(f_string_ascii_period_s, print->to); } else { - fl_print_format("%[%r%]", print->to, print->set->warning, byte_dump_character_placeholder_s, print->set->warning); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->warning, byte_dump_character_placeholder_s, print->set->warning); } if (main->setting.flag & byte_dump_main_flag_wide_e) { @@ -377,7 +330,7 @@ extern "C" { width_utf = macro_f_utf_byte_width_is(c); if (invalid[i]) { - fl_print_format("%[%r%]", print->to, print->set->error, byte_dump_character_incomplete_s, print->set->error); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->error, byte_dump_character_incomplete_s, print->set->error); if (main->setting.flag & byte_dump_main_flag_wide_e) { f_print_dynamic_raw(f_string_ascii_space_s, print->to); @@ -496,10 +449,10 @@ extern "C" { // Print invalid placeholder for invalid UTF-8 widths. if (invalid[i]) { - fl_print_format("%[%r%]", print->to, print->set->error, byte_dump_character_incomplete_s, print->set->error); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->error, byte_dump_character_incomplete_s, print->set->error); } else { - fl_print_format("%[%r%]", print->to, print->set->warning, byte_dump_character_incomplete_s, print->set->warning); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->warning, byte_dump_character_incomplete_s, print->set->warning); } } else if (width_utf == 2) { @@ -588,13 +541,13 @@ extern "C" { if (width_utf > 1 && at + 1 < main->setting.width) { if (main->setting.flag & byte_dump_main_flag_placeholder_e) { if (invalid[i]) { - fl_print_format("%[%r%]", print->to, print->set->error, byte_dump_character_placeholder_s, print->set->error); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->error, byte_dump_character_placeholder_s, print->set->error); } else if (main->setting.flag & byte_dump_main_flag_classic_e) { f_print_dynamic_raw(f_string_ascii_period_s, print->to); } else { - fl_print_format("%[%r%]", print->to, print->set->warning, byte_dump_character_placeholder_s, print->set->warning); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->warning, byte_dump_character_placeholder_s, print->set->warning); } } else { @@ -610,13 +563,13 @@ extern "C" { if (width_utf > 2 && at + 1 < main->setting.width) { if (main->setting.flag & byte_dump_main_flag_placeholder_e) { if (invalid[i]) { - fl_print_format("%[%r%]", print->to, print->set->error, byte_dump_character_placeholder_s, print->set->error); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->error, byte_dump_character_placeholder_s, print->set->error); } else if (main->setting.flag & byte_dump_main_flag_classic_e) { f_print_dynamic_raw(f_string_ascii_period_s, print->to); } else { - fl_print_format("%[%r%]", print->to, print->set->warning, byte_dump_character_placeholder_s, print->set->warning); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->warning, byte_dump_character_placeholder_s, print->set->warning); } } else { @@ -632,13 +585,13 @@ extern "C" { if (width_utf > 3 && at + 1 < main->setting.width) { if (main->setting.flag & byte_dump_main_flag_placeholder_e) { if (invalid[i]) { - fl_print_format("%[%r%]", print->to, print->set->error, byte_dump_character_placeholder_s, print->set->error); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->error, byte_dump_character_placeholder_s, print->set->error); } else if (main->setting.flag & byte_dump_main_flag_classic_e) { f_print_dynamic_raw(f_string_ascii_period_s, print->to); } else { - fl_print_format("%[%r%]", print->to, print->set->warning, byte_dump_character_placeholder_s, print->set->warning); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->warning, byte_dump_character_placeholder_s, print->set->warning); } } else { @@ -660,13 +613,13 @@ extern "C" { for (; at < main->setting.width; ++at) { if (invalid[at]) { - fl_print_format("%[%r%]", print->to, print->set->error, byte_dump_character_placeholder_s, print->set->error); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->error, byte_dump_character_placeholder_s, print->set->error); } else if (main->setting.flag & byte_dump_main_flag_classic_e) { f_print_dynamic_raw(f_string_ascii_period_s, print->to); } else { - fl_print_format("%[%r%]", print->to, print->set->warning, byte_dump_character_placeholder_s, print->set->warning); + fl_print_format(byte_dump_format_raw_context_s.string, print->to, print->set->warning, byte_dump_character_placeholder_s, print->set->warning); } if (main->setting.flag & byte_dump_main_flag_wide_e) { diff --git a/level_3/byte_dump/c/main/process.c b/level_3/byte_dump/c/main/process.c index b937e2e..46f325a 100644 --- a/level_3/byte_dump/c/main/process.c +++ b/level_3/byte_dump/c/main/process.c @@ -241,24 +241,7 @@ extern "C" { if (cell.column && cell.column < main->setting.width) { while (cell.column < main->setting.width) { - if (main->setting.flag & byte_dump_main_flag_unicode_e) { - f_print_terminated(" ", main->program.output.to); - } - else if (main->setting.mode == byte_dump_mode_hexidecimal_e) { - f_print_terminated(" ", main->program.output.to); - } - else if (main->setting.mode == byte_dump_mode_duodecimal_e) { - f_print_terminated(" ", main->program.output.to); - } - else if (main->setting.mode == byte_dump_mode_octal_e) { - f_print_terminated(" ", main->program.output.to); - } - else if (main->setting.mode == byte_dump_mode_binary_e) { - f_print_terminated(" ", main->program.output.to); - } - else if (main->setting.mode == byte_dump_mode_decimal_e) { - f_print_terminated(" ", main->program.output.to); - } + f_print_dynamic_raw(byte_dump_padding_s[(main->setting.flag & byte_dump_main_flag_unicode_e) ? 0 : main->setting.mode], main->program.output.to); ++cell.column; @@ -273,22 +256,7 @@ extern "C" { f_print_dynamic_raw(f_string_space_s, main->program.output.to); } } - else if (main->setting.mode == byte_dump_mode_duodecimal_e) { - if (!(cell.column % 6)) { - f_print_dynamic_raw(f_string_space_s, main->program.output.to); - } - } - else if (main->setting.mode == byte_dump_mode_octal_e) { - if (!(cell.column % 6)) { - f_print_dynamic_raw(f_string_space_s, main->program.output.to); - } - } - else if (main->setting.mode == byte_dump_mode_binary_e) { - if (!(cell.column % 6)) { - f_print_dynamic_raw(f_string_space_s, main->program.output.to); - } - } - else if (main->setting.mode == byte_dump_mode_decimal_e) { + else if (main->setting.mode == byte_dump_mode_duodecimal_e || main->setting.mode == byte_dump_mode_octal_e || main->setting.mode == byte_dump_mode_binary_e || main->setting.mode == byte_dump_mode_decimal_e) { if (!(cell.column % 6)) { f_print_dynamic_raw(f_string_space_s, main->program.output.to); } -- 1.8.3.1