Implement GET DATA/TYPE=TXT.
[pspp-builds.git] / src / language / data-io / get-data.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007 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/gnumeric-reader.h>
22
23 #include <data/dictionary.h>
24 #include <data/format.h>
25 #include <data/procedure.h>
26 #include <language/command.h>
27 #include <language/data-io/data-parser.h>
28 #include <language/data-io/data-reader.h>
29 #include <language/data-io/file-handle.h>
30 #include <language/data-io/placement-parser.h>
31 #include <language/lexer/format-parser.h>
32 #include <language/lexer/lexer.h>
33 #include <libpspp/message.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) (msgid)
38
39 static int parse_get_gnm (struct lexer *lexer, struct dataset *);
40 static int parse_get_txt (struct lexer *lexer, struct dataset *);
41
42 int
43 cmd_get_data (struct lexer *lexer, struct dataset *ds)
44 {
45   lex_force_match (lexer, '/');
46
47   if (!lex_force_match_id (lexer, "TYPE"))
48     return CMD_FAILURE;
49
50   lex_force_match (lexer, '=');
51
52   if (lex_match_id (lexer, "GNM"))
53     return parse_get_gnm (lexer, ds);
54   else if (lex_match_id (lexer, "TXT"))
55     return parse_get_txt (lexer, ds);
56
57   msg (SE, _("Unsupported TYPE %s"), lex_tokid (lexer));
58   return CMD_FAILURE;
59 }
60
61
62 static int
63 parse_get_gnm (struct lexer *lexer, struct dataset *ds)
64 {
65   struct gnumeric_read_info gri  = {NULL, NULL, NULL, 1, true, -1};
66
67   lex_force_match (lexer, '/');
68
69   if (!lex_force_match_id (lexer, "FILE"))
70     goto error;
71
72   lex_force_match (lexer, '=');
73
74   if (!lex_force_string (lexer))
75     goto error;
76
77   gri.file_name = strdup (ds_cstr (lex_tokstr (lexer)));
78
79   lex_get (lexer);
80
81   while (lex_match (lexer, '/') )
82     {
83       if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
84         {
85           lex_match (lexer, '=');
86           gri.asw = lex_integer (lexer);
87         }
88       else if (lex_match_id (lexer, "SHEET"))
89         {
90           lex_match (lexer, '=');
91           if (lex_match_id (lexer, "NAME"))
92             {
93               if ( ! lex_force_string (lexer) )
94                 goto error;
95
96               gri.sheet_name = strdup (ds_cstr (lex_tokstr (lexer)));
97               gri.sheet_index = -1;
98             }
99           else if (lex_match_id (lexer, "INDEX"))
100             {
101               gri.sheet_index = lex_integer (lexer);
102             }
103           else
104             goto error;
105         }
106       else if (lex_match_id (lexer, "CELLRANGE"))
107         {
108           lex_match (lexer, '=');
109
110           if (lex_match_id (lexer, "FULL"))
111             {
112               gri.cell_range = NULL;
113               lex_put_back (lexer, T_ID);
114             }
115           else if (lex_match_id (lexer, "RANGE"))
116             {
117               if ( ! lex_force_string (lexer) )
118                 goto error;
119
120               gri.cell_range = strdup (ds_cstr (lex_tokstr (lexer)));
121             }
122           else
123             goto error;
124         }
125       else if (lex_match_id (lexer, "READNAMES"))
126         {
127           lex_match (lexer, '=');
128
129           if ( lex_match_id (lexer, "ON"))
130             {
131               gri.read_names = true;
132             }
133           else if (lex_match_id (lexer, "OFF"))
134             {
135               gri.read_names = false;
136             }
137           else
138             goto error;
139           lex_put_back (lexer, T_ID);
140         }
141       else
142         {
143           printf ("Unknown data file type \"\%s\"\n", lex_tokid (lexer));
144           goto error;
145         }
146       lex_get (lexer);
147     }
148
149   {
150     struct dictionary *dict = NULL;
151     struct casereader *reader = gnumeric_open_reader (&gri, &dict);
152
153     if ( reader )
154       proc_set_active_file (ds, reader, dict);
155   }
156
157   free (gri.file_name);
158   free (gri.sheet_name);
159   free (gri.cell_range);
160   return CMD_SUCCESS;
161
162  error:
163
164   free (gri.file_name);
165   free (gri.sheet_name);
166   free (gri.cell_range);
167   return CMD_FAILURE;
168 }
169
170 static bool
171 set_type (struct data_parser *parser, const char *subcommand,
172           enum data_parser_type type, bool *has_type)
173 {
174   if (!*has_type)
175     {
176       data_parser_set_type (parser, type);
177       *has_type = true;
178     }
179   else if (type != data_parser_get_type (parser))
180     {
181       msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
182                  "was stated or implied earlier in this command."),
183            subcommand,
184            type == DP_FIXED ? "FIXED" : "DELIMITED",
185            type == DP_FIXED ? "DELIMITED" : "FIXED");
186       return false;
187     }
188   return true;
189 }
190
191 static int
192 parse_get_txt (struct lexer *lexer, struct dataset *ds)
193 {
194   struct data_parser *parser = NULL;
195   struct dictionary *dict = NULL;
196   struct file_handle *fh = NULL;
197   struct dfm_reader *reader = NULL;
198
199   int record;
200   enum data_parser_type type;
201   bool has_type;
202
203   lex_force_match (lexer, '/');
204
205   if (!lex_force_match_id (lexer, "FILE"))
206     goto error;
207   lex_force_match (lexer, '=');
208   fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
209   if (fh == NULL)
210     goto error;
211
212   parser = data_parser_create ();
213   has_type = false;
214   data_parser_set_type (parser, DP_DELIMITED);
215   data_parser_set_span (parser, false);
216   data_parser_set_quotes (parser, ss_empty ());
217   data_parser_set_empty_line_has_field (parser, true);
218
219   for (;;)
220     {
221       if (!lex_force_match (lexer, '/'))
222         goto error;
223
224       if (lex_match_id (lexer, "ARRANGEMENT"))
225         {
226           bool ok;
227
228           lex_match (lexer, '=');
229           if (lex_match_id (lexer, "FIXED"))
230             ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
231           else if (lex_match_id (lexer, "DELIMITED"))
232             ok = set_type (parser, "ARRANGEMENT=DELIMITED",
233                            DP_DELIMITED, &has_type);
234           else
235             {
236               lex_error (lexer, _("expecting FIXED or DELIMITED"));
237               goto error;
238             }
239           if (!ok)
240             goto error;
241         }
242       else if (lex_match_id (lexer, "FIRSTCASE"))
243         {
244           lex_match (lexer, '=');
245           if (!lex_force_int (lexer))
246             goto error;
247           if (lex_integer (lexer) < 1)
248             {
249               msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
250               goto error;
251             }
252           data_parser_set_skip (parser, lex_integer (lexer) - 1);
253           lex_get (lexer);
254         }
255       else if (lex_match_id_n (lexer, "DELCASE", 4))
256         {
257           if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
258             goto error;
259           lex_match (lexer, '=');
260           if (lex_match_id (lexer, "LINE"))
261             data_parser_set_span (parser, false);
262           else if (lex_match_id (lexer, "VARIABLES"))
263             {
264               data_parser_set_span (parser, true);
265
266               /* VARIABLES takes an integer argument, but for no
267                  good reason.  We just ignore it. */
268               if (!lex_force_int (lexer))
269                 goto error;
270               lex_get (lexer);
271             }
272           else
273             {
274               lex_error (lexer, _("expecting LINE or VARIABLES"));
275               goto error;
276             }
277         }
278       else if (lex_match_id (lexer, "FIXCASE"))
279         {
280           if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
281             goto error;
282           lex_match (lexer, '=');
283           if (!lex_force_int (lexer))
284             goto error;
285           if (lex_integer (lexer) < 1)
286             {
287               msg (SE, _("Value of FIXCASE must be at least 1."));
288               goto error;
289             }
290           data_parser_set_records (parser, lex_integer (lexer));
291           lex_get (lexer);
292         }
293       else if (lex_match_id (lexer, "IMPORTCASES"))
294         {
295           lex_match (lexer, '=');
296           if (lex_match (lexer, T_ALL))
297             {
298               data_parser_set_case_limit (parser, -1);
299               data_parser_set_case_percent (parser, 100);
300             }
301           else if (lex_match_id (lexer, "FIRST"))
302             {
303               if (!lex_force_int (lexer))
304                 goto error;
305               if (lex_integer (lexer) < 1)
306                 {
307                   msg (SE, _("Value of FIRST must be at least 1."));
308                   goto error;
309                 }
310               data_parser_set_case_limit (parser, lex_integer (lexer));
311               lex_get (lexer);
312             }
313           else if (lex_match_id (lexer, "PERCENT"))
314             {
315               if (!lex_force_int (lexer))
316                 goto error;
317               if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
318                 {
319                   msg (SE, _("Value of PERCENT must be between 1 and 100."));
320                   goto error;
321                 }
322               data_parser_set_case_percent (parser, lex_integer (lexer));
323               lex_get (lexer);
324             }
325         }
326       else if (lex_match_id_n (lexer, "DELIMITERS", 4))
327         {
328           struct string hard_seps = DS_EMPTY_INITIALIZER;
329           const char *soft_seps = "";
330           struct substring s;
331           int c;
332
333           if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
334             goto error;
335           lex_match (lexer, '=');
336
337           if (!lex_force_string (lexer))
338             goto error;
339
340           s = ds_ss (lex_tokstr (lexer));
341           if (ss_match_string (&s, ss_cstr ("\\t")))
342             ds_put_cstr (&hard_seps, "\t");
343           if (ss_match_string (&s, ss_cstr ("\\\\")))
344             ds_put_cstr (&hard_seps, "\\");
345           while ((c = ss_get_char (&s)) != EOF)
346             if (c == ' ')
347               soft_seps = " ";
348             else
349               ds_put_char (&hard_seps, c);
350           data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
351           data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
352           ds_destroy (&hard_seps);
353
354           lex_get (lexer);
355         }
356       else if (lex_match_id (lexer, "QUALIFIER"))
357         {
358           if (!set_type (parser, "QUALIFIER", DP_DELIMITED, &has_type))
359             goto error;
360           lex_match (lexer, '=');
361
362           if (!lex_force_string (lexer))
363             goto error;
364
365           data_parser_set_quotes (parser, ds_ss (lex_tokstr (lexer)));
366           lex_get (lexer);
367         }
368       else if (lex_match_id (lexer, "VARIABLES"))
369         break;
370       else
371         {
372           lex_error (lexer, _("expecting VARIABLES"));
373           goto error;
374         }
375     }
376   lex_match (lexer, '=');
377
378   dict = dict_create ();
379   record = 1;
380   type = data_parser_get_type (parser);
381   do
382     {
383       char name[VAR_NAME_LEN + 1];
384       struct fmt_spec input, output;
385       int fc, lc;
386       struct variable *v;
387
388       while (type == DP_FIXED && lex_match (lexer, '/'))
389         {
390           if (!lex_force_int (lexer))
391             goto error;
392           if (lex_integer (lexer) < record)
393             {
394               msg (SE, _("The record number specified, %ld, is at or "
395                          "before the previous record, %d.  Data "
396                          "fields must be listed in order of "
397                          "increasing record number."),
398                    lex_integer (lexer), record);
399               goto error;
400             }
401           if (lex_integer (lexer) > data_parser_get_records (parser))
402             {
403               msg (SE, _("The record number specified, %ld, exceeds "
404                          "the number of records per case specified "
405                          "on FIXCASE, %d."),
406                    lex_integer (lexer), data_parser_get_records (parser));
407               goto error;
408             }
409           record = lex_integer (lexer);
410           lex_get (lexer);
411         }
412
413       if (!lex_force_id (lexer))
414         goto error;
415       strcpy (name, lex_tokid (lexer));
416       lex_get (lexer);
417
418       if (type == DP_DELIMITED)
419         {
420           if (!parse_format_specifier (lexer, &input)
421               || !fmt_check_input (&input))
422             goto error;
423         }
424       else
425         {
426           if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
427             goto error;
428           if (!parse_format_specifier_name (lexer, &input.type))
429             goto error;
430           input.w = lc - fc + 1;
431           input.d = 0;
432           if (!fmt_check_input (&input))
433             goto error;
434         }
435       output = fmt_for_output_from_input (&input);
436
437       v = dict_create_var (dict, name, fmt_var_width (&input));
438       if (v == NULL)
439         {
440           msg (SE, _("%s is a duplicate variable name."), name);
441           goto error;
442         }
443       var_set_both_formats (v, &output);
444
445       if (type == DP_DELIMITED)
446         data_parser_add_delimited_field (parser, &input,
447                                          var_get_case_index (v),
448                                          name);
449       else
450         data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
451                                      name, record, fc);
452     }
453   while (lex_token (lexer) != '.');
454
455   reader = dfm_open_reader (fh, lexer);
456   if (reader == NULL)
457     goto error;
458
459   data_parser_make_active_file (parser, ds, reader, dict);
460   fh_unref (fh);
461   return CMD_SUCCESS;
462
463  error:
464   data_parser_destroy (parser);
465   dict_destroy (dict);
466   fh_unref (fh);
467   return CMD_CASCADING_FAILURE;
468 }