Make cases simpler, faster, and easier to understand.
[pspp-builds.git] / src / language / data-io / list.q
index 4aae0bbb93356c72b52843507321671456571ea1..28820a8504b5fdfc15c8ede3338439ab0f1b4d7a 100644 (file)
@@ -1,41 +1,40 @@
-/* PSPP - computes sample statistics.
-   Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 1997-9, 2000, 2006, 2009 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 the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   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
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
 
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 #include "intprops.h"
-#include "size_max.h"
+#include "xmalloca.h"
+
 #include <data/casegrouper.h>
 #include <data/casereader.h>
 #include <data/dictionary.h>
 #include <data/data-out.h>
 #include <data/format.h>
 #include <data/procedure.h>
+#include <data/short-names.h>
 #include <data/variable.h>
 #include <language/command.h>
 #include <language/dictionary/split-file.h>
 #include <language/lexer/lexer.h>
-#include <libpspp/alloc.h>
 #include <libpspp/compiler.h>
-#include <libpspp/magic.h>
 #include <libpspp/message.h>
 #include <libpspp/message.h>
 #include <libpspp/misc.h>
@@ -45,6 +44,7 @@
 #include <output/table.h>
 
 #include "minmax.h"
+#include "xalloc.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -83,7 +83,7 @@ static unsigned n_chars_width (struct outp_driver *d);
 static void write_line (struct outp_driver *d, const char *s);
 
 /* Other functions. */
-static void list_case (struct ccase *, casenumber case_idx,
+static void list_case (const struct ccase *, casenumber case_idx,
                        const struct dataset *);
 static void determine_layout (void);
 static void clean_up (void);
@@ -143,15 +143,15 @@ cmd_list (struct lexer *lexer, struct dataset *ds)
     return CMD_FAILURE;
 
   /* Fill in defaults. */
-  if (cmd.step == NOT_LONG)
+  if (cmd.step == LONG_MIN)
     cmd.step = 1;
-  if (cmd.first == NOT_LONG)
+  if (cmd.first == LONG_MIN)
     cmd.first = 1;
-  if (cmd.last == NOT_LONG)
+  if (cmd.last == LONG_MIN)
     cmd.last = LONG_MAX;
   if (!cmd.sbc_variables)
     dict_get_vars (dict, &cmd.v_variables, &cmd.n_variables,
-                  (1u << DC_SYSTEM) | (1u << DC_SCRATCH));
+                   DC_SYSTEM | DC_SCRATCH);
   if (cmd.n_variables == 0)
     {
       msg (SE, _("No variables specified."));
@@ -236,15 +236,15 @@ cmd_list (struct lexer *lexer, struct dataset *ds)
        casegrouper_get_next_group (grouper, &group);
        casereader_destroy (group))
     {
-      struct ccase c;
+      struct ccase *c;
 
       write_all_headers (group, ds);
-      for (; casereader_read (group, &c); case_destroy (&c))
+      for (; (c = casereader_read (group)) != NULL; case_unref (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);
+            list_case (c, case_idx, ds);
         }
     }
   ok = casegrouper_destroy (grouper);
@@ -265,12 +265,13 @@ static void
 write_all_headers (struct casereader *input, const struct dataset *ds)
 {
   struct outp_driver *d;
-  struct ccase c;
+  struct ccase *c;
 
-  if (!casereader_peek (input, 0, &c))
+  c = casereader_peek (input, 0);
+  if (c == NULL)
     return;
-  output_split_file_values (ds, &c);
-  case_destroy (&c);
+  output_split_file_values (ds, c);
+  case_unref (c);
 
   for (d = outp_drivers (NULL); d; d = outp_drivers (d))
     {
@@ -465,8 +466,8 @@ write_fallback_headers (struct outp_driver *d)
   int line_number = 0;
 
   const char *Line = _("Line");
-  char *leader = local_alloc (strlen (Line)
-                              + INT_STRLEN_BOUND (line_number) + 1 + 1);
+  char *leader = xmalloca (strlen (Line)
+                           + INT_STRLEN_BOUND (line_number) + 1 + 1);
 
   while (index < cmd.n_variables)
     {
@@ -508,7 +509,7 @@ write_fallback_headers (struct outp_driver *d)
          }
 
          {
-           char varname[LONG_NAME_LEN + 2];
+           char varname[VAR_NAME_LEN + 2];
            snprintf (varname, sizeof varname,
                       " %s", var_get_name (cmd.v_variables[index]));
            write_varname (d, varname, leader_width);
@@ -520,7 +521,7 @@ write_fallback_headers (struct outp_driver *d)
   d->cp_x = 0;
   d->cp_y += d->font_height;
 
-  local_free (leader);
+  freea (leader);
 }
 
 /* There are three possible layouts for the LIST procedure:
@@ -649,7 +650,8 @@ determine_layout (void)
 
 /* Writes case C to output. */
 static void
-list_case (struct ccase *c, casenumber case_idx, const struct dataset *ds)
+list_case (const struct ccase *c, casenumber case_idx,
+           const struct dataset *ds)
 {
   struct dictionary *dict = dataset_dict (ds);
   struct outp_driver *d;