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