From 8b2d9089ff869c9e091e4e4208c2b86f184e1fe7 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Tue, 5 May 2020 00:00:50 -0500 Subject: [PATCH] Feature: add fss snatch functions for "snatching" content for a set of object names These are intended to be used for simple purposes. Directly implementing these in individual projects are recommended for more advanced usage. I intend to expand on these "snatch" functions in the near future. --- build/level_2/settings | 4 +- build/monolithic/settings | 4 +- level_2/fll_fss/c/fss.c | 89 ++++++++++++++++++++++++++++ level_2/fll_fss/c/fss.h | 114 ++++++++++++++++++++++++++++++++++++ level_2/fll_fss/data/build/settings | 4 +- 5 files changed, 209 insertions(+), 6 deletions(-) create mode 100644 level_2/fll_fss/c/fss.c create mode 100644 level_2/fll_fss/c/fss.h diff --git a/build/level_2/settings b/build/level_2/settings index ece6d60..231da09 100644 --- a/build/level_2/settings +++ b/build/level_2/settings @@ -12,9 +12,9 @@ build_linker ar build_libraries -lc build_libraries_fll -lfll_0 -lfll_1 build_libraries_fll-level -lfll_0 -lfll_1 -build_sources_library execute.c file.c fss_basic.c fss_basic_list.c fss_extended.c fss_extended_list.c fss_status.c program.c status.c +build_sources_library execute.c file.c fss.c fss_basic.c fss_basic_list.c fss_extended.c fss_extended_list.c fss_status.c program.c status.c build_sources_program -build_sources_headers execute.h file.h fss_basic.h fss_basic_list.h fss_extended.h fss_extended_list.h fss_status.h program.h status.h +build_sources_headers execute.h file.h fss.h fss_basic.h fss_basic_list.h fss_extended.h fss_extended_list.h fss_status.h program.h status.h build_shared yes build_static yes diff --git a/build/monolithic/settings b/build/monolithic/settings index c63cf4e..14bc65b 100644 --- a/build/monolithic/settings +++ b/build/monolithic/settings @@ -11,9 +11,9 @@ build_compiler gcc build_linker ar build_libraries -lc build_libraries_fll -build_sources_library level_0/console.c level_0/conversion.c level_0/file.c level_0/memory.c level_0/pipe.c level_0/print.c level_0/utf.c level_0/private-utf.c level_1/color.c level_1/console.c level_1/directory.c level_1/file.c level_1/fss.c level_1/fss_basic.c level_1/fss_basic_list.c level_1/fss_extended.c level_1/fss_extended_list.c level_1/print.c level_1/serialized.c level_1/private-serialized.c level_1/socket.c level_1/status.c level_1/string.c level_1/private-string.c level_1/utf.c level_1/private-utf.c level_1/utf_file.c level_2/execute.c level_2/file.c level_2/fss_basic.c level_2/fss_basic_list.c level_2/fss_extended.c level_2/fss_extended_list.c level_2/fss_status.c level_2/program.c level_2/status.c +build_sources_library level_0/console.c level_0/conversion.c level_0/file.c level_0/memory.c level_0/pipe.c level_0/print.c level_0/utf.c level_0/private-utf.c level_1/color.c level_1/console.c level_1/directory.c level_1/file.c level_1/fss.c level_1/fss_basic.c level_1/fss_basic_list.c level_1/fss_extended.c level_1/fss_extended_list.c level_1/print.c level_1/serialized.c level_1/private-serialized.c level_1/socket.c level_1/status.c level_1/string.c level_1/private-string.c level_1/utf.c level_1/private-utf.c level_1/utf_file.c level_2/execute.c level_2/file.c level_2/fss.c level_2/fss_basic.c level_2/fss_basic_list.c level_2/fss_extended.c level_2/fss_extended_list.c level_2/fss_status.c level_2/program.c level_2/status.c build_sources_program -build_sources_headers level_0/color.h level_0/console.h level_0/conversion.h level_0/file.h level_0/fss.h level_0/memory.h level_0/path_fll.h level_0/path_filesystem.h level_0/pipe.h level_0/print.h level_0/serialized.h level_0/socket.h level_0/status.h level_0/string.h level_0/type.h level_0/type_array.h level_0/utf.h level_1/color.h level_1/console.h level_1/directory.h level_1/file.h level_1/fss.h level_1/fss_basic.h level_1/fss_basic_list.h level_1/fss_status.h level_1/fss_extended.h level_1/fss_extended_list.h level_1/fss_macro.h level_1/print.h level_1/serialized.h level_1/socket.h level_1/status.h level_1/string.h level_1/utf.h level_1/utf_file.h level_2/execute.h level_2/file.h level_2/fss_basic.h level_2/fss_basic_list.h level_2/fss_extended.h level_2/fss_extended_list.h level_2/fss_status.h level_2/program.h level_2/status.h +build_sources_headers level_0/color.h level_0/console.h level_0/conversion.h level_0/file.h level_0/fss.h level_0/memory.h level_0/path_fll.h level_0/path_filesystem.h level_0/pipe.h level_0/print.h level_0/serialized.h level_0/socket.h level_0/status.h level_0/string.h level_0/type.h level_0/type_array.h level_0/utf.h level_1/color.h level_1/console.h level_1/directory.h level_1/file.h level_1/fss.h level_1/fss_basic.h level_1/fss_basic_list.h level_1/fss_status.h level_1/fss_extended.h level_1/fss_extended_list.h level_1/fss_macro.h level_1/print.h level_1/serialized.h level_1/socket.h level_1/status.h level_1/string.h level_1/utf.h level_1/utf_file.h level_2/execute.h level_2/file.h level_2/fss.h level_2/fss_basic.h level_2/fss_basic_list.h level_2/fss_extended.h level_2/fss_extended_list.h level_2/fss_status.h level_2/program.h level_2/status.h build_sources_bash build_sources_settings build_shared yes diff --git a/level_2/fll_fss/c/fss.c b/level_2/fll_fss/c/fss.c new file mode 100644 index 0000000..c33595e --- /dev/null +++ b/level_2/fll_fss/c/fss.c @@ -0,0 +1,89 @@ +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _di_fll_fss_snatch_ + f_return_status fll_fss_snatch(const f_string_dynamic buffer, const f_fss_objects objects, const f_fss_contents contents, const f_string names[], const f_string_length lengths[], f_string_dynamic *values[], const f_string_length size) { + #ifndef _di_level_2_parameter_checking_ + if (size == 0) return f_status_set_error(f_invalid_parameter); + #endif // _di_level_2_parameter_checking_ + + if (buffer.used == 0) return f_no_data; + if (objects.used == 0) return f_no_data; + if (contents.used == 0) return f_no_data; + + f_status status = f_none; + f_string_length length_object = 0; + + f_array_length i = 0; + f_array_length j = 0; + f_array_length k = 0; + + for (; i < objects.used; i++) { + length_object = (objects.array[i].stop - objects.array[i].start) + 1; + + for (j = 0; j < size; j++) { + if (fl_string_compare_trim(buffer.string + objects.array[i].start, names[j], length_object, lengths[j]) == f_equal_to) { + if (values[j]->used == 0) { + for (k = 0; k < contents.array[i].used; k++) { + status = fl_string_dynamic_partial_append_nulless(buffer, contents.array[i].array[k], values[j]); + + if (f_status_is_error(status)) break; + } // for + + if (f_status_is_error(status)) break; + } + } + } // for + + if (f_status_is_error(status)) break; + } // for + + return status; + } +#endif // _di_fll_fss_snatch_ + +#ifndef _di_fll_fss_snatch_mash_ + f_return_status fll_fss_snatch_mash(const f_string_dynamic buffer, const f_fss_objects objects, const f_fss_contents contents, const f_string names[], const f_string_length lengths[], f_string_dynamic *values[], const f_string_length size, const f_string glue, const f_string_length glue_length) { + #ifndef _di_level_2_parameter_checking_ + if (size == 0) return f_status_set_error(f_invalid_parameter); + #endif // _di_level_2_parameter_checking_ + + if (buffer.used == 0) return f_no_data; + if (objects.used == 0) return f_no_data; + if (contents.used == 0) return f_no_data; + + f_status status = f_none; + f_string_length length_object = 0; + + f_array_length i = 0; + f_array_length j = 0; + f_array_length k = 0; + + for (; i < objects.used; i++) { + length_object = (objects.array[i].stop - objects.array[i].start) + 1; + + for (j = 0; j < size; j++) { + if (fl_string_compare_trim(buffer.string + objects.array[i].start, names[j], length_object, lengths[j]) == f_equal_to) { + for (k = 0; k < contents.array[i].used; k++) { + status = fl_string_dynamic_partial_mash_nulless(glue, glue_length, buffer, contents.array[i].array[k], values[j]); + + if (f_status_is_error(status)) break; + } // for + + if (f_status_is_error(status)) break; + } + } // for + + if (f_status_is_error(status)) break; + } // for + + return status; + } +#endif // _di_fll_fss_snatch_mash_ + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_2/fll_fss/c/fss.h b/level_2/fll_fss/c/fss.h new file mode 100644 index 0000000..4a90619 --- /dev/null +++ b/level_2/fll_fss/c/fss.h @@ -0,0 +1,114 @@ +/** + * FLL - Level 2 + * + * Project: FSS + * API Version: 0.5 + * Licenses: lgplv2.1 + * + * Provide common FSS functions. + * + * @todo add "snatch" functions to grab contents in separate sets instead of appending (such as for fss extended). + */ +#ifndef _FLL_fss_h +#define _FLL_fss_h + +// fll-0 includes +#include +#include +#include +#include +#include +#include + +// fll-1 includes +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Perform simple search through all objects against the given set, saving all values when matched. + * + * Only the first match for each name is snatched, all others are ignored. + * + * This will trim the object names when comparing (removing leading/trailing whitespace). + * This will strip NULL charactes when copying. + * + * This performs only a simple search algorithm that should be acceptable for small sets where performance is generally not a concern. + * + * @param buffer + * The buffer to read from. + * @param objects + * This object mappings to process. + * @param contents + * This content mappings to process. + * @param names + * An array of strings to "snatch" from the buffer. + * @param lengths + * An array of lengths for each names string. + * @param values + * An array of values where "snatched" content is stored. + * Each of these must be of type f_string_dynamic. + * @param size + * The total size of the names, lengths, and values arrays. + * + * @return + * f_none on success. + * f_no_data when there is no buffer, objects or contents to process. + * f_error_reallocation (with error bit) on reallocation error. + * f_invalid_parameter (with error bit) if a parameter is invalid. + */ +#ifndef _di_fll_fss_snatch_ + extern f_return_status fll_fss_snatch(const f_string_dynamic buffer, const f_fss_objects objects, const f_fss_contents contents, const f_string names[], const f_string_length lengths[], f_string_dynamic *values[], const f_string_length size); +#endif // _di_fll_fss_snatch_ + +/** + * Perform simple search through all objects against the given set, saving all values when matched. + * + * This will append all duplicates using the provided glue. + * This will trim the object names when comparing (removing leading/trailing whitespace). + * This will strip NULL charactes when copying. + * + * This performs only a simple search algorithm that should be acceptable for small sets where performance is generally not a concern. + * + * @param buffer + * The buffer to read from. + * @param objects + * This object mappings to process. + * @param contents + * This content mappings to process. + * @param names + * An array of strings to "snatch" from the buffer. + * Each of these must be of type f_string. + * @param lengths + * An array of lengths for each names string. + * Each of these must be of type f_string_length. + * @param values + * An array of values where "snatched" content is stored. + * Each of these must be of type f_string_dynamic. + * @param size + * The total size of the names, lengths, and values arrays. + * @param glue + * A string to append between each duplicate name found when "snatching". + * @param glue_length + * The length of the glue string. + * + * @return + * f_none on success. + * f_no_data when there is no buffer, objects or contents to process. + * f_error_reallocation (with error bit) on reallocation error. + * f_invalid_parameter (with error bit) if a parameter is invalid. + */ +#ifndef _di_fll_fss_snatch_mash_ + extern f_return_status fll_fss_snatch_mash(const f_string_dynamic buffer, const f_fss_objects objects, const f_fss_contents contents, const f_string names[], const f_string_length lengths[], f_string_dynamic *values[], const f_string_length size, const f_string glue, const f_string_length glue_length); +#endif // _di_fll_fss_snatch_mash_ + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _FLL_fss_h diff --git a/level_2/fll_fss/data/build/settings b/level_2/fll_fss/data/build/settings index fae4f81..1f4577f 100644 --- a/level_2/fll_fss/data/build/settings +++ b/level_2/fll_fss/data/build/settings @@ -11,9 +11,9 @@ build_compiler gcc build_linker ar build_libraries -lc build_libraries_fll -lfl_string -lfl_status -lfl_fss -lf_file -lf_utf -lf_conversion -lf_memory -build_sources_library fss_basic.c fss_basic_list.c fss_extended.c fss_extended_list.c fss_status.c +build_sources_library fss.c fss_basic.c fss_basic_list.c fss_extended.c fss_extended_list.c fss_status.c build_sources_program -build_sources_headers fss_basic.h fss_basic_list.h fss_extended.h fss_extended_list.h fss_status.h +build_sources_headers fss.h fss_basic.h fss_basic_list.h fss_extended.h fss_extended_list.h fss_status.h build_sources_bash build_sources_settings build_shared yes -- 1.8.3.1