1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "data/any-writer.h"
22 #include "data/case-map.h"
23 #include "data/case.h"
24 #include "data/casereader.h"
25 #include "data/casewriter.h"
26 #include "data/dataset.h"
27 #include "data/dictionary.h"
28 #include "data/mdd-writer.h"
29 #include "data/por-file-writer.h"
30 #include "data/sys-file-writer.h"
31 #include "data/transformations.h"
32 #include "data/variable.h"
33 #include "language/command.h"
34 #include "language/data-io/file-handle.h"
35 #include "language/data-io/trim.h"
36 #include "language/lexer/lexer.h"
37 #include "libpspp/assertion.h"
38 #include "libpspp/compiler.h"
39 #include "libpspp/message.h"
41 #include "gl/xalloc.h"
44 #define _(msgid) gettext (msgid)
46 /* Writing system and portable files. */
48 /* Type of output file. */
51 SYSFILE_WRITER, /* System file. */
52 PORFILE_WRITER /* Portable file. */
55 /* Type of a command. */
58 XFORM_CMD, /* Transformation. */
59 PROC_CMD /* Procedure. */
62 static int parse_output_proc (struct lexer *, struct dataset *,
64 static int parse_output_trns (struct lexer *, struct dataset *,
68 cmd_save (struct lexer *lexer, struct dataset *ds)
70 return parse_output_proc (lexer, ds, SYSFILE_WRITER);
74 cmd_save_data_collection (struct lexer *lexer, struct dataset *ds)
76 return parse_output_proc (lexer, ds, SYSFILE_WRITER);
80 cmd_export (struct lexer *lexer, struct dataset *ds)
82 return parse_output_proc (lexer, ds, PORFILE_WRITER);
86 cmd_xsave (struct lexer *lexer, struct dataset *ds)
88 return parse_output_trns (lexer, ds, SYSFILE_WRITER);
92 cmd_xexport (struct lexer *lexer, struct dataset *ds)
94 return parse_output_trns (lexer, ds, PORFILE_WRITER);
99 struct casewriter *writer; /* Writer. */
102 static trns_proc_func output_trns_proc;
103 static trns_free_func output_trns_free;
104 static struct casewriter *parse_write_command (struct lexer *,
108 bool *retain_unselected);
110 /* Parses and performs the SAVE or EXPORT procedure. */
112 parse_output_proc (struct lexer *lexer, struct dataset *ds,
113 enum writer_type writer_type)
115 bool retain_unselected;
116 struct casewriter *output;
119 output = parse_write_command (lexer, ds, writer_type, PROC_CMD,
122 return CMD_CASCADING_FAILURE;
124 casereader_transfer (proc_open_filtering (ds, !retain_unselected), output);
125 ok = casewriter_destroy (output);
126 ok = proc_commit (ds) && ok;
128 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
131 /* Parses the XSAVE or XEXPORT transformation command. */
133 parse_output_trns (struct lexer *lexer, struct dataset *ds, enum writer_type writer_type)
135 struct output_trns *t = xmalloc (sizeof *t);
136 t->writer = parse_write_command (lexer, ds, writer_type, XFORM_CMD, NULL);
137 if (t->writer == NULL)
140 return CMD_CASCADING_FAILURE;
143 add_transformation (ds, output_trns_proc, output_trns_free, t);
147 /* Parses SAVE or XSAVE or EXPORT or XEXPORT command.
148 WRITER_TYPE identifies the type of file to write,
149 and COMMAND_TYPE identifies the type of command.
151 On success, returns a writer.
152 For procedures only, sets *RETAIN_UNSELECTED to true if cases
153 that would otherwise be excluded by FILTER or USE should be
156 On failure, returns a null pointer. */
157 static struct casewriter *
158 parse_write_command (struct lexer *lexer, struct dataset *ds,
159 enum writer_type writer_type,
160 enum command_type command_type,
161 bool *retain_unselected)
164 struct file_handle *handle; /* Output file. */
165 struct file_handle *metadata; /* MDD output file. */
166 struct dictionary *dict; /* Dictionary for output file. */
167 struct casewriter *writer; /* Writer. */
168 struct case_map_stage *stage; /* Preparation for 'map'. */
169 struct case_map *map; /* Map from input data to data for writer. */
170 const char *sav_name = "";
172 /* Common options. */
173 struct sfm_write_options sysfile_opts;
174 struct pfm_write_options porfile_opts;
176 assert (writer_type == SYSFILE_WRITER || writer_type == PORFILE_WRITER);
177 assert (command_type == XFORM_CMD || command_type == PROC_CMD);
178 assert ((retain_unselected != NULL) == (command_type == PROC_CMD));
180 if (command_type == PROC_CMD)
181 *retain_unselected = true;
185 dict = dict_clone (dataset_dict (ds));
189 sysfile_opts = sfm_writer_default_options ();
190 porfile_opts = pfm_writer_default_options ();
192 stage = case_map_stage_create (dict);
193 dict_delete_scratch_vars (dict);
195 lex_match (lexer, T_SLASH);
198 if (lex_match_id (lexer, "OUTFILE"))
202 lex_sbc_only_once ("OUTFILE");
206 lex_match (lexer, T_EQUALS);
208 handle = fh_parse (lexer, FH_REF_FILE, NULL);
212 else if (lex_match_id (lexer, "METADATA"))
214 if (metadata != NULL)
216 lex_sbc_only_once ("METADATA");
220 lex_match (lexer, T_EQUALS);
222 metadata = fh_parse (lexer, FH_REF_FILE, NULL);
223 if (metadata == NULL)
226 else if (lex_match_id (lexer, "NAMES"))
228 /* Not yet implemented. */
230 else if (lex_match_id (lexer, "PERMISSIONS"))
234 lex_match (lexer, T_EQUALS);
235 if (lex_match_id (lexer, "READONLY"))
237 else if (lex_match_id (lexer, "WRITEABLE"))
241 lex_error_expecting (lexer, "READONLY", "WRITEABLE");
244 sysfile_opts.create_writeable = porfile_opts.create_writeable = cw;
246 else if (command_type == PROC_CMD && lex_match_id (lexer, "UNSELECTED"))
248 lex_match (lexer, T_EQUALS);
249 if (lex_match_id (lexer, "RETAIN"))
250 *retain_unselected = true;
251 else if (lex_match_id (lexer, "DELETE"))
252 *retain_unselected = false;
255 lex_error_expecting (lexer, "RETAIN", "DELETE");
259 else if (writer_type == SYSFILE_WRITER
260 && lex_match_id (lexer, "COMPRESSED"))
261 sysfile_opts.compression = ANY_COMP_SIMPLE;
262 else if (writer_type == SYSFILE_WRITER
263 && lex_match_id (lexer, "UNCOMPRESSED"))
264 sysfile_opts.compression = ANY_COMP_NONE;
265 else if (writer_type == SYSFILE_WRITER
266 && lex_match_id (lexer, "ZCOMPRESSED"))
267 sysfile_opts.compression = ANY_COMP_ZLIB;
268 else if (writer_type == SYSFILE_WRITER
269 && lex_match_id (lexer, "VERSION"))
271 lex_match (lexer, T_EQUALS);
272 if (!lex_force_int (lexer))
274 sysfile_opts.version = lex_integer (lexer);
277 else if (writer_type == PORFILE_WRITER && lex_match_id (lexer, "TYPE"))
279 lex_match (lexer, T_EQUALS);
280 if (lex_match_id (lexer, "COMMUNICATIONS"))
281 porfile_opts.type = PFM_COMM;
282 else if (lex_match_id (lexer, "TAPE"))
283 porfile_opts.type = PFM_TAPE;
286 lex_error_expecting (lexer, "COMM", "TAPE");
290 else if (writer_type == PORFILE_WRITER && lex_match_id (lexer, "DIGITS"))
292 lex_match (lexer, T_EQUALS);
293 if (!lex_force_int (lexer))
295 porfile_opts.digits = lex_integer (lexer);
298 else if (!parse_dict_trim (lexer, dict))
301 if (!lex_match (lexer, T_SLASH))
304 if (lex_end_of_command (lexer) != CMD_SUCCESS)
307 if (!handle && !metadata)
309 msg (SE, _("The OUTFILE or METADATA subcommand is required."));
313 dict_delete_scratch_vars (dict);
314 dict_compact_values (dict);
319 sav_name = (fh_get_referent (handle) == FH_REF_FILE
320 ? fh_get_file_name (handle)
321 : fh_get_name (handle));
322 if (fh_get_referent (handle) == FH_REF_FILE)
327 writer = sfm_open_writer (handle, dict, sysfile_opts);
330 writer = pfm_open_writer (handle, dict, porfile_opts);
335 writer = any_writer_open (handle, dict);
342 if (!mdd_write (metadata, dict, sav_name))
346 map = case_map_stage_get_case_map (stage);
347 case_map_stage_destroy (stage);
349 writer = case_map_create_output_translator (map, writer);
357 case_map_stage_destroy (stage);
360 casewriter_destroy (writer);
362 case_map_destroy (map);
366 /* Writes case *C to the system file specified on XSAVE or XEXPORT. */
368 output_trns_proc (void *trns_, struct ccase **c, casenumber case_num UNUSED)
370 struct output_trns *t = trns_;
371 casewriter_write (t->writer, case_ref (*c));
372 return TRNS_CONTINUE;
375 /* Frees an XSAVE or XEXPORT transformation.
376 Returns true if successful, false if an I/O error occurred. */
378 output_trns_free (void *trns_)
380 struct output_trns *t = trns_;
381 bool ok = casewriter_destroy (t->writer);