From 0924f9269fc216667d732e78f406c53453792eb0 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sat, 23 Nov 2024 22:43:40 -0600 Subject: [PATCH] Update: Switch iki_write to using f_iki_write(). The old iki_write is using the simple processing functions. Switch to using the dedicated `f_iki_write()` function. Switch to using the main cache design. --- level_3/iki_write/c/main/common.c | 4 +++ level_3/iki_write/c/main/common/print.c | 3 +- level_3/iki_write/c/main/common/print.h | 3 +- level_3/iki_write/c/main/common/type.c | 19 ++++++++--- level_3/iki_write/c/main/common/type.h | 58 ++++++++++++++++++++++++--------- level_3/iki_write/c/main/iki_write.c | 35 +++++++++----------- level_3/iki_write/c/main/iki_write.h | 8 ++--- level_3/iki_write/c/main/print/error.c | 2 +- level_3/iki_write/c/main/process.c | 36 +++++++------------- level_3/iki_write/c/main/process.h | 11 +++---- 10 files changed, 99 insertions(+), 80 deletions(-) diff --git a/level_3/iki_write/c/main/common.c b/level_3/iki_write/c/main/common.c index 59b4cd5..cf62d63 100644 --- a/level_3/iki_write/c/main/common.c +++ b/level_3/iki_write/c/main/common.c @@ -174,6 +174,10 @@ extern "C" { if (main->program.parameters.array[iki_write_parameter_wrap_e].result & f_console_result_found_e) { main->setting.flag |= iki_write_main_flag_wrap_d; + main->setting.state.flag |= f_iki_state_flag_force_wrap_e; + } + else { + main->setting.state.flag &= ~f_iki_state_flag_force_wrap_e; } main->setting.quote = f_iki_syntax_quote_double_s; diff --git a/level_3/iki_write/c/main/common/print.c b/level_3/iki_write/c/main/common/print.c index 44d8002..d22642b 100644 --- a/level_3/iki_write/c/main/common/print.c +++ b/level_3/iki_write/c/main/common/print.c @@ -9,12 +9,11 @@ extern "C" { "f_console_parameter_process", "f_file_read", "f_file_stream_open", - "f_iki_object_is", + "f_iki_write", "f_memory_arrays_resize", "f_string_dynamic_partial_append_nulless", "f_string_dynamic_seek_to", "f_thread_create", - "fll_iki_content_escape", "fll_program_parameter_process_context_standard", "fll_program_parameter_process_verbosity_standard", }; diff --git a/level_3/iki_write/c/main/common/print.h b/level_3/iki_write/c/main/common/print.h index 1121f2d..bba8c09 100644 --- a/level_3/iki_write/c/main/common/print.h +++ b/level_3/iki_write/c/main/common/print.h @@ -42,12 +42,11 @@ extern "C" { iki_write_f_f_console_parameter_process_e, iki_write_f_f_file_read_e, iki_write_f_f_file_stream_open_e, - iki_write_f_f_iki_object_is_e, + iki_write_f_f_iki_write_e, iki_write_f_f_memory_arrays_resize_e, iki_write_f_f_string_dynamic_partial_append_nulless_e, iki_write_f_f_string_dynamic_seek_to_e, iki_write_f_f_thread_create_e, - iki_write_f_fll_iki_content_escape_e, iki_write_f_fll_program_parameter_process_context_standard_e, iki_write_f_fll_program_parameter_process_verbosity_standard_e, }; // enum diff --git a/level_3/iki_write/c/main/common/type.c b/level_3/iki_write/c/main/common/type.c index ed5f528..11f9da4 100644 --- a/level_3/iki_write/c/main/common/type.c +++ b/level_3/iki_write/c/main/common/type.c @@ -4,12 +4,26 @@ extern "C" { #endif +#ifndef _di_iki_write_cache_delete_ + void iki_write_cache_delete(iki_write_cache_t * const cache) { + + if (!cache) return; + + f_memory_array_resize(0, sizeof(f_char_t), (void **) &cache->buffer.string, &cache->buffer.used, &cache->buffer.size); + f_memory_array_resize(0, sizeof(f_char_t), (void **) &cache->content.string, &cache->content.used, &cache->content.size); + f_memory_array_resize(0, sizeof(f_char_t), (void **) &cache->iki.string, &cache->iki.used, &cache->iki.size); + f_memory_array_resize(0, sizeof(f_char_t), (void **) &cache->object.string, &cache->object.used, &cache->object.size); + } +#endif // _di_iki_write_cache_delete_ + #ifndef _di_iki_write_main_delete_ void iki_write_main_delete(iki_write_main_t * const main) { if (!main) return; fll_program_data_delete(&main->program); + + iki_write_cache_delete(&main->cache); iki_write_setting_delete(&main->setting); } #endif // _di_iki_write_main_delete_ @@ -19,11 +33,6 @@ extern "C" { if (!setting) return; - f_memory_array_resize(0, sizeof(f_char_t), (void **) &setting->escaped.string, &setting->escaped.used, &setting->escaped.size); - f_memory_array_resize(0, sizeof(f_char_t), (void **) &setting->buffer.string, &setting->buffer.used, &setting->buffer.size); - f_memory_array_resize(0, sizeof(f_char_t), (void **) &setting->object.string, &setting->object.used, &setting->object.size); - f_memory_array_resize(0, sizeof(f_char_t), (void **) &setting->content.string, &setting->content.used, &setting->content.size); - f_memory_arrays_resize(0, sizeof(f_string_dynamic_t), (void **) &setting->objects.array, &setting->objects.used, &setting->objects.size, &f_string_dynamics_delete_callback); f_memory_arrays_resize(0, sizeof(f_string_dynamic_t), (void **) &setting->contents.array, &setting->contents.used, &setting->contents.size, &f_string_dynamics_delete_callback); } diff --git a/level_3/iki_write/c/main/common/type.h b/level_3/iki_write/c/main/common/type.h index d684511..35d4f69 100644 --- a/level_3/iki_write/c/main/common/type.h +++ b/level_3/iki_write/c/main/common/type.h @@ -17,6 +17,31 @@ extern "C" { #endif /** + * The iki write main program cache. + * + * buffer: A buffer used during processing the file. + * content: A buffer used to hold a Content during processing. + * iki: A buffer used for writing the IKI during processing. + * object: A buffer used to hold an Object during processing. + */ +#ifndef _di_iki_write_setting_t_ + typedef struct { + f_string_dynamic_t buffer; + f_string_dynamic_t content; + f_string_dynamic_t iki; + f_string_dynamic_t object; + } iki_write_cache_t; + + #define iki_write_cache_t_initialize \ + { \ + f_string_dynamic_t_initialize, \ + f_string_dynamic_t_initialize, \ + f_string_dynamic_t_initialize, \ + f_string_dynamic_t_initialize, \ + } +#endif // _di_iki_write_cache_t_ + +/** * The iki write main program settings. * * This is passed to the program-specific main entry point to designate program settings. @@ -29,11 +54,6 @@ extern "C" { * * quote: This holds the quote used during processing. * - * escaped: A buffer used for escaping strings during processing. - * buffer: A buffer used during processing the file. - * object: A buffer used to hold an Object during processing. - * content: A buffer used to hold a Content during processing. - * * objects: An array of objects passed as values to the "--object" parameter. * contents: An array of objects passed as values to the "--content" parameter and must match the length of objects. */ @@ -46,11 +66,6 @@ extern "C" { f_string_static_t quote; - f_string_dynamic_t escaped; - f_string_dynamic_t buffer; - f_string_dynamic_t object; - f_string_dynamic_t content; - f_string_dynamics_t objects; f_string_dynamics_t contents; } iki_write_setting_t; @@ -61,10 +76,6 @@ extern "C" { F_okay, \ f_state_t_initialize, \ f_string_static_t_initialize, \ - f_string_dynamic_t_initialize, \ - f_string_dynamic_t_initialize, \ - f_string_dynamic_t_initialize, \ - f_string_dynamic_t_initialize, \ f_string_dynamics_t_initialize, \ f_string_dynamics_t_initialize, \ } @@ -73,23 +84,40 @@ extern "C" { /** * The main program data as a single structure. * + * cache: The cache data. * program: The main program data. * setting: The settings data. */ #ifndef _di_iki_write_main_t_ typedef struct { + iki_write_cache_t cache; fll_program_data_t program; iki_write_setting_t setting; } iki_write_main_t; #define iki_write_main_t_initialize \ { \ + iki_write_cache_t_initialize, \ fll_program_data_t_initialize, \ iki_write_setting_t_initialize, \ } #endif // _di_iki_write_main_t_ /** + * Delete the program main cache data. + * + * @param cache + * The program main cache data. + * + * Must not be NULL. + * + * @see f_memory_array_resize() + */ +#ifndef _di_iki_write_cache_delete_ + extern void iki_write_cache_delete(iki_write_cache_t * const cache); +#endif // _di_iki_write_cache_delete_ + +/** * De-allocate main program data. * * @param main @@ -100,6 +128,7 @@ extern "C" { * This does not alter main.setting.state.status. * * @see fll_program_data_delete() + * @see iki_write_cache_delete() * @see iki_write_setting_delete() */ #ifndef _di_iki_write_main_delete_ @@ -116,7 +145,6 @@ extern "C" { * * This does not alter setting.state.status. * - * @see f_memory_array_resize() * @see f_memory_arrays_resize() */ #ifndef _di_iki_write_setting_delete_ diff --git a/level_3/iki_write/c/main/iki_write.c b/level_3/iki_write/c/main/iki_write.c index c7dc893..f5c0ea6 100644 --- a/level_3/iki_write/c/main/iki_write.c +++ b/level_3/iki_write/c/main/iki_write.c @@ -25,11 +25,8 @@ extern "C" { return; } - main->setting.escaped.used = 0; - if (main->setting.flag & iki_write_main_flag_pipe_d) { bool object_ended = F_false; - f_number_unsigned_t previous = 0; f_range_t range = f_range_t_initialize; f_status_t status = F_okay; @@ -38,9 +35,9 @@ extern "C" { pipe.id = F_type_descriptor_input_d; pipe.size_read = 1; - main->setting.buffer.used = 0; - main->setting.object.used = 0; - main->setting.content.used = 0; + main->cache.buffer.used = 0; + main->cache.object.used = 0; + main->cache.content.used = 0; range.start = 0; @@ -58,7 +55,7 @@ extern "C" { } if (status != F_okay_eof) { - status = f_file_read(pipe, &main->setting.buffer); + status = f_file_read(pipe, &main->cache.buffer); if (F_status_is_error(status)) { main->setting.state.status = F_status_set_error(F_pipe); @@ -70,7 +67,7 @@ extern "C" { break; } - if (!main->setting.buffer.used) { + if (!main->cache.buffer.used) { main->setting.state.status = F_status_set_error(F_parameter); fll_program_print_error_pipe_missing_content(&main->program.error); @@ -80,11 +77,11 @@ extern "C" { break; } - range.stop = main->setting.buffer.used - 1; + range.stop = main->cache.buffer.used - 1; } previous = range.start; - main->setting.state.status = f_string_dynamic_seek_to(main->setting.buffer, f_string_ascii_feed_form_s.string[0], &range); + main->setting.state.status = f_string_dynamic_seek_to(main->cache.buffer, f_string_ascii_feed_form_s.string[0], &range); if (main->setting.state.status == F_data_not_stop) { main->setting.state.status = F_status_set_error(F_parameter); @@ -112,10 +109,10 @@ extern "C" { range.start = previous; if (object_ended) { - main->setting.content.used = 0; + main->cache.content.used = 0; - if (main->setting.buffer.used) { - main->setting.state.status = f_string_dynamic_partial_append_nulless(main->setting.buffer, range, &main->setting.content); + if (main->cache.buffer.used) { + main->setting.state.status = f_string_dynamic_partial_append_nulless(main->cache.buffer, range, &main->cache.content); if (F_status_is_error(main->setting.state.status)) { iki_write_print_error(&main->program.error, macro_iki_write_f(f_string_dynamic_partial_append_nulless)); @@ -126,7 +123,7 @@ extern "C" { } } - iki_write_process(main, main->setting.object, main->setting.content); + iki_write_process(main, main->cache.object, main->cache.content); if (F_status_is_error(main->setting.state.status)) { object_ended = F_false; @@ -139,9 +136,9 @@ extern "C" { object_ended = F_false; } else { - main->setting.object.used = 0; + main->cache.object.used = 0; - main->setting.state.status = f_string_dynamic_partial_append_nulless(main->setting.buffer, range, &main->setting.object); + main->setting.state.status = f_string_dynamic_partial_append_nulless(main->cache.buffer, range, &main->cache.object); if (F_status_is_error(main->setting.state.status)) { iki_write_print_error(&main->program.error, macro_iki_write_f(f_string_dynamic_partial_append_nulless)); @@ -156,15 +153,15 @@ extern "C" { // Restore the range, positioned after the new line. range.start = range.stop + 2; - range.stop = main->setting.buffer.used - 1; + range.stop = main->cache.buffer.used - 1; // Only clear the buffer and reset the start when the entire buffer has been processed. if (range.start > range.stop) { range.start = 0; - main->setting.buffer.used = 0; + main->cache.buffer.used = 0; } - } while (status != F_okay_eof || main->setting.buffer.used || object_ended); + } while (status != F_okay_eof || main->cache.buffer.used || object_ended); if (object_ended) { main->setting.state.status = F_status_set_error(F_parameter); diff --git a/level_3/iki_write/c/main/iki_write.h b/level_3/iki_write/c/main/iki_write.h index 006a668..192f656 100644 --- a/level_3/iki_write/c/main/iki_write.h +++ b/level_3/iki_write/c/main/iki_write.h @@ -88,18 +88,14 @@ extern "C" { * * This alters main.setting.state.status: * F_okay on success. - * F_true on success when performing verification and verify passed. - * F_false on success when performing verification and verify failed. * F_interrupt on (exit) signal received. * * F_failure (with error bit) on processing failure. * F_parameter (with error bit) if main is NULL or setting is NULL. * - * Errors (with error bit) from: f_iki_object_is(). - * Errors (with error bit) from: fll_iki_content_escape(). + * Errors (with error bit) from: iki_write_process(). * - * @see f_iki_object_is() - * @see fll_iki_content_escape() + * @see iki_write_process() */ #ifndef _di_iki_write_main_ extern void iki_write_main(iki_write_main_t * const main); diff --git a/level_3/iki_write/c/main/print/error.c b/level_3/iki_write/c/main/print/error.c index 85e88af..fa389dc 100644 --- a/level_3/iki_write/c/main/print/error.c +++ b/level_3/iki_write/c/main/print/error.c @@ -60,7 +60,7 @@ extern "C" { fl_print_format("%[%QThe object '%]", print->to, print->set->error, print->prefix, print->set->error); fl_print_format(f_string_format_Q_single_s.string, print->to, print->set->notable, object, print->set->notable); - fl_print_format("%[' is not a valid IKI Object.%]%r", print->to, print->set->error, print->set->error, f_string_eol_s); + fl_print_format("%[' is not a valid IKI Object (IKI Vocabulary).%]%r", print->to, print->set->error, print->set->error, f_string_eol_s); f_file_stream_unlock(print->to); diff --git a/level_3/iki_write/c/main/process.c b/level_3/iki_write/c/main/process.c index 8cd8ce1..3fd6316 100644 --- a/level_3/iki_write/c/main/process.c +++ b/level_3/iki_write/c/main/process.c @@ -7,6 +7,8 @@ extern "C" { #ifndef _di_iki_write_process_ void iki_write_process(iki_write_main_t * const main, const f_string_static_t object, const f_string_static_t content) { + if (!main) return; + if (!object.used) { main->setting.state.status = F_status_set_error(F_failure); @@ -15,38 +17,24 @@ extern "C" { return; } - main->setting.state.status = f_iki_object_is(object); - - if (main->setting.state.status == F_false) { - main->setting.state.status = F_status_set_error(F_failure); + main->cache.iki.used = 0; - iki_write_print_error_object_not_valid(&main->program.error, object); - - return; - } + f_iki_write(object, content, main->setting.quote, &main->cache.iki, &main->setting.state); if (F_status_is_error(main->setting.state.status)) { - iki_write_print_error(&main->program.error, macro_iki_write_f(f_iki_object_is)); + if (F_status_set_fine(main->setting.state.status) == F_syntax) { + iki_write_print_error_object_not_valid(&main->program.error, object); + } + else { + iki_write_print_error(&main->program.error, macro_iki_write_f(f_iki_write)); + } return; } - main->setting.escaped.used = 0; - - main->setting.state.status = fll_iki_content_escape(content, main->setting.quote, &main->setting.escaped); + f_print_dynamic(main->cache.iki, main->program.output.to); - if (F_status_is_error(main->setting.state.status)) { - iki_write_print_error(&main->program.error, macro_iki_write_f(fll_iki_content_escape)); - - return; - } - - if (main->setting.flag & iki_write_main_flag_wrap_d) { - fl_print_format("%r%Q%r%r%r%Q%r", main->program.output.to, f_iki_syntax_wrap_open_s, object, f_iki_syntax_wrap_close_s, f_iki_syntax_separator_s, main->setting.quote, main->setting.escaped, main->setting.quote); - } - else { - fl_print_format("%Q%r%r%Q%r", main->program.output.to, object, f_iki_syntax_separator_s, main->setting.quote, main->setting.escaped, main->setting.quote); - } + main->setting.state.status = F_okay; } #endif // _di_iki_write_process_ diff --git a/level_3/iki_write/c/main/process.h b/level_3/iki_write/c/main/process.h index 485e877..59d5b50 100644 --- a/level_3/iki_write/c/main/process.h +++ b/level_3/iki_write/c/main/process.h @@ -17,7 +17,7 @@ extern "C" { #endif /** - * Process a given object and content, printing the IKI if valid or an error if invalid. + * Process a given object and content, printing it. * * @param main * The program and settings data. @@ -25,8 +25,7 @@ extern "C" { * This alters main.setting.state.status: * F_okay on success. * - * Errors (with error bit) from: f_iki_object_is(). - * Errors (with error bit) from: fll_iki_content_escape(). + * Errors (with error bit) from: f_iki_write(). * * F_failure (with error bit) for any other failure. * @param object @@ -34,11 +33,11 @@ extern "C" { * @param content * The content to escape and print. * - * @see f_iki_object_is() - * @see fll_iki_content_escape() + * @see iki_write_print_error_object_not_valid() + * @see f_iki_write() */ #ifndef _di_iki_write_process_ - extern void iki_write_process(iki_write_main_t * const main, const f_string_static_t object, const f_string_static_t content) F_attribute_visibility_internal_d; + extern void iki_write_process(iki_write_main_t * const main, const f_string_static_t object, const f_string_static_t content); #endif // _di_iki_write_process_ #ifdef __cplusplus -- 1.8.3.1