From 604406ea676f33f4a76652f19c3db327345b7c3d Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Wed, 28 Aug 2019 19:48:28 -0500 Subject: [PATCH] Update: add UTF-8 support to fl_console Also remove unnecessary looping. --- level_0/f_console/c/console.h | 13 ++- level_1/fl_console/c/console.c | 178 ++++++++++++++--------------- level_1/fl_console/c/console.h | 21 +++- level_1/fl_console/data/build/dependencies | 1 + level_1/fl_console/data/build/settings | 4 +- 5 files changed, 117 insertions(+), 100 deletions(-) diff --git a/level_0/f_console/c/console.h b/level_0/f_console/c/console.h index 138bcf7..7dcd80d 100644 --- a/level_0/f_console/c/console.h +++ b/level_0/f_console/c/console.h @@ -85,7 +85,8 @@ extern "C" { #endif // _di_f_console_max_size_ #ifndef _di_f_console_default_allocation_step_ - #define f_console_default_allocation_step f_memory_default_allocation_step + // provide a UTF-8 friendly allocation step. + #define f_console_default_allocation_step 4 #endif // _di_f_console_default_allocation_step_ #ifndef _di_f_console_types_ @@ -94,7 +95,7 @@ extern "C" { /** * none: parameter not found. * found: parameter found. - * additional: parameter found, extra data exists (such as '-f filename', filename would be the extra data). + * additional: parameter found, extra data exists (such as '-f filename', 'filename' would be the extra data). */ enum { f_console_result_none, @@ -146,8 +147,8 @@ extern "C" { * perform checks against short & long options to see if the string is one of them (normal). */ #define f_console_is_enable(result, string, short_option, long_option, max_length) \ - ((result == f_console_short_enable && strncmp(string, short_option, 1) == 0) || \ - (result == f_console_long_enable && strncmp(string, long_option, max_length) == 0)) + ((result == f_console_short_enable && strncmp(string, short_option, max_length) == 0) || \ + (result == f_console_long_enable && strncmp(string, long_option, max_length) == 0)) #endif // _di_f_console_is_enable_ #ifndef _di_f_console_is_disable_ @@ -155,8 +156,8 @@ extern "C" { * perform checks against short & long options to see if the string is one of them (inverse). */ #define f_console_is_disable(result, string, short_option, long_option, max_length) \ - ((result == f_console_short_disable && strncmp(string, short_option, 1) == 0) || \ - (result == f_console_long_disable && strncmp(string, long_option, max_length) == 0)) + ((result == f_console_short_disable && strncmp(string, short_option, max_length) == 0) || \ + (result == f_console_long_disable && strncmp(string, long_option, max_length) == 0)) #endif // _di_f_console_is_disable_ #ifndef _di_f_console_is_extra_enable_ diff --git a/level_1/fl_console/c/console.c b/level_1/fl_console/c/console.c index 0936b93..e82069e 100644 --- a/level_1/fl_console/c/console.c +++ b/level_1/fl_console/c/console.c @@ -22,7 +22,6 @@ extern "C" { f_string_lengths extra_initiator = f_string_lengths_initialize; - // loop through and read all parameters while (location < argc) { f_console_identify(argv[location], &result); @@ -49,140 +48,141 @@ extern "C" { // Now handle the normal commands if (argv[location][0] == f_console_symbol_enable) { - while (sub_location < string_length) { + if (sub_location < string_length) { for (parameter_counter = 0; parameter_counter < total_parameters; parameter_counter++) { - if (parameters[parameter_counter].type == f_console_type_normal) { - if (parameters[parameter_counter].symbol_short != 0 && parameters[parameter_counter].symbol_long != 0) { - if (f_console_is_enable(result, &argv[location][sub_location], parameters[parameter_counter].symbol_short, parameters[parameter_counter].symbol_long, string_length + 1)) { - parameters[parameter_counter].result = f_console_result_found; - - if (parameters[parameter_counter].has_additional) { - if (extra_initiator.used >= extra_initiator.size) { - f_status allocation_status = f_none; - - f_resize_string_lengths(allocation_status, extra_initiator, extra_initiator.size + f_console_default_allocation_step); - if (f_error_is_error(allocation_status)) { - f_delete_string_lengths(status, extra_initiator); - return f_error_set_error(allocation_status); - } - } + if (parameters[parameter_counter].type != f_console_type_normal) { + continue; + } + + if (parameters[parameter_counter].symbol_short != 0 && parameters[parameter_counter].symbol_long != 0) { + if (f_console_is_enable(result, &argv[location][sub_location], parameters[parameter_counter].symbol_short, parameters[parameter_counter].symbol_long, string_length + 1)) { + parameters[parameter_counter].result = f_console_result_found; - extra_initiator.array[extra_initiator.used] = parameter_counter; - extra_initiator.used++; + if (parameters[parameter_counter].has_additional) { + if (extra_initiator.used >= extra_initiator.size) { + f_status allocation_status = f_none; + + f_resize_string_lengths(allocation_status, extra_initiator, extra_initiator.size + f_console_default_allocation_step); + if (f_error_is_error(allocation_status)) { + f_delete_string_lengths(status, extra_initiator); + return f_error_set_error(allocation_status); + } } + + extra_initiator.array[extra_initiator.used] = parameter_counter; + extra_initiator.used++; } } + } - if (parameters[parameter_counter].symbol_extra != 0) { - if (f_console_is_extra_enable(result, &argv[location][sub_location], parameters[parameter_counter].symbol_extra, string_length + 1)) { - parameters[parameter_counter].result = f_console_result_found; + if (parameters[parameter_counter].symbol_extra != 0) { + if (f_console_is_extra_enable(result, &argv[location][sub_location], parameters[parameter_counter].symbol_extra, string_length + 1)) { + parameters[parameter_counter].result = f_console_result_found; - if (parameters[parameter_counter].has_additional) { - if (extra_initiator.used >= extra_initiator.size) { - f_status allocation_status = f_none; + if (parameters[parameter_counter].has_additional) { + if (extra_initiator.used >= extra_initiator.size) { + f_status allocation_status = f_none; - f_resize_string_lengths(allocation_status, extra_initiator, extra_initiator.size + f_console_default_allocation_step); + f_resize_string_lengths(allocation_status, extra_initiator, extra_initiator.size + f_console_default_allocation_step); - if (f_error_is_error(allocation_status)) { - f_delete_string_lengths(status, extra_initiator); - return f_error_set_error(allocation_status); - } + if (f_error_is_error(allocation_status)) { + f_delete_string_lengths(status, extra_initiator); + return f_error_set_error(allocation_status); } - - extra_initiator.array[extra_initiator.used] = parameter_counter; - extra_initiator.used++; } + + extra_initiator.array[extra_initiator.used] = parameter_counter; + extra_initiator.used++; } } } } // for - - sub_location += increments; - } // while - - // now handle the inverse commands + } } + // now handle the inverse commands else if (argv[location][0] == f_console_symbol_disable) { - while (sub_location < string_length) { + if (sub_location < string_length) { for (parameter_counter = 0; parameter_counter < total_parameters; parameter_counter++) { - if (parameters[parameter_counter].type == f_console_type_inverse) { - if (parameters[parameter_counter].symbol_short != 0 && parameters[parameter_counter].symbol_long != 0) { - if (f_console_is_disable(result, &argv[location][sub_location], parameters[parameter_counter].symbol_short, parameters[parameter_counter].symbol_long, string_length + 1)) { - parameters[parameter_counter].result = f_console_result_found; + if (parameters[parameter_counter].type != f_console_type_inverse) { + continue; + } - if (parameters[parameter_counter].has_additional) { - if (extra_initiator.used >= extra_initiator.size) { - f_status allocation_status = f_none; + if (parameters[parameter_counter].symbol_short != 0 && parameters[parameter_counter].symbol_long != 0) { + if (f_console_is_disable(result, &argv[location][sub_location], parameters[parameter_counter].symbol_short, parameters[parameter_counter].symbol_long, string_length + 1)) { + parameters[parameter_counter].result = f_console_result_found; - f_resize_string_lengths(allocation_status, extra_initiator, extra_initiator.size + f_console_default_allocation_step); + if (parameters[parameter_counter].has_additional) { + if (extra_initiator.used >= extra_initiator.size) { + f_status allocation_status = f_none; - if (f_error_is_error(allocation_status)) { - f_delete_string_lengths(status, extra_initiator); - return f_error_set_error(allocation_status); - } - } + f_resize_string_lengths(allocation_status, extra_initiator, extra_initiator.size + f_console_default_allocation_step); - extra_initiator.array[extra_initiator.used] = parameter_counter; - extra_initiator.used++; + if (f_error_is_error(allocation_status)) { + f_delete_string_lengths(status, extra_initiator); + return f_error_set_error(allocation_status); + } } + + extra_initiator.array[extra_initiator.used] = parameter_counter; + extra_initiator.used++; } } + } - if (parameters[parameter_counter].symbol_extra != 0) { - if (f_console_is_extra_disable(result, &argv[location][sub_location], parameters[parameter_counter].symbol_extra, string_length + 1)) { - parameters[parameter_counter].result = f_console_result_found; + if (parameters[parameter_counter].symbol_extra != 0) { + if (f_console_is_extra_disable(result, &argv[location][sub_location], parameters[parameter_counter].symbol_extra, string_length + 1)) { + parameters[parameter_counter].result = f_console_result_found; - if (parameters[parameter_counter].has_additional) { - if (extra_initiator.used >= extra_initiator.size) { - f_status allocation_status = f_none; + if (parameters[parameter_counter].has_additional) { + if (extra_initiator.used >= extra_initiator.size) { + f_status allocation_status = f_none; - f_resize_string_lengths(allocation_status, extra_initiator, extra_initiator.size + f_console_default_allocation_step); + f_resize_string_lengths(allocation_status, extra_initiator, extra_initiator.size + f_console_default_allocation_step); - if (f_error_is_error(allocation_status)) { - f_delete_string_lengths(status, extra_initiator); - return f_error_set_error(allocation_status); - } + if (f_error_is_error(allocation_status)) { + f_delete_string_lengths(status, extra_initiator); + return f_error_set_error(allocation_status); } - - extra_initiator.array[extra_initiator.used] = parameter_counter; - extra_initiator.used++; } + + extra_initiator.array[extra_initiator.used] = parameter_counter; + extra_initiator.used++; } } } } // for - - sub_location += increments; - } // while + } } else { // use found to determine if the remaining parameter should be populated found = f_false; for (parameter_counter = 0; parameter_counter < total_parameters; parameter_counter++) { - if (parameters[parameter_counter].type == f_console_type_other) { - if (parameters[parameter_counter].length > 0 && parameters[parameter_counter].symbol_other != 0) { - if (strncmp(argv[location], parameters[parameter_counter].symbol_other, parameters[parameter_counter].length + 1) == 0) { - f_status allocation_status = f_none; + if (parameters[parameter_counter].type != f_console_type_other) { + continue; + } - if (parameters[parameter_counter].additional.used >= parameters[parameter_counter].additional.size) { - f_resize_string_lengths(allocation_status, parameters[parameter_counter].additional, parameters[parameter_counter].additional.size + f_console_default_allocation_step); - } + if (parameters[parameter_counter].length > 0 && parameters[parameter_counter].symbol_other != 0) { + if (strncmp(argv[location], parameters[parameter_counter].symbol_other, parameters[parameter_counter].length + 1) == 0) { + f_status allocation_status = f_none; - if (f_error_is_error(allocation_status)) { - f_delete_string_lengths(status, extra_initiator); - return f_error_set_error(allocation_status); - } + if (parameters[parameter_counter].additional.used >= parameters[parameter_counter].additional.size) { + f_resize_string_lengths(allocation_status, parameters[parameter_counter].additional, parameters[parameter_counter].additional.size + f_console_default_allocation_step); + } - parameters[parameter_counter].result = f_console_result_found; + if (f_error_is_error(allocation_status)) { + f_delete_string_lengths(status, extra_initiator); + return f_error_set_error(allocation_status); + } - // when "other" is supplied, the extra will be recycled to represent the location of the "other" such that ordering can be determined by the caller - parameters[parameter_counter].additional.array[parameters[parameter_counter].additional.used] = location; - parameters[parameter_counter].additional.used++; + parameters[parameter_counter].result = f_console_result_found; - found = f_true; - break; - } + // when "other" is supplied, the extra will be recycled to represent the location of the "other" such that ordering can be determined by the caller + parameters[parameter_counter].additional.array[parameters[parameter_counter].additional.used] = location; + parameters[parameter_counter].additional.used++; + + found = f_true; + break; } } } // for diff --git a/level_1/fl_console/c/console.h b/level_1/fl_console/c/console.h index 31a5d4d..f33f900 100644 --- a/level_1/fl_console/c/console.h +++ b/level_1/fl_console/c/console.h @@ -23,10 +23,25 @@ extern "C" { #endif +/** + * Process console parameters. + * + * @param argc + * The number of parameters passed to the process. + * @param argv + * The parameters passed to the process. + * @param parameters + * The console parameters to look for. + * @param total_parameters + * The used size of the parameters array. + * + * @return + * f_none on success. + * f_no_data if no "extra" parameters were found. + * f_invalid_parameter (with error bit) if a parameter is invalid. + * f_reallocation_error (with error bit) on memory reallocation error. + */ #ifndef _di_fl_process_parameters_ - /** - * Process console parameters. - */ extern f_return_status fl_process_parameters(const f_array_length argc, const f_string argv[], f_console_parameter parameters[], const f_array_length total_parameters, f_string_lengths *remaining); #endif // _di_fl_process_parameters_ diff --git a/level_1/fl_console/data/build/dependencies b/level_1/fl_console/data/build/dependencies index 8acdfae..a355d97 100644 --- a/level_1/fl_console/data/build/dependencies +++ b/level_1/fl_console/data/build/dependencies @@ -3,3 +3,4 @@ f_errors f_strings f_console f_memory +f_utf diff --git a/level_1/fl_console/data/build/settings b/level_1/fl_console/data/build/settings index 4b515f5..600b1af 100644 --- a/level_1/fl_console/data/build/settings +++ b/level_1/fl_console/data/build/settings @@ -10,9 +10,9 @@ version_micro 0 build_compiler gcc build_linker ar build_libraries -lc -build_libraries_fll -lf_memory -lf_console +build_libraries_fll -lf_memory -lf_console -lf_utf build_sources_library console.c -build_sources_program +build_sources_program build_sources_headers console.h build_sources_bash build_sources_settings -- 1.8.3.1