output: Break struct pivot_table_look out of struct pivot_table.
[pspp] / utilities / pspp-output.c
index 58547196bec6451e2b7a1f5b755d4ca1109887a5..256a6e257519d517b17e8a502445387082c8fe8f 100644 (file)
@@ -35,6 +35,7 @@
 #include "output/spv/spv-legacy-data.h"
 #include "output/spv/spv-output.h"
 #include "output/spv/spv-select.h"
+#include "output/spv/spv-table-look.h"
 #include "output/spv/spv.h"
 #include "output/table-item.h"
 #include "output/text-item.h"
@@ -75,6 +76,9 @@ static bool raw;
 /* -f, --force: Keep output file even on error. */
 static bool force;
 
+/* --table-look: TableLook to replace table style for conversion. */
+static struct pivot_table_look *table_look;
+
 /* Number of warnings issued. */
 static size_t n_warnings;
 
@@ -278,6 +282,9 @@ run_convert (int argc UNUSED, char **argv)
   if (err)
     error (1, 0, "%s", err);
 
+  if (table_look)
+    spv_item_set_table_look (spv_get_root (spv), table_look);
+
   output_engine_push ();
   output_set_filename (argv[1]);
   string_map_replace (&output_options, "output-file", argv[2]);
@@ -318,6 +325,43 @@ run_convert (int argc UNUSED, char **argv)
     }
 }
 
+static const struct pivot_table *
+get_first_table (const struct spv_reader *spv)
+{
+  struct spv_item **items;
+  size_t n_items;
+  spv_select (spv, criteria, n_criteria, &items, &n_items);
+
+  for (size_t i = 0; i < n_items; i++)
+    if (spv_item_is_table (items[i]))
+      {
+        free (items);
+        return spv_item_get_table (items[i]);
+      }
+
+  free (items);
+  return NULL;
+}
+
+static void
+run_get_table_look (int argc UNUSED, char **argv)
+{
+  struct spv_reader *spv;
+  char *err = spv_open (argv[1], &spv);
+  if (err)
+    error (1, 0, "%s", err);
+
+  const struct pivot_table *table = get_first_table (spv);
+  if (!table)
+    error (1, 0, "%s: no tables found", argv[1]);
+
+  err = spv_table_look_write (argv[2], pivot_table_get_look (table));
+  if (err)
+    error (1, 0, "%s", err);
+
+  spv_close (spv);
+}
+
 static void
 run_dump (int argc UNUSED, char **argv)
 {
@@ -665,6 +709,7 @@ static const struct command commands[] =
     { "detect", 1, 1, run_detect },
     { "dir", 1, 1, run_directory },
     { "convert", 2, 2, run_convert },
+    { "get-table-look", 2, 2, run_get_table_look },
 
     /* Undocumented commands. */
     { "dump", 1, 1, run_dump },
@@ -746,6 +791,11 @@ main (int argc, char **argv)
 
   c->run (argc, argv);
 
+  if (table_look)
+    {
+      pivot_table_look_uninit (table_look);
+      free (table_look);
+    }
   i18n_done ();
 
   return n_warnings ? EXIT_FAILURE : EXIT_SUCCESS;
@@ -872,6 +922,20 @@ parse_members (const char *arg)
   string_array_parse (&cm->members, ss_cstr (arg), ss_cstr (","));
 }
 
+static void
+parse_table_look (const char *arg)
+{
+  if (table_look)
+    {
+      pivot_table_look_uninit (table_look);
+      free (table_look);
+    }
+
+  char *error_s = spv_table_look_read (arg, &table_look);
+  if (error_s)
+    error (1, 0, "%s", error_s);
+}
+
 static void
 parse_options (int argc, char *argv[])
 {
@@ -892,6 +956,7 @@ parse_options (int argc, char *argv[])
           OPT_OR,
           OPT_SORT,
           OPT_RAW,
+          OPT_TABLE_LOOK,
         };
       static const struct option long_options[] =
         {
@@ -912,6 +977,7 @@ parse_options (int argc, char *argv[])
 
           /* "convert" command options. */
           { "force", no_argument, NULL, 'f' },
+          { "table-look", required_argument, NULL, OPT_TABLE_LOOK },
 
           /* "dump-light-table" command options. */
           { "sort", no_argument, NULL, OPT_SORT },
@@ -987,6 +1053,10 @@ parse_options (int argc, char *argv[])
           raw = true;
           break;
 
+        case OPT_TABLE_LOOK:
+          parse_table_look (optarg);
+          break;
+
         case 'f':
           force = true;
           break;
@@ -1030,6 +1100,7 @@ The following commands are available:\n\
   detect FILE            Detect whether FILE is an SPV file.\n\
   dir FILE               List tables and other items in FILE.\n\
   convert SOURCE DEST    Convert .spv SOURCE to DEST.\n\
+  get-table-look SOURCE DEST  Copies first selected TableLook into DEST\n\
 \n\
 Input selection options for \"dir\" and \"convert\":\n\
   --select=CLASS...   include only some kinds of objects\n\
@@ -1048,6 +1119,7 @@ The following options override \"convert\" behavior:\n\
   -O format=FORMAT          set destination format to FORMAT\n\
   -O OPTION=VALUE           set output option\n\
   -f, --force               keep output file even given errors\n\
+  --table-look=FILE         override tables' style with TableLook from FILE\n\
 Other options:\n\
   --help              display this help and exit\n\
   --version           output version information and exit\n",