00e78c8f00c1a39922ae3179200b6f8fb8591a9c
[pspp] / src / language / data-io / get.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006-2007, 2010-15 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/message.h"
35 #include "libpspp/str.h"
36
37 #include "gl/xalloc.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41 \f
42 /* Reading system and portable files. */
43
44 /* Type of command. */
45 enum reader_command
46   {
47     GET_CMD,
48     IMPORT_CMD
49   };
50
51 static int parse_read_command (struct lexer *, struct dataset *,
52                                enum reader_command);
53
54 /* GET. */
55 int
56 cmd_get (struct lexer *lexer, struct dataset *ds)
57 {
58   return parse_read_command (lexer, ds, GET_CMD);
59 }
60
61 /* IMPORT. */
62 int
63 cmd_import (struct lexer *lexer, struct dataset *ds)
64 {
65   return parse_read_command (lexer, ds, IMPORT_CMD);
66 }
67
68 /* Parses a GET or IMPORT command. */
69 static int
70 parse_read_command (struct lexer *lexer, struct dataset *ds,
71                     enum reader_command command)
72 {
73   struct casereader *reader = NULL;
74   struct file_handle *fh = NULL;
75   struct dictionary *dict = NULL;
76   struct case_map *map = NULL;
77   struct case_map_stage *stage = NULL;
78   char *encoding = NULL;
79
80   for (;;)
81     {
82       lex_match (lexer, T_SLASH);
83
84       if (lex_match_id (lexer, "FILE") || lex_is_string (lexer))
85         {
86           lex_match (lexer, T_EQUALS);
87
88           fh_unref (fh);
89           fh = fh_parse (lexer, FH_REF_FILE, NULL);
90           if (fh == NULL)
91             goto error;
92         }
93       else if (command == GET_CMD && lex_match_id (lexer, "ENCODING"))
94         {
95           lex_match (lexer, T_EQUALS);
96
97           if (!lex_force_string (lexer))
98             goto error;
99
100           free (encoding);
101           encoding = ss_xstrdup (lex_tokss (lexer));
102
103           lex_get (lexer);
104         }
105       else if (command == IMPORT_CMD && lex_match_id (lexer, "TYPE"))
106         {
107           lex_match (lexer, T_EQUALS);
108
109           if (!lex_match_id (lexer, "COMM")
110               && !lex_match_id (lexer, "TAPE"))
111             {
112               lex_error_expecting (lexer, "COMM", "TAPE");
113               goto error;
114             }
115         }
116       else
117         break;
118     }
119
120   if (fh == NULL)
121     {
122       lex_sbc_missing ("FILE");
123       goto error;
124     }
125
126   reader = any_reader_open_and_decode (fh, encoding, &dict, NULL);
127   if (reader == NULL)
128     goto error;
129
130   if (dict_get_var_cnt (dict) == 0)
131     {
132       msg (SE, _("%s: Data file dictionary has no variables."),
133            fh_get_name (fh));
134       goto error;
135     }
136
137   stage = case_map_stage_create (dict);
138
139   while (lex_token (lexer) != T_ENDCMD)
140     {
141       lex_match (lexer, T_SLASH);
142       if (!parse_dict_trim (lexer, dict, false))
143         goto error;
144     }
145   dict_compact_values (dict);
146
147   map = case_map_stage_get_case_map (stage);
148   case_map_stage_destroy (stage);
149   if (map != NULL)
150     reader = case_map_create_input_translator (map, reader);
151
152   dataset_set_dict (ds, dict);
153   dataset_set_source (ds, reader);
154
155   fh_unref (fh);
156   free (encoding);
157   return CMD_SUCCESS;
158
159  error:
160   case_map_stage_destroy (stage);
161   fh_unref (fh);
162   casereader_destroy (reader);
163   if (dict != NULL)
164     dict_unref (dict);
165   free (encoding);
166   return CMD_CASCADING_FAILURE;
167 }