GET: Fix confusion over the type of the 'type' parameter.
[pspp-builds.git] / src / language / data-io / get.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2007, 2010, 2011 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-map.h"
23 #include "data/case.h"
24 #include "data/casereader.h"
25 #include "data/dataset.h"
26 #include "data/dictionary.h"
27 #include "data/por-file-writer.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 "gl/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,
70                     enum reader_command command)
71 {
72   struct casereader *reader = NULL;
73   struct file_handle *fh = NULL;
74   struct dictionary *dict = NULL;
75   struct case_map *map = NULL;
76
77   for (;;)
78     {
79       lex_match (lexer, T_SLASH);
80
81       if (lex_match_id (lexer, "FILE") || lex_is_string (lexer))
82         {
83           lex_match (lexer, T_EQUALS);
84
85           fh_unref (fh);
86           fh = fh_parse (lexer, FH_REF_FILE, NULL);
87           if (fh == NULL)
88             goto error;
89         }
90       else if (command == IMPORT_CMD && lex_match_id (lexer, "TYPE"))
91         {
92           lex_match (lexer, T_EQUALS);
93
94           if (!lex_match_id (lexer, "COMM")
95               && !lex_match_id (lexer, "TAPE"))
96             {
97               lex_error_expecting (lexer, "COMM", "TAPE", NULL_SENTINEL);
98               goto error;
99             }
100         }
101       else
102         break;
103     }
104
105   if (fh == NULL)
106     {
107       lex_sbc_missing ("FILE");
108       goto error;
109     }
110
111   reader = any_reader_open (fh, &dict);
112   if (reader == NULL)
113     goto error;
114
115   case_map_prepare_dict (dict);
116
117   while (lex_token (lexer) != T_ENDCMD)
118     {
119       lex_match (lexer, T_SLASH);
120       if (!parse_dict_trim (lexer, dict))
121         goto error;
122     }
123   dict_compact_values (dict);
124
125   map = case_map_from_dict (dict);
126   if (map != NULL)
127     reader = case_map_create_input_translator (map, reader);
128
129   dataset_set_dict (ds, dict);
130   dataset_set_source (ds, reader);
131
132   fh_unref (fh);
133   return CMD_SUCCESS;
134
135  error:
136   fh_unref (fh);
137   casereader_destroy (reader);
138   if (dict != NULL)
139     dict_destroy (dict);
140   return CMD_CASCADING_FAILURE;
141 }