lexer: Use lex_is_string() more consistently.
[pspp] / src / language / data-io / get.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2007, 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/any-reader.h>
22 #include <data/case.h>
23 #include <data/case-map.h>
24 #include <data/casereader.h>
25 #include <data/dictionary.h>
26 #include <data/por-file-writer.h>
27 #include <data/procedure.h>
28 #include <language/command.h>
29 #include <language/data-io/file-handle.h>
30 #include <language/data-io/trim.h>
31 #include <language/lexer/lexer.h>
32 #include <libpspp/compiler.h>
33 #include <libpspp/misc.h>
34 #include <libpspp/str.h>
35
36 #include "xalloc.h"
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40 \f
41 /* Reading system and portable files. */
42
43 /* Type of command. */
44 enum reader_command
45   {
46     GET_CMD,
47     IMPORT_CMD
48   };
49
50 static int parse_read_command (struct lexer *, struct dataset *,
51                                enum reader_command);
52
53 /* GET. */
54 int
55 cmd_get (struct lexer *lexer, struct dataset *ds)
56 {
57   return parse_read_command (lexer, ds, GET_CMD);
58 }
59
60 /* IMPORT. */
61 int
62 cmd_import (struct lexer *lexer, struct dataset *ds)
63 {
64   return parse_read_command (lexer, ds, IMPORT_CMD);
65 }
66
67 /* Parses a GET or IMPORT command. */
68 static int
69 parse_read_command (struct lexer *lexer, struct dataset *ds, enum reader_command type)
70 {
71   struct casereader *reader = NULL;
72   struct file_handle *fh = NULL;
73   struct dictionary *dict = NULL;
74   struct case_map *map = NULL;
75
76   for (;;)
77     {
78       lex_match (lexer, '/');
79
80       if (lex_match_id (lexer, "FILE") || lex_is_string (lexer))
81         {
82           lex_match (lexer, '=');
83
84           fh_unref (fh);
85           fh = fh_parse (lexer, FH_REF_FILE | FH_REF_SCRATCH);
86           if (fh == NULL)
87             goto error;
88         }
89       else if (type == IMPORT_CMD && lex_match_id (lexer, "TYPE"))
90         {
91           lex_match (lexer, '=');
92
93           if (lex_match_id (lexer, "COMM"))
94             type = PFM_COMM;
95           else if (lex_match_id (lexer, "TAPE"))
96             type = PFM_TAPE;
97           else
98             {
99               lex_error (lexer, _("expecting %s or %s"), "COMM", "TAPE");
100               goto error;
101             }
102         }
103       else
104         break;
105     }
106
107   if (fh == NULL)
108     {
109       lex_sbc_missing (lexer, "FILE");
110       goto error;
111     }
112
113   reader = any_reader_open (fh, &dict);
114   if (reader == NULL)
115     goto error;
116
117   case_map_prepare_dict (dict);
118
119   while (lex_token (lexer) != '.')
120     {
121       lex_match (lexer, '/');
122       if (!parse_dict_trim (lexer, dict))
123         goto error;
124     }
125   dict_compact_values (dict);
126
127   map = case_map_from_dict (dict);
128   if (map != NULL)
129     reader = case_map_create_input_translator (map, reader);
130
131   proc_set_active_file (ds, reader, dict);
132
133   fh_unref (fh);
134   return CMD_SUCCESS;
135
136  error:
137   fh_unref (fh);
138   casereader_destroy (reader);
139   if (dict != NULL)
140     dict_destroy (dict);
141   return CMD_CASCADING_FAILURE;
142 }