1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2010 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.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>
39 #define _(msgid) gettext (msgid)
41 /* Reading system and portable files. */
43 /* Type of command. */
50 static int parse_read_command (struct lexer *, struct dataset *,
55 cmd_get (struct lexer *lexer, struct dataset *ds)
57 return parse_read_command (lexer, ds, GET_CMD);
62 cmd_import (struct lexer *lexer, struct dataset *ds)
64 return parse_read_command (lexer, ds, IMPORT_CMD);
67 /* Parses a GET or IMPORT command. */
69 parse_read_command (struct lexer *lexer, struct dataset *ds, enum reader_command type)
71 struct casereader *reader = NULL;
72 struct file_handle *fh = NULL;
73 struct dictionary *dict = NULL;
74 struct case_map *map = NULL;
78 lex_match (lexer, T_SLASH);
80 if (lex_match_id (lexer, "FILE") || lex_is_string (lexer))
82 lex_match (lexer, T_EQUALS);
85 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_SCRATCH);
89 else if (type == IMPORT_CMD && lex_match_id (lexer, "TYPE"))
91 lex_match (lexer, T_EQUALS);
93 if (lex_match_id (lexer, "COMM"))
95 else if (lex_match_id (lexer, "TAPE"))
99 lex_error (lexer, _("expecting %s or %s"), "COMM", "TAPE");
109 lex_sbc_missing (lexer, "FILE");
113 reader = any_reader_open (fh, &dict);
117 case_map_prepare_dict (dict);
119 while (lex_token (lexer) != T_ENDCMD)
121 lex_match (lexer, T_SLASH);
122 if (!parse_dict_trim (lexer, dict))
125 dict_compact_values (dict);
127 map = case_map_from_dict (dict);
129 reader = case_map_create_input_translator (map, reader);
131 proc_set_active_file (ds, reader, dict);
138 casereader_destroy (reader);
141 return CMD_CASCADING_FAILURE;