pc+-file-reader: Drop unneeded struct member.
[pspp] / src / language / commands / sys-file-info.c
index c3ceefb6992b68dbcabdda3f2a9fb8a11e69489f..6611dcd957c237a9e14594f4b1317418150c8e22 100644 (file)
 #include "data/missing-values.h"
 #include "data/value-labels.h"
 #include "data/variable.h"
+#include "data/varset.h"
 #include "data/vector.h"
 #include "language/command.h"
 #include "language/commands/file-handle.h"
 #include "language/lexer/lexer.h"
+#include "language/lexer/macro.h"
 #include "language/lexer/variable-parser.h"
 #include "libpspp/array.h"
 #include "libpspp/hash-functions.h"
@@ -271,23 +273,17 @@ error:
 \f
 /* DISPLAY utility. */
 
-static void display_macros (void);
 static void display_documents (const struct dictionary *dict);
 static void display_vectors (const struct dictionary *dict, int sorted);
 
 int
 cmd_display (struct lexer *lexer, struct dataset *ds)
 {
-  /* Whether to sort the list of variables alphabetically. */
-  int sorted;
-
   /* Variables to display. */
   size_t n;
   const struct variable **vl;
 
-  if (lex_match_id (lexer, "MACROS"))
-    display_macros ();
-  else if (lex_match_id (lexer, "DOCUMENTS"))
+  if (lex_match_id (lexer, "DOCUMENTS"))
     display_documents (dataset_dict (ds));
   else if (lex_match_id (lexer, "FILE"))
     {
@@ -308,7 +304,7 @@ cmd_display (struct lexer *lexer, struct dataset *ds)
     {
       int flags;
 
-      sorted = lex_match_id (lexer, "SORTED");
+      bool sorted = lex_match_id (lexer, "SORTED");
 
       if (lex_match_id (lexer, "VECTORS"))
        {
@@ -397,10 +393,118 @@ cmd_display (struct lexer *lexer, struct dataset *ds)
   return CMD_SUCCESS;
 }
 
-static void
-display_macros (void)
+static int
+compare_macros_by_name (const void *a_, const void *b_, const void *aux UNUSED)
 {
-  msg (SW, _("Macros not supported."));
+  const struct macro *const *ap = a_;
+  const struct macro *const *bp = b_;
+  const struct macro *a = *ap;
+  const struct macro *b = *bp;
+
+  return utf8_strcasecmp (a->name, b->name);
+}
+
+int
+cmd_display_macros (struct lexer *lexer, struct dataset *ds UNUSED)
+{
+  const struct macro_set *set = lex_get_macros (lexer);
+
+  if (hmap_is_empty (&set->macros))
+    {
+      msg (SN, _("No macros to display."));
+      return CMD_SUCCESS;
+    }
+
+  const struct macro **macros = xnmalloc (hmap_count (&set->macros),
+                                          sizeof *macros);
+  size_t n = 0;
+  const struct macro *m;
+  HMAP_FOR_EACH (m, struct macro, hmap_node, &set->macros)
+    macros[n++] = m;
+  assert (n == hmap_count (&set->macros));
+  sort (macros, n, sizeof *macros, compare_macros_by_name, NULL);
+
+  struct pivot_table *table = pivot_table_create (N_("Macros"));
+
+  struct pivot_dimension *attributes = pivot_dimension_create (
+    table, PIVOT_AXIS_COLUMN, N_("Attributes"));
+  pivot_category_create_leaf (attributes->root,
+                              pivot_value_new_text (N_("Source Location")));
+
+  struct pivot_dimension *names = pivot_dimension_create (
+    table, PIVOT_AXIS_ROW, N_("Name"));
+  names->root->show_label = true;
+
+  for (size_t i = 0; i < n; i++)
+    {
+      const struct macro *m = macros[i];
+
+      pivot_category_create_leaf (names->root,
+                                  pivot_value_new_user_text (m->name, -1));
+
+      struct string location = DS_EMPTY_INITIALIZER;
+      msg_location_format (m->location, &location);
+      pivot_table_put2 (
+        table, 0, i,
+        pivot_value_new_user_text_nocopy (ds_steal_cstr (&location)));
+    }
+
+  pivot_table_submit (table);
+
+  free (macros);
+
+  return CMD_SUCCESS;
+}
+
+int
+cmd_display_variable_sets (struct lexer *lexer UNUSED, struct dataset *ds)
+{
+  const struct dictionary *dict = dataset_dict (ds);
+  size_t n_varsets = dict_get_n_varsets (dict);
+  if (n_varsets == 0)
+    {
+      msg (SN, _("No variable sets defined."));
+      return CMD_SUCCESS;
+    }
+
+  struct pivot_table *table = pivot_table_create (N_("Variable Sets"));
+  pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Attributes"),
+                          N_("Variable"));
+  struct pivot_dimension *varset_dim = pivot_dimension_create (
+    table, PIVOT_AXIS_ROW, N_("Variable Set and Position"));
+  varset_dim->root->show_label = true;
+
+  for (size_t i = 0; i < n_varsets; i++)
+    {
+      const struct varset *vs = dict_get_varset (dict, i);
+
+      struct pivot_category *group = pivot_category_create_group__ (
+        varset_dim->root, pivot_value_new_user_text (
+          vs->name, -1));
+
+      for (size_t j = 0; j < vs->n_vars; j++)
+        {
+          struct variable *var = vs->vars[j];
+
+          int row = pivot_category_create_leaf (
+            group, pivot_value_new_integer (j + 1));
+
+          pivot_table_put2 (table, 0, row, pivot_value_new_variable (var));
+        }
+
+      if (!vs->n_vars)
+        {
+          int row = pivot_category_create_leaf (
+            group, pivot_value_new_user_text ("n/a", -1));
+
+          pivot_table_put2 (table, 0, row,
+                            pivot_value_new_text (N_("(empty)")));
+        }
+    }
+
+  pivot_table_submit (table);
+
+  return CMD_SUCCESS;
 }
 
 static char *
@@ -514,7 +618,7 @@ display_variables (const struct variable **vl, size_t n, int flags)
 
       if (flags & DF_PRINT_FORMAT)
         {
-          const struct fmt_spec *print = var_get_print_format (v);
+          struct fmt_spec print = var_get_print_format (v);
           char s[FMT_STRING_LEN_MAX + 1];
 
           pivot_table_put2 (
@@ -524,7 +628,7 @@ display_variables (const struct variable **vl, size_t n, int flags)
 
       if (flags & DF_WRITE_FORMAT)
         {
-          const struct fmt_spec *write = var_get_write_format (v);
+          struct fmt_spec write = var_get_write_format (v);
           char s[FMT_STRING_LEN_MAX + 1];
 
           pivot_table_put2 (