- struct file_handle *fh = NULL;
- struct dictionary *dict = NULL;
- struct save_trns *t = NULL;
- int compress = get_scompression ();
- const int default_version = 3;
- int version = default_version;
- short no_name_table = 0;
-
- t = xmalloc (sizeof *t);
- t->h.proc = save_trns_proc;
- t->h.free = save_trns_free;
- t->writer = NULL;
- t->map = NULL;
- case_nullify (&t->bounce);
-
+ if (aw != NULL)
+ {
+ switch (aw->writer_type)
+ {
+ case PORFILE_WRITER:
+ pfm_close_writer (aw->writer);
+ break;
+ case SYSFILE_WRITER:
+ sfm_close_writer (aw->writer);
+ break;
+ }
+ destroy_case_map (aw->map);
+ case_destroy (&aw->bounce);
+ free (aw);
+ }
+}
+
+/* Parses SAVE or XSAVE or EXPORT or XEXPORT command.
+ WRITER_TYPE identifies the type of file to write,
+ and COMMAND_TYPE identifies the type of command.
+
+ On success, returns a writer.
+ For procedures only, sets *RETAIN_UNSELECTED to true if cases
+ that would otherwise be excluded by FILTER or USE should be
+ included.
+
+ On failure, returns a null pointer. */
+static struct any_writer *
+parse_write_command (enum writer_type writer_type,
+ enum command_type command_type,
+ bool *retain_unselected)
+{
+ /* Common data. */
+ struct file_handle *handle; /* Output file. */
+ struct dictionary *dict; /* Dictionary for output file. */
+ struct any_writer *aw; /* Writer. */
+
+ /* 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;
+
+ assert (writer_type == SYSFILE_WRITER || writer_type == PORFILE_WRITER);
+ assert (command_type == XFORM_CMD || command_type == PROC_CMD);
+ assert ((retain_unselected != NULL) == (command_type == PROC_CMD));
+
+ if (command_type == PROC_CMD)
+ *retain_unselected = true;
+
+ handle = NULL;
+ dict = dict_clone (default_dict);
+ aw = xmalloc (sizeof *aw);
+ aw->writer_type = writer_type;
+ aw->writer = NULL;
+ aw->map = NULL;
+ case_nullify (&aw->bounce);
+ print_map = false;
+ print_short_names = false;
+ sysfile_opts = sfm_writer_default_options ();
+ porfile_opts = pfm_writer_default_options ();
+
+ start_case_map (dict);
+ dict_delete_scratch_vars (dict);