SAVE TRANSLATE: Allow variable names with space, etc. in output.
[pspp] / src / language / data-io / save-translate.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010, 2011, 2013, 2016 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
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"
35
36 #include "xalloc.h"
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40 #define N_(msgid) (msgid)
41
42 int
43 cmd_save_translate (struct lexer *lexer, struct dataset *ds)
44 {
45   enum { CSV_FILE = 1, TAB_FILE } type;
46
47   struct dictionary *dict;
48   struct case_map_stage *stage;
49   struct case_map *map;
50   struct casewriter *writer;
51   struct file_handle *handle;
52
53   struct csv_writer_options csv_opts;
54
55   bool replace;
56
57   bool retain_unselected;
58   bool recode_user_missing;
59   bool include_var_names;
60   bool use_value_labels;
61   bool use_print_formats;
62   char decimal;
63   char delimiter;
64   char qualifier;
65
66   bool ok;
67
68   type = 0;
69
70   dict = dict_clone (dataset_dict (ds));
71   dict_set_names_must_be_ids (dict, false);
72   stage = NULL;
73   map = NULL;
74
75   handle = NULL;
76   replace = false;
77
78   retain_unselected = true;
79   recode_user_missing = false;
80   include_var_names = false;
81   use_value_labels = false;
82   use_print_formats = false;
83   decimal = settings_get_decimal_char (FMT_F);
84   delimiter = 0;
85   qualifier = '"';
86
87   stage = case_map_stage_create (dict);
88   dict_delete_scratch_vars (dict);
89
90   while (lex_token (lexer) != T_ENDCMD)
91     {
92       if (!lex_force_match (lexer, T_SLASH))
93         goto error;
94
95       if (lex_match_id (lexer, "OUTFILE"))
96         {
97           if (handle != NULL)
98             {
99               lex_sbc_only_once ("OUTFILE");
100               goto error;
101             }
102
103           lex_match (lexer, T_EQUALS);
104
105           handle = fh_parse (lexer, FH_REF_FILE, NULL);
106           if (handle == NULL)
107             goto error;
108         }
109       else if (lex_match_id (lexer, "TYPE"))
110         {
111           if (type != 0)
112             {
113               lex_sbc_only_once ("TYPE");
114               goto error;
115             }
116
117           lex_match (lexer, T_EQUALS);
118           if (lex_match_id (lexer, "CSV"))
119             type = CSV_FILE;
120           else if (lex_match_id (lexer, "TAB"))
121             type = TAB_FILE;
122           else
123             {
124               lex_error_expecting (lexer, "CSV", "TAB", NULL_SENTINEL);
125               goto error;
126             }
127         }
128       else if (lex_match_id (lexer, "REPLACE"))
129         replace = true;
130       else if (lex_match_id (lexer, "FIELDNAMES"))
131         include_var_names = true;
132       else if (lex_match_id (lexer, "MISSING"))
133         {
134           lex_match (lexer, T_EQUALS);
135           if (lex_match_id (lexer, "IGNORE"))
136             recode_user_missing = false;
137           else if (lex_match_id (lexer, "RECODE"))
138             recode_user_missing = true;
139           else
140             {
141               lex_error_expecting (lexer, "IGNORE", "RECODE", NULL_SENTINEL);
142               goto error;
143             }
144         }
145       else if (lex_match_id (lexer, "CELLS"))
146         {
147           lex_match (lexer, T_EQUALS);
148           if (lex_match_id (lexer, "VALUES"))
149             use_value_labels = false;
150           else if (lex_match_id (lexer, "LABELS"))
151             use_value_labels = true;
152           else
153             {
154               lex_error_expecting (lexer, "VALUES", "LABELS", NULL_SENTINEL);
155               goto error;
156             }
157         }
158       else if (lex_match_id (lexer, "TEXTOPTIONS"))
159         {
160           lex_match (lexer, T_EQUALS);
161           for (;;)
162             {
163               if (lex_match_id (lexer, "DELIMITER"))
164                 {
165                   lex_match (lexer, T_EQUALS);
166                   if (!lex_force_string (lexer))
167                     goto error;
168                   /* XXX should support multibyte UTF-8 delimiters */
169                   if (ss_length (lex_tokss (lexer)) != 1)
170                     {
171                       msg (SE, _("The %s string must contain exactly one "
172                                  "character."), "DELIMITER");
173                       goto error;
174                     }
175                   delimiter = ss_first (lex_tokss (lexer));
176                   lex_get (lexer);
177                 }
178               else if (lex_match_id (lexer, "QUALIFIER"))
179                 {
180                   lex_match (lexer, T_EQUALS);
181                   if (!lex_force_string (lexer))
182                     goto error;
183                   /* XXX should support multibyte UTF-8 qualifiers */
184                   if (ss_length (lex_tokss (lexer)) != 1)
185                     {
186                       msg (SE, _("The %s string must contain exactly one "
187                                  "character."), "QUALIFIER");
188                       goto error;
189                     }
190                   qualifier = ss_first (lex_tokss (lexer));
191                   lex_get (lexer);
192                 }
193               else if (lex_match_id (lexer, "DECIMAL"))
194                 {
195                   lex_match (lexer, T_EQUALS);
196                   if (lex_match_id (lexer, "DOT"))
197                     decimal = '.';
198                   else if (lex_match_id (lexer, "COMMA"))
199                     decimal = ',';
200                   else
201                     {
202                       lex_error_expecting (lexer, "DOT", "COMMA",
203                                            NULL_SENTINEL);
204                       goto error;
205                     }
206                 }
207               else if (lex_match_id (lexer, "FORMAT"))
208                 {
209                   lex_match (lexer, T_EQUALS);
210                   if (lex_match_id (lexer, "PLAIN"))
211                     use_print_formats = false;
212                   else if (lex_match_id (lexer, "VARIABLE"))
213                     use_print_formats = true;
214                   else
215                     {
216                       lex_error_expecting (lexer, "PLAIN", "VARIABLE",
217                                            NULL_SENTINEL);
218                       goto error;
219                     }
220                 }
221               else
222                 break;
223             }
224         }
225       else if (lex_match_id (lexer, "UNSELECTED"))
226         {
227           lex_match (lexer, T_EQUALS);
228           if (lex_match_id (lexer, "RETAIN"))
229             retain_unselected = true;
230           else if (lex_match_id (lexer, "DELETE"))
231             retain_unselected = false;
232           else
233             {
234               lex_error_expecting (lexer, "RETAIN", "DELETE", NULL_SENTINEL);
235               goto error;
236             }
237         }
238       else if (!parse_dict_trim (lexer, dict))
239         goto error;
240     }
241
242   if (type == 0)
243     {
244       lex_sbc_missing ("TYPE");
245       goto error;
246     }
247   else if (handle == NULL)
248     {
249       lex_sbc_missing ("OUTFILE");
250       goto error;
251     }
252   else if (!replace && fn_exists (handle))
253     {
254       msg (SE, _("Output file `%s' exists but %s was not specified."),
255            fh_get_file_name (handle), "REPLACE");
256       goto error;
257     }
258
259   dict_delete_scratch_vars (dict);
260   dict_compact_values (dict);
261
262   csv_opts.recode_user_missing = recode_user_missing;
263   csv_opts.include_var_names = include_var_names;
264   csv_opts.use_value_labels = use_value_labels;
265   csv_opts.use_print_formats = use_print_formats;
266   csv_opts.decimal = decimal;
267   csv_opts.delimiter = (delimiter ? delimiter
268                         : type == TAB_FILE ? '\t'
269                         : decimal == '.' ? ','
270                         : ';');
271   csv_opts.qualifier = qualifier;
272
273   writer = csv_writer_open (handle, dict, &csv_opts);
274   if (writer == NULL)
275     goto error;
276   fh_unref (handle);
277
278   map = case_map_stage_get_case_map (stage);
279   case_map_stage_destroy (stage);
280   if (map != NULL)
281     writer = case_map_create_output_translator (map, writer);
282   dict_destroy (dict);
283
284   casereader_transfer (proc_open_filtering (ds, !retain_unselected), writer);
285   ok = casewriter_destroy (writer);
286   ok = proc_commit (ds) && ok;
287
288   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
289
290 error:
291   case_map_stage_destroy (stage);
292   fh_unref (handle);
293   dict_destroy (dict);
294   case_map_destroy (map);
295   return CMD_FAILURE;
296 }