c366f56c46e8529b771efea734244afb6bf51d81
[pspp] / src / language / data-io / save-translate.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010, 2011, 2013 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   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_match (lexer, T_SLASH))
90     {
91       if (lex_match_id (lexer, "OUTFILE"))
92         {
93           if (handle != NULL)
94             {
95               lex_sbc_only_once ("OUTFILE");
96               goto error;
97             }
98
99           lex_match (lexer, T_EQUALS);
100
101           handle = fh_parse (lexer, FH_REF_FILE, NULL);
102           if (handle == NULL)
103             goto error;
104         }
105       else if (lex_match_id (lexer, "TYPE"))
106         {
107           if (type != 0)
108             {
109               lex_sbc_only_once ("TYPE");
110               goto error;
111             }
112
113           lex_match (lexer, T_EQUALS);
114           if (lex_match_id (lexer, "CSV"))
115             type = CSV_FILE;
116           else if (lex_match_id (lexer, "TAB"))
117             type = TAB_FILE;
118           else
119             {
120               lex_error_expecting (lexer, "CSV", "TAB", NULL_SENTINEL);
121               goto error;
122             }
123         }
124       else if (lex_match_id (lexer, "REPLACE"))
125         replace = true;
126       else if (lex_match_id (lexer, "FIELDNAMES"))
127         include_var_names = true;
128       else if (lex_match_id (lexer, "MISSING"))
129         {
130           lex_match (lexer, T_EQUALS);
131           if (lex_match_id (lexer, "IGNORE"))
132             recode_user_missing = false;
133           else if (lex_match_id (lexer, "RECODE"))
134             recode_user_missing = true;
135           else
136             {
137               lex_error_expecting (lexer, "IGNORE", "RECODE", NULL_SENTINEL);
138               goto error;
139             }
140         }
141       else if (lex_match_id (lexer, "CELLS"))
142         {
143           lex_match (lexer, T_EQUALS);
144           if (lex_match_id (lexer, "VALUES"))
145             use_value_labels = false;
146           else if (lex_match_id (lexer, "LABELS"))
147             use_value_labels = true;
148           else
149             {
150               lex_error_expecting (lexer, "VALUES", "LABELS", NULL_SENTINEL);
151               goto error;
152             }
153         }
154       else if (lex_match_id (lexer, "TEXTOPTIONS"))
155         {
156           lex_match (lexer, T_EQUALS);
157           for (;;)
158             {
159               if (lex_match_id (lexer, "DELIMITER"))
160                 {
161                   lex_match (lexer, T_EQUALS);
162                   if (!lex_force_string (lexer))
163                     goto error;
164                   /* XXX should support multibyte UTF-8 delimiters */
165                   if (ss_length (lex_tokss (lexer)) != 1)
166                     {
167                       msg (SE, _("The %s string must contain exactly one "
168                                  "character."), "DELIMITER");
169                       goto error;
170                     }
171                   delimiter = ss_first (lex_tokss (lexer));
172                   lex_get (lexer);
173                 }
174               else if (lex_match_id (lexer, "QUALIFIER"))
175                 {
176                   lex_match (lexer, T_EQUALS);
177                   if (!lex_force_string (lexer))
178                     goto error;
179                   /* XXX should support multibyte UTF-8 qualifiers */
180                   if (ss_length (lex_tokss (lexer)) != 1)
181                     {
182                       msg (SE, _("The %s string must contain exactly one "
183                                  "character."), "QUALIFIER");
184                       goto error;
185                     }
186                   qualifier = ss_first (lex_tokss (lexer));
187                   lex_get (lexer);
188                 }
189               else if (lex_match_id (lexer, "DECIMAL"))
190                 {
191                   lex_match (lexer, T_EQUALS);
192                   if (lex_match_id (lexer, "DOT"))
193                     decimal = '.';
194                   else if (lex_match_id (lexer, "COMMA"))
195                     decimal = ',';
196                   else
197                     {
198                       lex_error_expecting (lexer, "DOT", "COMMA",
199                                            NULL_SENTINEL);
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                                            NULL_SENTINEL);
214                       goto error;
215                     }
216                 }
217               else
218                 break;
219             }
220         }
221       else if (lex_match_id (lexer, "UNSELECTED"))
222         {
223           lex_match (lexer, T_EQUALS);
224           if (lex_match_id (lexer, "RETAIN"))
225             retain_unselected = true;
226           else if (lex_match_id (lexer, "DELETE"))
227             retain_unselected = false;
228           else
229             {
230               lex_error_expecting (lexer, "RETAIN", "DELETE", NULL_SENTINEL);
231               goto error;
232             }
233         }
234       else if (!parse_dict_trim (lexer, dict))
235         goto error;
236     }
237   if (lex_end_of_command (lexer) != CMD_SUCCESS)
238     goto error;
239
240   if (type == 0)
241     {
242       lex_sbc_missing ("TYPE");
243       goto error;
244     }
245   else if (handle == NULL)
246     {
247       lex_sbc_missing ("OUTFILE");
248       goto error;
249     }
250   else if (!replace && fn_exists (handle))
251     {
252       msg (SE, _("Output file `%s' exists but %s was not specified."),
253            fh_get_file_name (handle), "REPLACE");
254       goto error;
255     }
256
257   dict_delete_scratch_vars (dict);
258   dict_compact_values (dict);
259
260   csv_opts.recode_user_missing = recode_user_missing;
261   csv_opts.include_var_names = include_var_names;
262   csv_opts.use_value_labels = use_value_labels;
263   csv_opts.use_print_formats = use_print_formats;
264   csv_opts.decimal = decimal;
265   csv_opts.delimiter = (delimiter ? delimiter
266                         : type == TAB_FILE ? '\t'
267                         : decimal == '.' ? ','
268                         : ';');
269   csv_opts.qualifier = qualifier;
270
271   writer = csv_writer_open (handle, dict, &csv_opts);
272   if (writer == NULL)
273     goto error;
274   fh_unref (handle);
275
276   map = case_map_stage_get_case_map (stage);
277   case_map_stage_destroy (stage);
278   if (map != NULL)
279     writer = case_map_create_output_translator (map, writer);
280   dict_destroy (dict);
281
282   casereader_transfer (proc_open_filtering (ds, !retain_unselected), writer);
283   ok = casewriter_destroy (writer);
284   ok = proc_commit (ds) && ok;
285
286   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
287
288 error:
289   case_map_stage_destroy (stage);
290   fh_unref (handle);
291   dict_destroy (dict);
292   case_map_destroy (map);
293   return CMD_FAILURE;
294 }