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