From 578618e0513b6a6e5c463edbf8d358f3f309ba10 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Tue, 28 Apr 2020 23:04:15 -0500 Subject: [PATCH] Feature: add functions for combining the additional console parameter together --- level_2/fll_program/c/program.c | 62 +++++++++++++++++++++++++++++ level_2/fll_program/c/program.h | 49 +++++++++++++++++++++++ level_2/fll_program/data/build/dependencies | 1 + level_2/fll_program/data/build/settings | 2 +- 4 files changed, 113 insertions(+), 1 deletion(-) diff --git a/level_2/fll_program/c/program.c b/level_2/fll_program/c/program.c index 3472228..3cfb828 100644 --- a/level_2/fll_program/c/program.c +++ b/level_2/fll_program/c/program.c @@ -150,6 +150,68 @@ extern "C" { } #endif // _di_fll_program_parameter_process_ +#ifndef _di_fll_program_parameter_additional_mash_ + f_return_status fll_program_parameter_additional_mash(const f_string glue, const f_string_length glue_length, const f_string *argv, const f_string_lengths additional, f_string_dynamic *destination) { + #ifndef _di_level_2_parameter_checking_ + if (glue_length < 1) return f_status_set_error(f_invalid_parameter); + if (destination == 0) return f_status_set_error(f_invalid_parameter); + #endif // _di_level_2_parameter_checking_ + + f_status status = f_none; + + f_string_length length = 0; + + for (f_string_length i = 0; i < additional.used; i++) { + length = strnlen(argv[additional.array[i]], f_console_max_size); + + if (length > 0) { + status = fl_string_mash(glue, glue_length, argv[additional.array[i]], length, destination); + + if (f_status_is_error(status)) return f_status_set_error(f_string_too_large); + } + } // for + + return status; + } +#endif // _di_fll_program_parameter_additional_mash_ + +#ifndef _di_fll_program_parameter_additional_trim_mash_ + f_return_status fll_program_parameter_additional_trim_mash(const f_string glue, const f_string_length glue_length, const f_string *argv, const f_string_lengths additional, f_string_dynamic *destination) { + #ifndef _di_level_2_parameter_checking_ + if (glue_length < 1) return f_status_set_error(f_invalid_parameter); + if (destination == 0) return f_status_set_error(f_invalid_parameter); + #endif // _di_level_2_parameter_checking_ + + f_status status = f_none; + + f_string_dynamic ripped = f_string_dynamic_initialize; + + for (f_string_length i = 0; i < additional.used; i++) { + status = fl_string_rip_trim(argv[additional.array[i]], 0, strnlen(argv[additional.array[i]], f_console_max_size), &ripped); + + if (f_status_is_error(status)) { + f_macro_string_dynamic_delete_simple(ripped); + return status; + } + + if (ripped.used > 0) { + status = fl_string_dynamic_mash(glue, glue_length, ripped, destination); + + if (f_status_is_error(status)) { + f_macro_string_dynamic_delete_simple(ripped); + return f_status_set_error(f_string_too_large); + } + } + } // for + + if (ripped.size) { + f_macro_string_dynamic_delete(status, ripped); + } + + return status; + } +#endif // _di_fll_program_parameter_additional_trim_mash_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/level_2/fll_program/c/program.h b/level_2/fll_program/c/program.h index 7d8b436..5b23300 100644 --- a/level_2/fll_program/c/program.h +++ b/level_2/fll_program/c/program.h @@ -21,6 +21,7 @@ // fll-1 includes #include +#include #ifdef __cplusplus extern "C" { @@ -158,6 +159,54 @@ extern "C" { extern f_return_status fll_program_parameter_process(const f_console_arguments arguments, f_console_parameters parameters, const f_console_parameter_ids choices, f_string_lengths *remaining, fl_color_context *context); #endif // _di_fll_program_parameter_process_ +/** + * Mash together all additional arguments associated with a given console parameter. + * + * @param glue + * A string to append between the source and destination, such as a space: ' '. + * @param glue_length + * The number of bytes the glue takes up. + * @param argv + * The program argument array to parse. + * @param destination + * The destination string the source and glue are appended onto. + * + * @return + * f_none on success. + * f_string_max_size (with error bit) if the combined string is too large. + * f_invalid_parameter (with error bit) if a parameter is invalid. + * f_error_allocation (with error bit) on memory allocation error. + * f_error_reallocation (with error bit) on memory reallocation error. + */ +#ifndef _di_fll_program_parameter_additional_mash_ + extern f_return_status fll_program_parameter_additional_mash(const f_string glue, const f_string_length glue_length, const f_string *argv, const f_string_lengths additional, f_string_dynamic *destination); +#endif // _di_fll_program_parameter_additional_mash_ + +/** + * Mash together all additional arguments associated with a given console parameter. + * + * The console parameter is trimmed before mashing. + * + * @param glue + * A string to append between the source and destination, such as a space: ' '. + * @param glue_length + * The number of bytes the glue takes up. + * @param argv + * The program argument array to parse. + * @param destination + * The destination string the source and glue are appended onto. + * + * @return + * f_none on success. + * f_string_max_size (with error bit) if the combined string is too large. + * f_invalid_parameter (with error bit) if a parameter is invalid. + * f_error_allocation (with error bit) on memory allocation error. + * f_error_reallocation (with error bit) on memory reallocation error. + */ +#ifndef _di_fll_program_parameter_additional_trim_mash_ + extern f_return_status fll_program_parameter_additional_trim_mash(const f_string glue, const f_string_length glue_length, const f_string *argv, const f_string_lengths additional, f_string_dynamic *destination); +#endif // _di_fll_program_parameter_additional_trim_mash_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/level_2/fll_program/data/build/dependencies b/level_2/fll_program/data/build/dependencies index 33e491a..7a2bf68 100644 --- a/level_2/fll_program/data/build/dependencies +++ b/level_2/fll_program/data/build/dependencies @@ -4,3 +4,4 @@ f_memory f_string f_console fl_color +fl_string diff --git a/level_2/fll_program/data/build/settings b/level_2/fll_program/data/build/settings index af125a6..055cfed 100644 --- a/level_2/fll_program/data/build/settings +++ b/level_2/fll_program/data/build/settings @@ -10,7 +10,7 @@ version_micro 0 build_compiler gcc build_linker ar build_libraries -lc -build_libraries_fll -lfl_color -lf_utf -lf_print -lf_file -lf_console -lf_memory +build_libraries_fll -lfl_color -lfl_string -lf_utf -lf_print -lf_file -lf_console -lf_memory build_sources_library program.c build_sources_program build_sources_headers program.h -- 1.8.3.1