From 43006fd41f5b3922bcde5a7c7bdd4455313ebff0 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Wed, 29 Apr 2020 21:56:00 -0500 Subject: [PATCH] Feature: add parameter rip functions for ripping console parameters into an array of strings --- level_2/fll_program/c/program.c | 70 +++++++++++++++++++++++++++++++++++++++++ level_2/fll_program/c/program.h | 52 ++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) diff --git a/level_2/fll_program/c/program.c b/level_2/fll_program/c/program.c index 7be4ba9..3fb05b0 100644 --- a/level_2/fll_program/c/program.c +++ b/level_2/fll_program/c/program.c @@ -214,6 +214,76 @@ extern "C" { } #endif // _di_fll_program_parameter_additional_mash_trim_ +#ifndef _di_fll_program_parameter_additional_rip_ + f_return_status fll_program_parameter_additional_rip(const f_string *argv, const f_string_lengths additional, f_string_dynamics *result) { + #ifndef _di_level_2_parameter_checking_ + if (argv == 0) return f_status_set_error(f_invalid_parameter); + if (result == 0) return f_status_set_error(f_invalid_parameter); + #endif // _di_level_2_parameter_checking_ + + f_status status = f_none; + + for (f_string_length i = 0; i < additional.used; i++) { + f_string_dynamic ripped = f_string_dynamic_initialize; + + status = fl_string_rip(argv[additional.array[i]], 0, strnlen(argv[additional.array[i]], f_console_max_size), &ripped); + + if (f_status_is_error(status)) return status; + + if (status == f_no_data) { + status = f_none; + } + else { + if (result->used >= result->size) { + f_macro_string_dynamics_resize(status, (*result), result->size + f_console_default_allocation_step); + + if (f_status_is_error(status)) return status; + } + + result->array[result->used] = ripped; + result->used++; + } + } // for + + return status; + } +#endif // _di_fll_program_parameter_additional_rip_ + +#ifndef _di_fll_program_parameter_additional_rip_trim_ + f_return_status fll_program_parameter_additional_rip_trim(const f_string *argv, const f_string_lengths additional, f_string_dynamics *result) { + #ifndef _di_level_2_parameter_checking_ + if (argv == 0) return f_status_set_error(f_invalid_parameter); + if (result == 0) return f_status_set_error(f_invalid_parameter); + #endif // _di_level_2_parameter_checking_ + + f_status status = f_none; + + for (f_string_length i = 0; i < additional.used; i++) { + f_string_dynamic ripped = f_string_dynamic_initialize; + + 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)) return status; + + if (status == f_no_data) { + status = f_none; + } + else { + if (result->used >= result->size) { + f_macro_string_dynamics_resize(status, (*result), result->size + f_console_default_allocation_step); + + if (f_status_is_error(status)) return status; + } + + result->array[result->used] = ripped; + result->used++; + } + } // for + + return status; + } +#endif // _di_fll_program_parameter_additional_rip_trim_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/level_2/fll_program/c/program.h b/level_2/fll_program/c/program.h index 52583e1..68c55b7 100644 --- a/level_2/fll_program/c/program.h +++ b/level_2/fll_program/c/program.h @@ -210,6 +210,58 @@ extern "C" { extern f_return_status fll_program_parameter_additional_mash_trim(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_trim_ +/** + * Allocate new strings from all of the provided locations. + * + * Empty console parameters are ignored. + * + * @param argv + * The program argument array to parse. + * @param additional + * The string locations where the console parameters are found. + * @param result + * An array of dynamic strings each representing a console parameter. + * + * @return + * f_none on success. + * f_no_data if nothing to rip, no allocations or reallocations are performed. + * 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. + * + * @see fl_string_rip() + * @see fll_program_parameter_additional_rip_trim() + */ +#ifndef _di_fll_program_parameter_additional_rip_ + extern f_return_status fll_program_parameter_additional_rip(const f_string *argv, const f_string_lengths additional, f_string_dynamics *result); +#endif // _di_fll_program_parameter_additional_rip_ + +/** + * Allocate new strings from all of the provided locations. + * + * The console parameters are trimmed. + * + * @param argv + * The program argument array to parse. + * @param additional + * The string locations where the console parameters are found. + * @param result + * An array of dynamic strings each representing a console parameter. + * + * @return + * f_none on success. + * f_no_data if nothing to rip, no allocations or reallocations are performed. + * 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. + * + * @see fl_string_rip() + * @see fll_program_parameter_additional_rip() + */ +#ifndef _di_fll_program_parameter_additional_rip_trim_ + extern f_return_status fll_program_parameter_additional_rip_trim(const f_string *argv, const f_string_lengths additional, f_string_dynamics *result); +#endif // _di_fll_program_parameter_additional_rip_trim_ + #ifdef __cplusplus } // extern "C" #endif -- 1.8.3.1