Actually implement the new procedure code and adapt all of its clients
[pspp-builds.git] / src / language / data-io / list.q
index b1473fe693ff53a4cae6932725cc83682534240a..28627bbb0b8196bd992c5a16d12a7740b9ed0dbf 100644 (file)
@@ -1,6 +1,5 @@
 /* PSPP - computes sample statistics.
    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
-   Written by Ben Pfaff <blp@gnu.org>.
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -24,7 +23,8 @@
 
 #include "intprops.h"
 #include "size_max.h"
-#include <data/case.h>
+#include <data/casegrouper.h>
+#include <data/casereader.h>
 #include <data/dictionary.h>
 #include <data/data-out.h>
 #include <data/format.h>
@@ -74,9 +74,6 @@ struct list_ext
 /* Parsed command. */
 static struct cmd_list cmd;
 
-/* Current case number. */
-static int case_idx;
-
 /* Line buffer. */
 static struct string line_buffer;
 
@@ -86,11 +83,12 @@ static unsigned n_chars_width (struct outp_driver *d);
 static void write_line (struct outp_driver *d, const char *s);
 
 /* Other functions. */
-static bool list_cases (const struct ccase *, void *, const struct dataset *);
+static void list_case (struct ccase *, casenumber case_idx,
+                       const struct dataset *);
 static void determine_layout (void);
 static void clean_up (void);
 static void write_header (struct outp_driver *);
-static void write_all_headers (const struct ccase *, void *, const struct dataset*);
+static void write_all_headers (struct casereader *, const struct dataset*);
 
 /* Returns the number of text lines that can fit on the remainder of
    the page. */
@@ -134,7 +132,11 @@ write_line (struct outp_driver *d, const char *s)
 int
 cmd_list (struct lexer *lexer, struct dataset *ds)
 {
-  struct variable casenum_var;
+  struct dictionary *dict = dataset_dict (ds);
+  struct variable *casenum_var = NULL;
+  struct casegrouper *grouper;
+  struct casereader *group;
+  casenumber case_idx;
   bool ok;
 
   if (!parse_list (lexer, ds, &cmd, NULL))
@@ -148,7 +150,7 @@ cmd_list (struct lexer *lexer, struct dataset *ds)
   if (cmd.last == NOT_LONG)
     cmd.last = LONG_MAX;
   if (!cmd.sbc_variables)
-    dict_get_vars (dataset_dict (ds), &cmd.v_variables, &cmd.n_variables,
+    dict_get_vars (dict, &cmd.v_variables, &cmd.n_variables,
                   (1u << DC_SYSTEM) | (1u << DC_SCRATCH));
   if (cmd.n_variables == 0)
     {
@@ -188,12 +190,12 @@ cmd_list (struct lexer *lexer, struct dataset *ds)
   /* Weighting variable. */
   if (cmd.weight == LST_WEIGHT)
     {
-      if (dict_get_weight (dataset_dict (ds)) != NULL)
+      if (dict_get_weight (dict) != NULL)
        {
          size_t i;
 
          for (i = 0; i < cmd.n_variables; i++)
-           if (cmd.v_variables[i] == dict_get_weight (dataset_dict (ds)))
+           if (cmd.v_variables[i] == dict_get_weight (dict))
              break;
          if (i >= cmd.n_variables)
            {
@@ -202,7 +204,7 @@ cmd_list (struct lexer *lexer, struct dataset *ds)
              cmd.v_variables = xnrealloc (cmd.v_variables, cmd.n_variables,
                                            sizeof *cmd.v_variables);
              cmd.v_variables[cmd.n_variables - 1]
-                = dict_get_weight (dataset_dict (ds));
+                = dict_get_weight (dict);
            }
        }
       else
@@ -215,10 +217,8 @@ cmd_list (struct lexer *lexer, struct dataset *ds)
       /* Initialize the case-number variable. */
       int width = cmd.last == LONG_MAX ? 5 : intlog10 (cmd.last);
       struct fmt_spec format = fmt_for_output (FMT_F, width, 0);
-      var_set_name (&casenum_var, "Case#");
-      casenum_var.width = 0;
-      casenum_var.fv = -1;
-      var_set_both_formats (&casenum_var, &format);
+      casenum_var = var_create ("Case#", 0);
+      var_set_both_formats (casenum_var, &format);
 
       /* Add the weight variable at the beginning of the variable list. */
       cmd.n_variables++;
@@ -226,28 +226,52 @@ cmd_list (struct lexer *lexer, struct dataset *ds)
                                    cmd.n_variables, sizeof *cmd.v_variables);
       memmove (&cmd.v_variables[1], &cmd.v_variables[0],
               (cmd.n_variables - 1) * sizeof *cmd.v_variables);
-      cmd.v_variables[0] = &casenum_var;
+      cmd.v_variables[0] = casenum_var;
     }
 
   determine_layout ();
 
   case_idx = 0;
-  ok = procedure_with_splits (ds, write_all_headers, list_cases, NULL, NULL);
+  for (grouper = casegrouper_create_splits (proc_open (ds), dict);
+       casegrouper_get_next_group (grouper, &group);
+       casereader_destroy (group)) 
+    {
+      struct ccase c;
+      
+      write_all_headers (group, ds);
+      for (; casereader_read (group, &c); case_destroy (&c)) 
+        {
+          case_idx++;
+          if (case_idx >= cmd.first && case_idx <= cmd.last
+              && (case_idx - cmd.first) % cmd.step == 0)
+            list_case (&c, case_idx, ds); 
+        }
+    }
+  ok = casegrouper_destroy (grouper);
+  ok = proc_commit (ds) && ok;
+
   ds_destroy(&line_buffer);
 
   clean_up ();
 
+  var_destroy (casenum_var);    
+
   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
 }
 
 /* Writes headers to all devices.  This is done at the beginning of
    each SPLIT FILE group. */
 static void
-write_all_headers (const struct ccase *c, void *aux UNUSED, const struct dataset *ds)
+write_all_headers (struct casereader *input, const struct dataset *ds)
 {
   struct outp_driver *d;
+  struct ccase c;
+
+  if (!casereader_peek (input, 0, &c))
+    return;
+  output_split_file_values (ds, &c);
+  case_destroy (&c);
 
-  output_split_file_values (ds, c);
   for (d = outp_drivers (NULL); d; d = outp_drivers (d))
     {
       if (!d->class->special)
@@ -312,7 +336,7 @@ write_header (struct outp_driver *d)
       /* Put in vertical names. */
       for (i = x = 0; i < prc->n_vertical; i++)
        {
-         struct variable *v = cmd.v_variables[i];
+         const struct variable *v = cmd.v_variables[i];
           const char *name = var_get_name (v);
           size_t name_len = strlen (name);
           const struct fmt_spec *print = var_get_print_format (v);
@@ -328,7 +352,7 @@ write_header (struct outp_driver *d)
       /* Put in horizontal names. */
       for (; i < cmd.n_variables; i++)
        {
-         struct variable *v = cmd.v_variables[i];
+         const struct variable *v = cmd.v_variables[i];
           const char *name = var_get_name (v);
           size_t name_len = strlen (name);
           const struct fmt_spec *print = var_get_print_format (v);
@@ -551,7 +575,7 @@ determine_layout (void)
       /* Try layout #1. */
       for (width = cmd.n_variables - 1, column = 0; column < cmd.n_variables; column++)
        {
-         struct variable *v = cmd.v_variables[column];
+         const struct variable *v = cmd.v_variables[column];
           int fmt_width = var_get_print_format (v)->w;
           int name_len = strlen (var_get_name (v));
          width += MAX (fmt_width, name_len);
@@ -567,7 +591,7 @@ determine_layout (void)
           column < cmd.n_variables && width <= max_width;
           column++) 
         {
-          struct variable *v = cmd.v_variables[column];
+          const struct variable *v = cmd.v_variables[column];
           int fmt_width = var_get_print_format (v)->w;
           size_t name_len = strlen (var_get_name (v));
           width += fmt_width;
@@ -584,7 +608,7 @@ determine_layout (void)
 #endif
          for (column = cmd.n_variables; column-- != 0; )
            {
-             struct variable *v = cmd.v_variables[column];
+             const struct variable *v = cmd.v_variables[column];
               int name_len = strlen (var_get_name (v));
               int fmt_width = var_get_print_format (v)->w;
              int trial_width = width - fmt_width + MAX (fmt_width, name_len);
@@ -603,7 +627,7 @@ determine_layout (void)
               column < prc->n_vertical;
               column++) 
             {
-              struct variable *var = cmd.v_variables[column];
+              const struct variable *var = cmd.v_variables[column];
               size_t name_len = strlen (var_get_name (var));
               prc->header_rows = MAX (prc->header_rows, name_len); 
             }
@@ -624,16 +648,12 @@ determine_layout (void)
 }
 
 /* Writes case C to output. */
-static bool
-list_cases (const struct ccase *c, void *aux UNUSED, const struct dataset *ds UNUSED)
+static void
+list_case (struct ccase *c, casenumber case_idx, const struct dataset *ds)
 {
+  struct dictionary *dict = dataset_dict (ds);
   struct outp_driver *d;
   
-  case_idx++;
-  if (case_idx < cmd.first || case_idx > cmd.last
-      || (cmd.step != 1 && (case_idx - cmd.first) % cmd.step))
-    return true;
-
   for (d = outp_drivers (NULL); d; d = outp_drivers (d))
     if (d->class->special == 0)
       {
@@ -650,7 +670,7 @@ list_cases (const struct ccase *c, void *aux UNUSED, const struct dataset *ds UN
       
        for (column = 0; column < cmd.n_variables; column++)
          {
-           struct variable *v = cmd.v_variables[column];
+           const struct variable *v = cmd.v_variables[column];
             const struct fmt_spec *print = var_get_print_format (v);
            int width;
 
@@ -681,9 +701,10 @@ list_cases (const struct ccase *c, void *aux UNUSED, const struct dataset *ds UN
            if (width > print->w)
               ds_put_char_multiple(&line_buffer, ' ', width - print->w);
 
-            if (fmt_is_string (print->type) || v->fv != -1)
+            if (fmt_is_string (print->type)
+                || dict_contains_var (dict, v))
              {
-                data_out (case_data (c, v->fv), print,
+                data_out (case_data (c, v), print,
                           ds_put_uninit (&line_buffer, print->w));
              }
             else 
@@ -715,12 +736,13 @@ list_cases (const struct ccase *c, void *aux UNUSED, const struct dataset *ds UN
        
        for (column = 0; column < cmd.n_variables; column++)
          {
-           struct variable *v = cmd.v_variables[column];
+           const struct variable *v = cmd.v_variables[column];
             const struct fmt_spec *print = var_get_print_format (v);
            char buf[256];
            
-            if (fmt_is_string (print->type) || v->fv != -1)
-             data_out (case_data (c, v->fv), print, buf);
+            if (fmt_is_string (print->type)
+                || dict_contains_var (dict, v))
+             data_out (case_data (c, v), print, buf);
             else 
               {
                 union value case_idx_value;
@@ -737,8 +759,6 @@ list_cases (const struct ccase *c, void *aux UNUSED, const struct dataset *ds UN
       }
     else
       NOT_REACHED ();
-
-  return true;
 }
 
 /*