]> Kevux Git Server - fll/commitdiff
Update: add missing -f/--file processing to iki_write.
authorKevin Day <thekevinday@gmail.com>
Sat, 19 Sep 2020 04:09:21 +0000 (23:09 -0500)
committerKevin Day <thekevinday@gmail.com>
Sat, 19 Sep 2020 04:22:22 +0000 (23:22 -0500)
I completely forgot to finish this.

The file option should now write to a file in append mode if specified.

level_3/iki_write/c/iki_write.c
level_3/iki_write/c/private-iki_write.c
level_3/iki_write/c/private-iki_write.h

index 87cf4bf6c4ef48139967293af5b1fa1b7867edef..f9ee15c8bb1175ea695b44cf3c85276c086cacf3 100644 (file)
@@ -106,7 +106,45 @@ extern "C" {
       data->verbosity = iki_write_verbosity_normal;
     }
 
-    if (!data->process_pipe) {
+    f_file_t output = f_file_t_initialize;
+
+    output.id = f_type_descriptor_output;
+    output.flag = f_file_flag_create | f_file_flag_write_only | f_file_flag_append;
+
+    if (F_status_is_fine(status)) {
+      if (data->parameters[iki_write_parameter_file].result == f_console_result_additional) {
+        if (data->parameters[iki_write_parameter_file].additional.used > 1) {
+          if (data->verbosity != iki_write_verbosity_quiet) {
+            fl_color_print(f_type_error, data->context.error, data->context.reset, "ERROR: The parameter '");
+            fl_color_print(f_type_error, data->context.notable, data->context.reset, "%s%s", f_console_symbol_long_enable, iki_write_long_file);
+            fl_color_print_line(f_type_error, data->context.error, data->context.reset, "' may only be specified once.");
+          }
+
+          status = F_status_set_error(F_parameter);
+        }
+        else {
+          const f_string_length_t location = data->parameters[iki_write_parameter_file].additional.array[0];
+
+          output.id = -1;
+          status = f_file_open(arguments.argv[location], f_file_mode_all_rw, &output);
+
+          if (F_status_is_error(status)) {
+            iki_write_print_error_file(data->context, data->verbosity, F_status_set_fine(status), "f_file_open", arguments.argv[location], "open", 0, F_true);
+          }
+        }
+      }
+      else if (data->parameters[iki_write_parameter_file].result == f_console_result_found) {
+        if (data->verbosity != iki_write_verbosity_quiet) {
+          fl_color_print(f_type_error, data->context.error, data->context.reset, "ERROR: The parameter '");
+          fl_color_print(f_type_error, data->context.notable, data->context.reset, "%s%s", f_console_symbol_long_enable, iki_write_long_file);
+          fl_color_print_line(f_type_error, data->context.error, data->context.reset, "' was specified, but no value was given.");
+        }
+
+        status = F_status_set_error(F_parameter);
+      }
+    }
+
+    if (F_status_is_fine(status) && !data->process_pipe) {
       if (data->parameters[iki_write_parameter_object].result != f_console_result_additional && data->parameters[iki_write_parameter_content].result != f_console_result_additional) {
         if (data->verbosity != iki_write_verbosity_quiet) {
           fprintf(f_type_error, "%c", f_string_eol[0]);
@@ -232,10 +270,10 @@ extern "C" {
               }
             }
 
-            status = iki_write_process(*data, object, content, quote, f_type_output, &escaped);
+            status = iki_write_process(*data, object, content, quote, output.id, &escaped);
             if (F_status_is_error(status)) break;
 
-            fprintf(f_type_output, "%c", f_string_eol[0]);
+            dprintf(output.id, "%c", f_string_eol[0]);
 
             object_ended = F_false;
           }
@@ -292,14 +330,14 @@ extern "C" {
           content.used = strnlen(content.string, f_console_length_size);
           content.size = content.used;
 
-          status = iki_write_process(*data, object, content, quote, f_type_output, &escaped);
+          status = iki_write_process(*data, object, content, quote, output.id, &escaped);
           if (F_status_is_error(status)) break;
 
-          fprintf(f_type_output, "%c", f_string_eol[0]);
+          dprintf(output.id, "%c", f_string_eol[0]);
         } // for
 
         // ensure there is always a newline at the end, unless in quiet mode.
-        if (F_status_is_fine(status) && data->verbosity != iki_write_verbosity_quiet) {
+        if (F_status_is_fine(status) && data->verbosity != iki_write_verbosity_quiet && data->parameters[iki_write_parameter_file].result == f_console_result_none) {
           fprintf(f_type_output, "%c", f_string_eol[0]);
         }
       }
@@ -307,6 +345,12 @@ extern "C" {
       f_macro_string_dynamic_t_delete_simple(escaped);
     }
 
+    if (data->parameters[iki_write_parameter_file].result == f_console_result_additional) {
+      if (output.id != -1) {
+        f_file_close(&output.id);
+      }
+    }
+
     // ensure a newline is always put at the end of the program execution, unless in quiet mode.
     if (data->verbosity != iki_write_verbosity_quiet) {
       if (F_status_is_error(status)) {
index 7d5eea1506ef084cb3d777a3b496b22d18240a53..4746d6b8aad4a4fcf280a51fe76fca2ebf3cb0fc 100644 (file)
@@ -268,7 +268,7 @@ extern "C" {
 #endif // _di_iki_write_print_error_file_
 
 #ifndef _di_iki_write_process_
-  f_return_status iki_write_process(const iki_write_data_t data, const f_string_static_t object, const f_string_static_t content, const uint8_t quote, FILE *output, f_string_dynamic_t *escaped) {
+  f_return_status iki_write_process(const iki_write_data_t data, const f_string_static_t object, const f_string_static_t content, const uint8_t quote, const int output, f_string_dynamic_t *escaped) {
 
     if (!object.used) {
       if (data.verbosity != iki_write_verbosity_quiet) {
@@ -314,10 +314,7 @@ extern "C" {
       return F_status_set_error(F_failure);
     }
 
-    f_print_string_dynamic(output, object);
-    fprintf(output, "%c%c", f_iki_syntax_separator, quote);
-    f_print_string_dynamic(output, *escaped);
-    fprintf(output, "%c", quote);
+    dprintf(output, "%s%c%c%s%c", object.string, f_iki_syntax_separator, quote, escaped->string, quote);
 
     return F_none;
   }
index d1fec29830276972f1a3333ed4cf707ce695c8e3..88bccdb0bd93676d4b6bc73841ca8d5841cac59a 100644 (file)
@@ -75,7 +75,7 @@ extern "C" {
  *   The quote character to use.
  *   This is either f_iki_syntax_quote_single or f_iki_syntax_quote_double.
  * @param output
- *   The output file (or stdout) to print to.
+ *   The output file descriptor to print processed iki text to.
  * @param escaped
  *   A string buffer used as a string cache to save the string into while escaping.
  *
@@ -84,7 +84,7 @@ extern "C" {
  *   F_failure (with error bit) for any othe failure.
  */
 #ifndef _di_iki_write_process_
-  extern f_return_status iki_write_process(const iki_write_data_t data, const f_string_static_t object, const f_string_static_t content, const uint8_t quote, FILE *output, f_string_dynamic_t *escaped) f_gcc_attribute_visibility_internal;
+  extern f_return_status iki_write_process(const iki_write_data_t data, const f_string_static_t object, const f_string_static_t content, const uint8_t quote, const int output, f_string_dynamic_t *escaped) f_gcc_attribute_visibility_internal;
 #endif // _di_iki_write_process_
 
 #ifdef __cplusplus