1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 2011, 2013, 2016 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/case-map.h"
22 #include "data/casereader.h"
23 #include "data/casewriter.h"
24 #include "data/csv-file-writer.h"
25 #include "data/dataset.h"
26 #include "data/dictionary.h"
27 #include "data/file-name.h"
28 #include "data/format.h"
29 #include "data/settings.h"
30 #include "language/command.h"
31 #include "language/data-io/file-handle.h"
32 #include "language/data-io/trim.h"
33 #include "language/lexer/lexer.h"
34 #include "libpspp/message.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) (msgid)
42 cmd_save_translate (struct lexer *lexer, struct dataset *ds)
44 enum { CSV_FILE = 1, TAB_FILE } type;
46 struct dictionary *dict;
47 struct case_map_stage *stage;
49 struct casewriter *writer;
50 struct file_handle *handle;
54 bool retain_unselected;
55 bool recode_user_missing;
56 bool include_var_names;
57 bool use_value_labels;
58 bool use_print_formats;
67 dict = dict_clone (dataset_dict (ds));
68 dict_set_names_must_be_ids (dict, false);
75 retain_unselected = true;
76 recode_user_missing = false;
77 include_var_names = false;
78 use_value_labels = false;
79 use_print_formats = false;
80 decimal = settings_get_fmt_settings ()->decimal;
84 stage = case_map_stage_create (dict);
85 dict_delete_scratch_vars (dict);
87 while (lex_token (lexer) != T_ENDCMD)
89 if (!lex_force_match (lexer, T_SLASH))
92 if (lex_match_id (lexer, "OUTFILE"))
96 lex_sbc_only_once ("OUTFILE");
100 lex_match (lexer, T_EQUALS);
102 handle = fh_parse (lexer, FH_REF_FILE, NULL);
106 else if (lex_match_id (lexer, "TYPE"))
110 lex_sbc_only_once ("TYPE");
114 lex_match (lexer, T_EQUALS);
115 if (lex_match_id (lexer, "CSV"))
117 else if (lex_match_id (lexer, "TAB"))
121 lex_error_expecting (lexer, "CSV", "TAB");
125 else if (lex_match_id (lexer, "REPLACE"))
127 else if (lex_match_id (lexer, "FIELDNAMES"))
128 include_var_names = true;
129 else if (lex_match_id (lexer, "MISSING"))
131 lex_match (lexer, T_EQUALS);
132 if (lex_match_id (lexer, "IGNORE"))
133 recode_user_missing = false;
134 else if (lex_match_id (lexer, "RECODE"))
135 recode_user_missing = true;
138 lex_error_expecting (lexer, "IGNORE", "RECODE");
142 else if (lex_match_id (lexer, "CELLS"))
144 lex_match (lexer, T_EQUALS);
145 if (lex_match_id (lexer, "VALUES"))
146 use_value_labels = false;
147 else if (lex_match_id (lexer, "LABELS"))
148 use_value_labels = true;
151 lex_error_expecting (lexer, "VALUES", "LABELS");
155 else if (lex_match_id (lexer, "TEXTOPTIONS"))
157 lex_match (lexer, T_EQUALS);
160 if (lex_match_id (lexer, "DELIMITER"))
162 lex_match (lexer, T_EQUALS);
163 if (!lex_force_string (lexer))
165 /* XXX should support multibyte UTF-8 delimiters */
166 if (ss_length (lex_tokss (lexer)) != 1)
168 msg (SE, _("The %s string must contain exactly one "
169 "character."), "DELIMITER");
172 delimiter = ss_first (lex_tokss (lexer));
175 else if (lex_match_id (lexer, "QUALIFIER"))
177 lex_match (lexer, T_EQUALS);
178 if (!lex_force_string (lexer))
180 /* XXX should support multibyte UTF-8 qualifiers */
181 if (ss_length (lex_tokss (lexer)) != 1)
183 msg (SE, _("The %s string must contain exactly one "
184 "character."), "QUALIFIER");
187 qualifier = ss_first (lex_tokss (lexer));
190 else if (lex_match_id (lexer, "DECIMAL"))
192 lex_match (lexer, T_EQUALS);
193 if (lex_match_id (lexer, "DOT"))
195 else if (lex_match_id (lexer, "COMMA"))
199 lex_error_expecting (lexer, "DOT", "COMMA");
203 else if (lex_match_id (lexer, "FORMAT"))
205 lex_match (lexer, T_EQUALS);
206 if (lex_match_id (lexer, "PLAIN"))
207 use_print_formats = false;
208 else if (lex_match_id (lexer, "VARIABLE"))
209 use_print_formats = true;
212 lex_error_expecting (lexer, "PLAIN", "VARIABLE");
220 else if (lex_match_id (lexer, "UNSELECTED"))
222 lex_match (lexer, T_EQUALS);
223 if (lex_match_id (lexer, "RETAIN"))
224 retain_unselected = true;
225 else if (lex_match_id (lexer, "DELETE"))
226 retain_unselected = false;
229 lex_error_expecting (lexer, "RETAIN", "DELETE");
233 else if (!parse_dict_trim (lexer, dict, true))
239 lex_sbc_missing ("TYPE");
242 else if (handle == NULL)
244 lex_sbc_missing ("OUTFILE");
247 else if (!replace && fn_exists (handle))
249 msg (SE, _("Output file `%s' exists but %s was not specified."),
250 fh_get_file_name (handle), "REPLACE");
254 dict_delete_scratch_vars (dict);
255 dict_compact_values (dict);
257 struct csv_writer_options csv_opts = {
258 .recode_user_missing = recode_user_missing,
259 .include_var_names = include_var_names,
260 .use_value_labels = use_value_labels,
261 .use_print_formats = use_print_formats,
263 .delimiter = (delimiter ? delimiter
264 : type == TAB_FILE ? '\t'
265 : decimal == '.' ? ','
267 .qualifier = qualifier,
269 writer = csv_writer_open (handle, dict, &csv_opts);
274 map = case_map_stage_get_case_map (stage);
275 case_map_stage_destroy (stage);
277 writer = case_map_create_output_translator (map, writer);
280 casereader_transfer (proc_open_filtering (ds, !retain_unselected), writer);
281 ok = casewriter_destroy (writer);
282 ok = proc_commit (ds) && ok;
284 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
287 case_map_stage_destroy (stage);
290 case_map_destroy (map);