posix-xprintf-functions.patch from patch #6230.
[pspp-builds.git] / src / language / dictionary / sys-file-info.c
index d0f2147f9071d0914bfbbd002cc2fc985fdcb712..2369b99ec809d92679b21e9c69ae186ea346e101 100644 (file)
@@ -1,41 +1,40 @@
-/* PSPP - computes sample statistics.
+/* PSPP - a program for statistical analysis.
    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
-   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 <ctype.h>
 #include <stdlib.h>
 
+#include <data/casereader.h>
 #include <data/dictionary.h>
 #include <data/file-handle-def.h>
+#include <data/format.h>
+#include <data/missing-values.h>
 #include <data/procedure.h>
 #include <data/sys-file-reader.h>
 #include <data/value-labels.h>
 #include <data/variable.h>
+#include <data/vector.h>
 #include <language/command.h>
 #include <language/data-io/file-handle.h>
 #include <language/lexer/lexer.h>
 #include <language/lexer/variable-parser.h>
-#include <libpspp/alloc.h>
 #include <libpspp/array.h>
 #include <libpspp/hash.h>
-#include <libpspp/magic.h>
 #include <libpspp/message.h>
 #include <libpspp/message.h>
 #include <libpspp/misc.h>
@@ -44,6 +43,7 @@
 #include <output/table.h>
 
 #include "minmax.h"
+#include "xalloc.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -60,8 +60,8 @@ enum
     AS_VECTOR
   };
 
-static int describe_variable (struct variable *v, struct tab_table *t, int r, int as);
-     
+static int describe_variable (const struct variable *v, struct tab_table *t, int r, int as);
+
 /* Sets the widths of all the columns and heights of all the rows in
    table T for driver D. */
 static void
@@ -85,7 +85,7 @@ cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
   struct file_handle *h;
   struct dictionary *d;
   struct tab_table *t;
-  struct sfm_reader *reader;
+  struct casereader *reader;
   struct sfm_read_info info;
   int r, nr;
   int i;
@@ -100,9 +100,9 @@ cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
   reader = sfm_open_reader (h, &d, &info);
   if (!reader)
     return CMD_FAILURE;
-  sfm_close_reader (reader);
+  casereader_destroy (reader);
 
-  t = tab_create (2, 9, 0);
+  t = tab_create (2, 10, 0);
   tab_vline (t, TAL_GAP, 1, 0, 8);
   tab_text (t, 0, 0, TAB_LEFT, _("File:"));
   tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
@@ -116,24 +116,36 @@ cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
   tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
   tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
                info.creation_date, info.creation_time, info.product);
-  tab_text (t, 0, 3, TAB_LEFT, _("Endian:"));
-  tab_text (t, 1, 3, TAB_LEFT, info.big_endian ? _("Big.") : _("Little."));
-  tab_text (t, 0, 4, TAB_LEFT, _("Variables:"));
-  tab_text (t, 1, 4, TAB_LEFT | TAT_PRINTF, "%d",
-               dict_get_var_cnt (d));
-  tab_text (t, 0, 5, TAB_LEFT, _("Cases:"));
-  tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF,
-               info.case_cnt == -1 ? _("Unknown") : "%d", info.case_cnt);
-  tab_text (t, 0, 6, TAB_LEFT, _("Type:"));
-  tab_text (t, 1, 6, TAB_LEFT, _("System File."));
-  tab_text (t, 0, 7, TAB_LEFT, _("Weight:"));
+  tab_text (t, 0, 3, TAB_LEFT, _("Integer Format:"));
+  tab_text (t, 1, 3, TAB_LEFT,
+            info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian.")
+            : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian.")
+            : _("Unknown."));
+  tab_text (t, 0, 4, TAB_LEFT, _("Real Format:"));
+  tab_text (t, 1, 4, TAB_LEFT,
+            info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
+            : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
+            : info.float_format == FLOAT_VAX_D ? _("VAX D.")
+            : info.float_format == FLOAT_VAX_G ? _("VAX G.")
+            : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
+            : _("Unknown."));
+  tab_text (t, 0, 5, TAB_LEFT, _("Variables:"));
+  tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF, "%zu", dict_get_var_cnt (d));
+  tab_text (t, 0, 6, TAB_LEFT, _("Cases:"));
+  tab_text (t, 1, 6, TAB_LEFT | TAT_PRINTF,
+            info.case_cnt == -1 ? _("Unknown") : "%ld",
+            (long int) info.case_cnt);
+  tab_text (t, 0, 7, TAB_LEFT, _("Type:"));
+  tab_text (t, 1, 7, TAB_LEFT, _("System File."));
+  tab_text (t, 0, 8, TAB_LEFT, _("Weight:"));
   {
     struct variable *weight_var = dict_get_weight (d);
-    tab_text (t, 1, 7, TAB_LEFT,
-              weight_var != NULL ? weight_var->name : _("Not weighted.")); 
+    tab_text (t, 1, 8, TAB_LEFT,
+              (weight_var != NULL
+               ? var_get_name (weight_var) : _("Not weighted.")));
   }
-  tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
-  tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
+  tab_text (t, 0, 9, TAB_LEFT, _("Mode:"));
+  tab_text (t, 1, 9, TAB_LEFT | TAT_PRINTF,
                _("Compression %s."), info.compressed ? _("on") : _("off"));
   tab_dim (t, tab_natural_dimensions);
   tab_submit (t);
@@ -150,9 +162,9 @@ cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
   for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
     {
       struct variable *v = dict_get_var (d, i);
-      const int nvl = val_labs_count (v->val_labs);
-      
-      if (r + 10 + nvl > nr)
+      const int nvl = val_labs_count (var_get_value_labels (v));
+
+      if (r + 13 + nvl > nr)
        {
          nr = MAX (nr * dict_get_var_cnt (d) / (i + 1), nr);
          nr += 10 + nvl;
@@ -171,7 +183,7 @@ cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
   tab_submit (t);
 
   dict_destroy (d);
-  
+
   return lex_end_of_command (lexer);
 }
 \f
@@ -179,7 +191,7 @@ cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
 
 static void display_macros (void);
 static void display_documents (const struct dictionary *dict);
-static void display_variables (struct variable **, size_t, int);
+static void display_variables (const struct variable **, size_t, int);
 static void display_vectors (const struct dictionary *dict, int sorted);
 
 int
@@ -190,7 +202,7 @@ cmd_display (struct lexer *lexer, struct dataset *ds)
 
   /* Variables to display. */
   size_t n;
-  struct variable **vl;
+  const struct variable **vl;
 
   if (lex_match_id (lexer, "MACROS"))
     display_macros ();
@@ -244,7 +256,7 @@ cmd_display (struct lexer *lexer, struct dataset *ds)
 
       if (lex_token (lexer) != '.')
        {
-         if (!parse_variables (lexer, dataset_dict (ds), &vl, &n, PV_NONE))
+         if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n, PV_NONE))
            {
              free (vl);
              return CMD_FAILURE;
@@ -258,7 +270,7 @@ cmd_display (struct lexer *lexer, struct dataset *ds)
        {
          size_t i, m;
          for (i = 0, m = n; i < n; i++)
-           if (dict_class_from_id (vl[i]->name) != DC_SCRATCH)
+           if (dict_class_from_id (var_get_name (vl[i])) != DC_SCRATCH)
              {
                vl[i] = NULL;
                m--;
@@ -274,7 +286,7 @@ cmd_display (struct lexer *lexer, struct dataset *ds)
        }
 
       if (sorted)
-       sort (vl, n, sizeof *vl, compare_var_ptr_names, NULL);
+       sort (vl, n, sizeof *vl, compare_var_ptrs_by_name, NULL);
 
       display_variables (vl, n, as);
 
@@ -302,25 +314,18 @@ display_documents (const struct dictionary *dict)
                                  "contain any documents."));
   else
     {
-      size_t n_lines = strlen (documents) / 80;
-      char buf[81];
+      struct string line = DS_EMPTY_INITIALIZER;
       size_t i;
 
       tab_output_text (TAB_LEFT | TAT_TITLE,
                       _("Documents in the active file:"));
       som_blank_line ();
-      buf[80] = 0;
-      for (i = 0; i < n_lines; i++)
-       {
-         int len = 79;
-
-         memcpy (buf, &documents[i * 80], 80);
-         while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
-                && len > 0)
-           len--;
-         buf[len + 1] = 0;
-         tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, buf);
-       }
+      for (i = 0; i < dict_get_document_line_cnt (dict); i++)
+        {
+          dict_get_document_line (dict, i, &line);
+          tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, ds_cstr (&line));
+        }
+      ds_destroy (&line);
     }
 }
 
@@ -333,7 +338,7 @@ variables_dim (struct tab_table *t, struct outp_driver *d)
 {
   int pc;
   int i;
-  
+
   t->w[0] = tab_natural_width (t, d, 0);
   if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
     {
@@ -348,11 +353,11 @@ variables_dim (struct tab_table *t, struct outp_driver *d)
   for (i = 0; i < t->nr; i++)
     t->h[i] = tab_natural_height (t, d, i);
 }
-  
+
 static void
-display_variables (struct variable **vl, size_t n, int as)
+display_variables (const struct variable **vl, size_t n, int as)
 {
-  struct variable **vp = vl;           /* Variable pointer. */
+  const struct variable **vp = vl;     /* Variable pointer. */
   struct tab_table *t;
   int nc;                      /* Number of columns. */
   int nr;                      /* Number of rows. */
@@ -387,10 +392,10 @@ display_variables (struct variable **vl, size_t n, int as)
   else if (as == AS_LABELS)
     tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
   tab_dim (t, variables_dim);
-    
+
   for (i = r = 1; i <= n; i++)
     {
-      struct variable *v;
+      const struct variable *v;
 
       while (*vp == NULL)
        vp++;
@@ -398,9 +403,9 @@ display_variables (struct variable **vl, size_t n, int as)
 
       if (as == AS_DICTIONARY || as == AS_VARIABLES)
        {
-         int nvl = val_labs_count (v->val_labs);
-      
-         if (r + 10 + nvl > nr)
+         int nvl = val_labs_count (var_get_value_labels (v));
+
+         if (r + 13 + nvl > nr)
            {
              nr = MAX (nr * n / (i + 1), nr);
              nr += 10 + nvl;
@@ -409,13 +414,17 @@ display_variables (struct variable **vl, size_t n, int as)
 
          r = describe_variable (v, t, r, as);
        } else {
-         tab_text (t, 0, r, TAB_LEFT, v->name);
+         tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
          if (as == AS_LABELS)
-           tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
-                           v->label == NULL ? "(no label)" : v->label);
+            {
+              const char *label = var_get_label (v);
+              tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
+                              label != NULL ? "(no label)" : label);
+            }
          if (as != AS_NAMES)
            {
-             tab_text (t, pc, r, TAT_PRINTF, "%d", v->index + 1);
+             tab_text (t, pc, r, TAT_PRINTF, "%zu",
+                        var_get_dict_index (v) + 1);
              tab_hline (t, TAL_1, 0, nc - 1, r);
            }
          r++;
@@ -439,51 +448,72 @@ display_variables (struct variable **vl, size_t n, int as)
 /* Puts a description of variable V into table T starting at row R.
    The variable will be described in the format AS.  Returns the next
    row available for use in the table. */
-static int 
-describe_variable (struct variable *v, struct tab_table *t, int r, int as)
+static int
+describe_variable (const struct variable *v, struct tab_table *t, int r, int as)
 {
+  const struct fmt_spec *print = var_get_print_format (v);
+  const struct fmt_spec *write = var_get_write_format (v);
+  enum measure m = var_get_measure (v);
+  enum alignment a = var_get_alignment (v);
+
   /* Put the name, var label, and position into the first row. */
-  tab_text (t, 0, r, TAB_LEFT, v->name);
-  tab_text (t, 3, r, TAT_PRINTF, "%d", v->index + 1);
+  tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
+  tab_text (t, 3, r, TAT_PRINTF, "%zu", var_get_dict_index (v) + 1);
 
-  if (as == AS_DICTIONARY && v->label)
+  if (as == AS_DICTIONARY && var_has_label (v))
     {
-      tab_joint_text (t, 1, r, 2, r, TAB_LEFT, v->label);
+      tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
       r++;
     }
-  
+
   /* Print/write format, or print and write formats. */
-  if (v->print.type == v->write.type
-      && v->print.w == v->write.w
-      && v->print.d == v->write.d)
+  if (fmt_equal (print, write))
     {
       char str[FMT_STRING_LEN_MAX + 1];
       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
-                     fmt_to_string (&v->print, str));
+                     fmt_to_string (print, str));
       r++;
     }
   else
     {
       char str[FMT_STRING_LEN_MAX + 1];
       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
-                     _("Print Format: %s"), fmt_to_string (&v->print, str));
+                     _("Print Format: %s"), fmt_to_string (print, str));
       r++;
       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
-                     _("Write Format: %s"), fmt_to_string (&v->write, str));
+                     _("Write Format: %s"), fmt_to_string (write, str));
       r++;
     }
 
+  /* Measurement level, display width, alignment. */
+  tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
+                  _("Measure: %s"),
+                  m == MEASURE_NOMINAL ? _("Nominal")
+                  : m == MEASURE_ORDINAL ? _("Ordinal")
+                  : _("Scale"));
+  r++;
+  tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
+                  _("Display Alignment: %s"),
+                  a == ALIGN_LEFT ? _("Left")
+                  : a == ALIGN_CENTRE ? _("Center")
+                  : _("Right"));
+  r++;
+  tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
+                  _("Display Width: %d"), var_get_display_width (v));
+  r++;
+
   /* Missing values if any. */
-  if (!mv_is_empty (&v->miss))
+  if (var_has_missing_values (v))
     {
       char buf[128];
       char *cp;
       struct missing_values mv;
       int cnt = 0;
-      
+
       cp = stpcpy (buf, _("Missing Values: "));
-      mv_copy (&mv, &v->miss);
-      if (mv_has_range (&mv)) 
+
+      mv_copy (&mv, var_get_missing_values (v));
+      if (mv_has_range (&mv))
         {
           double x, y;
           mv_pop_range (&mv, &x, &y);
@@ -495,19 +525,19 @@ describe_variable (struct variable *v, struct tab_table *t, int r, int as)
             cp += sprintf (cp, "%g THRU %g", x, y);
           cnt++;
         }
-      while (mv_has_value (&mv)) 
+      while (mv_has_value (&mv))
         {
           union value value;
           mv_pop_value (&mv, &value);
           if (cnt++ > 0)
             cp += sprintf (cp, "; ");
-          if (v->type == NUMERIC)
+          if (var_is_numeric (v))
             cp += sprintf (cp, "%g", value.f);
-          else 
+          else
             {
               *cp++ = '"';
-             memcpy (cp, value.s, v->width);
-             cp += v->width;
+             memcpy (cp, value.s, var_get_width (v));
+             cp += var_get_width (v);
              *cp++ = '"';
               *cp = '\0';
             }
@@ -518,8 +548,9 @@ describe_variable (struct variable *v, struct tab_table *t, int r, int as)
     }
 
   /* Value labels. */
-  if (as == AS_DICTIONARY && val_labs_count (v->val_labs))
+  if (as == AS_DICTIONARY && var_has_value_labels (v))
     {
+      const struct val_labs *val_labs = var_get_value_labels (v);
       struct val_labs_iterator *i;
       struct val_lab *vl;
       int orig_r = r;
@@ -531,15 +562,15 @@ describe_variable (struct variable *v, struct tab_table *t, int r, int as)
 #endif
 
       tab_hline (t, TAL_1, 1, 2, r);
-      for (vl = val_labs_first_sorted (v->val_labs, &i); vl != NULL;
-           vl = val_labs_next (v->val_labs, &i))
+      for (vl = val_labs_first_sorted (val_labs, &i); vl != NULL;
+           vl = val_labs_next (val_labs, &i))
         {
          char buf[128];
 
-         if (v->type == ALPHA)
+         if (var_is_alpha (v))
            {
-             memcpy (buf, vl->value.s, v->width);
-             buf[v->width] = 0;
+             memcpy (buf, vl->value.s, var_get_width (v));
+             buf[var_get_width (v)] = 0;
            }
          else
            sprintf (buf, "%g", vl->value.f);
@@ -558,17 +589,6 @@ describe_variable (struct variable *v, struct tab_table *t, int r, int as)
   return r;
 }
 
-static int
-compare_vectors_by_name (const void *a_, const void *b_)
-{
-  struct vector *const *pa = a_;
-  struct vector *const *pb = b_;
-  struct vector *a = *pa;
-  struct vector *b = *pb;
-  
-  return strcasecmp (a->name, b->name);
-}
-
 /* Display a list of vectors.  If SORTED is nonzero then they are
    sorted alphabetically. */
 static void
@@ -578,7 +598,9 @@ display_vectors (const struct dictionary *dict, int sorted)
   int i;
   struct tab_table *t;
   size_t nvec;
-  
+  size_t nrow;
+  size_t row;
+
   nvec = dict_get_vector_cnt (dict);
   if (nvec == 0)
     {
@@ -587,32 +609,52 @@ display_vectors (const struct dictionary *dict, int sorted)
     }
 
   vl = xnmalloc (nvec, sizeof *vl);
+  nrow = 0;
   for (i = 0; i < nvec; i++)
-    vl[i] = dict_get_vector (dict, i);
+    {
+      vl[i] = dict_get_vector (dict, i);
+      nrow += vector_get_var_cnt (vl[i]);
+    }
   if (sorted)
-    qsort (vl, nvec, sizeof *vl, compare_vectors_by_name);
+    qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
 
-  t = tab_create (1, nvec + 1, 0);
+  t = tab_create (4, nrow + 1, 0);
   tab_headers (t, 0, 0, 1, 0);
   tab_columns (t, TAB_COL_DOWN, 1);
   tab_dim (t, tab_natural_dimensions);
-  tab_hline (t, TAL_1, 0, 0, 1);
+  tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
+  tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
+  tab_hline (t, TAL_2, 0, 3, 1);
   tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
+  tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
+  tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
+  tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
   tab_flags (t, SOMF_NO_TITLE);
-  for (i = 0; i < nvec; i++)
-    tab_text (t, 0, i + 1, TAB_LEFT, vl[i]->name);
-  tab_submit (t);
-
-  free (vl);
-}
-
-
-
-
-
-
 
+  row = 1;
+  for (i = 0; i < nvec; i++)
+    {
+      const struct vector *vec = vl[i];
+      size_t j;
 
+      tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
+                      TAB_LEFT, vector_get_name (vl[i]));
 
+      for (j = 0; j < vector_get_var_cnt (vec); j++)
+        {
+          struct variable *var = vector_get_var (vec, j);
+          char fmt_string[FMT_STRING_LEN_MAX + 1];
+          fmt_to_string (var_get_print_format (var), fmt_string);
+
+          tab_text (t, 1, row, TAB_RIGHT | TAT_PRINTF, "%zu", j + 1);
+          tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
+          tab_text (t, 3, row, TAB_LEFT, fmt_string);
+          row++;
+        }
+      tab_hline (t, TAL_1, 0, 3, row);
+    }
 
+  tab_submit (t);
 
+  free (vl);
+}