From 2a2b0af0c7dd2ff0310b69606fd023c990714ade Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Mon, 6 Jun 2022 23:58:24 -0500 Subject: [PATCH] Cleanup: The f_print project code syntax, adding an inline function. --- level_0/f_print/c/print.c | 89 +++++++------- level_0/f_print/c/print.h | 4 + level_0/f_print/c/private-print.c | 245 ++++++++------------------------------ 3 files changed, 101 insertions(+), 237 deletions(-) diff --git a/level_0/f_print/c/print.c b/level_0/f_print/c/print.c index 09414a9..2ef492d 100644 --- a/level_0/f_print/c/print.c +++ b/level_0/f_print/c/print.c @@ -5,6 +5,32 @@ extern "C" { #endif +/** + * Inline helper function to reduce amount of code typed. + * + * This will keep trying to print using fwrite_unlocked() until all bytes are written or an error occurs. + * + * @return + * F_none on success. + * + * F_output (with error bit) on error. + * + * @see fwrite_unlocked() + * @see ferror_unlocked() + */ +static inline f_status_t private_inline_f_print_write_unlocked(const f_string_t string, const f_array_length_t total, FILE * const stream) { + + f_array_length_t count = 0; + + do { + count += fwrite_unlocked(string, sizeof(f_char_t), total - count, stream); + if (ferror_unlocked(stream)) return F_status_set_error(F_output); + + } while (count < total); + + return F_none; +} + #ifndef _di_f_print_ f_status_t f_print(const f_string_t string, const f_array_length_t length, FILE * const stream) { #ifndef _di_level_0_parameter_checking_ @@ -25,11 +51,9 @@ extern "C" { if (!stream) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (!fwrite_unlocked(&character, 1, 1, stream)) { - return F_status_set_error(F_output); - } + clearerr_unlocked(stream); - return F_none; + return private_inline_f_print_write_unlocked((const f_string_t) &character, 1, stream); } #endif // _di_f_print_character_ @@ -39,33 +63,28 @@ extern "C" { if (!stream) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ + clearerr_unlocked(stream); + if (character == 0x7f) { - if (fwrite_unlocked(f_print_sequence_delete_s.string, 1, f_print_sequence_delete_s.used, stream) == f_print_sequence_delete_s.used) { - return F_none; - } - } - else if (macro_f_utf_byte_width_is(character) == 1) { - if (fwrite_unlocked(f_print_sequence_unknown_s.string, 1, f_print_sequence_unknown_s.used, stream) == f_print_sequence_unknown_s.used) { - return F_none; - } + return private_inline_f_print_write_unlocked(f_print_sequence_delete_s.string, f_print_sequence_delete_s.used, stream); } - else if (macro_f_utf_byte_width_is(character) > 1) { - if (fwrite_unlocked(&character, 1, 1, stream)) { - return F_utf; - } + + if (macro_f_utf_byte_width_is(character) == 1) { + return private_inline_f_print_write_unlocked(f_print_sequence_unknown_s.string, f_print_sequence_unknown_s.used, stream); } - else if (character > 0x1f) { - if (fwrite_unlocked(&character, 1, 1, stream)) { - return F_none; - } + + if (macro_f_utf_byte_width_is(character) > 1) { + const f_status_t status = private_inline_f_print_write_unlocked((const f_string_t) &character, 1, stream); + if (F_status_is_error(status)) return status; + + return F_utf; } - else { - if (fwrite_unlocked(f_print_sequence_set_control_s[(unsigned int) character].string, 1, f_print_sequence_set_control_s[(unsigned int) character].used, stream) == f_print_sequence_set_control_s[(unsigned int) character].used) { - return F_none; - } + + if (character > 0x1f) { + return private_inline_f_print_write_unlocked((const f_string_t) &character, 1, stream); } - return F_status_set_error(F_output); + return private_inline_f_print_write_unlocked(f_print_sequence_set_control_s[(unsigned int) character].string, f_print_sequence_set_control_s[(unsigned int) character].used, stream); } #endif // _di_f_print_character_safely_ @@ -699,10 +718,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -713,10 +729,7 @@ extern "C" { do { count += fwrite_unlocked(safe.string + count, 1, safe.used - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < safe.used); @@ -731,10 +744,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -754,10 +764,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); } diff --git a/level_0/f_print/c/print.h b/level_0/f_print/c/print.h index 5223549..8797a50 100644 --- a/level_0/f_print/c/print.h +++ b/level_0/f_print/c/print.h @@ -86,6 +86,8 @@ extern "C" { * F_output (with error bit) on failure (fwrite_unlocked() returns 0). * F_parameter (with error bit) if a parameter is invalid. * + * @see clearerr_unlocked() + * @see ferror_unlocked() * @see fwrite_unlocked() */ #ifndef _di_f_print_character_ @@ -119,6 +121,8 @@ extern "C" { * F_output (with error bit) on failure (fwrite_unlocked() returns 0). * F_parameter (with error bit) if a parameter is invalid. * + * @see clearerr_unlocked() + * @see ferror_unlocked() * @see fwrite_unlocked() */ #ifndef _di_f_print_character_safely_ diff --git a/level_0/f_print/c/private-print.c b/level_0/f_print/c/private-print.c index bfdeb20..16c34c3 100644 --- a/level_0/f_print/c/private-print.c +++ b/level_0/f_print/c/private-print.c @@ -26,10 +26,7 @@ extern "C" { do { count += fwrite_unlocked(string + i + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -90,10 +87,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -115,10 +109,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -160,10 +151,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -186,10 +174,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -213,10 +198,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -258,10 +240,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -284,10 +263,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -309,10 +285,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -358,10 +331,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -384,10 +354,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -417,10 +384,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -431,10 +395,7 @@ extern "C" { do { count += fwrite_unlocked(safe.string + count, 1, safe.used - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < safe.used); @@ -449,10 +410,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -469,10 +427,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); } @@ -509,10 +464,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -535,10 +487,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -563,10 +512,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -584,10 +530,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -598,10 +541,7 @@ extern "C" { do { count += fwrite_unlocked(safe.string + count, 1, safe.used - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < safe.used); @@ -616,10 +556,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -636,10 +573,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); } @@ -671,10 +605,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -694,10 +625,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -716,10 +644,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); } @@ -755,10 +680,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -786,10 +708,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -800,10 +719,7 @@ extern "C" { do { count += fwrite_unlocked(safe.string + count, 1, safe.used - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < safe.used); @@ -818,10 +734,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -838,10 +751,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); } @@ -877,10 +787,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -903,10 +810,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -924,10 +828,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -938,10 +839,7 @@ extern "C" { do { count += fwrite_unlocked(safe.string + count, 1, safe.used - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < safe.used); @@ -956,10 +854,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -976,10 +871,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); } @@ -1000,10 +892,7 @@ extern "C" { do { count += fwrite_unlocked(string + count, 1, length - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < length); } @@ -1015,10 +904,7 @@ extern "C" { do { count += fwrite_unlocked(string + count, 1, F_print_write_max_d - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < F_print_write_max_d); } @@ -1027,10 +913,7 @@ extern "C" { do { count += fwrite_unlocked(string + count, 1, length - total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < length - total); @@ -1075,10 +958,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -1089,10 +969,7 @@ extern "C" { do { count += fwrite_unlocked(safe.string + count, 1, safe.used - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < safe.used); @@ -1107,10 +984,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -1127,10 +1001,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); } @@ -1166,10 +1037,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -1187,10 +1055,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -1201,10 +1066,7 @@ extern "C" { do { count += fwrite_unlocked(safe.string + count, 1, safe.used - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -1219,10 +1081,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); @@ -1239,10 +1098,7 @@ extern "C" { do { count += fwrite_unlocked(string + start + count, 1, total - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < total); } @@ -1284,10 +1140,7 @@ extern "C" { do { count += fwrite_unlocked(string + count, 1, length - count, stream); - - if (ferror_unlocked(stream)) { - return F_status_set_error(F_output); - } + if (ferror_unlocked(stream)) return F_status_set_error(F_output); } while (count < length); -- 1.8.3.1