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