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