Fix memory leak in SYSFILE command
[pspp] / src / language / dictionary / sys-file-info.c
index b9e0a85c48c27b972529069d40c7861a055d14e4..92ade932ef17384fdde49786ffbf9bee71293d82 100644 (file)
@@ -21,6 +21,7 @@
 #include <float.h>
 #include <stdlib.h>
 
+#include "data/any-reader.h"
 #include "data/attributes.h"
 #include "data/casereader.h"
 #include "data/dataset.h"
@@ -28,7 +29,6 @@
 #include "data/file-handle-def.h"
 #include "data/format.h"
 #include "data/missing-values.h"
-#include "data/sys-file-reader.h"
 #include "data/value-labels.h"
 #include "data/variable.h"
 #include "data/vector.h"
@@ -76,19 +76,20 @@ static unsigned int dict_display_mask (const struct dictionary *);
 
 static struct table *describe_variable (const struct variable *v, int flags);
 
-static void report_encodings (const struct file_handle *,
-                              const struct sfm_reader *);
+static void report_encodings (const struct file_handle *, struct pool *,
+                              char **titles, bool *ids,
+                              char **strings, size_t n_strings);
 
 /* SYSFILE INFO utility. */
 int
 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
 {
-  struct sfm_reader *sfm_reader;
+  struct any_reader *any_reader;
   struct file_handle *h;
   struct dictionary *d;
   struct tab_table *t;
   struct casereader *reader;
-  struct sfm_read_info info;
+  struct any_read_info info;
   char *encoding;
   struct table *table;
   int r, i;
@@ -130,21 +131,36 @@ cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
       goto error;
     }
 
-  sfm_reader = sfm_open (h);
-  if (sfm_reader == NULL)
-    goto error;
+  any_reader = any_reader_open (h);
+  if (!any_reader)
+    {
+      free (encoding);
+      return CMD_FAILURE;
+    }
 
   if (encoding && !strcasecmp (encoding, "detect"))
     {
-      report_encodings (h, sfm_reader);
+      char **titles, **strings;
+      struct pool *pool;
+      size_t n_strings;
+      bool *ids;
+
+      pool = pool_create ();
+      n_strings = any_reader_get_strings (any_reader, pool,
+                                          &titles, &ids, &strings);
+      any_reader_close (any_reader);
+
+      report_encodings (h, pool, titles, ids, strings, n_strings);
       fh_unref (h);
+      pool_destroy (pool);
+      free (encoding);
+      
       return CMD_SUCCESS;
     }
 
-  reader = sfm_decode (sfm_reader, encoding, &d, &info);
+  reader = any_reader_decode (any_reader, encoding, &d, &info);
   if (!reader)
     goto error;
-
   casereader_destroy (reader);
 
   t = tab_create (2, 11 + (info.product_ext != NULL));
@@ -198,7 +214,7 @@ cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
   r++;
 
   tab_text (t, 0, r, TAB_LEFT, _("Type:"));
-  tab_text (t, 1, r++, TAB_LEFT, _("System File"));
+  tab_text (t, 1, r++, TAB_LEFT, gettext (info.klass->name));
 
   tab_text (t, 0, r, TAB_LEFT, _("Weight:"));
   {
@@ -210,8 +226,8 @@ cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
 
   tab_text (t, 0, r, TAB_LEFT, _("Compression:"));
   tab_text_format (t, 1, r++, TAB_LEFT,
-                   info.compression == SFM_COMP_NONE ? _("None")
-                   : info.compression == SFM_COMP_SIMPLE ? "SAV"
+                   info.compression == ANY_COMP_NONE ? _("None")
+                   : info.compression == ANY_COMP_SIMPLE ? "SAV"
                    : "ZSAV");
 
   tab_text (t, 0, r, TAB_LEFT, _("Encoding:"));
@@ -232,12 +248,13 @@ cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
                           describe_variable (dict_get_var (d, i),
                                              DF_ALL & ~DF_AT_ATTRIBUTES));
 
-  table_item_submit (table_item_create (table, NULL /* XXX */));
+  table_item_submit (table_item_create (table, NULL /* XXX */, NULL));
 
   dict_destroy (d);
 
   fh_unref (h);
-  sfm_read_info_destroy (&info);
+  free (encoding);
+  any_read_info_destroy (&info);
   return CMD_SUCCESS;
 
 error:
@@ -433,17 +450,7 @@ display_variables (const struct variable **vl, size_t n, int flags)
   for (i = 0; i < n; i++)
     table = table_vpaste (table, describe_variable (vl[i], flags));
 
-#if 0
-  tab_hline (t, flags & ~DF_DICT_INDEX ? TAL_2 : TAL_1, 0, nc - 1, 1);
-  if (flags)
-    {
-      tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
-      tab_vline (t, TAL_1, 1, 0, r - 1);
-    }
-  if (flags & ~DF_DICT_INDEX)
-    tab_vline (t, TAL_1, nc - 1, 0, r - 1);
-#endif
-  table_item_submit (table_item_create (table, NULL /* XXX */));
+  table_item_submit (table_item_create (table, NULL /* XXX */, NULL));
 }
 \f
 static bool
@@ -516,7 +523,8 @@ display_data_file_attributes (struct attrset *set, int flags)
 {
   if (count_attributes (set, flags))
     table_item_submit (table_item_create (describe_attributes (set, flags),
-                                          _("Custom data file attributes.")));
+                                          _("Custom data file attributes."),
+                                          NULL));
 }
 
 static struct table *
@@ -550,12 +558,8 @@ describe_value_labels (const struct variable *var)
   return &t->table;
 }
 
-/* Puts a description of variable V into table T starting at row
-   R.  The variable will be described in the format given by
-   FLAGS.  Returns the next row available for use in the
-   table. */
 static struct table *
-describe_variable (const struct variable *v, int flags)
+describe_variable_details (const struct variable *v, int flags)
 {
   struct table *table;
   struct string s;
@@ -669,11 +673,21 @@ describe_variable (const struct variable *v, int flags)
           table, table_create_nested (describe_attributes (attrs, flags)));
     }
 
-  if (table == NULL)
-    table = table_from_string (TAB_LEFT, "");
+  return table ? table : table_from_string (TAB_LEFT, "");
+}
 
+/* Puts a description of variable V into table T starting at row
+   R.  The variable will be described in the format given by
+   FLAGS.  Returns the next row available for use in the
+   table. */
+static struct table *
+describe_variable (const struct variable *v, int flags)
+{
+  struct table *table;
+
+  table = flags & ~DF_DICT_INDEX ? describe_variable_details (v, flags) : NULL;
   table = table_hpaste (table_from_string (0, var_get_name (v)),
-                        table_stomp (table));
+                        table ? table_stomp (table) : NULL);
   if (flags & DF_DICT_INDEX)
     {
       char s[INT_BUFSIZE_BOUND (size_t)];
@@ -944,22 +958,15 @@ equal_suffix (const struct encoding *encodings, size_t n_encodings,
 }
 
 static void
-report_encodings (const struct file_handle *h, const struct sfm_reader *r)
+report_encodings (const struct file_handle *h, struct pool *pool,
+                  char **titles, bool *ids, char **strings, size_t n_strings)
 {
-  char **titles;
-  char **strings;
-  bool *ids;
   struct encoding encodings[N_ENCODING_NAMES];
-  size_t n_encodings, n_strings, n_unique_strings;
+  size_t n_encodings, n_unique_strings;
   size_t i, j;
   struct tab_table *t;
-  struct text_item *text;
-  struct pool *pool;
   size_t row;
 
-  pool = pool_create ();
-  n_strings = sfm_get_strings (r, pool, &titles, &ids, &strings);
-
   n_encodings = 0;
   for (i = 0; i < N_ENCODING_NAMES; i++)
     {
@@ -994,20 +1001,15 @@ report_encodings (const struct file_handle *h, const struct sfm_reader *r)
   if (!n_encodings)
     {
       msg (SW, _("No valid encodings found."));
-      pool_destroy (pool);
       return;
     }
 
-  text = text_item_create_format (
-    TEXT_ITEM_PARAGRAPH,
-    _("The following table lists the encodings that can successfully read %s, "
-      "by specifying the encoding name on the GET command's ENCODING "
-      "subcommand.  Encodings that yield identical text are listed "
-      "together."), fh_get_name (h));
-  text_item_submit (text);
-
   t = tab_create (2, n_encodings + 1);
   tab_title (t, _("Usable encodings for %s."), fh_get_name (h));
+  tab_caption (t, _("Encodings that can successfully read %s (by specifying "
+                    "the encoding name on the GET command's ENCODING "
+                    "subcommand).  Encodings that yield identical text are "
+                    "listed together."), fh_get_name (h));
   tab_headers (t, 1, 0, 1, 0);
   tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 1, n_encodings);
   tab_hline (t, TAL_1, 0, 1, 1);
@@ -1034,20 +1036,13 @@ report_encodings (const struct file_handle *h, const struct sfm_reader *r)
     if (!all_equal (encodings, n_encodings, i))
       n_unique_strings++;
   if (!n_unique_strings)
-    {
-      pool_destroy (pool);
-      return;
-    }
-
-  text = text_item_create_format (
-    TEXT_ITEM_PARAGRAPH,
-    _("The following table lists text strings in the file dictionary that "
-      "the encodings above interpret differently, along with those "
-      "interpretations."));
-  text_item_submit (text);
+    return;
 
   t = tab_create (3, (n_encodings * n_unique_strings) + 1);
   tab_title (t, _("%s encoded text strings."), fh_get_name (h));
+  tab_caption (t, _("Text strings in the file dictionary that the previously "
+                    "listed encodings interpret differently, along with the "
+                    "interpretations."));
   tab_headers (t, 1, 0, 1, 0);
   tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 2, n_encodings * n_unique_strings);
   tab_hline (t, TAL_1, 0, 2, 1);
@@ -1090,8 +1085,6 @@ report_encodings (const struct file_handle *h, const struct sfm_reader *r)
           }
       }
   tab_submit (t);
-
-  pool_destroy (pool);
 }
 
 static unsigned int