From: Kevin Day Date: Fri, 10 Dec 2021 02:39:34 +0000 (-0600) Subject: Update: Improve error handling and add F_utf_fragment to generic FLL error printing. X-Git-Tag: 0.5.7~57 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=701ad8cfe3c690969e4fd2c682dc98b49ce5b21c;p=fll Update: Improve error handling and add F_utf_fragment to generic FLL error printing. --- diff --git a/level_2/fll_error/c/private-error.c b/level_2/fll_error/c/private-error.c index 7cd5874..a8fcae6 100644 --- a/level_2/fll_error/c/private-error.c +++ b/level_2/fll_error/c/private-error.c @@ -108,7 +108,23 @@ extern "C" { if (print.verbosity != f_console_verbosity_quiet) { flockfile(print.to.stream); - fl_print_format("%c%[%SInvalid UTF-8 character", print.to.stream, f_string_eol_s[0], print.context, print.prefix); + fl_print_format("%c%[%SInvalid UTF-8 character found", print.to.stream, f_string_eol_s[0], print.context, print.prefix); + + private_fll_error_print_function(print, function); + + fl_print_format(".%]%c", print.to.stream, print.context, f_string_eol_s[0]); + + funlockfile(print.to.stream); + } + + return F_false; + } + + if (status == F_utf_fragment) { + if (print.verbosity != f_console_verbosity_quiet) { + flockfile(print.to.stream); + + fl_print_format("%c%[%SInvalid UTF-8 character (Fragment) found", print.to.stream, f_string_eol_s[0], print.context, print.prefix); private_fll_error_print_function(print, function); diff --git a/level_3/utf8/c/private-print.c b/level_3/utf8/c/private-print.c index af811da..23b75c5 100644 --- a/level_3/utf8/c/private-print.c +++ b/level_3/utf8/c/private-print.c @@ -53,14 +53,48 @@ extern "C" { if (data->main->error.verbosity == f_console_verbosity_quiet) return; - fl_print_format("%c%[%SFailed to decode character '%]", data->main->error.to.stream, f_string_eol_s[0], data->main->context.set.error, data->main->error.prefix, data->main->context.set.error); - fl_print_format("%[%r%]", data->main->error.to.stream, data->main->context.set.notable, character, data->main->context.set.notable); - fl_print_format("%[', error status code%] ", data->main->error.to.stream, data->main->context.set.error, data->main->context.set.error, f_string_eol_s[0]); - fl_print_format("%[%ui%]", data->main->error.to.stream, data->main->context.set.notable, F_status_set_fine(status), data->main->context.set.notable); - fl_print_format("%[.%]%c", data->main->error.to.stream, data->main->context.set.error, data->main->context.set.error, f_string_eol_s[0]); + fl_print_format("%c%[%SFailed to decode character code '%]", data->main->error.to.stream, f_string_eol_s[0], data->main->context.set.error, data->main->error.prefix, data->main->context.set.error); + + if (character.used) { + fl_print_format("%[0x", data->main->error.to.stream, data->main->context.set.notable); + + for (uint8_t i = 0; i < character.used; ++i) { + fl_print_format("%02_uii", data->main->error.to.stream, (uint8_t) character.string[i]); + } // for + + fl_print_format("%]", data->main->error.to.stream, data->main->context.set.notable); + } + + if (F_status_set_fine(status) == F_utf) { + fl_print_format("%[', not a valid UTF-8 character sequence.%]%c", data->main->error.to.stream, data->main->context.set.error, data->main->context.set.error, f_string_eol_s[0]); + } + else { + fl_print_format("%[', error status code%] ", data->main->error.to.stream, data->main->context.set.error, data->main->context.set.error, f_string_eol_s[0]); + fl_print_format("%[%ui%]", data->main->error.to.stream, data->main->context.set.notable, F_status_set_fine(status), data->main->context.set.notable); + fl_print_format("%[.%]%c", data->main->error.to.stream, data->main->context.set.error, data->main->context.set.error, f_string_eol_s[0]); + } } #endif // _di_utf8_print_error_decode_ +#ifndef _di_utf8_print_error_encode_ + void utf8_print_error_encode(utf8_data_t * const data, const f_status_t status, const uint32_t codepoint) { + + if (data->main->error.verbosity == f_console_verbosity_quiet) return; + + fl_print_format("%c%[%SFailed to encode Unicode codepoint '%]", data->main->error.to.stream, f_string_eol_s[0], data->main->context.set.error, data->main->error.prefix, data->main->context.set.error); + fl_print_format("%[U+%_U%]", data->main->error.to.stream, data->main->context.set.notable, codepoint, data->main->context.set.notable); + + if (F_status_set_fine(status) == F_utf) { + fl_print_format("%[', not a valid Unicode codepoint.%]%c", data->main->error.to.stream, data->main->context.set.error, data->main->context.set.error, f_string_eol_s[0]); + } + else { + fl_print_format("%[', error status code%] ", data->main->error.to.stream, data->main->context.set.error, data->main->context.set.error, f_string_eol_s[0]); + fl_print_format("%[%ui%]", data->main->error.to.stream, data->main->context.set.notable, F_status_set_fine(status), data->main->context.set.notable); + fl_print_format("%[.%]%c", data->main->error.to.stream, data->main->context.set.error, data->main->context.set.error, f_string_eol_s[0]); + } + } +#endif // _di_utf8_print_error_encode_ + #ifndef _di_utf8_print_error_no_from_ void utf8_print_error_no_from(utf8_data_t * const data) { diff --git a/level_3/utf8/c/private-print.h b/level_3/utf8/c/private-print.h index 8b0049e..bcf1bcd 100644 --- a/level_3/utf8/c/private-print.h +++ b/level_3/utf8/c/private-print.h @@ -58,13 +58,25 @@ extern "C" { * @param data * The program data. * @param character - * The character block to print. + * The character that is invalid. */ #ifndef _di_utf8_print_error_decode_ extern void utf8_print_error_decode(utf8_data_t * const data, const f_status_t status, const f_string_static_t character) F_attribute_visibility_internal_d; #endif // _di_utf8_print_error_decode_ /** + * Print error message when attempt to encode the character failed. + * + * @param data + * The program data. + * @param codepoint + * The codepoint that is invalid. + */ +#ifndef _di_utf8_print_error_encode_ + extern void utf8_print_error_encode(utf8_data_t * const data, const f_status_t status, const uint32_t codepoint) F_attribute_visibility_internal_d; +#endif // _di_utf8_print_error_encode_ + +/** * Print error message for when no sources are provided. * * @param data diff --git a/level_3/utf8/c/private-utf8.c b/level_3/utf8/c/private-utf8.c index ccee4d4..89e3c3f 100644 --- a/level_3/utf8/c/private-utf8.c +++ b/level_3/utf8/c/private-utf8.c @@ -57,6 +57,12 @@ extern "C" { else { status = utf8_detect_codepoint(data, current, &mode_codepoint); + if (F_status_is_error(status)) { + fll_error_print(data->main->error, F_status_set_fine(status), "utf8_detect_codepoint", F_true); + + break; + } + if (F_status_is_fine(status) && status != F_next) { status = utf8_convert_codepoint(data, current, &mode_codepoint); } diff --git a/level_3/utf8/c/private-utf8_binary.c b/level_3/utf8/c/private-utf8_binary.c index aa74772..dae939c 100644 --- a/level_3/utf8/c/private-utf8_binary.c +++ b/level_3/utf8/c/private-utf8_binary.c @@ -128,7 +128,7 @@ extern "C" { } while (F_status_is_fine(status) && status != F_signal); // Handle last (incomplete) character when the buffer ended before the character is supposed to end. - if (status != F_signal && next == F_false) { + if (F_status_is_error_not(status) && status != F_signal && next == F_false) { character.used = j; if (data->mode & utf8_mode_from_binary_d) { diff --git a/level_3/utf8/c/private-utf8_codepoint.c b/level_3/utf8/c/private-utf8_codepoint.c index 87e73bd..79c12b9 100644 --- a/level_3/utf8/c/private-utf8_codepoint.c +++ b/level_3/utf8/c/private-utf8_codepoint.c @@ -59,7 +59,7 @@ extern "C" { status = f_utf_unicode_from(codepoint, 4, &character.string); if (F_status_is_error(status)) { - utf8_print_error_decode(data, status, character); + utf8_print_error_encode(data, status, codepoint); } else { status = F_none; @@ -251,7 +251,7 @@ extern "C" { } while (F_status_is_fine(status) && status != F_signal); // Handle last (incomplete) character when the buffer ended before the character is supposed to end. - if (status != F_signal && next == F_false) { + if (F_status_is_error_not(status) && status != F_signal && next == F_false) { character.used = j; if (data->mode & utf8_mode_from_binary_d) {