data_out function to dynamically allocate return value.
[pspp-builds.git] / src / language / data-io / list.q
index d8256e32d25c504342262e928035d59bbe0cf4d8..b5a16f265481e8d4d5bc0c957a3412235da3e039 100644 (file)
@@ -1,41 +1,40 @@
-/* PSPP - computes sample statistics.
-   Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
-   Written by Ben Pfaff <blp@gnu.org>.
+/* 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 <data/case.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>
@@ -44,6 +43,9 @@
 #include <output/output.h>
 #include <output/table.h>
 
+#include "minmax.h"
+#include "xalloc.h"
+
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
@@ -72,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;
 
@@ -84,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 (const 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. */
@@ -115,7 +115,7 @@ static void
 write_line (struct outp_driver *d, const char *s)
 {
   struct outp_text text;
-  
+
   assert (d->cp_y + d->font_height <= d->length);
   text.font = OUTP_FIXED;
   text.justification = OUTP_LEFT;
@@ -127,27 +127,31 @@ write_line (struct outp_driver *d, const char *s)
   d->cp_x = 0;
   d->cp_y += d->font_height;
 }
-    
+
 /* Parses and executes the LIST procedure. */
 int
-cmd_list (struct dataset *ds)
+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 (ds, &cmd, NULL))
+  if (!parse_list (lexer, ds, &cmd, NULL))
     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 (dataset_dict (ds), &cmd.v_variables, &cmd.n_variables,
-                  (1u << DC_SYSTEM) | (1u << DC_SCRATCH));
+    dict_get_vars (dict, &cmd.v_variables, &cmd.n_variables,
+                   DC_SYSTEM | DC_SCRATCH);
   if (cmd.n_variables == 0)
     {
       msg (SE, _("No variables specified."));
@@ -186,12 +190,12 @@ cmd_list (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)
            {
@@ -200,7 +204,7 @@ cmd_list (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
@@ -211,12 +215,10 @@ cmd_list (struct dataset *ds)
   if (cmd.numbering == LST_NUMBERED)
     {
       /* Initialize the case-number variable. */
-      strcpy (casenum_var.name, "Case#");
-      casenum_var.type = NUMERIC;
-      casenum_var.fv = -1;
-      casenum_var.print = fmt_for_output (FMT_F,
-                                          (cmd.last == LONG_MAX
-                                           ? 5 : intlog10 (cmd.last)), 0);
+      int width = cmd.last == LONG_MAX ? 5 : intlog10 (cmd.last);
+      struct fmt_spec format = fmt_for_output (FMT_F, width, 0);
+      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++;
@@ -224,28 +226,53 @@ cmd_list (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 (; (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);
+        }
+    }
+  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;
 
+  c = casereader_peek (input, 0);
+  if (c == NULL)
+    return;
   output_split_file_values (ds, c);
+  case_unref (c);
+
   for (d = outp_drivers (NULL); d; d = outp_drivers (d))
     {
       if (!d->class->special)
@@ -256,15 +283,15 @@ write_all_headers (const struct ccase *c, void *aux UNUSED, const struct dataset
       else if (d->class == &html_class)
        {
          struct html_driver_ext *x = d->ext;
-  
+
          fputs ("<TABLE BORDER=1>\n  <TR>\n", x->file);
-         
+
          {
            size_t i;
 
            for (i = 0; i < cmd.n_variables; i++)
              fprintf (x->file, "    <TH><EM>%s</EM></TH>\n",
-                      cmd.v_variables[i]->name);
+                      var_get_name (cmd.v_variables[i]));
          }
 
          fputs ("  </TR>\n", x->file);
@@ -283,7 +310,7 @@ write_header (struct outp_driver *d)
 
   if (!prc->header_rows)
     return;
-  
+
   if (n_lines_remaining (d) < prc->header_rows + 1)
     {
       outp_eject_page (d);
@@ -295,7 +322,7 @@ write_header (struct outp_driver *d)
     {
       size_t i;
       size_t x;
-      
+
       /* Allocate, initialize header. */
       prc->header = xnmalloc (prc->header_rows, sizeof *prc->header);
       {
@@ -310,27 +337,33 @@ 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);
          size_t j;
 
-         memset (&prc->header[prc->header_rows - 1][x], '-', v->print.w);
-         x += v->print.w - 1;
-         for (j = 0; j < strlen (v->name); j++)
-           prc->header[strlen (v->name) - j - 1][x] = v->name[j];
+         memset (&prc->header[prc->header_rows - 1][x], '-', print->w);
+         x += print->w - 1;
+         for (j = 0; j < name_len; j++)
+           prc->header[name_len - j - 1][x] = name[j];
          x += 2;
        }
 
       /* 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);
+
          memset (&prc->header[prc->header_rows - 1][x], '-',
-                 max (v->print.w, (int) strlen (v->name)));
-         if ((int) strlen (v->name) < v->print.w)
-           x += v->print.w - strlen (v->name);
-         memcpy (&prc->header[0][x], v->name, strlen (v->name));
-         x += strlen (v->name) + 1;
+                 MAX (print->w, (int) name_len));
+         if ((int) name_len < print->w)
+           x += print->w - name_len;
+         memcpy (&prc->header[0][x], name, name_len);
+         x += name_len + 1;
        }
 
       /* Add null bytes. */
@@ -347,23 +380,23 @@ write_header (struct outp_driver *d)
     }
 
   /* Write out the header, in back-to-front order except for the last line. */
-  if (prc->header_rows >= 2) 
+  if (prc->header_rows >= 2)
     {
       size_t i;
-        
+
       for (i = prc->header_rows - 1; i-- != 0; )
-        write_line (d, prc->header[i]); 
+        write_line (d, prc->header[i]);
     }
   write_line (d, prc->header[prc->header_rows - 1]);
 }
-      
-  
+
+
 /* Frees up all the memory we've allocated. */
 static void
 clean_up (void)
 {
   struct outp_driver *d;
-  
+
   for (d = outp_drivers (NULL); d; d = outp_drivers (d))
     if (d->class->special == 0)
       {
@@ -389,7 +422,7 @@ clean_up (void)
       }
     else
       NOT_REACHED ();
-  
+
   free (cmd.v_variables);
 }
 
@@ -401,7 +434,7 @@ write_varname (struct outp_driver *d, char *string, int indent)
 {
   struct outp_text text;
   int width;
-  
+
   if (d->cp_x + outp_string_width (d, string, OUTP_FIXED) > d->width)
     {
       d->cp_y += d->font_height;
@@ -427,15 +460,15 @@ static void
 write_fallback_headers (struct outp_driver *d)
 {
   const int max_width = n_chars_width(d) - 10;
-  
+
   int index = 0;
   int width = 0;
   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)
     {
       struct outp_text text;
@@ -444,7 +477,7 @@ write_fallback_headers (struct outp_driver *d)
       /* Ensure that there is enough room for a line of text. */
       if (d->cp_y + d->font_height > d->length)
        outp_eject_page (d);
-      
+
       /* The leader is a string like `Line 1: '.  Write the leader. */
       sprintf (leader, "%s %d:", Line, ++line_number);
       text.font = OUTP_FIXED;
@@ -464,7 +497,7 @@ write_fallback_headers (struct outp_driver *d)
 
        entry:
          {
-           int var_width = cmd.v_variables[index]->print.w;
+           int var_width = var_get_print_format (cmd.v_variables[index])->w;
            if (width + var_width > max_width && width != 0)
              {
                width = 0;
@@ -474,11 +507,11 @@ write_fallback_headers (struct outp_driver *d)
              }
            width += var_width;
          }
-         
+
          {
-           char varname[LONG_NAME_LEN + 2];
+           char varname[VAR_NAME_LEN + 2];
            snprintf (varname, sizeof varname,
-                      " %s", cmd.v_variables[index]->name);
+                      " %s", var_get_name (cmd.v_variables[index]));
            write_varname (d, varname, leader_width);
          }
        }
@@ -487,8 +520,8 @@ 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:
@@ -511,11 +544,11 @@ static void
 determine_layout (void)
 {
   struct outp_driver *d;
-  
+
   /* This is the largest page width of any driver, so we can tell what
      size buffer to allocate. */
   int largest_page_width = 0;
-  
+
   for (d = outp_drivers (NULL); d; d = outp_drivers (d))
     {
       size_t column;   /* Current column. */
@@ -527,13 +560,13 @@ determine_layout (void)
 
       if (d->class == &html_class)
        continue;
-      
+
       assert (d->class->special == 0);
 
       outp_open_page (d);
-      
+
       max_width = n_chars_width (d);
-      largest_page_width = max (largest_page_width, max_width);
+      largest_page_width = MAX (largest_page_width, max_width);
 
       prc = d->prc = xmalloc (sizeof *prc);
       prc->type = 0;
@@ -543,8 +576,10 @@ 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];
-         width += max (v->print.w, (int) strlen (v->name));
+         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);
        }
       if (width <= max_width)
        {
@@ -555,14 +590,16 @@ determine_layout (void)
       /* Try layout #2. */
       for (width = cmd.n_variables - 1, height = 0, column = 0;
           column < cmd.n_variables && width <= max_width;
-          column++) 
+          column++)
         {
-          struct variable *v = cmd.v_variables[column];
-          width += v->print.w;
-          if (strlen (v->name) > height)
-            height = strlen (v->name);
+          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;
+          if (name_len > height)
+            height = name_len;
         }
-      
+
       /* If it fit then we need to determine how many labels can be
          written horizontally. */
       if (width <= max_width && height <= SHORT_NAME_LEN)
@@ -572,10 +609,10 @@ determine_layout (void)
 #endif
          for (column = cmd.n_variables; column-- != 0; )
            {
-             struct variable *v = cmd.v_variables[column];
-             int trial_width = (width - v->print.w
-                                + max (v->print.w, (int) strlen (v->name)));
-             
+             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);
              if (trial_width > max_width)
                {
                  prc->n_vertical = column + 1;
@@ -590,8 +627,11 @@ determine_layout (void)
          for (prc->header_rows = 0, column = 0;
               column < prc->n_vertical;
               column++)
-           prc->header_rows = max (prc->header_rows,
-                                   strlen (cmd.v_variables[column]->name));
+            {
+              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);
+            }
          prc->header_rows++;
          continue;
        }
@@ -609,15 +649,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 (const 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)
@@ -628,21 +665,26 @@ list_cases (const struct ccase *c, void *aux UNUSED, const struct dataset *ds UN
 
        if (!prc->header_rows)
          {
-           ds_put_format(&line_buffer, "%8s: ", cmd.v_variables[0]->name);
+           ds_put_format(&line_buffer, "%8s: ",
+                          var_get_name (cmd.v_variables[0]));
          }
-       
-      
+
+
        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;
 
            if (prc->type == 0 && column >= prc->n_vertical)
-             width = max ((int) strlen (v->name), v->print.w);
+              {
+                int name_len = strlen (var_get_name (v));
+                width = MAX (name_len, print->w);
+              }
            else
-             width = v->print.w;
+             width = print->w;
 
-           if (width + ds_length(&line_buffer) > max_width && 
+           if (width + ds_length(&line_buffer) > max_width &&
                ds_length(&line_buffer) != 0)
              {
                if (!n_lines_remaining (d))
@@ -650,43 +692,43 @@ list_cases (const struct ccase *c, void *aux UNUSED, const struct dataset *ds UN
                    outp_eject_page (d);
                    write_header (d);
                  }
-             
+
                write_line (d, ds_cstr (&line_buffer));
                ds_clear(&line_buffer);
 
                if (!prc->header_rows)
-                 {
-                   ds_put_format (&line_buffer, "%8s: ", v->name);
-                 }
+                  ds_put_format (&line_buffer, "%8s: ", var_get_name (v));
              }
 
-           if (width > v->print.w)
-             {
-               ds_put_char_multiple(&line_buffer, ' ', width - v->print.w);
-             }
+           if (width > print->w)
+              ds_put_char_multiple(&line_buffer, ' ', width - print->w);
 
-            if (fmt_is_string (v->print.type) || v->fv != -1)
+            if (fmt_is_string (print->type)
+                || dict_contains_var (dict, v))
              {
-                data_out (case_data (c, v->fv), &v->print,
-                          ds_put_uninit (&line_buffer, v->print.w));
+               char *s = data_out (case_data (c, v), print);
+               ds_put_cstr (&line_buffer, s);
+               free (s);
              }
-            else 
+            else
               {
+               char *s;
                 union value case_idx_value;
                 case_idx_value.f = case_idx;
-                data_out (&case_idx_value, &v->print,
-                          ds_put_uninit (&line_buffer,v->print.w)); 
+                s = data_out (&case_idx_value, print);
+               ds_put_cstr (&line_buffer, s);
+               free (s);
               }
 
-           ds_put_char(&line_buffer, ' ');
+           ds_put_char (&line_buffer, ' ');
          }
-      
+
        if (!n_lines_remaining (d))
          {
            outp_eject_page (d);
            write_header (d);
          }
-             
+
        write_line (d, ds_cstr (&line_buffer));
        ds_clear(&line_buffer);
       }
@@ -696,35 +738,36 @@ list_cases (const struct ccase *c, void *aux UNUSED, const struct dataset *ds UN
        int column;
 
        fputs ("  <TR>\n", x->file);
-       
+
        for (column = 0; column < cmd.n_variables; column++)
          {
-           struct variable *v = cmd.v_variables[column];
-           char buf[256];
-           
-            if (fmt_is_string (v->print.type) || v->fv != -1)
-             data_out (case_data (c, v->fv), &v->print, buf);
-            else 
+           const struct variable *v = cmd.v_variables[column];
+            const struct fmt_spec *print = var_get_print_format (v);
+           char *s = NULL;
+
+            if (fmt_is_string (print->type)
+                || dict_contains_var (dict, v))
+             s = data_out (case_data (c, v), print);
+            else
               {
                 union value case_idx_value;
                 case_idx_value.f = case_idx;
-                data_out (&case_idx_value, &v->print, buf);
+                s = data_out (&case_idx_value, print);
               }
 
             fputs ("    <TD>", x->file);
-            html_put_cell_contents (d, TAB_FIX, ss_buffer (buf, v->print.w));
+            html_put_cell_contents (d, TAB_FIX, ss_buffer (s, print->w));
+           free (s);
             fputs ("</TD>\n", x->file);
          }
-         
+
        fputs ("  </TR>\n", x->file);
       }
     else
       NOT_REACHED ();
-
-  return true;
 }
 
-/* 
+/*
    Local Variables:
    mode: c
    End: