]> Kevux Git Server - fll/commitdiff
Bugfix: When using --at with --total for --content, the returned number is always...
authorKevin Day <thekevinday@gmail.com>
Sat, 26 Mar 2022 02:38:08 +0000 (21:38 -0500)
committerKevin Day <thekevinday@gmail.com>
Sat, 26 Mar 2022 02:38:08 +0000 (21:38 -0500)
The code need to count all newlines rather than always returning 1.
This is likely a bug from copying the code from fss_basic_read where the non-zero count would indeed always be 1.

level_3/fss_basic_list_read/c/private-read.c

index 0778eafb5dac3ceacc3e2c3fb8cab77f9da91cec..8720f41e67862278020e6da238787ee745eb77f8 100644 (file)
@@ -419,16 +419,27 @@ extern "C" {
           fll_print_format("%ul%r", main->output.to.stream, data->contents.array[i].used, f_string_eol_s);
         }
         else if (data->option & fss_basic_list_read_data_option_total_d) {
-          flockfile(main->output.to.stream);
+          f_array_length_t total = 0;
+          f_array_length_t j = 0;
+          f_array_length_t k = 0;
 
-          if (data->contents.array[i].used) {
-            fss_basic_list_read_print_one(main);
-          }
-          else {
-            fss_basic_list_read_print_zero(main);
+          // Count each new line.
+          for (; j < data->contents.array[i].used; ++j) {
+
+            if (data->contents.array[i].array[j].start > data->contents.array[i].array[j].stop) continue;
+            if (data->contents.array[i].array[j].start > data->buffer.used) continue;
+
+            for (k = data->contents.array[i].array[j].start; k <= data->contents.array[i].array[j].stop && k < data->buffer.used; ++k) {
+              if (data->buffer.string[k] == f_string_eol_s.string[0]) ++total;
+            } // for
+          } // for
+
+          // If there are no newline characters but there is data, then this represents a single line.
+          if (data->contents.array[i].used && !total) {
+            total = 1;
           }
 
-          funlockfile(main->output.to.stream);
+          fll_print_format("%ul%r", main->output.to.stream, total, f_string_eol_s);
         }
         else {
           fss_basic_list_read_print_at(main, i, *delimits_object, *delimits_content, data);