1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2010, 2011, 2012, 2013 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/str.h"
36 #include "gl/xalloc.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,
70 enum reader_command command)
72 struct casereader *reader = NULL;
73 struct file_handle *fh = NULL;
74 struct dictionary *dict = NULL;
75 struct case_map *map = NULL;
76 struct case_map_stage *stage = NULL;
77 char *encoding = NULL;
81 lex_match (lexer, T_SLASH);
83 if (lex_match_id (lexer, "FILE") || lex_is_string (lexer))
85 lex_match (lexer, T_EQUALS);
88 fh = fh_parse (lexer, FH_REF_FILE, NULL);
92 else if (command == GET_CMD && lex_match_id (lexer, "ENCODING"))
94 lex_match (lexer, T_EQUALS);
96 if (!lex_force_string (lexer))
100 encoding = ss_xstrdup (lex_tokss (lexer));
104 else if (command == IMPORT_CMD && lex_match_id (lexer, "TYPE"))
106 lex_match (lexer, T_EQUALS);
108 if (!lex_match_id (lexer, "COMM")
109 && !lex_match_id (lexer, "TAPE"))
111 lex_error_expecting (lexer, "COMM", "TAPE", NULL_SENTINEL);
121 lex_sbc_missing ("FILE");
125 reader = any_reader_open (fh, encoding, &dict);
129 stage = case_map_stage_create (dict);
131 while (lex_token (lexer) != T_ENDCMD)
133 lex_match (lexer, T_SLASH);
134 if (!parse_dict_trim (lexer, dict))
137 dict_compact_values (dict);
139 map = case_map_stage_get_case_map (stage);
140 case_map_stage_destroy (stage);
142 reader = case_map_create_input_translator (map, reader);
144 dataset_set_dict (ds, dict);
145 dataset_set_source (ds, reader);
152 case_map_stage_destroy (stage);
154 casereader_destroy (reader);
158 return CMD_CASCADING_FAILURE;