From: Kevin Day Date: Sat, 27 Jun 2020 04:33:04 +0000 (-0500) Subject: Progress: IKI Read. X-Git-Tag: 0.5.0~123 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=e7b881bf8aa7b6e2128919097caa74eab9911925;p=fll Progress: IKI Read. Implement the basics of raw mode. Raw mode should instead work in conjunction with the content, literal, and object modes so redesign accordingly. Add some additional documentation to the help section. Expand and Substitute are mostly ignored thus far and will be implemented at a later time. The substitution will be copied from the argv as-is, so plan on using static strings instead of dynamic strings. There will be function argument cleaning to do once I determine how Expand and Substitute will be implemented. Until then, the arguments are fine as-is. --- diff --git a/level_3/iki_read/c/iki_read.c b/level_3/iki_read/c/iki_read.c index 5795d33..2b53777 100644 --- a/level_3/iki_read/c/iki_read.c +++ b/level_3/iki_read/c/iki_read.c @@ -22,18 +22,18 @@ extern "C" { fll_program_print_help_option(context, iki_read_short_at, iki_read_long_at, f_console_symbol_short_enable, f_console_symbol_long_enable, " Select variable at this numeric index."); fll_program_print_help_option(context, iki_read_short_line, iki_read_long_line, f_console_symbol_short_enable, f_console_symbol_long_enable, "Print only the variables at the given line."); fll_program_print_help_option(context, iki_read_short_name, iki_read_long_name, f_console_symbol_short_enable, f_console_symbol_long_enable, "Select variables with this name."); + fll_program_print_help_option(context, iki_read_short_raw, iki_read_long_raw, f_console_symbol_short_enable, f_console_symbol_long_enable, " Print raw of the data instead of just the variable data."); printf("%c", f_string_eol[0]); fll_program_print_help_option(context, iki_read_short_literal, iki_read_long_literal, f_console_symbol_short_enable, f_console_symbol_long_enable, "Print the entire variable instead of the content."); fll_program_print_help_option(context, iki_read_short_object, iki_read_long_object, f_console_symbol_short_enable, f_console_symbol_long_enable, " Print the variable name instead of the variable content."); - fll_program_print_help_option(context, iki_read_short_raw, iki_read_long_raw, f_console_symbol_short_enable, f_console_symbol_long_enable, " Print the raw data instead of the variable data."); fll_program_print_help_option(context, iki_read_short_total, iki_read_long_total, f_console_symbol_short_enable, f_console_symbol_long_enable, " Print the total number of variables."); printf("%c", f_string_eol[0]); fll_program_print_help_option(context, iki_read_short_substitute, iki_read_long_substitute, f_console_symbol_short_enable, f_console_symbol_long_enable,"Substitute the entire variable for the given name and content value with the given string."); - fll_program_print_help_option(context, iki_read_short_expand, iki_read_long_expand, f_console_symbol_short_enable, f_console_symbol_long_enable, " Expand all values."); + fll_program_print_help_option(context, iki_read_short_expand, iki_read_long_expand, f_console_symbol_short_enable, f_console_symbol_long_enable, " Expand variables into their respective content."); fll_program_print_help_usage(context, iki_read_name, "filename(s)"); @@ -41,7 +41,7 @@ extern "C" { printf("%c", f_string_eol[0], f_string_eol[0]); - printf(" This program will find and print all content following the IKI standard, without focusing on any particular vocabulary specification.%c", f_string_eol[0]); + printf(" This program will find and print variables, vocabularies, or content following the IKI standard, without focusing on any particular vocabulary specification.%c", f_string_eol[0]); printf("%c", f_string_eol[0]); @@ -77,6 +77,18 @@ extern "C" { printf("%c", f_string_eol[0]); + printf(" All substitution is applied before any expansion when both the ", f_string_eol[0]); + fl_color_print(f_type_output, context.notable, context.reset, "%s%s", f_console_symbol_long_enable, iki_read_long_substitute); + printf(" option and the "); + fl_color_print(f_type_output, context.notable, context.reset, "%s%s", f_console_symbol_long_enable, iki_read_long_expand); + printf(" option are specified.%c", f_string_eol[0]); + + printf("%c", f_string_eol[0]); + + printf(" The default behavior is to only display content portion of the IKI variable.%c", f_string_eol[0]); + + printf("%c", f_string_eol[0]); + return F_none; } #endif // _di_iki_read_print_help_ @@ -272,19 +284,6 @@ extern "C" { status = F_status_set_error(F_parameter); } - if (data->parameters[iki_read_parameter_raw].result == f_console_result_found) { - if (data->verbosity != iki_read_verbosity_quiet) { - fprintf(f_type_error, "%c", f_string_eol[0]); - fl_color_print(f_type_error, data->context.error, data->context.reset, "ERROR: Cannot specify the '"); - fl_color_print(f_type_error, data->context.notable, data->context.reset, "%s%s", f_console_symbol_long_enable, iki_read_long_literal); - fl_color_print(f_type_error, data->context.error, data->context.reset, "' parameter with the '"); - fl_color_print(f_type_error, data->context.notable, data->context.reset, "%s%s", f_console_symbol_long_enable, iki_read_long_raw); - fl_color_print_line(f_type_error, data->context.error, data->context.reset, "' parameter."); - } - - status = F_status_set_error(F_parameter); - } - if (data->parameters[iki_read_parameter_total].result == f_console_result_found) { if (data->verbosity != iki_read_verbosity_quiet) { fprintf(f_type_error, "%c", f_string_eol[0]); @@ -301,19 +300,6 @@ extern "C" { data->mode = iki_read_mode_literal; } else if (data->parameters[iki_read_parameter_object].result == f_console_result_found) { - if (data->parameters[iki_read_parameter_raw].result == f_console_result_found) { - if (data->verbosity != iki_read_verbosity_quiet) { - fprintf(f_type_error, "%c", f_string_eol[0]); - fl_color_print(f_type_error, data->context.error, data->context.reset, "ERROR: Cannot specify the '"); - fl_color_print(f_type_error, data->context.notable, data->context.reset, "%s%s", f_console_symbol_long_enable, iki_read_long_object); - fl_color_print(f_type_error, data->context.error, data->context.reset, "' parameter with the '"); - fl_color_print(f_type_error, data->context.notable, data->context.reset, "%s%s", f_console_symbol_long_enable, iki_read_long_raw); - fl_color_print_line(f_type_error, data->context.error, data->context.reset, "' parameter."); - } - - status = F_status_set_error(F_parameter); - } - if (data->parameters[iki_read_parameter_total].result == f_console_result_found) { if (data->verbosity != iki_read_verbosity_quiet) { fprintf(f_type_error, "%c", f_string_eol[0]); @@ -329,7 +315,14 @@ extern "C" { data->mode = iki_read_mode_object; } - else if (data->parameters[iki_read_parameter_raw].result == f_console_result_found) { + else if (data->parameters[iki_read_parameter_total].result == f_console_result_found) { + data->mode = iki_read_mode_total; + } + else { + data->mode = iki_read_mode_content; + } + + if (data->parameters[iki_read_parameter_raw].result == f_console_result_found) { if (data->parameters[iki_read_parameter_total].result == f_console_result_found) { if (data->verbosity != iki_read_verbosity_quiet) { fprintf(f_type_error, "%c", f_string_eol[0]); @@ -342,14 +335,6 @@ extern "C" { status = F_status_set_error(F_parameter); } - - data->mode = iki_read_mode_raw; - } - else if (data->parameters[iki_read_parameter_total].result == f_console_result_found) { - data->mode = iki_read_mode_total; - } - else { - data->mode = iki_read_mode_content; } if (F_status_is_error(status)) { diff --git a/level_3/iki_read/c/iki_read.h b/level_3/iki_read/c/iki_read.h index 4838005..e7a6cf1 100644 --- a/level_3/iki_read/c/iki_read.h +++ b/level_3/iki_read/c/iki_read.h @@ -65,7 +65,6 @@ extern "C" { iki_read_mode_content = 1, iki_read_mode_literal, iki_read_mode_object, - iki_read_mode_raw, iki_read_mode_total, }; @@ -145,27 +144,24 @@ extern "C" { #define iki_read_replacement_with "with" typedef struct { - f_string_dynamic vocabulary; - f_string_dynamic replace; - f_string_dynamic with; + f_string_static vocabulary; + f_string_static replace; + f_string_static with; } iki_read_replacement; #define iki_read_replacement_initialize \ { \ - f_string_dynamic_initialize, \ - f_string_dynamic_initialize, \ - f_string_dynamic_initialize, \ + f_string_static_initialize, \ + f_string_static_initialize, \ + f_string_static_initialize, \ } - #define macro_iki_read_replacement_delete_simple(replacement) \ - f_macro_string_dynamic_delete_simple(replacement.vocabulary); \ - f_macro_string_dynamic_delete_simple(replacement.replace); \ - f_macro_string_dynamic_delete_simple(replacement.with); - - #define macro_iki_read_replacement_destroy_simple(replacement) \ - f_macro_string_dynamic_destroy_simple(replacement.vocabulary); \ - f_macro_string_dynamic_destroy_simple(replacement.replace); \ - f_macro_string_dynamic_destroy_simple(replacement.with); + #define macro_iki_read_replacement_initialize(vocabulary, replace, with) \ + { \ + f_macro_string_static_initialize(vocabulary), \ + f_macro_string_static_initialize(replace), \ + f_macro_string_static_initialize(with), \ + } #endif // _di_iki_read_replacement_ #ifndef _di_iki_read_replacements_ @@ -186,7 +182,6 @@ extern "C" { replacements.used = replacements.size; \ while (replacements.used > 0) { \ replacements.used--; \ - macro_iki_read_replacement_delete_simple(replacements.array[replacements.used]); \ } \ if (replacements.used == 0) f_macro_memory_structure_delete_simple(replacements, iki_read_replacement) @@ -194,7 +189,6 @@ extern "C" { replacements.used = replacements.size; \ while (replacements.used > 0) { \ replacements.used--; \ - macro_iki_read_replacement_destroy_simple(replacements.array[replacements.used]); \ } \ if (replacements.used == 0) f_macro_memory_structure_destroy_simple(replacements, iki_read_replacement) @@ -203,7 +197,6 @@ extern "C" { if (new_length < replacements.size) { \ f_array_length i = replacements.size - new_length; \ for (; i < replacements.size; i++) { \ - macro_iki_read_replacement_delete(status, replacements.array[i]); \ if (status != F_none) break; \ } \ } \ @@ -224,7 +217,6 @@ extern "C" { if (new_length < replacements.size) { \ f_array_length i = replacements.size - new_length; \ for (; i < replacements.size; i++) { \ - macro_iki_read_replacement_delete(status, replacements.array[i]); \ if (status != F_none) break; \ } \ } \ diff --git a/level_3/iki_read/c/private-iki_read.c b/level_3/iki_read/c/private-iki_read.c index 8fd5ff3..f95e45a 100644 --- a/level_3/iki_read/c/private-iki_read.c +++ b/level_3/iki_read/c/private-iki_read.c @@ -310,21 +310,47 @@ extern "C" { f_iki_vocabulary vocabulary = f_iki_vocabulary_initialize; f_iki_content content = f_iki_content_initialize; - if (data->mode == iki_read_mode_content) { - status = iki_read_process_buffer_ranges(arguments, file_name, data, &variable, &vocabulary, &content, &content); - } - else if (data->mode == iki_read_mode_literal) { - status = iki_read_process_buffer_ranges(arguments, file_name, data, &variable, &vocabulary, &content, &variable); - } - else if (data->mode == iki_read_mode_object) { - status = iki_read_process_buffer_ranges(arguments, file_name, data, &variable, &vocabulary, &content, &vocabulary); - } - else if (data->mode == iki_read_mode_raw) { - // @todo + if (data->parameters[iki_read_parameter_raw].result == f_console_result_found) { + f_string_range buffer_range = f_macro_string_range_initialize(data->buffer.used); + + if (iki_read_process_at(arguments, file_name, data, &buffer_range)) { + if (buffer_range.start > data->buffer.used) { + return F_data_not; + } + } + + if (data->mode == iki_read_mode_content) { + status = iki_read_process_buffer_ranges_raw(arguments, file_name, buffer_range, data, &variable, &vocabulary, &content, &content); + } + else if (data->mode == iki_read_mode_literal) { + status = iki_read_process_buffer_ranges_raw(arguments, file_name, buffer_range, data, &variable, &vocabulary, &content, &variable); + } + else if (data->mode == iki_read_mode_object) { + status = iki_read_process_buffer_ranges_raw(arguments, file_name, buffer_range, data, &variable, &vocabulary, &content, &vocabulary); + } } else if (data->mode == iki_read_mode_total) { status = iki_read_process_buffer_total(arguments, file_name, data, &variable, &vocabulary, &content); } + else { + f_string_range buffer_range = f_macro_string_range_initialize(data->buffer.used); + + if (iki_read_process_at(arguments, file_name, data, &buffer_range)) { + if (buffer_range.start > data->buffer.used) { + return F_data_not; + } + } + + if (data->mode == iki_read_mode_content) { + status = iki_read_process_buffer_ranges(arguments, file_name, data, &buffer_range, &variable, &vocabulary, &content, &content); + } + else if (data->mode == iki_read_mode_literal) { + status = iki_read_process_buffer_ranges(arguments, file_name, data, &buffer_range, &variable, &vocabulary, &content, &variable); + } + else if (data->mode == iki_read_mode_object) { + status = iki_read_process_buffer_ranges(arguments, file_name, data, &buffer_range, &variable, &vocabulary, &content, &vocabulary); + } + } f_macro_iki_variable_delete_simple(variable); f_macro_iki_vocabulary_delete_simple(vocabulary); @@ -335,19 +361,12 @@ extern "C" { #endif // _di_iki_read_process_buffer_ #ifndef _di_iki_read_process_buffer_ranges_ - f_return_status iki_read_process_buffer_ranges(const f_console_arguments arguments, const f_string file_name, iki_read_data *data, f_iki_variable *variable, f_iki_vocabulary *vocabulary, f_iki_content *content, f_string_ranges *ranges) { + f_return_status iki_read_process_buffer_ranges(const f_console_arguments arguments, const f_string file_name, iki_read_data *data, f_string_range *buffer_range, f_iki_variable *variable, f_iki_vocabulary *vocabulary, f_iki_content *content, f_string_ranges *ranges) { f_status status = F_none; - f_string_range range = f_macro_string_range_initialize(data->buffer.used); bool unmatched = F_true; - if (iki_read_process_at(arguments, file_name, data, &range)) { - if (range.start > data->buffer.used) { - return F_data_not; - } - } - - status = fl_iki_read(&data->buffer, &range, variable, vocabulary, content); + status = fl_iki_read(&data->buffer, buffer_range, variable, vocabulary, content); if (F_status_is_error(status)) { iki_read_print_error(data->context, data->verbosity, F_status_set_fine(status), "fl_iki_read", F_true); return status; @@ -360,7 +379,7 @@ extern "C" { f_array_length i = 0; f_array_length j = 0; - range.start = 0; + buffer_range->start = 0; for (; i < data->parameters[iki_read_parameter_name].additional.used; i++) { index = data->parameters[iki_read_parameter_name].additional.array[i]; @@ -374,10 +393,10 @@ extern "C" { return status; } - range.stop = name.used - 1; + buffer_range->stop = name.used - 1; for (j = 0; j < vocabulary->used; j++) { - status = fl_string_dynamic_partial_compare(name, data->buffer, range, vocabulary->array[j]); + status = fl_string_dynamic_partial_compare(name, data->buffer, *buffer_range, vocabulary->array[j]); if (status == F_equal_to) { unmatched = F_false; @@ -406,6 +425,58 @@ extern "C" { } #endif // _di_iki_read_process_buffer_ranges_ +#ifndef _di_iki_read_process_buffer_ranges_raw_ + f_return_status iki_read_process_buffer_ranges_raw(const f_console_arguments arguments, const f_string file_name, const f_string_range buffer_range, iki_read_data *data, f_iki_variable *variable, f_iki_vocabulary *vocabulary, f_iki_content *content, f_string_ranges *ranges) { + f_status status = F_none; + f_string_range range = buffer_range; + + status = fl_iki_read(&data->buffer, &range, variable, vocabulary, content); + if (F_status_is_error(status)) { + iki_read_print_error(data->context, data->verbosity, F_status_set_fine(status), "fl_iki_read", F_true); + return status; + } + + if (data->parameters[iki_read_parameter_name].result == f_console_result_additional) { + // @todo (may be better to merge this logic in the block below.) + } + else if (variable->used) { + f_string_length i = buffer_range.start; + f_array_length j = 0; + + range = buffer_range; + + while (i <= range.stop && j < variable->used) { + if (i < variable->array[j].start) { + range.stop = variable->array[j].start - 1; + + f_print_string_dynamic_partial(f_type_output, data->buffer, range); + + range.start = variable->array[j].stop + 1; + range.stop = buffer_range.stop; + + i = variable->array[j].start; + } + + // @todo handle expansion and substitution. + f_print_string_dynamic_partial(f_type_output, data->buffer, ranges->array[j]); + + i = variable->array[j].stop + 1; + j++; + } // while + + if (i <= buffer_range.stop) { + range.start = i; + f_print_string_dynamic_partial(f_type_output, data->buffer, range); + } + } + else { + f_print_string_dynamic_partial(f_type_output, data->buffer, buffer_range); + } + + return F_none; + } +#endif // _di_iki_read_process_buffer_ranges_raw_ + #ifndef _di_iki_read_process_buffer_total_ f_return_status iki_read_process_buffer_total(const f_console_arguments arguments, const f_string file_name, iki_read_data *data, f_iki_variable *variable, f_iki_vocabulary *vocabulary, f_iki_content *content) { f_status status = F_none; diff --git a/level_3/iki_read/c/private-iki_read.h b/level_3/iki_read/c/private-iki_read.h index d132daa..71e1b5b 100644 --- a/level_3/iki_read/c/private-iki_read.h +++ b/level_3/iki_read/c/private-iki_read.h @@ -142,6 +142,8 @@ extern "C" { * The name of the file being processed. * @param data * The program specific data. + * @param buffer_range + * The range within the buffer to process. * @param variable * The ranges representing a variable. * @param vocabulary @@ -159,10 +161,43 @@ extern "C" { * Status codes (with error bit) are returned on any problem. */ #ifndef _di_iki_read_process_buffer_ranges_ - extern f_return_status iki_read_process_buffer_ranges(const f_console_arguments arguments, const f_string file_name, iki_read_data *data, f_iki_variable *variable, f_iki_vocabulary *vocabulary, f_iki_content *content, f_string_ranges *ranges) f_gcc_attribute_visibility_internal; + extern f_return_status iki_read_process_buffer_ranges(const f_console_arguments arguments, const f_string file_name, iki_read_data *data, f_string_range *buffer_range, f_iki_variable *variable, f_iki_vocabulary *vocabulary, f_iki_content *content, f_string_ranges *ranges) f_gcc_attribute_visibility_internal; #endif // _di_iki_read_process_buffer_ranges_ /** + * Process a given buffer, printing the given buffer in raw mode based on the given ranges. + * + * The entire variable is replaced with the range from the associated ranges. + * + * @param arguments + * The console arguments passed to the program. + * @param file_name + * The name of the file being processed. + * @param buffer_range + * The range within the buffer to process. + * @param data + * The program specific data. + * @param variable + * The ranges representing a variable. + * @param vocabulary + * The ranges representing a vocabulary. + * @param content + * The ranges representing content. + * @param ranges + * The ranges to print when matched. + * Should be one of: variable, vocabulary, or content. + * + * @return + * F_none on success. + * F_data_not on success, but nothing to print. + * + * Status codes (with error bit) are returned on any problem. + */ +#ifndef _di_iki_read_process_buffer_ranges_raw_ + extern f_return_status iki_read_process_buffer_ranges_raw(const f_console_arguments arguments, const f_string file_name, const f_string_range buffer_range, iki_read_data *data, f_iki_variable *variable, f_iki_vocabulary *vocabulary, f_iki_content *content, f_string_ranges *ranges) f_gcc_attribute_visibility_internal; +#endif // _di_iki_read_process_buffer_ranges_raw_ + +/** * Process a given buffer, printing the total. * * @param arguments