1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006-2007, 2010-15 Free Software Foundation, Inc.
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.
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.
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/>. */
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"
37 #include "gl/xalloc.h"
40 #define _(msgid) gettext (msgid)
42 /* Reading system and portable files. */
44 /* Type of command. */
51 static int parse_read_command (struct lexer *, struct dataset *,
56 cmd_get (struct lexer *lexer, struct dataset *ds)
58 return parse_read_command (lexer, ds, GET_CMD);
63 cmd_import (struct lexer *lexer, struct dataset *ds)
65 return parse_read_command (lexer, ds, IMPORT_CMD);
68 /* Parses a GET or IMPORT command. */
70 parse_read_command (struct lexer *lexer, struct dataset *ds,
71 enum reader_command command)
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;
82 lex_match (lexer, T_SLASH);
84 if (lex_match_id (lexer, "FILE") || lex_is_string (lexer))
86 lex_match (lexer, T_EQUALS);
89 fh = fh_parse (lexer, FH_REF_FILE, NULL);
93 else if (command == GET_CMD && lex_match_id (lexer, "ENCODING"))
95 lex_match (lexer, T_EQUALS);
97 if (!lex_force_string (lexer))
101 encoding = ss_xstrdup (lex_tokss (lexer));
105 else if (command == IMPORT_CMD && lex_match_id (lexer, "TYPE"))
107 lex_match (lexer, T_EQUALS);
109 if (!lex_match_id (lexer, "COMM")
110 && !lex_match_id (lexer, "TAPE"))
112 lex_error_expecting (lexer, "COMM", "TAPE");
122 lex_sbc_missing ("FILE");
126 reader = any_reader_open_and_decode (fh, encoding, &dict, NULL);
130 if (dict_get_n_vars (dict) == 0)
132 msg (SE, _("%s: Data file dictionary has no variables."),
137 stage = case_map_stage_create (dict);
139 while (lex_token (lexer) != T_ENDCMD)
141 lex_match (lexer, T_SLASH);
142 if (!parse_dict_trim (lexer, dict, false))
145 dict_compact_values (dict);
147 map = case_map_stage_get_case_map (stage);
148 case_map_stage_destroy (stage);
150 reader = case_map_create_input_translator (map, reader);
152 dataset_set_dict (ds, dict);
153 dataset_set_source (ds, reader);
160 case_map_stage_destroy (stage);
162 casereader_destroy (reader);
166 return CMD_CASCADING_FAILURE;