Clean up how transformations work.
[pspp] / src / language / data-io / save.c
index e94c020b168d3a59473b396c0c64aa405bcf1877..88f3c75ee59a23d66a628995843d3b6dbaf23f27 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2006, 2007, 2008 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 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
 
 #include <stdlib.h>
 
-#include <data/any-writer.h>
-#include <data/case-map.h>
-#include <data/case.h>
-#include <data/casereader.h>
-#include <data/casewriter.h>
-#include <data/dictionary.h>
-#include <data/por-file-writer.h>
-#include <data/procedure.h>
-#include <data/sys-file-writer.h>
-#include <data/transformations.h>
-#include <data/variable.h>
-#include <language/command.h>
-#include <language/data-io/file-handle.h>
-#include <language/data-io/trim.h>
-#include <language/lexer/lexer.h>
-#include <libpspp/assertion.h>
-#include <libpspp/compiler.h>
-
-#include "xalloc.h"
+#include "data/any-writer.h"
+#include "data/case-map.h"
+#include "data/case.h"
+#include "data/casereader.h"
+#include "data/casewriter.h"
+#include "data/dataset.h"
+#include "data/dictionary.h"
+#include "data/mdd-writer.h"
+#include "data/por-file-writer.h"
+#include "data/sys-file-writer.h"
+#include "data/transformations.h"
+#include "data/variable.h"
+#include "language/command.h"
+#include "language/data-io/file-handle.h"
+#include "language/data-io/trim.h"
+#include "language/lexer/lexer.h"
+#include "libpspp/assertion.h"
+#include "libpspp/compiler.h"
+#include "libpspp/message.h"
+
+#include "gl/xalloc.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -68,6 +70,12 @@ cmd_save (struct lexer *lexer, struct dataset *ds)
   return parse_output_proc (lexer, ds, SYSFILE_WRITER);
 }
 
+int
+cmd_save_data_collection (struct lexer *lexer, struct dataset *ds)
+{
+  return parse_output_proc (lexer, ds, SYSFILE_WRITER);
+}
+
 int
 cmd_export (struct lexer *lexer, struct dataset *ds)
 {
@@ -91,8 +99,7 @@ struct output_trns
     struct casewriter *writer;          /* Writer. */
   };
 
-static trns_proc_func output_trns_proc;
-static trns_free_func output_trns_free;
+static const struct trns_class output_trns_class;
 static struct casewriter *parse_write_command (struct lexer *,
                                                struct dataset *,
                                                enum writer_type,
@@ -105,7 +112,6 @@ parse_output_proc (struct lexer *lexer, struct dataset *ds,
                    enum writer_type writer_type)
 {
   bool retain_unselected;
-  struct variable *saved_filter_variable;
   struct casewriter *output;
   bool ok;
 
@@ -114,16 +120,10 @@ parse_output_proc (struct lexer *lexer, struct dataset *ds,
   if (output == NULL)
     return CMD_CASCADING_FAILURE;
 
-  saved_filter_variable = dict_get_filter (dataset_dict (ds));
-  if (retain_unselected)
-    dict_set_filter (dataset_dict (ds), NULL);
-
-  casereader_transfer (proc_open (ds), output);
+  casereader_transfer (proc_open_filtering (ds, !retain_unselected), output);
   ok = casewriter_destroy (output);
   ok = proc_commit (ds) && ok;
 
-  dict_set_filter (dataset_dict (ds), saved_filter_variable);
-
   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
 }
 
@@ -139,7 +139,7 @@ parse_output_trns (struct lexer *lexer, struct dataset *ds, enum writer_type wri
       return CMD_CASCADING_FAILURE;
     }
 
-  add_transformation (ds, output_trns_proc, output_trns_free, t);
+  add_transformation (ds, &output_trns_class, t);
   return CMD_SUCCESS;
 }
 
@@ -161,13 +161,14 @@ parse_write_command (struct lexer *lexer, struct dataset *ds,
 {
   /* Common data. */
   struct file_handle *handle; /* Output file. */
+  struct file_handle *metadata; /* MDD output file. */
   struct dictionary *dict;    /* Dictionary for output file. */
   struct casewriter *writer;  /* Writer. */
+  struct case_map_stage *stage; /* Preparation for 'map'. */
   struct case_map *map;       /* Map from input data to data for writer. */
+  const char *sav_name = "";
 
   /* Common options. */
-  bool print_map;             /* Print map?  TODO. */
-  bool print_short_names;     /* Print long-to-short name map.  TODO. */
   struct sfm_write_options sysfile_opts;
   struct pfm_write_options porfile_opts;
 
@@ -179,18 +180,18 @@ parse_write_command (struct lexer *lexer, struct dataset *ds,
     *retain_unselected = true;
 
   handle = NULL;
+  metadata = NULL;
   dict = dict_clone (dataset_dict (ds));
   writer = NULL;
+  stage = NULL;
   map = NULL;
-  print_map = false;
-  print_short_names = false;
   sysfile_opts = sfm_writer_default_options ();
   porfile_opts = pfm_writer_default_options ();
 
-  case_map_prepare_dict (dict);
+  stage = case_map_stage_create (dict);
   dict_delete_scratch_vars (dict);
 
-  lex_match (lexer, '/');
+  lex_match (lexer, T_SLASH);
   for (;;)
     {
       if (lex_match_id (lexer, "OUTFILE"))
@@ -201,139 +202,172 @@ parse_write_command (struct lexer *lexer, struct dataset *ds,
               goto error;
             }
 
-         lex_match (lexer, '=');
+         lex_match (lexer, T_EQUALS);
 
-         handle = fh_parse (lexer, FH_REF_FILE | FH_REF_SCRATCH);
+         handle = fh_parse (lexer, FH_REF_FILE, NULL);
          if (handle == NULL)
            goto error;
        }
+      else if (lex_match_id (lexer, "METADATA"))
+       {
+          if (metadata != NULL)
+            {
+              lex_sbc_only_once ("METADATA");
+              goto error;
+            }
+
+         lex_match (lexer, T_EQUALS);
+
+         metadata = fh_parse (lexer, FH_REF_FILE, NULL);
+         if (metadata == NULL)
+           goto error;
+       }
       else if (lex_match_id (lexer, "NAMES"))
-        print_short_names = true;
+        {
+          /* Not yet implemented. */
+        }
       else if (lex_match_id (lexer, "PERMISSIONS"))
         {
           bool cw;
 
-          lex_match (lexer, '=');
+          lex_match (lexer, T_EQUALS);
           if (lex_match_id (lexer, "READONLY"))
             cw = false;
           else if (lex_match_id (lexer, "WRITEABLE"))
             cw = true;
           else
             {
-              lex_error (lexer, _("expecting %s or %s"),
-                         "READONLY", "WRITEABLE");
+              lex_error_expecting (lexer, "READONLY", "WRITEABLE");
               goto error;
             }
           sysfile_opts.create_writeable = porfile_opts.create_writeable = cw;
         }
       else if (command_type == PROC_CMD && lex_match_id (lexer, "UNSELECTED"))
         {
-          lex_match (lexer, '=');
+          lex_match (lexer, T_EQUALS);
           if (lex_match_id (lexer, "RETAIN"))
             *retain_unselected = true;
           else if (lex_match_id (lexer, "DELETE"))
             *retain_unselected = false;
           else
             {
-              lex_error (lexer, _("expecting %s or %s"), "RETAIN", "DELETE");
+              lex_error_expecting (lexer, "RETAIN", "DELETE");
               goto error;
             }
         }
       else if (writer_type == SYSFILE_WRITER
                && lex_match_id (lexer, "COMPRESSED"))
-       sysfile_opts.compress = true;
+       sysfile_opts.compression = ANY_COMP_SIMPLE;
       else if (writer_type == SYSFILE_WRITER
                && lex_match_id (lexer, "UNCOMPRESSED"))
-       sysfile_opts.compress = false;
+       sysfile_opts.compression = ANY_COMP_NONE;
+      else if (writer_type == SYSFILE_WRITER
+               && lex_match_id (lexer, "ZCOMPRESSED"))
+       sysfile_opts.compression = ANY_COMP_ZLIB;
       else if (writer_type == SYSFILE_WRITER
                && lex_match_id (lexer, "VERSION"))
        {
-         lex_match (lexer, '=');
-         if (!lex_force_int (lexer))
+         lex_match (lexer, T_EQUALS);
+         if (!lex_force_int_range (lexer, "VERSION", 2, 3))
             goto error;
           sysfile_opts.version = lex_integer (lexer);
           lex_get (lexer);
        }
       else if (writer_type == PORFILE_WRITER && lex_match_id (lexer, "TYPE"))
         {
-          lex_match (lexer, '=');
+          lex_match (lexer, T_EQUALS);
           if (lex_match_id (lexer, "COMMUNICATIONS"))
             porfile_opts.type = PFM_COMM;
           else if (lex_match_id (lexer, "TAPE"))
             porfile_opts.type = PFM_TAPE;
           else
             {
-              lex_error (lexer, _("expecting %s or %s"), "COMM", "TAPE");
+              lex_error_expecting (lexer, "COMM", "TAPE");
               goto error;
             }
         }
       else if (writer_type == PORFILE_WRITER && lex_match_id (lexer, "DIGITS"))
         {
-          lex_match (lexer, '=');
-          if (!lex_force_int (lexer))
+          lex_match (lexer, T_EQUALS);
+          if (!lex_force_int_range (lexer, "DIGITS", 1, INT_MAX))
             goto error;
           porfile_opts.digits = lex_integer (lexer);
           lex_get (lexer);
         }
-      else if (!parse_dict_trim (lexer, dict))
+      else if (!parse_dict_trim (lexer, dict, false))
         goto error;
 
-      if (!lex_match (lexer, '/'))
+      if (!lex_match (lexer, T_SLASH))
        break;
     }
   if (lex_end_of_command (lexer) != CMD_SUCCESS)
     goto error;
 
-  if (handle == NULL)
+  if (!handle && !metadata)
     {
-      lex_sbc_missing (lexer, "OUTFILE");
+      msg (SE, _("The OUTFILE or METADATA subcommand is required."));
       goto error;
     }
 
   dict_delete_scratch_vars (dict);
   dict_compact_values (dict);
 
-  if (fh_get_referent (handle) == FH_REF_FILE)
+  if (handle)
     {
-      switch (writer_type)
+      if (metadata)
+        sav_name = (fh_get_referent (handle) == FH_REF_FILE
+                    ? fh_get_file_name (handle)
+                    : fh_get_name (handle));
+      if (fh_get_referent (handle) == FH_REF_FILE)
         {
-        case SYSFILE_WRITER:
-          writer = sfm_open_writer (handle, dict, sysfile_opts);
-          break;
-        case PORFILE_WRITER:
-          writer = pfm_open_writer (handle, dict, porfile_opts);
-          break;
+          switch (writer_type)
+            {
+            case SYSFILE_WRITER:
+              writer = sfm_open_writer (handle, dict, sysfile_opts);
+              break;
+            case PORFILE_WRITER:
+              writer = pfm_open_writer (handle, dict, porfile_opts);
+              break;
+            }
         }
+      else
+        writer = any_writer_open (handle, dict);
+      if (writer == NULL)
+        goto error;
     }
-  else
-    writer = any_writer_open (handle, dict);
-  if (writer == NULL)
-    goto error;
 
-  map = case_map_from_dict (dict);
+  if (metadata)
+    {
+      if (!mdd_write (metadata, dict, sav_name))
+        goto error;
+    }
+
+  map = case_map_stage_get_case_map (stage);
+  case_map_stage_destroy (stage);
   if (map != NULL)
     writer = case_map_create_output_translator (map, writer);
-  dict_destroy (dict);
+  dict_unref (dict);
 
   fh_unref (handle);
+  fh_unref (metadata);
   return writer;
 
  error:
+  case_map_stage_destroy (stage);
   fh_unref (handle);
+  fh_unref (metadata);
   casewriter_destroy (writer);
-  dict_destroy (dict);
+  dict_unref (dict);
   case_map_destroy (map);
   return NULL;
 }
 
-/* Writes case C to the system file specified on XSAVE or XEXPORT. */
-static int
-output_trns_proc (void *trns_, struct ccase *c, casenumber case_num UNUSED)
+/* Writes case *C to the system file specified on XSAVE or XEXPORT. */
+static enum trns_result
+output_trns_proc (void *trns_, struct ccase **c, casenumber case_num UNUSED)
 {
   struct output_trns *t = trns_;
-  struct ccase tmp;
-  case_clone (&tmp, c);
-  casewriter_write (t->writer, &tmp);
+  casewriter_write (t->writer, case_ref (*c));
   return TRNS_CONTINUE;
 }
 
@@ -347,3 +381,9 @@ output_trns_free (void *trns_)
   free (t);
   return ok;
 }
+
+static const struct trns_class output_trns_class = {
+  .name = "XSAVE/XEXPORT",
+  .execute = output_trns_proc,
+  .destroy = output_trns_free,
+};