From 8b6a224ba06e92a3e2b38460487f319e9c1d0d6d Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Wed, 28 Jul 2021 22:17:12 -0500 Subject: [PATCH] Progress: Continue working on print changes. This begins replacing f_color code. The byte_dump is migrated and has exposed bugs in the number processing. For now, the number processing has been disabled solely for the purposes of this commit. I will have to review and rethink the logic with the precision/width and number handling in printf. --- level_0/f_color/c/color.c | 370 +++++++--------------------- level_0/f_color/c/color.h | 258 ++------------------ level_0/f_print/c/print-common.h | 10 +- level_1/fl_print/c/print.c | 153 +----------- level_1/fl_print/c/print.h | 4 +- level_1/fl_print/c/private-print.c | 185 ++++++++++++-- level_1/fl_print/c/private-print.h | 10 +- level_1/fl_print/data/build/dependencies | 1 + level_2/fll_print/c/print.c | 2 + level_2/fll_program/c/program.c | 30 +-- level_3/byte_dump/c/byte_dump.c | 198 +++++++++------ level_3/byte_dump/c/byte_dump.h | 2 + level_3/byte_dump/c/private-byte_dump.c | 387 +++++++++++++++--------------- level_3/byte_dump/data/build/dependencies | 2 + level_3/byte_dump/data/build/settings | 2 +- 15 files changed, 636 insertions(+), 978 deletions(-) diff --git a/level_0/f_color/c/color.c b/level_0/f_color/c/color.c index 7411c79..f3c00f2 100644 --- a/level_0/f_color/c/color.c +++ b/level_0/f_color/c/color.c @@ -4,294 +4,11 @@ extern "C" { #endif -#ifndef _di_f_color_set_ - f_status_t f_color_set(FILE *stream, const f_color_format_t format, const char *color1, const char *color2, const char *color3, const char *color4, const char *color5) { - #ifndef _di_level_1_parameter_checking_ - if (!stream) return F_status_set_error(F_parameter); - if (!color1) return F_status_set_error(F_parameter); - - // make sure all data is in the proper order - if (!color2 && (color3 != 0 || color4 != 0 || color5 != 0)) return F_status_set_error(F_parameter); - if (!color3 && (color4 != 0 || color5 != 0)) return F_status_set_error(F_parameter); - if (!color4 && color5 != 0) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!color2) fprintf(stream, "%s%s%s", format.begin, color1, format.end); - else if (!color3) fprintf(stream, "%s%s%s%s%s", format.begin, color1, format.medium, color2, format.end); - else if (!color4) fprintf(stream, "%s%s%s%s%s%s%s", format.begin, color1, format.medium, color2, format.medium, color3, format.end); - else if (!color5) fprintf(stream, "%s%s%s%s%s%s%s%s%s", format.begin, color1, format.medium, color2, format.medium, color3, format.medium, color4, format.end); - else fprintf(stream, "%s%s%s%s%s%s%s%s%s%s%s", format.begin, color1, format.medium, color2, format.medium, color3, format.medium, color4, format.medium, color5, format.end); - - return F_none; - } -#endif // _di_f_color_set_ - -#ifndef _di_f_color_set_to_ - f_status_t f_color_set_to(const int id, const f_color_format_t format, const char *color1, const char *color2, const char *color3, const char *color4, const char *color5) { - #ifndef _di_level_1_parameter_checking_ - if (id == -1) return F_status_set_error(F_parameter); - if (!color1) return F_status_set_error(F_parameter); - - // make sure all data is in the proper order - if (!color2 && (color3 != 0 || color4 != 0 || color5 != 0)) return F_status_set_error(F_parameter); - if (!color3 && (color4 != 0 || color5 != 0)) return F_status_set_error(F_parameter); - if (!color4 && color5 != 0) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!color2) dprintf(id, "%s%s%s", format.begin, color1, format.end); - else if (!color3) dprintf(id, "%s%s%s%s%s", format.begin, color1, format.medium, color2, format.end); - else if (!color4) dprintf(id, "%s%s%s%s%s%s%s", format.begin, color1, format.medium, color2, format.medium, color3, format.end); - else if (!color5) dprintf(id, "%s%s%s%s%s%s%s%s%s", format.begin, color1, format.medium, color2, format.medium, color3, format.medium, color4, format.end); - else dprintf(id, "%s%s%s%s%s%s%s%s%s%s%s", format.begin, color1, format.medium, color2, format.medium, color3, format.medium, color4, format.medium, color5, format.end); - - return F_none; - } -#endif // _di_f_color_set_to_ - -#ifndef _di_f_color_save_ - f_status_t f_color_save(f_string_dynamic_t *buffer, const f_color_format_t format, const char *color1, const char *color2, const char *color3, const char *color4, const char *color5) { - #ifndef _di_level_1_parameter_checking_ - if (!buffer) return F_status_set_error(F_parameter); - if (!color1) return F_status_set_error(F_parameter); - - // make sure all data is in the proper order - if (!color2 && (color3 != 0 || color4 != 0 || color5 != 0)) return F_status_set_error(F_parameter); - if (!color3 && (color4 != 0 || color5 != 0)) return F_status_set_error(F_parameter); - if (!color4 && color5 != 0) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - f_array_length_t string_size = strnlen(format.begin, f_color_max_size) + strnlen(format.end, f_color_max_size) + 1; - - if (!color2) string_size += strnlen(color1, f_color_max_size); - else if (!color3) string_size += strnlen(color1, f_color_max_size) + strnlen(color2, f_color_max_size); - else if (!color4) string_size += strnlen(color1, f_color_max_size) + strnlen(color2, f_color_max_size) + strnlen(color3, f_color_max_size); - else if (!color5) string_size += strnlen(color1, f_color_max_size) + strnlen(color2, f_color_max_size) + strnlen(color3, f_color_max_size) + strnlen(color4, f_color_max_size); - else string_size += strnlen(color1, f_color_max_size) + strnlen(color2, f_color_max_size) + strnlen(color3, f_color_max_size) + strnlen(color4, f_color_max_size) + strnlen(color5, f_color_max_size); - - // make sure there is enough allocated space, if not, then allocate some more - if (buffer->size - buffer->used - 1 < string_size) { - f_status_t status = F_none; - - macro_f_string_dynamic_t_resize(status, (*buffer), buffer->used + string_size + 1); // the additional 1 is the EOS - - if (F_status_is_error(status)) { - return status; - } - } - - if (!color2) { - strncat(buffer->string, format.begin, f_color_max_size); - strncat(buffer->string, color1, f_color_max_size); - strncat(buffer->string, format.end, f_color_max_size); - } - else if (!color3) { - strncat(buffer->string, format.begin, f_color_max_size); - strncat(buffer->string, color1, f_color_max_size); - strncat(buffer->string, format.medium, f_color_max_size); - strncat(buffer->string, color2, f_color_max_size); - strncat(buffer->string, format.end, f_color_max_size); - } - else if (!color4) { - strncat(buffer->string, format.begin, f_color_max_size); - strncat(buffer->string, color1, f_color_max_size); - strncat(buffer->string, format.medium, f_color_max_size); - strncat(buffer->string, color2, f_color_max_size); - strncat(buffer->string, format.medium, f_color_max_size); - strncat(buffer->string, color3, f_color_max_size); - strncat(buffer->string, format.end, f_color_max_size); - } - else if (!color5) { - strncat(buffer->string, format.begin, f_color_max_size); - strncat(buffer->string, color1, f_color_max_size); - strncat(buffer->string, format.medium, f_color_max_size); - strncat(buffer->string, color2, f_color_max_size); - strncat(buffer->string, format.medium, f_color_max_size); - strncat(buffer->string, color3, f_color_max_size); - strncat(buffer->string, format.medium, f_color_max_size); - strncat(buffer->string, color4, f_color_max_size); - strncat(buffer->string, format.end, f_color_max_size); - } - else { - strncat(buffer->string, format.begin, f_color_max_size); - strncat(buffer->string, color1, f_color_max_size); - strncat(buffer->string, format.medium, f_color_max_size); - strncat(buffer->string, color2, f_color_max_size); - strncat(buffer->string, format.medium, f_color_max_size); - strncat(buffer->string, color3, f_color_max_size); - strncat(buffer->string, format.medium, f_color_max_size); - strncat(buffer->string, color4, f_color_max_size); - strncat(buffer->string, format.medium, f_color_max_size); - strncat(buffer->string, color5, f_color_max_size); - strncat(buffer->string, format.end, f_color_max_size); - } - - // update the amount of space that is to be used - buffer->used += string_size; - - // do not forget the EOS - buffer->string[buffer->used] = 0; - - return F_none; - } -#endif // _di_f_color_save_ - -#ifndef _di_f_color_print_ - f_status_t f_color_print(FILE *stream, const f_color_set_t set, const f_string_t string, ...) { - #ifndef _di_level_1_parameter_checking_ - if (!stream) return F_status_set_error(F_parameter); - if (!string) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (set.before) { - fprintf(stream, "%s", set.before->string); - } - - va_list ap; - - va_start(ap, string); - - vfprintf(stream, string, ap); - - va_end(ap); - - if (set.after) { - fprintf(stream, "%s", set.after->string); - } - - return F_none; - } -#endif // _di_f_color_print_ - -#ifndef _di_f_color_print2_ - f_status_t f_color_print2(FILE *stream, const f_color_set_t set, const f_color_set_t extra, const f_string_t string, ...) { - #ifndef _di_level_1_parameter_checking_ - if (!stream) return F_status_set_error(F_parameter); - if (!string) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (set.before) { - fprintf(stream, "%s", set.before->string); - } - - if (extra.before) { - fprintf(stream, "%s", extra.before->string); - } - - va_list ap; - - va_start(ap, string); - - vfprintf(stream, string, ap); - - va_end(ap); - - if (set.after) { - fprintf(stream, "%s", set.after->string); - } - - if (extra.after) { - fprintf(stream, "%s", extra.after->string); - } - - return F_none; - } -#endif // _di_f_color_print2_ - -#ifndef _di_f_color_print_code_ - f_status_t f_color_print_code(FILE *stream, const f_string_static_t color) { - #ifndef _di_level_1_parameter_checking_ - if (!stream) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (color.used) { - fprintf(stream, "%s", color.string); - } - - return F_none; - } -#endif // _di_f_color_print_code_ - -#ifndef _di_f_color_print_code_to_ - f_status_t f_color_print_code_to(const int id, const f_string_static_t color) { - #ifndef _di_level_1_parameter_checking_ - if (id == -1) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (color.used) { - dprintf(id, "%s", color.string); - } - - return F_none; - } -#endif // _di_f_color_print_code_to_ - -#ifndef _di_f_color_print_to_ - f_status_t f_color_print_to(const int id, const f_color_set_t set, const f_string_t string, ...) { - #ifndef _di_level_1_parameter_checking_ - if (id == -1) return F_status_set_error(F_parameter); - if (!string) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (set.before) { - dprintf(id, "%s", set.before->string); - } - - va_list ap; - - va_start(ap, string); - - vdprintf(id, string, ap); - - va_end(ap); - - if (set.after) { - dprintf(id, "%s", set.after->string); - } - - return F_none; - } -#endif // _di_f_color_print_to_ - -#ifndef _di_f_color_print2_to_ - f_status_t f_color_print2_to(const int id, const f_color_set_t set, const f_color_set_t extra, const f_string_t string, ...) { - #ifndef _di_level_1_parameter_checking_ - if (id == -1) return F_status_set_error(F_parameter); - if (!string) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (set.before) { - dprintf(id, "%s", set.before->string); - } - - if (extra.before) { - dprintf(id, "%s", extra.before->string); - } - - va_list ap; - - va_start(ap, string); - - vdprintf(id, string, ap); - - va_end(ap); - - if (set.after) { - dprintf(id, "%s", set.after->string); - } - - if (extra.after) { - dprintf(id, "%s", extra.after->string); - } - - return F_none; - } -#endif // _di_f_color_print2_to_ - #ifndef _di_f_color_load_context_ f_status_t f_color_load_context(f_color_context_t *context, const bool use_light_colors) { - #ifndef _di_level_1_parameter_checking_ + #ifndef _di_level_0_parameter_checking_ if (!context) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ + #endif // _di_level_0_parameter_checking_ f_status_t status = F_none; @@ -393,6 +110,89 @@ extern "C" { } #endif // _di_f_color_load_context_ +#ifndef _di_f_color_save_ + f_status_t f_color_save(f_string_dynamic_t *buffer, const f_color_format_t format, const char *color1, const char *color2, const char *color3, const char *color4, const char *color5) { + #ifndef _di_level_0_parameter_checking_ + if (!buffer) return F_status_set_error(F_parameter); + if (!color1) return F_status_set_error(F_parameter); + + // make sure all data is in the proper order. + if (!color2 && (color3 != 0 || color4 != 0 || color5 != 0)) return F_status_set_error(F_parameter); + if (!color3 && (color4 != 0 || color5 != 0)) return F_status_set_error(F_parameter); + if (!color4 && color5 != 0) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ + + f_array_length_t string_size = strnlen(format.begin, f_color_max_size) + strnlen(format.end, f_color_max_size) + 1; + + if (!color2) string_size += strnlen(color1, f_color_max_size); + else if (!color3) string_size += strnlen(color1, f_color_max_size) + strnlen(color2, f_color_max_size); + else if (!color4) string_size += strnlen(color1, f_color_max_size) + strnlen(color2, f_color_max_size) + strnlen(color3, f_color_max_size); + else if (!color5) string_size += strnlen(color1, f_color_max_size) + strnlen(color2, f_color_max_size) + strnlen(color3, f_color_max_size) + strnlen(color4, f_color_max_size); + else string_size += strnlen(color1, f_color_max_size) + strnlen(color2, f_color_max_size) + strnlen(color3, f_color_max_size) + strnlen(color4, f_color_max_size) + strnlen(color5, f_color_max_size); + + if (buffer->size - buffer->used - 1 < string_size) { + f_status_t status = F_none; + + macro_f_string_dynamic_t_resize(status, (*buffer), buffer->used + string_size + 1); + + if (F_status_is_error(status)) { + return status; + } + } + + if (!color2) { + strncat(buffer->string, format.begin, f_color_max_size); + strncat(buffer->string, color1, f_color_max_size); + strncat(buffer->string, format.end, f_color_max_size); + } + else if (!color3) { + strncat(buffer->string, format.begin, f_color_max_size); + strncat(buffer->string, color1, f_color_max_size); + strncat(buffer->string, format.medium, f_color_max_size); + strncat(buffer->string, color2, f_color_max_size); + strncat(buffer->string, format.end, f_color_max_size); + } + else if (!color4) { + strncat(buffer->string, format.begin, f_color_max_size); + strncat(buffer->string, color1, f_color_max_size); + strncat(buffer->string, format.medium, f_color_max_size); + strncat(buffer->string, color2, f_color_max_size); + strncat(buffer->string, format.medium, f_color_max_size); + strncat(buffer->string, color3, f_color_max_size); + strncat(buffer->string, format.end, f_color_max_size); + } + else if (!color5) { + strncat(buffer->string, format.begin, f_color_max_size); + strncat(buffer->string, color1, f_color_max_size); + strncat(buffer->string, format.medium, f_color_max_size); + strncat(buffer->string, color2, f_color_max_size); + strncat(buffer->string, format.medium, f_color_max_size); + strncat(buffer->string, color3, f_color_max_size); + strncat(buffer->string, format.medium, f_color_max_size); + strncat(buffer->string, color4, f_color_max_size); + strncat(buffer->string, format.end, f_color_max_size); + } + else { + strncat(buffer->string, format.begin, f_color_max_size); + strncat(buffer->string, color1, f_color_max_size); + strncat(buffer->string, format.medium, f_color_max_size); + strncat(buffer->string, color2, f_color_max_size); + strncat(buffer->string, format.medium, f_color_max_size); + strncat(buffer->string, color3, f_color_max_size); + strncat(buffer->string, format.medium, f_color_max_size); + strncat(buffer->string, color4, f_color_max_size); + strncat(buffer->string, format.medium, f_color_max_size); + strncat(buffer->string, color5, f_color_max_size); + strncat(buffer->string, format.end, f_color_max_size); + } + + buffer->used += string_size; + buffer->string[buffer->used] = 0; + + return F_none; + } +#endif // _di_f_color_save_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/level_0/f_color/c/color.h b/level_0/f_color/c/color.h index 17105a5..48e1351 100644 --- a/level_0/f_color/c/color.h +++ b/level_0/f_color/c/color.h @@ -32,94 +32,52 @@ extern "C" { #endif /** - * Given some file or standard io, and push color information to that file or standard io. - * - * Up to 5 colors may be associted with a single color format block. - * - * @param stream - * The file stream or standard output. - * @param format - * The color format parts. - * @param color1 - * A color to assign, set to 0 to disable. - * @param color2 - * A color to assign, set to 0 to disable. - * @param color3 - * A color to assign, set to 0 to disable. - * @param color4 - * A color to assign, set to 0 to disable. - * @param color5 - * A color to assign, set to 0 to disable. - * - * @return - * F_none on success. + * Load the appropriate colors into the color context. * - * F_parameter (with error bit) if a parameter is invalid. - */ -#ifndef _di_f_color_set_ - extern f_status_t f_color_set(FILE *stream, const f_color_format_t format, const char *color1, const char *color2, const char *color3, const char *color4, const char *color5); - - #define macro_fl_color_set_1(stream, format, color1) f_color_set(stream, format, color1, 0, 0, 0, 0); - #define macro_fl_color_set_2(stream, format, color1, color2) f_color_set(stream, format, color1, color2, 0, 0, 0); - #define macro_fl_color_set_3(stream, format, color1, color2, color3) f_color_set(stream, format, color1, color2, color3, 0, 0); - #define macro_fl_color_set_4(stream, format, color1, color2, color3, color4) f_color_set(stream, format, color1, color2, color3, color4, 0); - #define macro_fl_color_set_5(stream, format, color1, color2, color3, color4, color5) f_color_set(stream, format, color1, color2, color3, color4, color5); -#endif // _di_f_color_set_ - -/** - * Given some file descriptor, and push color information to that descriptor. + * This will handle the difference betweem xorg terminals and linux consoles. + * If you wish to use non-standard colors either redefine this function or don't use it. * - * Up to 5 colors may be associted with a single color format block. + * The default/fallback behavior is f_color_xterminal. * - * @param id - * The file descriptor to print to. - * @param format - * The color format parts. - * @param color1 - * A color to assign, set to 0 to disable. - * @param color2 - * A color to assign, set to 0 to disable. - * @param color3 - * A color to assign, set to 0 to disable. - * @param color4 - * A color to assign, set to 0 to disable. - * @param color5 - * A color to assign, set to 0 to disable. + * @param context + * The color context the load the color codes into. + * @param use_light_colors + * Set to F_true to use colors for light backgrounds. + * Set to F_false to use colors for dark backgrounds. * * @return * F_none on success. * + * F_memory_not (with error bit) on out of memory. * F_parameter (with error bit) if a parameter is invalid. */ -#ifndef _di_f_color_set_to_ - extern f_status_t f_color_set_to(const int id, const f_color_format_t format, const char *color1, const char *color2, const char *color3, const char *color4, const char *color5); - - #define macro_fl_color_set_to_1(id, format, color1) f_color_set_to(id, format, color1, 0, 0, 0, 0); - #define macro_fl_color_set_to_2(id, format, color1, color2) f_color_set_to(id, format, color1, color2, 0, 0, 0); - #define macro_fl_color_set_to_3(id, format, color1, color2, color3) f_color_set_to(id, format, color1, color2, color3, 0, 0); - #define macro_fl_color_set_to_4(id, format, color1, color2, color3, color4) f_color_set_to(id, format, color1, color2, color3, color4, 0); - #define macro_fl_color_set_to_5(id, format, color1, color2, color3, color4, color5) f_color_set_to(id, format, color1, color2, color3, color4, color5); -#endif // _di_f_color_set_to_ +#ifndef _di_f_color_load_context_ + extern f_status_t f_color_load_context(f_color_context_t *context, const bool use_light_colors); +#endif // _di_f_color_load_context_ /** * Save color information to some string. * * Up to 5 colors may be associted with a single color format block. * + * This is environment sensitive. + * If TERM is not defined or set to "linux", then macro_f_color_t_set_linux() is used. + * Otherwise macro_f_color_t_set_xterminal() is used. + * * @param buffer * The string to save the colors to. * @param format * The color format parts. * @param color1 - * A color to assign, set to 0 to disable. + * A color to assign, set to NULL to not use. * @param color2 - * A color to assign, set to 0 to disable. + * A color to assign, set to NULL to not use. * @param color3 - * A color to assign, set to 0 to disable. + * A color to assign, set to NULL to not use. * @param color4 - * A color to assign, set to 0 to disable. + * A color to assign, set to NULL to not use. * @param color5 - * A color to assign, set to 0 to disable. + * A color to assign, set to NULL to not use. * * @return * F_none on success. @@ -137,178 +95,6 @@ extern "C" { #define macro_fl_color_save_5(buffer, format, color1, color2, color3, color4, color5) f_color_save(buffer, format, color1, color2, color3, color4, color5); #endif // _di_f_color_save_ -/** - * Print a string, wrapped in a given start and stop color. - * - * If the colors strings have nothing used in them, then this will only print the string. - * - * @param stream - * The file stream or standard output. - * @param set - * The color set used for printing. - * @param string - * The string to print. - * @param ... - * Variable arguments, processed in the same way fprintf() processes them. - * - * @return - * F_none on success. - * - * F_parameter (with error bit) if a parameter is invalid. - */ -#ifndef _di_f_color_print_ - extern f_status_t f_color_print(FILE *stream, const f_color_set_t set, const f_string_t string, ...); -#endif // _di_f_color_print_ - -/** - * Print a string, wrapped in a given start, extra, and stop color. - * - * If the colors strings have nothing used in them, then this will only print the string. - * - * It is common for colors to be bolded. - * This is intended to simplify printing bold colors. - * - * @param stream - * The file stream or standard output. - * @param set - * The color set used for printing. - * @param extra - * The a second color set used for printing, which gets appended after set.before and set.after, respectively. - * @param string - * The string to print. - * @param ... - * Variable arguments, processed in the same way fprintf() processes them. - * - * @return - * F_none on success. - * - * F_parameter (with error bit) if a parameter is invalid. - */ -#ifndef _di_f_color_print2_ - extern f_status_t f_color_print2(FILE *stream, const f_color_set_t set, const f_color_set_t extra, const f_string_t string, ...); -#endif // _di_f_color_print2_ - -/** - * Print a single color code to the given file or standard io. - * - * Be sure to forget to print the color reset when done. - * - * @param stream - * The file stream or standard output. - * @param start_color - * The color code to print. - * - * @return - * F_none on success. - * - * F_parameter (with error bit) if a parameter is invalid. - * - * Errors (with error bit) from: f_print_dynamic(). - */ -#ifndef _di_f_color_print_code_ - extern f_status_t f_color_print_code(FILE *stream, const f_string_static_t color); -#endif // _di_f_color_print_code_ - -/** - * Print a single color code to the given file represented by a file descriptor. - * - * Be sure to forget to print the color reset when done. - * - * @param id - * The file descriptor to print to. - * @param start_color - * The color code to print. - * - * @return - * F_none on success. - * - * F_parameter (with error bit) if a parameter is invalid. - * - * Errors (with error bit) from: f_print_to_dynamic(). - */ -#ifndef _di_f_color_print_code_to_ - extern f_status_t f_color_print_code_to(const int id, const f_string_static_t color); -#endif // _di_f_color_print_code_to_ - -/** - * Print a string, wrapped in a given start and stop color. - * - * If the colors strings have nothing used in them, then this will only print the string. - * - * @param id - * The file descriptor to print to. - * @param set - * The color set used for printing. - * @param string - * The string to print. - * @param ... - * Variable arguments, processed in the same way fprintf() processes them. - * - * @return - * F_none on success. - * - * F_parameter (with error bit) if a parameter is invalid. - * - * Errors (with error bit) from: f_print_dynamic(). - */ -#ifndef _di_f_color_print_to_ - extern f_status_t f_color_print_to(const int id, const f_color_set_t set, const f_string_t string, ...); -#endif // _di_f_color_print_to_ - -/** - * Print a string, wrapped in a given start, extra, and stop color. - * - * If the colors strings have nothing used in them, then this will only print the string. - * - * It is common for colors to be bolded. - * This is intended to simplify printing bold colors. - * - * @param id - * The file descriptor to print to. - * @param set - * The color set used for printing. - * @param extra - * The a second color set used for printing, which gets appended after set.before and set.after, respectively. - * @param string - * The string to print. - * @param ... - * Variable arguments, processed in the same way fprintf() processes them. - * - * @return - * F_none on success. - * - * F_parameter (with error bit) if a parameter is invalid. - * - * Errors (with error bit) from: f_print_dynamic(). - */ -#ifndef _di_f_color_print2_to_ - extern f_status_t f_color_print2_to(const int id, const f_color_set_t set, const f_color_set_t extra, const f_string_t string, ...); -#endif // _di_f_color_print2_to_ - -/** - * Load the appropriate colors into the color context. - * - * This will handle the difference betweem xorg terminals and linux consoles. - * If you wish to use non-standard colors either redefine this function or don't use it. - * - * The default/fallback behavior is f_color_xterminal. - * - * @param context - * The color context the load the color codes into. - * @param use_light_colors - * Set to F_true to use colors for light backgrounds. - * Set to F_false to use colors for dark backgrounds. - * - * @return - * F_none on success. - * - * F_memory_not (with error bit) on out of memory. - * F_parameter (with error bit) if a parameter is invalid. - */ -#ifndef _di_f_color_load_context_ - extern f_status_t f_color_load_context(f_color_context_t *context, const bool use_light_colors); -#endif // _di_f_color_load_context_ - #ifdef __cplusplus } // extern "C" #endif diff --git a/level_0/f_print/c/print-common.h b/level_0/f_print/c/print-common.h index 65d9e34..96108c3 100644 --- a/level_0/f_print/c/print-common.h +++ b/level_0/f_print/c/print-common.h @@ -117,12 +117,12 @@ extern "C" { * f_print_format_flag_*: * - align_left: "-", Use left-justification. * - convert: "#", Use alternate form conversion (prefixing 0b/0B, 0o/0O, 0t/0T, 0d/0D, 0x/0X). - * - ignore_index: ";", Ignore characters in the given positions from a f_array_length_t (only applies to string arguments but not character arguments). - * - ignore_range: ":", Ignore characters in the given ranges from a f_string_range_t (only applies to string arguments but not character arguments). + * - ignore_index: ";", Ignore characters in the given positions from a f_array_length_t (only applies to static/dynamic string arguments but not character arguments). + * - ignore_range: ":", Ignore characters in the given ranges from a f_string_range_t (only applies to static/dynamic string arguments but not character arguments). * - precision: Designates that a precision is in use. * - sign_always: "+", Always show the signs (+ or -). * - sign_pad: " ", Add a space where a sign would be if the sign is not displayed. - * - trim: "=", Trim leading and trailing whitespaces (only applies to string arguments but not character arguments). + * - trim: "=", Trim leading and trailing whitespaces (only applies to static/dynamic string arguments but not character arguments). * - uppercase: Display any base units as uppercase. * - width: Designates that a width is in use. * - zeros_leading: "0", Use leading zeros. @@ -164,6 +164,8 @@ extern "C" { * f_print_format_type_*: * - character: "c", type is a 1-byte unsigned character. * - character_safe: "C", type is a 1-byte unsigned character, where control characters and other problems are handled. + * - color_after: "]", type is f_color_set_t such that the f_color_set_t.after is used. + * - color_before: "[", type is f_color_set_t such that the f_color_set_t.begin is used. * - long: "l", "L", type is a signed integer. * - long_long: "ll", "LL", type is a signed integer. * - number: "n", "N", type is an f_number_signed_t. @@ -188,6 +190,8 @@ extern "C" { enum { f_print_format_type_character = 1, f_print_format_type_character_safe, + f_print_format_type_color_after, + f_print_format_type_color_before, f_print_format_type_long, f_print_format_type_long_long, f_print_format_type_number, diff --git a/level_1/fl_print/c/print.c b/level_1/fl_print/c/print.c index 11c0f88..d4cc32d 100644 --- a/level_1/fl_print/c/print.c +++ b/level_1/fl_print/c/print.c @@ -58,155 +58,24 @@ extern "C" { if (!ap) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ - uint8_t i = 0; - - char *current = string; - char replace = *current; - - while (*current) { - - if (macro_f_utf_byte_width_is(*current)) { - if (macro_f_utf_byte_width_is(*current) == 1) { - if (!fputc_unlocked(f_print_sequence_unknown_s[0], output)) { - return F_status_set_error(F_output); - } - } - else { - for (i = 0; i < macro_f_utf_byte_width_is(*current) && current[i]; ++i) { - // do nothing. - } // for - - if (i < macro_f_utf_byte_width_is(*current)) { - if (!fputc_unlocked(f_print_sequence_unknown_s[0], output)) { - return F_status_set_error(F_output); - } + f_status_t status = F_none; - break; - } + for (char *current = string; *current; current = current + 1) { - for (i = 0; i < macro_f_utf_byte_width_is(*current); ++i) { + if (*current == f_string_ascii_percent_s[0]) { + current = current + 1; - if (!fputc_unlocked(current[i], output)) { - return F_status_set_error(F_output); - } - } // for + status = private_fl_print_string_convert(current, output, ap); + if (F_status_is_error(status)) break; - current = current + (macro_f_utf_byte_width_is(*current) - 1); - } + if (!*current) break; } - else { - if (*current > 32 && *current != 127) { - replace = *current; - } - else if (replace == 1) { - replace = f_print_sequence_start_of_header_s[0]; - } - else if (replace == 2) { - replace = f_print_sequence_start_of_text_s[0]; - } - else if (replace == 3) { - replace = f_print_sequence_end_of_text_s[0]; - } - else if (replace == 4) { - replace = f_print_sequence_end_of_transmission_s[0]; - } - else if (replace == 5) { - replace = f_print_sequence_end_of_enquiry_s[0]; - } - else if (replace == 6) { - replace = f_print_sequence_acknowledge_s[0]; - } - else if (replace == 7) { - replace = f_print_sequence_bell_s[0]; - } - else if (replace == 8) { - replace = f_print_sequence_backspace_s[0]; - } - else if (replace == 9) { - replace = f_print_sequence_tab_s[0]; - } - else if (replace == 10) { - replace = f_print_sequence_new_line_s[0]; - } - else if (replace == 11) { - replace = f_print_sequence_tab_vertical_s[0]; - } - else if (replace == 12) { - replace = f_print_sequence_form_feed_s[0]; - } - else if (replace == 13) { - replace = f_print_sequence_carriage_return_s[0]; - } - else if (replace == 14) { - replace = f_print_sequence_shift_out_s[0]; - } - else if (replace == 15) { - replace = f_print_sequence_shift_in_s[0]; - } - else if (replace == 16) { - replace = f_print_sequence_data_link_escape_s[0]; - } - else if (replace == 17) { - replace = f_print_sequence_device_control_1_s[0]; - } - else if (replace == 18) { - replace = f_print_sequence_device_control_2_s[0]; - } - else if (replace == 19) { - replace = f_print_sequence_device_control_3_s[0]; - } - else if (replace == 20) { - replace = f_print_sequence_device_control_4_s[0]; - } - else if (replace == 21) { - replace = f_print_sequence_negative_acknowledge_s[0]; - } - else if (replace == 22) { - replace = f_print_sequence_synchronous_idle_s[0]; - } - else if (replace == 23) { - replace = f_print_sequence_end_of_transmission_block_s[0]; - } - else if (replace == 24) { - replace = f_print_sequence_cancel_s[0]; - } - else if (replace == 25) { - replace = f_print_sequence_end_of_medium_s[0]; - } - else if (replace == 26) { - replace = f_print_sequence_substitute_s[0]; - } - else if (replace == 27) { - replace = f_print_sequence_escape_s[0]; - } - else if (replace == 28) { - replace = f_print_sequence_file_separator_s[0]; - } - else if (replace == 29) { - replace = f_print_sequence_group_separator_s[0]; - } - else if (replace == 30) { - replace = f_print_sequence_record_separator_s[0]; - } - else if (replace == 31) { - replace = f_print_sequence_unit_separator_s[0]; - } - else if (replace == 32) { - replace = f_print_sequence_space_s[0]; - } - else if (replace == 127) { - replace = f_print_sequence_delete_s[0]; - } - - if (!fputc_unlocked(replace, output)) { - return F_status_set_error(F_output); - } + else if (!fputc_unlocked(*current, output)) { + return F_status_set_error(F_output); } + } // for - current = current + 1; - } // while - - return F_none; + return status; } #endif // _di_fl_print_string_va_ diff --git a/level_1/fl_print/c/print.h b/level_1/fl_print/c/print.h index 6110bd9..ec2b554 100644 --- a/level_1/fl_print/c/print.h +++ b/level_1/fl_print/c/print.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -134,7 +135,7 @@ extern "C" { /** * A formatted print function similar to (but not the same as) the c-library vfprintf() function. * - * @todo add the complex documentation. + * This is identical to fl_print_string() except it accepts a va_list as a variable instead of as "...". * * This print function does not use locking, be sure something like flockfile() and funlockfile() are appropriately called. * @@ -177,6 +178,7 @@ extern "C" { * @see f_print_dynamic_safely() * @see f_print_safely() * @see f_print_terminated() + * @see fl_print_string() */ #ifndef _di_fl_print_string_va_ extern f_status_t fl_print_string_va(const f_string_t string, FILE *output, va_list *ap); diff --git a/level_1/fl_print/c/private-print.c b/level_1/fl_print/c/private-print.c index 86b385c..de3dbc1 100644 --- a/level_1/fl_print/c/private-print.c +++ b/level_1/fl_print/c/private-print.c @@ -29,10 +29,10 @@ extern "C" { flag |= f_print_format_flag_convert; } else if (*current == f_string_ascii_colon_semi_s[0]) { - flag |= f_print_format_flag_ignore_index; // @todo + flag |= f_print_format_flag_ignore_index; } else if (*current == f_string_ascii_colon_s[0]) { - flag |= f_print_format_flag_ignore_range; // @todo + flag |= f_print_format_flag_ignore_range; } else if (*current == f_string_ascii_plus_s[0]) { flag |= f_print_format_flag_sign_always; @@ -43,8 +43,8 @@ extern "C" { else if (*current == f_string_ascii_equal_s[0]) { flag |= f_print_format_flag_trim; } - else if (*current == f_string_ascii_0_s[0] || *current == f_string_ascii_1_s[0] || *current == f_string_ascii_2_s[0] || *current == f_string_ascii_3_s[0] || *current == f_string_ascii_4_s[0] || *current == f_string_ascii_5_s[0] || *current == f_string_ascii_6_s[0] || *current == f_string_ascii_7_s[0] || *current == f_string_ascii_8_s[0] || *current == f_string_ascii_9_s[0]) { - + /* @fixme + else if (*current > 0x29 && *current < 0x40) { if (!(flag & f_print_format_flag_width)) { if (*current == f_string_ascii_0_s[0]) { flag |= f_print_format_flag_zeros_leading; @@ -60,7 +60,7 @@ extern "C" { else { return F_status_set_error(F_valid_not); } - } + }*/ else if (*current == f_string_ascii_dollar_s[0]) { // If followed immediately by a '$' this is ignored. // Use '%$' to separate, such as '%l%$l' would allow for '0l' to be printed where '%ll' would interpret the 'l', resulting in a long long. @@ -86,11 +86,10 @@ extern "C" { return F_status_set_error(F_valid_not); } - if (private_fl_print_convert_number(current, ap, &precision)) { + // @fixme this looks wrong here, probably needs to handle the current + 1 and not current. + /*if (private_fl_print_convert_number(current, ap, &precision)) { break; - } - - continue; + }*/ } else { if (*current == f_string_ascii_c_s[0]) { @@ -170,9 +169,51 @@ extern "C" { const f_string_static_t value = va_arg(*ap, f_string_static_t); if (flag & f_print_format_flag_trim) { + if (flag & f_print_format_flag_ignore_index) { + if (flag & f_print_format_flag_ignore_range) { + f_array_lengths_t except_at = va_arg(*ap, f_array_lengths_t); + f_string_ranges_t except_in = va_arg(*ap, f_string_ranges_t); + + return private_fl_print_trim_except_in(value.string, value.used, except_at, except_in, output); + } + + f_array_lengths_t except_at = va_arg(*ap, f_array_lengths_t); + f_string_ranges_t except_in = f_string_ranges_t_initialize; + + return private_fl_print_trim_except_in(value.string, value.used, except_at, except_in, output); + } + + if (flag & f_print_format_flag_ignore_range) { + f_array_lengths_t except_at = f_array_lengths_t_initialize; + f_string_ranges_t except_in = va_arg(*ap, f_string_ranges_t); + + return private_fl_print_trim_except_in(value.string, value.used, except_at, except_in, output); + } + return private_fl_print_trim(value.string, value.used, output); } + if (flag & f_print_format_flag_ignore_index) { + if (flag & f_print_format_flag_ignore_range) { + f_array_lengths_t except_at = va_arg(*ap, f_array_lengths_t); + f_string_ranges_t except_in = va_arg(*ap, f_string_ranges_t); + + return f_print_except_in(value.string, value.used, except_at, except_in, output); + } + + f_array_lengths_t except_at = va_arg(*ap, f_array_lengths_t); + f_string_ranges_t except_in = f_string_ranges_t_initialize; + + return f_print_except_in(value.string, value.used, except_at, except_in, output); + } + + if (flag & f_print_format_flag_ignore_range) { + f_array_lengths_t except_at = f_array_lengths_t_initialize; + f_string_ranges_t except_in = va_arg(*ap, f_string_ranges_t); + + return f_print_except_in(value.string, value.used, except_at, except_in, output); + } + return f_print_dynamic(value, output); } else if (*current == f_string_ascii_Q_s[0]) { @@ -181,7 +222,49 @@ extern "C" { const f_string_static_t value = va_arg(*ap, f_string_static_t); if (flag & f_print_format_flag_trim) { - return private_fl_print_trim(value.string, value.used, output); + if (flag & f_print_format_flag_ignore_index) { + if (flag & f_print_format_flag_ignore_range) { + f_array_lengths_t except_at = va_arg(*ap, f_array_lengths_t); + f_string_ranges_t except_in = va_arg(*ap, f_string_ranges_t); + + return private_fl_print_trim_except_in_safely(value.string, value.used, except_at, except_in, output); + } + + f_array_lengths_t except_at = va_arg(*ap, f_array_lengths_t); + f_string_ranges_t except_in = f_string_ranges_t_initialize; + + return private_fl_print_trim_except_in_safely(value.string, value.used, except_at, except_in, output); + } + + if (flag & f_print_format_flag_ignore_range) { + f_array_lengths_t except_at = f_array_lengths_t_initialize; + f_string_ranges_t except_in = va_arg(*ap, f_string_ranges_t); + + return private_fl_print_trim_except_in_safely(value.string, value.used, except_at, except_in, output); + } + + return private_fl_print_trim_safely(value.string, value.used, output); + } + + if (flag & f_print_format_flag_ignore_index) { + if (flag & f_print_format_flag_ignore_range) { + f_array_lengths_t except_at = va_arg(*ap, f_array_lengths_t); + f_string_ranges_t except_in = va_arg(*ap, f_string_ranges_t); + + return f_print_except_in_safely(value.string, value.used, except_at, except_in, output); + } + + f_array_lengths_t except_at = va_arg(*ap, f_array_lengths_t); + f_string_ranges_t except_in = f_string_ranges_t_initialize; + + return f_print_except_in_safely(value.string, value.used, except_at, except_in, output); + } + + if (flag & f_print_format_flag_ignore_range) { + f_array_lengths_t except_at = f_array_lengths_t_initialize; + f_string_ranges_t except_in = va_arg(*ap, f_string_ranges_t); + + return f_print_except_in_safely(value.string, value.used, except_at, except_in, output); } return f_print_dynamic_safely(value, output); @@ -192,7 +275,49 @@ extern "C" { const f_string_static_t value = va_arg(*ap, f_string_static_t); if (flag & f_print_format_flag_trim) { - return private_fl_print_trim(value.string, value.used, output); + if (flag & f_print_format_flag_ignore_index) { + if (flag & f_print_format_flag_ignore_range) { + f_array_lengths_t except_at = va_arg(*ap, f_array_lengths_t); + f_string_ranges_t except_in = va_arg(*ap, f_string_ranges_t); + + return private_fl_print_trim_except_in_raw(value.string, value.used, except_at, except_in, output); + } + + f_array_lengths_t except_at = va_arg(*ap, f_array_lengths_t); + f_string_ranges_t except_in = f_string_ranges_t_initialize; + + return private_fl_print_trim_except_in_raw(value.string, value.used, except_at, except_in, output); + } + + if (flag & f_print_format_flag_ignore_range) { + f_array_lengths_t except_at = f_array_lengths_t_initialize; + f_string_ranges_t except_in = va_arg(*ap, f_string_ranges_t); + + return private_fl_print_trim_except_in_raw(value.string, value.used, except_at, except_in, output); + } + + return private_fl_print_trim_raw(value.string, value.used, output); + } + + if (flag & f_print_format_flag_ignore_index) { + if (flag & f_print_format_flag_ignore_range) { + f_array_lengths_t except_at = va_arg(*ap, f_array_lengths_t); + f_string_ranges_t except_in = va_arg(*ap, f_string_ranges_t); + + return f_print_except_in_raw(value.string, value.used, except_at, except_in, output); + } + + f_array_lengths_t except_at = va_arg(*ap, f_array_lengths_t); + f_string_ranges_t except_in = f_string_ranges_t_initialize; + + return f_print_except_in_raw(value.string, value.used, except_at, except_in, output); + } + + if (flag & f_print_format_flag_ignore_range) { + f_array_lengths_t except_at = f_array_lengths_t_initialize; + f_string_ranges_t except_in = va_arg(*ap, f_string_ranges_t); + + return f_print_except_in_raw(value.string, value.used, except_at, except_in, output); } return f_print_dynamic_raw(value, output); @@ -202,11 +327,6 @@ extern "C" { // NULL terminated string. const f_string_t value = va_arg(*ap, f_string_t); - if (flag & f_print_format_flag_trim) { - // @todo: implement a: private_fl_print_trim_teminated(). - //return private_fl_print_trim_teminated(value, output); - } - return f_print_terminated(value, output); } else if (*current == f_string_ascii_S_s[0]) { @@ -214,11 +334,6 @@ extern "C" { // NULL terminated safe string. const f_string_t value = va_arg(*ap, f_string_t); - if (flag & f_print_format_flag_trim) { - // @todo: implement a: private_fl_print_trim_safely_teminated(). - //return private_fl_print_trim_teminated(value, output); - } - return f_print_safely_terminated(value, output); } else if (*current == f_string_ascii_u_s[0]) { @@ -289,6 +404,24 @@ extern "C" { type = f_print_format_type_size; flag |= f_print_format_flag_uppercase; } + else if (*current == f_string_ascii_bracket_open_s[0]) { + const f_color_set_t value = va_arg(*ap, f_color_set_t); + + if (value.before) { + return f_print_dynamic(*value.before, output); + } + + return F_none; + } + else if (*current == f_string_ascii_bracket_close_s[0]) { + const f_color_set_t value = va_arg(*ap, f_color_set_t); + + if (value.after) { + return f_print_dynamic(*value.after, output); + } + + return F_none; + } else { return F_status_set_error(F_valid_not); } @@ -410,7 +543,7 @@ extern "C" { *number = 0; - for (current = current + 1; *current; current = current + 1) { + for (; *current; current = current + 1) { if (*current == f_string_ascii_0_s[0]) { *number *= 10; @@ -457,11 +590,17 @@ extern "C" { break; } else { + + // reset position non-number. + current = current - 1; + break; } } // for - if (!*current) return F_false; + if (*current) { + return F_false; + } return F_true; } diff --git a/level_1/fl_print/c/private-print.h b/level_1/fl_print/c/private-print.h index 8a9ced0..0025154 100644 --- a/level_1/fl_print/c/private-print.h +++ b/level_1/fl_print/c/private-print.h @@ -72,16 +72,18 @@ extern "C" { * * This should be called to convert numbers after a '%', such as the field with or precision. * + * On return the current will point to either the last consecutive character representing a number, the asterisk, or NULL. + * * @param current * The current character position within the string. * @param ap * The variable arguments list. - * @param output - * The file stream to output to, including standard streams such as stdout and stderr. + * @param number + * The converted number. * * @return - * F_true on success and end of current is found. - * F_false on success without reaching the end of current. + * F_true on success and end of string (NULL) is found. + * F_false on success without reaching the end of string (NULL). * * @see va_arg() */ diff --git a/level_1/fl_print/data/build/dependencies b/level_1/fl_print/data/build/dependencies index 756ff60..a0ae75a 100644 --- a/level_1/fl_print/data/build/dependencies +++ b/level_1/fl_print/data/build/dependencies @@ -5,5 +5,6 @@ f_status f_memory f_string f_utf +f_color f_conversion f_print diff --git a/level_2/fll_print/c/print.c b/level_2/fll_print/c/print.c index b9092d3..d91f552 100644 --- a/level_2/fll_print/c/print.c +++ b/level_2/fll_print/c/print.c @@ -408,6 +408,8 @@ extern "C" { va_end(ap); funlockfile(output); + + return status; } #endif // _di_fll_print_string_ diff --git a/level_2/fll_program/c/program.c b/level_2/fll_program/c/program.c index 8861940..40097f2 100644 --- a/level_2/fll_program/c/program.c +++ b/level_2/fll_program/c/program.c @@ -8,11 +8,11 @@ extern "C" { f_status_t fll_program_print_help_header(const f_file_t output, const f_color_context_t context, const f_string_t name, const f_string_t version) { f_print_terminated(f_string_eol_s, output.stream); - fl_print_string(" %q%s%q%c", output.stream, *context.set.title.before, name, *context.set.title.after, f_string_eol_s[0]); - fl_print_string(" %qVersion %s%q%c", output.stream, *context.set.notable.before, version, *context.set.notable.after, f_string_eol_s[0]); + fl_print_string(" %[%s%]%c", output.stream, context.set.title, name, context.set.title, f_string_eol_s[0]); + fl_print_string(" %[Version %s%]%c", output.stream, context.set.notable, version, context.set.notable, f_string_eol_s[0]); f_print_terminated(f_string_eol_s, output.stream); - fl_print_string(" %qAvailable Options:%q ", output.stream, *context.set.important.before, *context.set.important.after); + fl_print_string(" %[Available Options:%] ", output.stream, context.set.important, context.set.important); return F_none; } @@ -22,8 +22,8 @@ extern "C" { f_status_t fll_program_print_help_option(const f_file_t output, const f_color_context_t context, const f_string_t option_short, const f_string_t option_long, const f_string_t symbol_short, const f_string_t symbol_long, const f_string_t description) { f_print_terminated(f_string_eol_s, output.stream); - fl_print_string(" %s%q%s%q", output.stream, symbol_short, *context.set.standout.before, option_short, *context.set.standout.after); - fl_print_string(", %s%q%s%q", output.stream, symbol_long, *context.set.standout.before, option_long, *context.set.standout.after); + fl_print_string(" %s%[%s%]", output.stream, symbol_short, context.set.standout, option_short, context.set.standout); + fl_print_string(", %s%[%s%]", output.stream, symbol_long, context.set.standout, option_long, context.set.standout); fl_print_string(" %S", output.stream, description); return F_none; @@ -34,7 +34,7 @@ extern "C" { f_status_t fll_program_print_help_option_long(const f_file_t output, const f_color_context_t context, const f_string_t option_long, const f_string_t symbol_long, const f_string_t description) { f_print_terminated(f_string_eol_s, output.stream); - fl_print_string(" %s%q%s%q", output.stream, symbol_long, *context.set.standout.before, option_long, *context.set.standout.after); + fl_print_string(" %s%[%s%]", output.stream, symbol_long, context.set.standout, option_long, context.set.standout); fl_print_string(" %S", output.stream, description); return F_none; @@ -45,7 +45,7 @@ extern "C" { f_status_t fll_program_print_help_option_other(const f_file_t output, const f_color_context_t context, const f_string_t option_other, const f_string_t description) { f_print_terminated(f_string_eol_s, output.stream); - fl_print_string(" %q%s%q", output.stream, *context.set.standout.before, option_other, *context.set.standout.after); + fl_print_string(" %[%s%]", output.stream, context.set.standout, option_other, context.set.standout); fl_print_string(" %S", output.stream, description); return F_none; @@ -57,15 +57,15 @@ extern "C" { f_print_terminated(f_string_eol_s, output.stream); f_print_terminated(f_string_eol_s, output.stream); - fl_print_string(" %qUsage:%q", output.stream, *context.set.important.before, *context.set.important.after); + fl_print_string(" %[Usage:%]", output.stream, context.set.important, context.set.important); f_print_terminated(f_string_eol_s, output.stream); - fl_print_string(" %q%S%q", output.stream, *context.set.standout.before, name, *context.set.standout.after); + fl_print_string(" %[%S%]", output.stream, context.set.standout, name, context.set.standout); - fl_print_string(" %q[%q options %q]%q", output.stream, *context.set.notable.before, *context.set.notable.after, *context.set.notable.before, *context.set.notable.after); + fl_print_string(" %[[%] options %[]%]", output.stream, context.set.notable, context.set.notable, context.set.notable, context.set.notable); if (parameters[0] != '\0') { - fl_print_string(" %q[%q%S%q]%q", output.stream, *context.set.notable.before, *context.set.notable.after, parameters, *context.set.notable.before, *context.set.notable.after); + fl_print_string(" %[[%]%S%[]%]", output.stream, context.set.notable, context.set.notable, parameters, context.set.notable, context.set.notable); } f_print_terminated(f_string_eol_s, output.stream); @@ -104,7 +104,10 @@ extern "C" { if (F_status_is_error(status)) return status; // load colors unless told not to. - if (decision != choices.id[0]) { + if (decision == choices.id[0]) { + context->mode = f_color_mode_no_color; + } + else { f_status_t allocation_status = F_none; macro_f_color_context_t_new(allocation_status, (*context)); @@ -112,9 +115,6 @@ extern "C" { status = f_color_load_context(context, decision == choices.id[1]); } - else { - context->mode = f_color_mode_no_color; - } return status; } diff --git a/level_3/byte_dump/c/byte_dump.c b/level_3/byte_dump/c/byte_dump.c index c997760..45b34a7 100644 --- a/level_3/byte_dump/c/byte_dump.c +++ b/level_3/byte_dump/c/byte_dump.c @@ -9,6 +9,8 @@ extern "C" { #ifndef _di_byte_dump_print_help_ f_status_t byte_dump_print_help(const f_file_t output, const f_color_context_t context) { + flockfile(output.stream); + fll_program_print_help_header(output, context, byte_dump_name_long, byte_dump_version); fll_program_print_help_option(output, context, f_console_standard_short_help_s, f_console_standard_long_help_s, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Print this help message."); @@ -21,7 +23,7 @@ extern "C" { fll_program_print_help_option(output, context, f_console_standard_short_debug_s, f_console_standard_long_debug_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Enable debugging, inceasing verbosity beyond normal output."); fll_program_print_help_option(output, context, f_console_standard_short_version_s, f_console_standard_long_version_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Print only the version number."); - fprintf(output.stream, "%c", f_string_eol_s[0]); + f_print_terminated(f_string_eol_s, output.stream); fll_program_print_help_option(output, context, byte_dump_short_binary, byte_dump_long_binary, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Display binary representation."); fll_program_print_help_option(output, context, byte_dump_short_decimal, byte_dump_long_decimal, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Display decimal representation."); @@ -30,20 +32,21 @@ extern "C" { fll_program_print_help_option(output, context, byte_dump_short_octal, byte_dump_long_octal, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Display octal representation."); fll_program_print_help_option(output, context, byte_dump_short_unicode, byte_dump_long_unicode, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Display using Unicode representation for valid Unicode (like: U+0000)."); - fprintf(output.stream, "%c", f_string_eol_s[0]); + f_print_terminated(f_string_eol_s, output.stream); fll_program_print_help_option(output, context, byte_dump_short_first, byte_dump_long_first, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Start reading at this byte offset."); fll_program_print_help_option(output, context, byte_dump_short_last, byte_dump_long_last, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Stop reading at this (inclusive) byte offset."); fll_program_print_help_option(output, context, byte_dump_short_width, byte_dump_long_width, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Set number of columns of Bytes to display."); - fprintf(output.stream, "%c", f_string_eol_s[0]); + f_print_terminated(f_string_eol_s, output.stream); fll_program_print_help_option(output, context, byte_dump_short_text, byte_dump_long_text, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Include a column of text when displaying the bytes."); fll_program_print_help_option(output, context, byte_dump_short_placeholder, byte_dump_long_placeholder, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, "Use a placeholder character instead of a space for placeholders."); - fprintf(output.stream, "%c%c", f_string_eol_s[0], f_string_eol_s[0]); + f_print_terminated(f_string_eol_s, output.stream); + f_print_terminated(f_string_eol_s, output.stream); - f_color_print(output.stream, context.set.important, " Special Options: "); + fl_print_string(" %[Special Options:%] ", output.stream, context.set.important, context.set.important); fll_program_print_help_option_long(output, context, byte_dump_long_normal, f_console_symbol_long_enable_s, " Display UTF-8 symbols for ASCII control codes."); fll_program_print_help_option_long(output, context, byte_dump_long_simple, f_console_symbol_long_enable_s, " Display spaces for ASCII control codes."); @@ -51,33 +54,40 @@ extern "C" { fll_program_print_help_usage(output, context, byte_dump_name, "filename(s)"); - fprintf(output.stream, " When using the "); - f_color_print(output.stream, context.set.notable, "%s%s", f_console_symbol_long_enable_s, byte_dump_long_text); - fprintf(output.stream, " option, some UTF-8 characters may be replaced by your instance and cause display alignment issues."); + f_print_terminated(" When using the ", output.stream); + fl_print_string("%[%S%S%]", output.stream, context.set.notable, f_console_symbol_long_enable_s, byte_dump_long_text, context.set.notable); + f_print_terminated(" option, some UTF-8 characters may be replaced by your instance and cause display alignment issues.", output.stream); + + f_print_terminated(f_string_eol_s, output.stream); + f_print_terminated(f_string_eol_s, output.stream); - fprintf(output.stream, "%c%c", f_string_eol_s[0], f_string_eol_s[0]); + f_print_terminated(" Special UTF-8 characters and non-spacing UTF-8 characters may be replaced with a space (or a placeholder when the ", output.stream); + fl_print_string("%[%S%S%]", output.stream, context.set.notable, f_console_symbol_long_enable_s, byte_dump_long_placeholder, context.set.notable); + f_print_terminated(" option is used).", output.stream); - fprintf(output.stream, " Special UTF-8 characters and non-spacing UTF-8 characters may be replaced with a space (or a placeholder when the "); - f_color_print(output.stream, context.set.notable, "%s%s", f_console_symbol_long_enable_s, byte_dump_long_placeholder); - fprintf(output.stream, " option is used)."); + f_print_terminated(f_string_eol_s, output.stream); + f_print_terminated(f_string_eol_s, output.stream); - fprintf(output.stream, "%c%c", f_string_eol_s[0], f_string_eol_s[0]); + f_print_terminated(" UTF-8 \"Combining\" characters might have a space appended to allow a proper display but this may cause copy and paste issues.", output.stream); - fprintf(output.stream, " UTF-8 \"Combining\" characters might have a space appended to allow a proper display but this may cause copy and paste issues."); + f_print_terminated(f_string_eol_s, output.stream); + f_print_terminated(f_string_eol_s, output.stream); - fprintf(output.stream, "%c%c", f_string_eol_s[0], f_string_eol_s[0]); + f_print_terminated(" When ", output.stream); + fl_print_string("%[%S%S%]", output.stream, context.set.notable, f_console_symbol_long_enable_s, byte_dump_long_last, context.set.notable); + f_print_terminated(" is used, any UTF-8 sequences will still be printed in full should any part is found within the requested range.", output.stream); - fprintf(output.stream, " When "); - f_color_print(output.stream, context.set.notable, "%s%s", f_console_symbol_long_enable_s, byte_dump_long_last); - fprintf(output.stream, " is used, any UTF-8 sequences will still be printed in full should any part is found within the requested range."); + f_print_terminated(f_string_eol_s, output.stream); + f_print_terminated(f_string_eol_s, output.stream); - fprintf(output.stream, "%c%c", f_string_eol_s[0], f_string_eol_s[0]); + f_print_terminated(" When using the ", output.stream); + fl_print_string("%[%S%S%]", output.stream, context.set.notable, f_console_symbol_long_enable_s, byte_dump_long_unicode, context.set.notable); + f_print_terminated(" option, invalid Unicode will fallback to being displayed using one of the other modes.", output.stream); - fprintf(output.stream, " When using the "); - f_color_print(output.stream, context.set.notable, "%s%s", f_console_symbol_long_enable_s, byte_dump_long_unicode); - fprintf(output.stream, " option, invalid Unicode will fallback to being displayed using one of the other modes."); + f_print_terminated(f_string_eol_s, output.stream); + f_print_terminated(f_string_eol_s, output.stream); - fprintf(output.stream, "%c%c", f_string_eol_s[0], f_string_eol_s[0]); + funlockfile(output.stream); return F_none; } @@ -221,9 +231,13 @@ extern "C" { if (main->remaining.used > 0 || main->process_pipe) { if (main->parameters[byte_dump_parameter_width].result == f_console_result_found) { - f_color_print(main->error.to.stream, main->context.set.error, "%sThe parameter '", fll_error_print_error); - f_color_print(main->error.to.stream, main->context.set.notable, "%s%s", f_console_symbol_long_enable_s, byte_dump_long_width); - f_color_print(main->error.to.stream, main->context.set.error, "' was specified, but no value was given.%c", f_string_eol_s[0]); + flockfile(main->error.to.stream); + + fl_print_string("%[%sThe parameter '%]", main->error.to.stream, fll_error_print_error, main->context.set.error, main->context.set.error); + fl_print_string("%[%S%S%]", main->error.to.stream, main->context.set.notable, f_console_symbol_long_enable_s, byte_dump_long_width, main->context.set.notable); + fl_print_string("%[' was specified, but no value was given.%]%c", main->error.to.stream, main->context.set.error, main->context.set.error, f_string_eol_s[0]); + + funlockfile(main->error.to.stream); byte_dump_main_delete(main); return F_status_set_error(status); @@ -237,13 +251,17 @@ extern "C" { status = fl_conversion_string_to_number_unsigned(arguments.argv[index], range, &number); if (F_status_is_error(status) || number < 1 || number >= 0xfb) { - f_color_print(main->error.to.stream, main->context.set.error, "%sThe parameter '", fll_error_print_error); - f_color_print(main->error.to.stream, main->context.set.notable, "%s%s", f_console_symbol_long_enable_s, byte_dump_long_width); - f_color_print(main->error.to.stream, main->context.set.error, "' value can only be a number between "); - f_color_print(main->error.to.stream, main->context.set.notable, "0"); - f_color_print(main->error.to.stream, main->context.set.error, " and "); - f_color_print(main->error.to.stream, main->context.set.notable, "251"); - f_color_print(main->error.to.stream, main->context.set.error, ".%c", f_string_eol_s[0]); + flockfile(main->error.to.stream); + + fl_print_string("%[%sThe parameter '%]", main->error.to.stream, fll_error_print_error, main->context.set.error, main->context.set.error); + fl_print_string("%[%S%S%]", main->error.to.stream, main->context.set.notable, f_console_symbol_long_enable_s, byte_dump_long_width, main->context.set.notable); + fl_print_string("%[' value can only be a number (inclusively) between %]", main->error.to.stream, main->context.set.error, main->context.set.error); + fl_print_string("%[1%]", main->error.to.stream, main->context.set.notable, main->context.set.notable); + fl_print_string("%[ and %]", main->error.to.stream, main->context.set.error, main->context.set.error); + fl_print_string("%[250%]", main->error.to.stream, main->context.set.notable, main->context.set.notable); + fl_print_string("%[.%]%c", main->error.to.stream, main->context.set.error, main->context.set.error, f_string_eol_s[0]); + + funlockfile(main->error.to.stream); byte_dump_main_delete(main); return F_status_set_error(status); @@ -253,9 +271,13 @@ extern "C" { } if (main->parameters[byte_dump_parameter_first].result == f_console_result_found) { - f_color_print(main->error.to.stream, main->context.set.error, "%sThe parameter '", fll_error_print_error); - f_color_print(main->error.to.stream, main->context.set.notable, "%s%s", f_console_symbol_long_enable_s, byte_dump_long_first); - f_color_print(main->error.to.stream, main->context.set.error, "' was specified, but no value was given.%c", f_string_eol_s[0]); + flockfile(main->error.to.stream); + + fl_print_string("%[%sThe parameter '%]", main->error.to.stream, fll_error_print_error, main->context.set.error, main->context.set.error); + fl_print_string("%[%S%S%]", main->error.to.stream, main->context.set.notable, f_console_symbol_long_enable_s, byte_dump_long_first, main->context.set.notable); + fl_print_string("%[' was specified, but no value was given.%]%c", main->error.to.stream, main->context.set.error, main->context.set.error, f_string_eol_s[0]); + + funlockfile(main->error.to.stream); byte_dump_main_delete(main); return F_status_set_error(status); @@ -269,13 +291,17 @@ extern "C" { status = fl_conversion_string_to_number_unsigned(arguments.argv[index], range, &number); if (F_status_is_error(status) || number > f_number_t_size_unsigned) { - f_color_print(main->error.to.stream, main->context.set.error, "%sThe parameter '", fll_error_print_error); - f_color_print(main->error.to.stream, main->context.set.notable, "%s%s", f_console_symbol_long_enable_s, byte_dump_long_first); - f_color_print(main->error.to.stream, main->context.set.error, "' value can only be a number (inclusively) between "); - f_color_print(main->error.to.stream, main->context.set.notable, "0"); - f_color_print(main->error.to.stream, main->context.set.error, " and "); - f_color_print(main->error.to.stream, main->context.set.notable, "%llu", f_number_t_size_unsigned); - f_color_print(main->error.to.stream, main->context.set.error, ".%c", f_string_eol_s[0]); + flockfile(main->error.to.stream); + + fl_print_string("%[%sThe parameter '%]", main->error.to.stream, fll_error_print_error, main->context.set.error, main->context.set.error); + fl_print_string("%[%S%S%]", main->error.to.stream, main->context.set.notable, f_console_symbol_long_enable_s, byte_dump_long_first, main->context.set.notable); + fl_print_string("%[' value can only be a number (inclusively) between %]", main->error.to.stream, main->context.set.error, main->context.set.error); + fl_print_string("%[0%]", main->error.to.stream, main->context.set.notable, main->context.set.notable); + fl_print_string("%[ and %]", main->error.to.stream, main->context.set.error, main->context.set.error); + fl_print_string("%[%llu%]", main->error.to.stream, main->context.set.notable, f_number_t_size_unsigned, main->context.set.notable); + fl_print_string("%[.%]%c", main->error.to.stream, main->context.set.error, main->context.set.error, f_string_eol_s[0]); + + funlockfile(main->error.to.stream); byte_dump_main_delete(main); return F_status_set_error(status); @@ -285,9 +311,13 @@ extern "C" { } if (main->parameters[byte_dump_parameter_last].result == f_console_result_found) { - f_color_print(main->error.to.stream, main->context.set.error, "%sThe parameter '", fll_error_print_error); - f_color_print(main->error.to.stream, main->context.set.notable, "%s%s", f_console_symbol_long_enable_s, byte_dump_long_last); - f_color_print(main->error.to.stream, main->context.set.error, "' was specified, but no value was given.%c", f_string_eol_s[0]); + flockfile(main->error.to.stream); + + fl_print_string("%[%sThe parameter '%]", main->error.to.stream, fll_error_print_error, main->context.set.error, main->context.set.error); + fl_print_string("%[%S%S%]", main->error.to.stream, main->context.set.notable, f_console_symbol_long_enable_s, byte_dump_long_last, main->context.set.notable); + fl_print_string("%[' was specified, but no value was given.%]%c", main->error.to.stream, main->context.set.error, main->context.set.error, f_string_eol_s[0]); + + funlockfile(main->error.to.stream); byte_dump_main_delete(main); return F_status_set_error(status); @@ -301,13 +331,17 @@ extern "C" { status = fl_conversion_string_to_number_unsigned(arguments.argv[index], range, &number); if (F_status_is_error(status) || number < 0 || number > f_number_t_size_unsigned) { - f_color_print(main->error.to.stream, main->context.set.error, "%sThe parameter '", fll_error_print_error); - f_color_print(main->error.to.stream, main->context.set.notable, "%s%s", f_console_symbol_long_enable_s, byte_dump_long_last); - f_color_print(main->error.to.stream, main->context.set.error, "' value can only be a number (inclusively) between "); - f_color_print(main->error.to.stream, main->context.set.notable, "0"); - f_color_print(main->error.to.stream, main->context.set.error, " and "); - f_color_print(main->error.to.stream, main->context.set.notable, "%llu", f_number_t_size_unsigned); - f_color_print(main->error.to.stream, main->context.set.error, ".%c", f_string_eol_s[0]); + flockfile(main->error.to.stream); + + fl_print_string("%[%sThe parameter '%]", main->error.to.stream, fll_error_print_error, main->context.set.error, main->context.set.error); + fl_print_string("%[%S%S%]", main->error.to.stream, main->context.set.notable, f_console_symbol_long_enable_s, byte_dump_long_last, main->context.set.notable); + fl_print_string("%[' value can only be a number (inclusively) between %]", main->error.to.stream, main->context.set.error, main->context.set.error); + fl_print_string("%[0%]", main->error.to.stream, main->context.set.notable, main->context.set.notable); + fl_print_string("%[ and %]", main->error.to.stream, main->context.set.error, main->context.set.error); + fl_print_string("%[%llu%]", main->error.to.stream, main->context.set.notable, f_number_t_size_unsigned, main->context.set.notable); + fl_print_string("%[.%]%c", main->error.to.stream, main->context.set.error, main->context.set.error, f_string_eol_s[0]); + + funlockfile(main->error.to.stream); byte_dump_main_delete(main); return F_status_set_error(status); @@ -318,11 +352,15 @@ extern "C" { if (main->parameters[byte_dump_parameter_first].result == f_console_result_additional && main->parameters[byte_dump_parameter_last].result == f_console_result_additional) { if (main->first > main->last) { - f_color_print(main->error.to.stream, main->context.set.error, "%sThe parameter '", fll_error_print_error); - f_color_print(main->error.to.stream, main->context.set.notable, "%s%s", f_console_symbol_long_enable_s, byte_dump_long_first); - f_color_print(main->error.to.stream, main->context.set.error, "' value cannot be greater than the parameter '"); - f_color_print(main->error.to.stream, main->context.set.notable, "%s%s", f_console_symbol_long_enable_s, byte_dump_long_last); - f_color_print(main->error.to.stream, main->context.set.error, "' value.%c", f_string_eol_s[0]); + flockfile(main->error.to.stream); + + fl_print_string("%[%sThe parameter '%]", main->error.to.stream, fll_error_print_error, main->context.set.error, main->context.set.error); + fl_print_string("%[%S%S%]", main->error.to.stream, main->context.set.notable, f_console_symbol_long_enable_s, byte_dump_long_first, main->context.set.notable); + fl_print_string("%[' value cannot be greater than the parameter '%]", main->error.to.stream, main->context.set.error, main->context.set.error); + fl_print_string("%[%S%S%]", main->error.to.stream, main->context.set.notable, f_console_symbol_long_enable_s, byte_dump_long_last, main->context.set.notable); + fl_print_string("%[' value.%]%c", main->error.to.stream, main->context.set.error, main->context.set.error, f_string_eol_s[0]); + + funlockfile(main->error.to.stream); byte_dump_main_delete(main); return F_status_set_error(status); @@ -338,26 +376,30 @@ extern "C" { file.id = f_type_descriptor_input; file.stream = f_type_input; - printf("%c", f_string_eol_s[0]); - f_color_print(main->output.stream, main->context.set.title, "Piped Byte Dump: (in "); + flockfile(main->output.stream); + + f_print_terminated(f_string_eol_s, main->output.stream); + fl_print_string("%[Piped Byte Dump: (in ", main->output.stream, main->context.set.title); if (main->mode == byte_dump_mode_hexidecimal) { - f_color_print(main->output.stream, main->context.set.title, "Hexidecimal"); + f_print_terminated("Hexidecimal", main->output.stream); } else if (main->mode == byte_dump_mode_duodecimal) { - f_color_print(main->output.stream, main->context.set.title, "Duodecimal"); + f_print_terminated("Duodecimal", main->output.stream); } else if (main->mode == byte_dump_mode_octal) { - f_color_print(main->output.stream, main->context.set.title, "Octal"); + f_print_terminated("Octal", main->output.stream); } else if (main->mode == byte_dump_mode_binary) { - f_color_print(main->output.stream, main->context.set.title, "Binary"); + f_print_terminated("Binary", main->output.stream); } else if (main->mode == byte_dump_mode_decimal) { - f_color_print(main->output.stream, main->context.set.title, "Decimal"); + f_print_terminated("Decimal", main->output.stream); } - f_color_print(main->output.stream, main->context.set.title, ")%c", f_string_eol_s[0]); + fl_print_string(")%]%c", main->output.stream, main->context.set.title, f_string_eol_s[0]); + + funlockfile(main->output.stream); status = byte_dump_file(*main, 0, file); @@ -408,28 +450,30 @@ extern "C" { return status; } - printf("%c", f_string_eol_s[0]); - f_color_print(main->output.stream, main->context.set.title, "Byte Dump of: "); - f_color_print(main->output.stream, main->context.set.notable, "%s", arguments.argv[main->remaining.array[counter]]); - f_color_print(main->output.stream, main->context.set.title, " (in "); + flockfile(main->output.stream); + + f_print_terminated(f_string_eol_s, main->output.stream); + fl_print_string("%[Byte Dump of: %]%[", main->output.stream, main->context.set.title, main->context.set.title, main->context.set.notable); + fll_print_safely_terminated(arguments.argv[main->remaining.array[counter]], main->output.stream); + fl_print_string("%]%[ (in ", main->output.stream, main->context.set.notable, main->context.set.title); if (main->mode == byte_dump_mode_hexidecimal) { - f_color_print(main->output.stream, main->context.set.title, "Hexidecimal"); + f_print_terminated("Hexidecimal", main->output.stream); } else if (main->mode == byte_dump_mode_duodecimal) { - f_color_print(main->output.stream, main->context.set.title, "Duodecimal"); + f_print_terminated("Duodecimal", main->output.stream); } else if (main->mode == byte_dump_mode_octal) { - f_color_print(main->output.stream, main->context.set.title, "Octal"); + f_print_terminated("Octal", main->output.stream); } else if (main->mode == byte_dump_mode_binary) { - f_color_print(main->output.stream, main->context.set.title, "Binary"); + f_print_terminated("Binary", main->output.stream); } else if (main->mode == byte_dump_mode_decimal) { - f_color_print(main->output.stream, main->context.set.title, "Decimal"); + f_print_terminated("Decimal", main->output.stream); } - f_color_print(main->output.stream, main->context.set.title, ")%c", f_string_eol_s[0]); + fl_print_string(")%]%c", main->output.stream, main->context.set.title, f_string_eol_s[0]); status = byte_dump_file(*main, arguments.argv[main->remaining.array[counter]], file); @@ -448,7 +492,7 @@ extern "C" { } } else { - f_color_print(main->error.to.stream, main->context.set.error, "%sYou failed to specify one or more filenames.%c", fll_error_print_error, f_string_eol_s[0]); + fll_print_string("%[%sYou failed to specify one or more filenames.%]%c", main->error.to.stream, main->context.set.error, fll_error_print_error, main->context.set.error, f_string_eol_s[0]); status = F_status_set_error(F_parameter); } diff --git a/level_3/byte_dump/c/byte_dump.h b/level_3/byte_dump/c/byte_dump.h index 5f5877d..5c36737 100644 --- a/level_3/byte_dump/c/byte_dump.h +++ b/level_3/byte_dump/c/byte_dump.h @@ -36,11 +36,13 @@ // fll-1 includes #include #include +#include #include #include // fll-2 includes #include +#include #include #ifdef __cplusplus diff --git a/level_3/byte_dump/c/private-byte_dump.c b/level_3/byte_dump/c/private-byte_dump.c index cd8c0fe..63e81f8 100644 --- a/level_3/byte_dump/c/private-byte_dump.c +++ b/level_3/byte_dump/c/private-byte_dump.c @@ -134,6 +134,8 @@ extern "C" { } } + flockfile(main.output.stream); + if (byte_dump_print_character_fragment(main, characters, invalid, width_utf, 1, &previous, &cell, &offset) == F_true) { character_reset = F_true; } @@ -158,19 +160,31 @@ extern "C" { if (main.last) { position += width_utf; - if (position >= main.last) break; + if (position >= main.last) { + funlockfile(main.output.stream); + + break; + } } } else if (main.last) { ++position; - if (position >= main.last) break; + if (position >= main.last) { + funlockfile(main.output.stream); + + break; + } } + funlockfile(main.output.stream); + width_utf = -1; } // for } + flockfile(main.output.stream); + // Print placeholders to fill out the remaining line and then optionally print the text block. if (cell.column > 0 && cell.column < main.width) { previous.bytes = 0; @@ -179,22 +193,22 @@ extern "C" { while (cell.column < main.width) { if (main.parameters[byte_dump_parameter_unicode].result == f_console_result_found) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } else if (main.mode == byte_dump_mode_hexidecimal) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } else if (main.mode == byte_dump_mode_duodecimal) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } else if (main.mode == byte_dump_mode_octal) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } else if (main.mode == byte_dump_mode_binary) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } else if (main.mode == byte_dump_mode_decimal) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } ++cell.column; @@ -202,32 +216,32 @@ extern "C" { if (cell.column < main.width) { if (main.parameters[byte_dump_parameter_unicode].result == f_console_result_found) { if (!(cell.column % 4)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_hexidecimal) { if (!(cell.column % 8)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_duodecimal) { if (!(cell.column % 6)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_octal) { if (!(cell.column % 6)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_binary) { if (!(cell.column % 6)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_decimal) { if (!(cell.column % 6)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } } @@ -237,28 +251,36 @@ extern "C" { byte_dump_print_text(main, characters, invalid, &previous, &offset); } else { - fprintf(main.output.stream, "%c", f_string_eol_s[0]); + f_print_terminated(f_string_eol_s, main.output.stream); } } - fprintf(main.output.stream, "%c", f_string_eol_s[0]); + f_print_terminated(f_string_eol_s, main.output.stream); + + funlockfile(main.output.stream); // make sure to flush standard out to help prevent standard error from causing poblems. fflush(main.output.stream); if (found_invalid_utf) { - f_color_print(main.error.to.stream, main.context.set.error, "Invalid UTF-8 codes were detected for file '"); - f_color_print(main.error.to.stream, main.context.set.notable, "%s", file_name ? file_name : "-"); - f_color_print(main.error.to.stream, main.context.set.error, "'."); - fprintf(main.error.to.stream, "%c%c", f_string_eol_s[0], f_string_eol_s[0]); + flockfile(main.error.to.stream); + + fl_print_string("%[Invalid UTF-8 codes were detected for file '%]", main.error.to.stream, main.context.set.error, main.context.set.error); + fl_print_string("%[%S%]", main.error.to.stream, main.context.set.notable, file_name ? file_name : "-", main.context.set.notable); + fl_print_string("%['.%]%c%c", main.error.to.stream, main.context.set.error, main.context.set.error, f_string_eol_s[0], f_string_eol_s[0]); + + funlockfile(main.error.to.stream); } if (ferror(file.stream)) { // @todo: determine what the error is and display it. - f_color_print(main.error.to.stream, main.context.set.error, "%sread() failed for '", fll_error_print_error); - f_color_print(main.error.to.stream, main.context.set.notable, "%s", file_name ? file_name : "-"); - f_color_print(main.error.to.stream, main.context.set.error, "'."); - fprintf(main.error.to.stream, "%c%c", f_string_eol_s[0], f_string_eol_s[0]); + flockfile(main.error.to.stream); + + fl_print_string("%[%Sread() failed for '%]", main.error.to.stream, fll_error_print_error, main.context.set.error, main.context.set.error); + fl_print_string("%[%S%]", main.error.to.stream, main.context.set.notable, file_name ? file_name : "-", main.context.set.notable); + fl_print_string("%['.%]%c%c", main.error.to.stream, main.context.set.error, main.context.set.error, f_string_eol_s[0], f_string_eol_s[0]); + + funlockfile(main.error.to.stream); status = F_status_set_error(F_failure); } @@ -271,6 +293,7 @@ extern "C" { #ifndef _di_byte_dump_print_character_fragment_ bool byte_dump_print_character_fragment(const byte_dump_main_t main, const f_utf_string_static_t characters, const char invalid[], const uint8_t width_utf, const char byte_current, byte_dump_previous_t *previous, byte_dump_cell_t *cell, uint8_t *offset) { + char byte = 0; bool reset = F_false; @@ -291,7 +314,7 @@ extern "C" { } if (!cell->column) { - f_color_print(main.output.stream, main.context.set.notable, "%016X ", (uint64_t) cell->row); + fl_print_string("%[%016ULL%] ", main.output.stream, main.context.set.notable, (uint64_t) cell->row, main.context.set.notable); if (*offset) { uint8_t offset_to_print = *offset; @@ -300,22 +323,22 @@ extern "C" { while (offset_to_print && cell->column < main.width) { if (main.parameters[byte_dump_parameter_unicode].result == f_console_result_found) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } else if (main.mode == byte_dump_mode_hexidecimal) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } else if (main.mode == byte_dump_mode_duodecimal) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } else if (main.mode == byte_dump_mode_octal) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } else if (main.mode == byte_dump_mode_binary) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } else if (main.mode == byte_dump_mode_decimal) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } --offset_to_print; @@ -324,32 +347,32 @@ extern "C" { if (cell->column < main.width) { if (main.parameters[byte_dump_parameter_unicode].result == f_console_result_found) { if (!(cell->column % 4)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_hexidecimal) { if (!(cell->column % 8)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_duodecimal) { if (!(cell->column % 6)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_octal) { if (!(cell->column % 6)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_binary) { if (!(cell->column % 4)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_decimal) { if (!(cell->column % 6)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } } @@ -390,111 +413,73 @@ extern "C" { } if (width_utf < 4) { - fprintf(main.output.stream, " U+%04X ", (uint32_t) unicode); + fl_print_string(" U+%04_U ", main.output.stream, unicode); } else { - fprintf(main.output.stream, " U+%06X", (uint32_t) unicode); + fl_print_string(" U+%06_U ", main.output.stream, unicode); } } else { // Pad the characters that are incomplete fragments of an already printed valid Unicode. - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } } else { if (main.mode == byte_dump_mode_hexidecimal) { if (main.parameters[byte_dump_parameter_unicode].result == f_console_result_found) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } if (invalid[character_current]) { - f_color_print(main.output.stream, main.context.set.error, " %02x", (uint8_t) byte); + fl_print_string(" %[%02_uii%]", main.output.stream, main.context.set.error, (uint8_t) byte, main.context.set.error); } else { - fprintf(main.output.stream, " %02x", (uint8_t) byte); + fl_print_string(" %02_uii", main.output.stream, (uint8_t) byte); } } else if (main.mode == byte_dump_mode_duodecimal) { if (main.parameters[byte_dump_parameter_unicode].result == f_console_result_found) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } if (invalid[character_current]) { - fprintf(main.output.stream, "%s", main.context.error.string); - } - - fprintf(main.output.stream, " %01d", byte / 144); - - uint8_t current = (byte % 144) / 12; - - if (current == 11) { - fprintf(main.output.stream, "%s", f_string_ascii_b_s); - } - else if (current == 10) { - fprintf(main.output.stream, "%s", f_string_ascii_a_s); - } - else { - fprintf(main.output.stream, "%01d", current); - } - - current = (byte % 144) % 12; - - if (current == 11) { - fprintf(main.output.stream, "%s", f_string_ascii_b_s); - } - else if (current == 10) { - fprintf(main.output.stream, "%s", f_string_ascii_a_s); + fl_print_string(" %[%02&uii%]", main.output.stream, main.context.set.error, (uint8_t) byte, main.context.set.error); } else { - fprintf(main.output.stream, "%01d", current); - } - - if (invalid[character_current]) { - fprintf(main.output.stream, "%s", main.context.reset.string); + fl_print_string(" %02&uii", main.output.stream, (uint8_t) byte); } } else if (main.mode == byte_dump_mode_octal) { if (main.parameters[byte_dump_parameter_unicode].result == f_console_result_found) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } if (invalid[character_current]) { - f_color_print(main.output.stream, main.context.set.error, " %03o", (uint8_t) byte); + fl_print_string(" %[%03@uii%]", main.output.stream, main.context.set.error, (uint8_t) byte, main.context.set.error); } else { - fprintf(main.output.stream, " %03o", (uint8_t) byte); + fl_print_string(" %03@uii", main.output.stream, (uint8_t) byte); } } else if (main.mode == byte_dump_mode_binary) { - char binary_string[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - - binary_string[0] = ((byte >> 7) & 0x01) ? f_string_ascii_1_s[0] : f_string_ascii_0_s[0]; - binary_string[1] = ((byte >> 6) & 0x01) ? f_string_ascii_1_s[0] : f_string_ascii_0_s[0]; - binary_string[2] = ((byte >> 5) & 0x01) ? f_string_ascii_1_s[0] : f_string_ascii_0_s[0]; - binary_string[3] = ((byte >> 4) & 0x01) ? f_string_ascii_1_s[0] : f_string_ascii_0_s[0]; - binary_string[4] = ((byte >> 3) & 0x01) ? f_string_ascii_1_s[0] : f_string_ascii_0_s[0]; - binary_string[5] = ((byte >> 2) & 0x01) ? f_string_ascii_1_s[0] : f_string_ascii_0_s[0]; - binary_string[6] = ((byte >> 1) & 0x01) ? f_string_ascii_1_s[0] : f_string_ascii_0_s[0]; - binary_string[7] = (byte & 0x01) ? f_string_ascii_1_s[0] : f_string_ascii_0_s[0]; - if (invalid[character_current]) { - f_color_print(main.output.stream, main.context.set.error, " %s", binary_string); + fl_print_string(" %[%09!uii%]", main.output.stream, main.context.set.error, (uint8_t) byte, main.context.set.error); } else { - fprintf(main.output.stream, " %s", binary_string); + fl_print_string(" %09!uii", main.output.stream, (uint8_t) byte); } } else if (main.mode == byte_dump_mode_decimal) { if (main.parameters[byte_dump_parameter_unicode].result == f_console_result_found) { - fprintf(main.output.stream, " "); + f_print_terminated(" ", main.output.stream); } if (invalid[character_current]) { - f_color_print(main.output.stream, main.context.set.error, " %3d", (uint8_t) byte); + fl_print_string(" %[%03uii%]", main.output.stream, main.context.set.error, (uint8_t) byte, main.context.set.error); } else { - fprintf(main.output.stream, " %3d", (uint8_t) byte); + fl_print_string(" %03uii", main.output.stream, (uint8_t) byte); } } } @@ -515,7 +500,7 @@ extern "C" { byte_dump_print_text(main, characters, invalid, previous, offset); } else { - fprintf(main.output.stream, "%c", f_string_eol_s[0]); + f_print_terminated(f_string_eol_s, main.output.stream); } cell->column = 0; @@ -533,32 +518,32 @@ extern "C" { else { if (main.parameters[byte_dump_parameter_unicode].result == f_console_result_found) { if (!(cell->column % 4)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_hexidecimal) { if (!(cell->column % 8)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_duodecimal) { if (!(cell->column % 6)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_octal) { if (!(cell->column % 6)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_binary) { if (!(cell->column % 4)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (main.mode == byte_dump_mode_decimal) { if (!(cell->column % 6)) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } } @@ -569,17 +554,19 @@ extern "C" { #ifndef _di_byte_dump_print_text_ void byte_dump_print_text(const byte_dump_main_t main, const f_utf_string_static_t characters, const char invalid[], byte_dump_previous_t *previous, uint8_t *offset) { + uint8_t j = 0; uint8_t output = 0; uint8_t width_utf = 0; bool printed = F_false; - f_color_print(main.output.stream, main.context.set.notable, " %s ", byte_dump_character_wall); + fl_print_string(" %[%s%] ", main.output.stream, main.context.set.notable, byte_dump_character_wall, main.context.set.notable); if (*offset > 0) { if (main.parameters[byte_dump_parameter_classic].result == f_console_result_found) { while (*offset > 0 && j < main.width) { - fprintf(main.output.stream, "%s", f_string_ascii_period_s); + + f_print_terminated(f_string_ascii_period_s, main.output.stream); --(*offset); ++j; } // while @@ -592,7 +579,8 @@ extern "C" { } while (*offset > 0 && j < main.width) { - f_color_print(main.output.stream, main.context.set.warning, "%s", placeholder); + + fl_print_string(" %[%s%] ", main.output.stream, main.context.set.warning, placeholder, main.context.set.warning); --(*offset); ++j; } // while @@ -612,19 +600,19 @@ extern "C" { for (; j < previous->bytes && j < main.width; ++j) { if (previous->invalid) { - f_color_print(main.output.stream, main.context.set.error, "%s", byte_dump_character_placeholder); + fl_print_string("%[%s%]", main.output.stream, main.context.set.error, byte_dump_character_placeholder, main.context.set.error); } else if (main.parameters[byte_dump_parameter_classic].result == f_console_result_found) { - fprintf(main.output.stream, "%s", f_string_ascii_period_s); + f_print_terminated(f_string_ascii_period_s, main.output.stream); } else { - f_color_print(main.output.stream, main.context.set.warning, "%s", byte_dump_character_placeholder); + fl_print_string("%[%s%]", main.output.stream, main.context.set.warning, byte_dump_character_placeholder, main.context.set.warning); } } // for } else { for (; j < previous->bytes && j < main.width; ++j) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } // for } } @@ -644,200 +632,213 @@ extern "C" { width_utf = macro_f_utf_byte_width_is(output); if (invalid[i]) { - f_color_print(main.output.stream, main.context.set.error, "%s", byte_dump_character_incomplete); + fl_print_string("%[%s%]", main.output.stream, main.context.set.error, byte_dump_character_incomplete, main.context.set.error); } else if (output >= 0 && output <= 32 || output == 127) { if (main.presentation == byte_dump_presentation_normal) { + fl_print_string("%[%[", main.output.stream, main.context.set.notable, main.context.set.warning); + if (!output) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_null); + f_print_terminated(byte_dump_sequence_null, main.output.stream); } else if (output == 1) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_start_of_header); + f_print_terminated(byte_dump_sequence_start_of_header, main.output.stream); } else if (output == 2) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_start_of_text); + f_print_terminated(byte_dump_sequence_start_of_text, main.output.stream); } else if (output == 3) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_end_of_text); + f_print_terminated(byte_dump_sequence_end_of_text, main.output.stream); } else if (output == 4) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_end_of_transmission); + f_print_terminated(byte_dump_sequence_end_of_transmission, main.output.stream); } else if (output == 5) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_end_of_enquiry); + f_print_terminated(byte_dump_sequence_end_of_enquiry, main.output.stream); } else if (output == 6) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_acknowledge); + f_print_terminated(byte_dump_sequence_acknowledge, main.output.stream); } else if (output == 7) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_bell); + f_print_terminated(byte_dump_sequence_bell, main.output.stream); } else if (output == 8) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_backspace); + f_print_terminated(byte_dump_sequence_backspace, main.output.stream); } else if (output == 9) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_tab); + f_print_terminated(byte_dump_sequence_tab, main.output.stream); } else if (output == 10) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_new_line); + f_print_terminated(byte_dump_sequence_new_line, main.output.stream); } else if (output == 11) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_tab_vertical); + f_print_terminated(byte_dump_sequence_tab_vertical, main.output.stream); } else if (output == 12) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_form_feed); + f_print_terminated(byte_dump_sequence_form_feed, main.output.stream); } else if (output == 13) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_carriage_return); + f_print_terminated(byte_dump_sequence_carriage_return, main.output.stream); } else if (output == 14) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_shift_out); + f_print_terminated(byte_dump_sequence_shift_out, main.output.stream); } else if (output == 15) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_shift_in); + f_print_terminated(byte_dump_sequence_shift_in, main.output.stream); } else if (output == 16) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_data_link_escape); + f_print_terminated(byte_dump_sequence_data_link_escape, main.output.stream); } else if (output == 17) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_device_control_1); + f_print_terminated(byte_dump_sequence_device_control_1, main.output.stream); } else if (output == 18) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_device_control_2); + f_print_terminated(byte_dump_sequence_device_control_2, main.output.stream); } else if (output == 19) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_device_control_3); + f_print_terminated(byte_dump_sequence_device_control_3, main.output.stream); } else if (output == 20) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_device_control_4); + f_print_terminated(byte_dump_sequence_device_control_4, main.output.stream); } else if (output == 21) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_negative_acknowledge); + f_print_terminated(byte_dump_sequence_negative_acknowledge, main.output.stream); } else if (output == 22) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_synchronous_idle); + f_print_terminated(byte_dump_sequence_synchronous_idle, main.output.stream); } else if (output == 23) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_end_of_transmission_block); + f_print_terminated(byte_dump_sequence_end_of_transmission_block, main.output.stream); } else if (output == 24) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_cancel); + f_print_terminated(byte_dump_sequence_cancel, main.output.stream); } else if (output == 25) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_end_of_medium); + f_print_terminated(byte_dump_sequence_end_of_medium, main.output.stream); } else if (output == 26) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_substitute); + f_print_terminated(byte_dump_sequence_substitute, main.output.stream); } else if (output == 27) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_escape); + f_print_terminated(byte_dump_sequence_escape, main.output.stream); } else if (output == 28) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_file_separator); + f_print_terminated(byte_dump_sequence_file_separator, main.output.stream); } else if (output == 29) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_group_separator); + f_print_terminated(byte_dump_sequence_group_separator, main.output.stream); } else if (output == 30) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_record_separator); + f_print_terminated(byte_dump_sequence_record_separator, main.output.stream); } else if (output == 31) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_unit_separator); + f_print_terminated(byte_dump_sequence_unit_separator, main.output.stream); } else if (output == 32) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_space); + f_print_terminated(byte_dump_sequence_space, main.output.stream); } else if (output == 127) { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_delete); + f_print_terminated(byte_dump_sequence_delete, main.output.stream); } + + fl_print_string("%]%]", main.output.stream, main.context.set.warning, main.context.set.notable); } else if (main.presentation == byte_dump_presentation_simple) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (main.presentation == byte_dump_presentation_classic) { - fprintf(main.output.stream, "%s", f_string_ascii_period_s); + f_print_terminated(f_string_ascii_period_s, main.output.stream); } } else if (f_utf_character_is_whitespace(characters.string[i]) == F_true) { if (main.parameters[byte_dump_parameter_classic].result == f_console_result_found) { - fprintf(main.output.stream, "%s", f_string_ascii_period_s); + f_print_terminated(f_string_ascii_period_s, main.output.stream); } else { - f_color_print2(main.output.stream, main.context.set.notable, main.context.set.warning, "%s", byte_dump_sequence_space); + fl_print_string("%[%[%s%]%]", main.output.stream, main.context.set.notable, main.context.set.warning, byte_dump_sequence_space, main.context.set.warning, main.context.set.notable); } } else if (f_utf_character_is_zero_width(characters.string[i]) == F_true) { if (main.presentation == byte_dump_presentation_classic) { - fprintf(main.output.stream, "%s", f_string_ascii_period_s); + f_print_terminated(f_string_ascii_period_s, main.output.stream); } else if (main.parameters[byte_dump_parameter_placeholder].result == f_console_result_found) { - f_color_print(main.output.stream, main.context.set.warning, "%s", byte_dump_character_placeholder); + fl_print_string("%[%[%s%]%]", main.output.stream, main.context.set.notable, main.context.set.warning, byte_dump_character_placeholder, main.context.set.warning, main.context.set.notable); } else { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (f_utf_character_is_control(characters.string[i]) == F_true) { + // print a space (or '.') for control characters. if (main.presentation == byte_dump_presentation_classic) { - f_color_print(main.output.stream, main.context.set.warning, f_string_ascii_period_s); + fl_print_string("%[%s%]", main.output.stream, main.context.set.warning, f_string_ascii_period_s, main.context.set.warning); } else { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else if (width_utf == 2 && characters.string[i] == 0xd89d0000) { + // U+061C - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 3 && characters.string[i] >= 0xefbfb000 && characters.string[i] <= 0xefbfbc00) { + // Use space to represent Specials codes. // 0xefbfbd00 is excluded because it is printable (and is the "Replacement Character" code). - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 3 && characters.string[i] >= 0xe290a700 && characters.string[i] <= 0xe290bf00) { + // Use space to represent Control Pictues codes that are not currently defined but are reserved. - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 3 && characters.string[i] >= 0xee808000 && characters.string[i] <= 0xefa3bf00) { + // Use space to represent Private Use Area codes. - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 4 && characters.string[i] >= 0xf09c80a0 && characters.string[i] <= 0xf09c80bd) { + // Use space to represent Vaiation Selectors Supplement codes. - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 4 && characters.string[i] >= 0xf3b08080 && characters.string[i] <= 0xf3bfbfbf) { + // Use space to represent Supplemental Private Use Area-A codes. - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 4 && characters.string[i] >= 0xf4808080 && characters.string[i] <= 0xf48fbfbf) { + // Use space to represent Supplemental Private Use Area-B codes. - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 1) { + // print invalid placeholder for invalid UTF-8 widths. if (invalid[i]) { - f_color_print(main.output.stream, main.context.set.error, "%s", byte_dump_character_incomplete); + fl_print_string("%[%s%]", main.output.stream, main.context.set.error, byte_dump_character_incomplete, main.context.set.error); } else { - f_color_print(main.output.stream, main.context.set.warning, "%s", byte_dump_character_incomplete); + fl_print_string("%[%s%]", main.output.stream, main.context.set.warning, byte_dump_character_incomplete, main.context.set.warning); } } else if (width_utf > 0) { - fprintf(main.output.stream, "%c", (uint8_t) output); + fl_print_string("%c", main.output.stream, (uint8_t) output); if (width_utf > 1) { output = macro_f_utf_character_t_to_char_2(characters.string[i]); - fprintf(main.output.stream, "%c", (uint8_t) output); + fl_print_string("%c", main.output.stream, (uint8_t) output); if (width_utf > 2) { output = macro_f_utf_character_t_to_char_3(characters.string[i]); - fprintf(main.output.stream, "%c", (uint8_t) output); + fl_print_string("%c", main.output.stream, (uint8_t) output); if (width_utf > 3) { output = macro_f_utf_character_t_to_char_4(characters.string[i]); - fprintf(main.output.stream, "%c", (uint8_t) output); + fl_print_string("%c", main.output.stream, (uint8_t) output); } } } @@ -845,56 +846,60 @@ extern "C" { // @todo: implement a function in f_utf, such as f_utf_is_combining(), for detecting these combining characters. // print a space for combining characters to combine into, thereby allowing it to be safely and readably displayed. if (width_utf == 2 && characters.string[i] >= 0xdea60000 && characters.string[i] <= 0xdeb00000) { + // Thana combining codes: U+07A6 to U+07B0. - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 2 && characters.string[i] >= 0xcc800000 && characters.string[i] <= 0xcdaf0000) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 3 && characters.string[i] >= 0xe1aab000 && characters.string[i] <= 0xe1abbf00) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 3 && characters.string[i] >= 0xe1b78000 && characters.string[i] <= 0xe1b7bf00) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 3 && characters.string[i] >= 0xe2839000 && characters.string[i] <= 0xe283bf00) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 2 && characters.string[i] >= 0xd8900000 && characters.string[i] <= 0xd89a0000) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 2 && characters.string[i] >= 0xd98b0000 && characters.string[i] <= 0xd99f0000) { + // Arabic, U+064B to U+065F. - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 2 && characters.string[i] >= 0xdb960000 && characters.string[i] <= 0xdb9c0000) { + // Arabic, U+06D6 to U+06DC. - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } else if (width_utf == 2 && characters.string[i] >= 0xd6910000 && characters.string[i] <= 0xd6bd0000) { + // Hebrew, U+0591 to U+05BD. - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } } else { - fprintf(main.output.stream, "%c", output); + fl_print_string("%c", main.output.stream, (uint8_t) output); } // When using UTF-8 characters, the character columns will not line up, so print placeholders to simulate the bytes that are not printed, if necessary for alignment. if (width_utf > 1 && j + 1 < main.width) { if (main.parameters[byte_dump_parameter_placeholder].result == f_console_result_found) { if (invalid[i]) { - f_color_print(main.output.stream, main.context.set.error, "%s", byte_dump_character_placeholder); + fl_print_string("%[%s%]", main.output.stream, main.context.set.error, byte_dump_character_placeholder, main.context.set.error); } else if (main.parameters[byte_dump_parameter_classic].result == f_console_result_found) { - fprintf(main.output.stream, "%s", f_string_ascii_period_s); + f_print_terminated(f_string_ascii_period_s, main.output.stream); } else { - f_color_print(main.output.stream, main.context.set.warning, "%s", byte_dump_character_placeholder); + fl_print_string("%[%s%]", main.output.stream, main.context.set.warning, byte_dump_character_placeholder, main.context.set.warning); } } else { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } ++j; @@ -902,17 +907,17 @@ extern "C" { if (width_utf > 2 && j + 1 < main.width) { if (main.parameters[byte_dump_parameter_placeholder].result == f_console_result_found) { if (invalid[i]) { - f_color_print(main.output.stream, main.context.set.error, "%s", byte_dump_character_placeholder); + fl_print_string("%[%s%]", main.output.stream, main.context.set.error, byte_dump_character_placeholder, main.context.set.error); } else if (main.parameters[byte_dump_parameter_classic].result == f_console_result_found) { - fprintf(main.output.stream, "%s", f_string_ascii_period_s); + f_print_terminated(f_string_ascii_period_s, main.output.stream); } else { - f_color_print(main.output.stream, main.context.set.warning, "%s", byte_dump_character_placeholder); + fl_print_string("%[%s%]", main.output.stream, main.context.set.warning, byte_dump_character_placeholder, main.context.set.warning); } } else { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } ++j; @@ -920,17 +925,17 @@ extern "C" { if (width_utf > 3 && j + 1 < main.width) { if (main.parameters[byte_dump_parameter_placeholder].result == f_console_result_found) { if (invalid[i]) { - f_color_print(main.output.stream, main.context.set.error, "%s", byte_dump_character_placeholder); + fl_print_string("%[%s%]", main.output.stream, main.context.set.error, byte_dump_character_placeholder, main.context.set.error); } else if (main.parameters[byte_dump_parameter_classic].result == f_console_result_found) { - fprintf(main.output.stream, "%s", f_string_ascii_period_s); + f_print_terminated(f_string_ascii_period_s, main.output.stream); } else { - f_color_print(main.output.stream, main.context.set.warning, "%s", byte_dump_character_placeholder); + fl_print_string("%[%s%]", main.output.stream, main.context.set.warning, byte_dump_character_placeholder, main.context.set.warning); } } else { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } ++j; @@ -944,24 +949,24 @@ extern "C" { for (; j < main.width; ++j) { if (invalid[j]) { - f_color_print(main.output.stream, main.context.set.error, "%s", byte_dump_character_placeholder); + fl_print_string("%[%s%]", main.output.stream, main.context.set.error, byte_dump_character_placeholder, main.context.set.error); } else if (main.parameters[byte_dump_parameter_classic].result == f_console_result_found) { - fprintf(main.output.stream, "%s", f_string_ascii_period_s); + f_print_terminated(f_string_ascii_period_s, main.output.stream); } else { - f_color_print(main.output.stream, main.context.set.warning, "%s", byte_dump_character_placeholder); + fl_print_string("%[%s%]", main.output.stream, main.context.set.warning, byte_dump_character_placeholder, main.context.set.warning); } } // for } else { for (; j < main.width; ++j) { - fprintf(main.output.stream, "%s", f_string_space_s); + f_print_terminated(f_string_space_s, main.output.stream); } // for } - f_color_print(main.output.stream, main.context.set.notable, " |"); - fprintf(main.output.stream, "%c", f_string_eol_s[0]); + fl_print_string(" %[|%]", main.output.stream, main.context.set.notable, main.context.set.notable); + f_print_terminated(f_string_eol_s, main.output.stream); } #endif // _di_byte_dump_file_ diff --git a/level_3/byte_dump/data/build/dependencies b/level_3/byte_dump/data/build/dependencies index 399e411..f198a5c 100644 --- a/level_3/byte_dump/data/build/dependencies +++ b/level_3/byte_dump/data/build/dependencies @@ -13,5 +13,7 @@ f_pipe f_print fl_console fl_conversion +fl_print fll_error +fll_print fll_program diff --git a/level_3/byte_dump/data/build/settings b/level_3/byte_dump/data/build/settings index 731328e..86d7f58 100644 --- a/level_3/byte_dump/data/build/settings +++ b/level_3/byte_dump/data/build/settings @@ -20,7 +20,7 @@ build_compiler gcc build_indexer ar build_language c build_libraries -lc -build_libraries-individual -lfll_error -lfll_program -lfl_console -lfl_conversion -lfl_string -lf_color -lf_console -lf_conversion -lf_file -lf_memory -lf_path -lf_pipe -lf_print -lf_string -lf_type_array -lf_utf +build_libraries-individual -lfll_error -lfll_print -lfll_program -lfl_console -lfl_conversion -lfl_print -lfl_string -lf_color -lf_console -lf_conversion -lf_file -lf_memory -lf_path -lf_pipe -lf_print -lf_string -lf_type_array -lf_utf build_libraries-level -lfll_2 -lfll_1 -lfll_0 build_libraries-monolithic -lfll build_sources_library byte_dump.c private-common.c private-byte_dump.c -- 1.8.3.1