From cf8a721be2e474c44500cebc542b99b9a2ed464b Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sat, 1 May 2021 18:37:40 -0500 Subject: [PATCH] Update: FSS Basic Read program to use memory allocate/dellocate functions and fix error messages. Remove the allocation and deallocate macros. Add allocation and deallocation functions in their place. This makes the common.h and common.c structure more consistent. Some of the error messages are not respecting verbosity. They were probably written before the current verbosity implementation was fully realized. --- level_3/fss_basic_read/c/fss_basic_read.c | 15 ++-- level_3/fss_basic_read/c/private-common.c | 33 +++++++++ level_3/fss_basic_read/c/private-common.h | 85 +++++++++-------------- level_3/fss_basic_read/c/private-fss_basic_read.c | 72 ++++++++----------- level_3/fss_basic_read/c/private-fss_basic_read.h | 28 +++++++- 5 files changed, 127 insertions(+), 106 deletions(-) diff --git a/level_3/fss_basic_read/c/fss_basic_read.c b/level_3/fss_basic_read/c/fss_basic_read.c index 38a135b..4ecc4ad 100644 --- a/level_3/fss_basic_read/c/fss_basic_read.c +++ b/level_3/fss_basic_read/c/fss_basic_read.c @@ -348,22 +348,18 @@ extern "C" { if (F_status_is_error_not(status)) { status = fss_basic_read_depth_process(arguments, *main, &depths); - - if (F_status_is_error(status)) { - fll_error_print(main->error, F_status_set_fine(status), "fss_basic_read_depth_process", F_true); - } } // This standard does not support nesting, so any depth greater than 0 can be predicted without processing the file. if (F_status_is_error_not(status) && depths.array[0].depth > 0) { - macro_fss_basic_read_depths_t_delete_simple(depths); - macro_f_fss_delimits_t_delete_simple(delimits); - if (main->parameters[fss_basic_read_parameter_total].result == f_console_result_found) { fprintf(main->output.stream, "0%c", f_string_eol_s[0]); } + fss_basic_read_depths_resize(0, &depths); + macro_f_fss_delimits_t_delete_simple(delimits); fss_basic_read_main_delete(main); + return F_none; } @@ -372,7 +368,8 @@ extern "C" { f_color_print(main->error.to.stream, main->context.set.notable, "%s%s", f_console_symbol_long_enable_s, fss_basic_read_long_select); f_color_print(main->error.to.stream, main->context.set.error, "' parameter requires a positive number.%c", f_string_eol_s[0]); - macro_fss_basic_read_depths_t_delete_simple(depths); + fss_basic_read_depths_resize(0, &depths); + status = F_status_set_error(F_parameter); } @@ -468,7 +465,7 @@ extern "C" { macro_f_fss_objects_t_delete_simple(main->objects); macro_f_string_dynamic_t_delete_simple(main->buffer); - macro_fss_basic_read_depths_t_delete_simple(depths); + fss_basic_read_depths_resize(0, &depths); macro_f_fss_delimits_t_delete_simple(delimits); } else { diff --git a/level_3/fss_basic_read/c/private-common.c b/level_3/fss_basic_read/c/private-common.c index b5e5148..eba3728 100644 --- a/level_3/fss_basic_read/c/private-common.c +++ b/level_3/fss_basic_read/c/private-common.c @@ -5,6 +5,39 @@ extern "C" { #endif +#ifndef _di_fss_basic_read_depth_delete_simple_ + void fss_basic_read_depth_delete_simple(fss_basic_read_depth_t *depth) { + + if (!depth) return; + + f_string_dynamic_resize(0, &depth->value_name); + } +#endif // _di_fss_basic_read_depth_delete_simple_ + + +#ifndef _di_fss_basic_read_depths_resize_ + f_status_t fss_basic_read_depths_resize(const f_array_length_t length, fss_basic_read_depths_t *depths) { + + if (!depths) return F_status_set_error(F_parameter); + + for (f_array_length_t i = length; i < depths->size; ++i) { + fss_basic_read_depth_delete_simple(&depths->array[i]); + } // for + + const f_status_t status = f_memory_resize(depths->size, length, sizeof(fss_basic_read_depth_t), (void **) & depths->array); + + if (F_status_is_error_not(status)) { + depths->size = length; + + if (depths->used > depths->size) { + depths->used = length; + } + } + + return status; + } +#endif // _di_fss_basic_read_depths_resize_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/level_3/fss_basic_read/c/private-common.h b/level_3/fss_basic_read/c/private-common.h index 3857fed..ab5c403 100644 --- a/level_3/fss_basic_read/c/private-common.h +++ b/level_3/fss_basic_read/c/private-common.h @@ -49,9 +49,6 @@ extern "C" { structure.index_name = 0; \ structure.value_at = 0; \ macro_f_string_dynamic_t_clear(structure.value_name) - - #define macro_fss_basic_read_depth_t_delete(status, structure) status = macro_f_string_dynamic_t_delete_simple(structure.value_name); - #define macro_fss_basic_read_depth_t_delete_simple(structure) macro_f_string_dynamic_t_delete_simple(structure.value_name); #endif // _di_fss_basic_read_depth_t_ /** @@ -72,54 +69,6 @@ extern "C" { #define fss_basic_read_depths_t_initialize { 0, 0, 0 } #define macro_fss_basic_read_depths_t_clear(depths) macro_f_memory_structure_clear(depths) - - #define macro_fss_basic_read_depths_t_delete(status, depths) \ - status = F_none; \ - depths.used = depths.size; \ - while (depths.used > 0) { \ - depths.used--; \ - macro_fss_basic_read_depth_t_delete(status, depths.array[depths.used]); \ - if (F_status_is_error(status)) break; \ - } \ - if (status == F_none) macro_f_memory_structure_delete(depths, fss_basic_read_depth_t) - - #define macro_fss_basic_read_depths_t_delete_simple(depths) \ - depths.used = depths.size; \ - while (depths.used > 0) { \ - depths.used--; \ - macro_fss_basic_read_depth_t_delete_simple(depths.array[depths.used]); \ - } \ - if (!depths.used) macro_f_memory_structure_delete_simple(depths, fss_basic_read_depth_t) - - #define macro_fss_basic_read_depths_t_resize(status, depths, new_length) \ - status = F_none; \ - if (new_length < depths.size) { \ - f_array_length_t i = depths.size - new_length; \ - for (; i < depths.size; i++) { \ - macro_fss_basic_read_depth_t_delete(status, depths.array[i]); \ - if (F_status_is_error(status)) break; \ - } \ - } \ - if (status == F_none) status = f_memory_resize(depths.size, new_length, sizeof(fss_basic_read_depth_t), (void **) & depths.array); \ - if (status == F_none) { \ - depths.size = new_length; \ - if (depths.used > depths.size) depths.used = new_length; \ - } - - #define macro_fss_basic_read_depths_t_adjust(status, depths, new_length) \ - status = F_none; \ - if (new_length < depths.size) { \ - f_array_length_t i = depths.size - new_length; \ - for (; i < depths.size; i++) { \ - macro_fss_basic_read_depth_t_delete(status, depths.array[i]); \ - if (F_status_is_error(status)) break; \ - } \ - } \ - if (status == F_none) status = f_memory_adjust(depths.size, new_length, sizeof(fss_basic_read_depth_t), (void **) & depths.array); \ - if (status == F_none) { \ - depths.size = new_length; \ - if (depths.used > depths.size) depths.used = new_length; \ - } #endif // _di_fss_basic_read_depths_t_ /** @@ -162,6 +111,40 @@ extern "C" { #define fss_basic_read_files_t_initialize { 0, 1, 1 } #endif // _di_fss_basic_read_files_t_ +/** + * Fully deallocate all memory for the given depth without caring about return status. + * + * @param depth + * The depth to deallocate. + */ +#ifndef _di_fss_basic_read_depth_delete_simple_ + extern void fss_basic_read_depth_delete_simple(fss_basic_read_depth_t *depth) f_attribute_visibility_internal; +#endif // _di_fss_basic_read_depth_delete_simple_ + +/** + * Resize the depth array. + * + * @param length + * The new size to use. + * @param depths + * The depth array to resize. + * + * @return + * F_none on success. + * + * Errors (with error bit) from: f_memory_resize(). + * + * Errors (with error bit) from: fss_basic_read_depths_increase(). + * + * @see f_memory_resize() + * + * @see fss_basic_read_depth_delete_simple() + * @see fss_basic_read_depths_increase() + */ +#ifndef _di_fss_basic_read_depths_resize_ + extern f_status_t fss_basic_read_depths_resize(const f_array_length_t length, fss_basic_read_depths_t *depths) f_attribute_visibility_internal; +#endif // _di_fss_basic_read_depths_resize_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/level_3/fss_basic_read/c/private-fss_basic_read.c b/level_3/fss_basic_read/c/private-fss_basic_read.c index 6be3eca..3260d47 100644 --- a/level_3/fss_basic_read/c/private-fss_basic_read.c +++ b/level_3/fss_basic_read/c/private-fss_basic_read.c @@ -18,12 +18,14 @@ extern "C" { depth_size = main.parameters[fss_basic_read_parameter_depth].values.used; } - macro_fss_basic_read_depths_t_resize(status, (*depths), depth_size); + if (depth_size > depths->size) { + status = fss_basic_read_depths_resize(depth_size, depths); - if (F_status_is_error(status)) { - f_color_print(main.error.to.stream, main.context.set.error, "%sUnable to allocate memory.%c", fll_error_print_error, f_string_eol_s[0]); + if (F_status_is_error(status)) { + fll_error_print(main.error, F_status_set_fine(status), "fss_basic_read_depths_resize", F_true); - return status; + return status; + } } depths->used = depth_size; @@ -54,6 +56,7 @@ extern "C" { if (F_status_is_error(status)) { fll_error_parameter_integer_print(main.error, F_status_set_fine(status), "fl_conversion_string_to_number_unsigned", F_true, fss_basic_read_long_depth, arguments.argv[position_depth]); + return status; } } @@ -104,38 +107,17 @@ extern "C" { } if (F_status_is_error(status)) { - f_status_t status_code = F_status_set_fine(status); - - // @todo: move error printing into common function. - if (status_code == F_memory_not) { - f_color_print(main.error.to.stream, main.context.set.error, "%sUnable to allocate memory.%c", fll_error_print_error, f_string_eol_s[0]); - } - else if (status_code == F_string_too_large) { - f_color_print(main.error.to.stream, main.context.set.error, "%sUnable to process '", fll_error_print_error); - f_color_print(main.error.to.stream, main.context.set.notable, "%s%s", f_console_symbol_long_enable_s, fss_basic_read_long_trim); - f_color_print(main.error.to.stream, main.context.set.error, "' because the maximum buffer size was reached.%c", f_string_eol_s[0]); - } - else { - f_string_t function = "f_string_append"; - - if (main.parameters[fss_basic_read_parameter_trim].result == f_console_result_found) { - function = "fl_string_rip"; - } - - f_color_print(main.error.to.stream, main.context.set.error, "%sAn unhandled error (", fll_error_print_error); - f_color_print(main.error.to.stream, main.context.set.notable, "%u", status_code); - f_color_print(main.error.to.stream, main.context.set.error, ") has occurred while calling "); - f_color_print(main.error.to.stream, main.context.set.notable, "%s()", function); - f_color_print(main.error.to.stream, main.context.set.error, ".%c", f_string_eol_s[0]); - } + fll_error_print(main.error, F_status_set_fine(status), main.parameters[fss_basic_read_parameter_trim].result == f_console_result_found ? "fl_string_rip" : "f_string_append", F_true); return status; } if (!depths->array[i].value_name.used) { - f_color_print(main.error.to.stream, main.context.set.error, "%sThe '", fll_error_print_error); - f_color_print(main.error.to.stream, main.context.set.notable, "%s%s", f_console_symbol_long_enable_s, fss_basic_read_long_name); - f_color_print(main.error.to.stream, main.context.set.error, "' must not be an empty string.%c", f_string_eol_s[0]); + if (main.error.verbosity != f_console_verbosity_quiet) { + f_color_print(main.error.to.stream, main.context.set.error, "%sThe '", fll_error_print_error); + f_color_print(main.error.to.stream, main.context.set.notable, "%s%s", f_console_symbol_long_enable_s, fss_basic_read_long_name); + f_color_print(main.error.to.stream, main.context.set.error, "' must not be an empty string.%c", f_string_eol_s[0]); + } return F_status_set_error(F_parameter); } @@ -148,22 +130,26 @@ extern "C" { for (f_array_length_t j = i + 1; j < depths->used; j++) { if (depths->array[i].depth == depths->array[j].depth) { - f_color_print(main.error.to.stream, main.context.set.error, "%sThe value '", fll_error_print_error); - f_color_print(main.error.to.stream, main.context.set.notable, "%llu", depths->array[i].depth); - f_color_print(main.error.to.stream, main.context.set.error, "' may only be specified once for the parameter '"); - f_color_print(main.error.to.stream, main.context.set.notable, "%s%s", f_console_symbol_long_enable_s, fss_basic_read_long_depth); - f_color_print(main.error.to.stream, main.context.set.error, "'.%c", f_string_eol_s[0]); + if (main.error.verbosity != f_console_verbosity_quiet) { + f_color_print(main.error.to.stream, main.context.set.error, "%sThe value '", fll_error_print_error); + f_color_print(main.error.to.stream, main.context.set.notable, "%llu", depths->array[i].depth); + f_color_print(main.error.to.stream, main.context.set.error, "' may only be specified once for the parameter '"); + f_color_print(main.error.to.stream, main.context.set.notable, "%s%s", f_console_symbol_long_enable_s, fss_basic_read_long_depth); + f_color_print(main.error.to.stream, main.context.set.error, "'.%c", f_string_eol_s[0]); + } return F_status_set_error(F_parameter); } else if (depths->array[i].depth > depths->array[j].depth) { - 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, fss_basic_read_long_depth); - f_color_print(main.error.to.stream, main.context.set.error, "' may not have the value '"); - f_color_print(main.error.to.stream, main.context.set.notable, "%llu", depths->array[i].depth); - f_color_print(main.error.to.stream, main.context.set.error, "' before the value '"); - f_color_print(main.error.to.stream, main.context.set.notable, "%llu", depths->array[j].depth); - f_color_print(main.error.to.stream, main.context.set.error, "'.%c", f_string_eol_s[0]); + if (main.error.verbosity != f_console_verbosity_quiet) { + 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, fss_basic_read_long_depth); + f_color_print(main.error.to.stream, main.context.set.error, "' may not have the value '"); + f_color_print(main.error.to.stream, main.context.set.notable, "%llu", depths->array[i].depth); + f_color_print(main.error.to.stream, main.context.set.error, "' before the value '"); + f_color_print(main.error.to.stream, main.context.set.notable, "%llu", depths->array[j].depth); + f_color_print(main.error.to.stream, main.context.set.error, "'.%c", f_string_eol_s[0]); + } return F_status_set_error(F_parameter); } diff --git a/level_3/fss_basic_read/c/private-fss_basic_read.h b/level_3/fss_basic_read/c/private-fss_basic_read.h index bd1d4ef..fd196e6 100644 --- a/level_3/fss_basic_read/c/private-fss_basic_read.h +++ b/level_3/fss_basic_read/c/private-fss_basic_read.h @@ -27,7 +27,17 @@ extern "C" { * @return * F_none on success. * - * Status codes (with error bit) are returned on any problem. + * Errors (with error bit) from: f_string_append(). + * Errors (with error bit) from: fl_string_rip(). + * Errors (with error bit) from: fl_conversion_string_to_number_unsigned(). + * + * Errors (with error bit) from: fss_basic_read_depths_resize(). + * + * @see f_string_append() + * @see fl_string_rip() + * @see fl_conversion_string_to_number_unsigned() + * + * @see fss_basic_read_depths_resize() */ #ifndef _di_fss_basic_read_depth_process_ extern f_status_t fss_basic_read_depth_process(const f_console_arguments_t arguments, const fss_basic_read_main_t main, fss_basic_read_depths_t *depths) f_attribute_visibility_internal; @@ -54,6 +64,8 @@ extern "C" { /** * Load a given number parameter. * + * This will print an error message on error. + * * @param arguments * The console arguments passed to the program. * @param main @@ -68,7 +80,11 @@ extern "C" { * @return * F_none on success. * - * Status codes (with error bit) are returned on any problem. + * Errors (with error bit) from: fl_conversion_string_to_number_unsigned(). + * + * @see fl_conversion_string_to_number_unsigned() + * + * @see fss_basic_read_depths_resize() */ #ifndef _di_fss_basic_read_load_number_ extern f_status_t fss_basic_read_load_number(const f_console_arguments_t arguments, const fss_basic_read_main_t main, const f_array_length_t parameter, const f_string_t name, f_number_unsigned_t *number) f_attribute_visibility_internal; @@ -141,6 +157,8 @@ extern "C" { /** * Perform the basic read processing on the buffer. * + * This will print an error message on error. + * * @param arguments * The console arguments passed to the program. * @param files @@ -155,7 +173,11 @@ extern "C" { * @return * F_none on success. * - * Status codes (with error bit) are returned on any problem. + * Errors (with error bit) from: fll_fss_basic_read() + * + * Errors (with error bit) from: fss_basic_read_load_setting() + * + * @see fll_fss_basic_read() * * @see fss_basic_read_load_setting() */ -- 1.8.3.1