subcase: Rename subcase_destroy() to subcase_uninit().
[pspp] / src / language / data-io / list.c
index e03bd08c126c903b5092a7c8aa1009d674613b3a..c98b6f9a5218dce96f157a8698af1a84857ec12e 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2006, 2009-2011, 2013 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2006, 2009-2011, 2013, 2014, 2016 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -36,8 +36,7 @@
 #include "libpspp/ll.h"
 #include "libpspp/message.h"
 #include "libpspp/misc.h"
-#include "output/tab.h"
-#include "output/table-item.h"
+#include "output/pivot-table.h"
 
 #include "gl/intprops.h"
 #include "gl/minmax.h"
@@ -45,6 +44,7 @@
 #include "gl/xmalloca.h"
 
 #include "gettext.h"
+#define N_(msgid) msgid
 #define _(msgid) gettext (msgid)
 
 enum numbering
@@ -84,56 +84,54 @@ list_execute (const struct lst_cmd *lcmd, struct dataset *ds)
   grouper = casegrouper_create_splits (proc_open (ds), dict);
   while (casegrouper_get_next_group (grouper, &group))
     {
-      struct ccase *ccase;
-      struct table *t;
-
-      ccase = casereader_peek (group, 0);
-      if (ccase != NULL)
+      struct ccase *c = casereader_peek (group, 0);
+      if (c != NULL)
         {
-          output_split_file_values (ds, ccase);
-          case_unref (ccase);
+          output_split_file_values (ds, c);
+          case_unref (c);
         }
 
       group = casereader_project (group, &sc);
-      if (lcmd->numbering == format_numbered)
-        group = casereader_create_arithmetic_sequence (group, 1, 1);
       group = casereader_select (group, lcmd->first - 1,
                                  (lcmd->last != LONG_MAX ? lcmd->last
                                   : CASENUMBER_MAX), lcmd->step);
 
-      if (lcmd->numbering == format_numbered)
-        {
-          struct fmt_spec fmt;
-          size_t col;
-          int width;
+      struct pivot_table *table = pivot_table_create (N_("Data List"));
+      table->show_values = table->show_variables = SETTINGS_VALUE_SHOW_VALUE;
 
-          width = lcmd->last == LONG_MAX ? 5 : intlog10 (lcmd->last);
-          fmt = fmt_for_output (FMT_F, width, 0);
-          col = caseproto_get_n_widths (casereader_get_proto (group)) - 1;
+      struct pivot_dimension *variables = pivot_dimension_create (
+        table, PIVOT_AXIS_COLUMN, N_("Variables"));
+      for (int i = 0; i < lcmd->n_variables; i++)
+        pivot_category_create_leaf (
+          variables->root, pivot_value_new_variable (lcmd->v_variables[i]));
 
-          t = table_from_casereader (group, col, _("Case Number"), &fmt);
-        }
+      struct pivot_dimension *cases = pivot_dimension_create (
+        table, PIVOT_AXIS_ROW, N_("Case Number"));
+      if (lcmd->numbering == format_numbered)
+        cases->root->show_label = true;
       else
-        t = NULL;
+        cases->hide_all_labels = true;
 
-      for (i = 0; i < lcmd->n_variables; i++)
+      casenumber case_num = lcmd->first;
+      for (; (c = casereader_read (group)) != NULL; case_unref (c))
         {
-          const struct variable *var = lcmd->v_variables[i];
-          struct table *c;
-
-          c = table_from_casereader (group, i, var_get_name (var),
-                                     var_get_print_format (var));
-          t = table_hpaste (t, c);
+          int case_idx = pivot_category_create_leaf (
+            cases->root, pivot_value_new_integer (case_num));
+          case_num += lcmd->step;
+
+          for (int i = 0; i < lcmd->n_variables; i++)
+            pivot_table_put2 (table, i, case_idx,
+                              pivot_value_new_var_value (
+                                lcmd->v_variables[i], case_data_idx (c, i)));
         }
-
       casereader_destroy (group);
 
-      table_item_submit (table_item_create (t, "Data List"));
+      pivot_table_submit (table);
     }
   ok = casegrouper_destroy (grouper);
   ok = proc_commit (ds) && ok;
 
-  subcase_destroy (&sc);
+  subcase_uninit (&sc);
   free (lcmd->v_variables);
 
   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
@@ -159,23 +157,23 @@ cmd_list (struct lexer *lexer, struct dataset *ds)
   while (lex_token (lexer) != T_ENDCMD)
     {
       lex_match (lexer, T_SLASH);
-      if (lex_match_id (lexer, "VARIABLES") )
+      if (lex_match_id (lexer, "VARIABLES"))
         {
           lex_match (lexer, T_EQUALS);
-          if (! parse_variables_const (lexer, dict, &cmd.v_variables, &cmd.n_variables, 0 ))
+          if (! parse_variables_const (lexer, dict, &cmd.v_variables, &cmd.n_variables, 0))
             {
               msg (SE, _("No variables specified."));
               return CMD_FAILURE;
             }
         }
-      else if (lex_match_id (lexer, "FORMAT") )
+      else if (lex_match_id (lexer, "FORMAT"))
         {
           lex_match (lexer, T_EQUALS);
-          if (lex_match_id (lexer, "NUMBERED") )
+          if (lex_match_id (lexer, "NUMBERED"))
             {
               cmd.numbering = format_numbered;
             }
-          else if (lex_match_id (lexer, "UNNUMBERED") )
+          else if (lex_match_id (lexer, "UNNUMBERED"))
             {
               cmd.numbering = format_unnumbered;
             }
@@ -189,36 +187,31 @@ cmd_list (struct lexer *lexer, struct dataset *ds)
       else if (lex_match_id (lexer, "CASES"))
         {
           lex_match (lexer, T_EQUALS);
-          lex_force_match_id (lexer, "FROM");
-
-          if (lex_force_int (lexer))
+          if (lex_match_id (lexer, "FROM") && lex_force_int (lexer))
             {
-             cmd.first = lex_integer (lexer);
+              cmd.first = lex_integer (lexer);
               lex_get (lexer);
             }
 
-          lex_force_match (lexer, T_TO);
-
-          if (lex_force_int (lexer))
+          if ((lex_match (lexer, T_TO) && lex_force_int (lexer))
+              || lex_is_integer (lexer))
             {
               cmd.last = lex_integer (lexer);
               lex_get (lexer);
             }
 
-          lex_force_match (lexer, T_BY);
-
-          if (lex_force_int (lexer))
+          if (lex_match (lexer, T_BY) && lex_force_int (lexer))
             {
               cmd.step = lex_integer (lexer);
               lex_get (lexer);
             }
         }
-      else if (! parse_variables_const (lexer, dict, &cmd.v_variables, &cmd.n_variables, 0 ))
+      else if (! parse_variables_const (lexer, dict, &cmd.v_variables, &cmd.n_variables, 0))
         {
           return CMD_FAILURE;
         }
     }
-        
+
 
   /* Verify arguments. */
   if (cmd.first > cmd.last)
@@ -233,15 +226,15 @@ cmd_list (struct lexer *lexer, struct dataset *ds)
 
   if (cmd.first < 1)
     {
-      msg (SW, _("The first case (%ld) to list is less than 1.  The value is "
-                 "being reset to 1."), cmd.first);
+      msg (SW, _("The first case (%ld) to list is numbered less than 1.  "
+                 "The value is being reset to 1."), cmd.first);
       cmd.first = 1;
     }
 
   if (cmd.last < 1)
     {
-      msg (SW, _("The last case (%ld) to list is less than 1.  The value is "
-                 "being reset to 1."), cmd.last);
+      msg (SW, _("The last case (%ld) to list is numbered less than 1.  "
+                 "The value is being reset to 1."), cmd.last);
       cmd.last = 1;
     }