1218a27b18bdfc147ca690581cec4bacdb015811
[pspp] / src / language / data-io / get.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2007, 2010, 2011, 2012, 2013 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   struct case_map_stage *stage = NULL;
77   char *encoding = NULL;
78
79   for (;;)
80     {
81       lex_match (lexer, T_SLASH);
82
83       if (lex_match_id (lexer, "FILE") || lex_is_string (lexer))
84         {
85           lex_match (lexer, T_EQUALS);
86
87           fh_unref (fh);
88           fh = fh_parse (lexer, FH_REF_FILE, NULL);
89           if (fh == NULL)
90             goto error;
91         }
92       else if (command == GET_CMD && lex_match_id (lexer, "ENCODING"))
93         {
94           lex_match (lexer, T_EQUALS);
95
96           if (!lex_force_string (lexer))
97             goto error;
98
99           free (encoding);
100           encoding = ss_xstrdup (lex_tokss (lexer));
101
102           lex_get (lexer);
103         }
104       else if (command == IMPORT_CMD && lex_match_id (lexer, "TYPE"))
105         {
106           lex_match (lexer, T_EQUALS);
107
108           if (!lex_match_id (lexer, "COMM")
109               && !lex_match_id (lexer, "TAPE"))
110             {
111               lex_error_expecting (lexer, "COMM", "TAPE", NULL_SENTINEL);
112               goto error;
113             }
114         }
115       else
116         break;
117     }
118
119   if (fh == NULL)
120     {
121       lex_sbc_missing ("FILE");
122       goto error;
123     }
124
125   reader = any_reader_open (fh, encoding, &dict);
126   if (reader == NULL)
127     goto error;
128
129   stage = case_map_stage_create (dict);
130
131   while (lex_token (lexer) != T_ENDCMD)
132     {
133       lex_match (lexer, T_SLASH);
134       if (!parse_dict_trim (lexer, dict))
135         goto error;
136     }
137   dict_compact_values (dict);
138
139   map = case_map_stage_get_case_map (stage);
140   case_map_stage_destroy (stage);
141   if (map != NULL)
142     reader = case_map_create_input_translator (map, reader);
143
144   dataset_set_dict (ds, dict);
145   dataset_set_source (ds, reader);
146
147   fh_unref (fh);
148   free (encoding);
149   return CMD_SUCCESS;
150
151  error:
152   case_map_stage_destroy (stage);
153   fh_unref (fh);
154   casereader_destroy (reader);
155   if (dict != NULL)
156     dict_destroy (dict);
157   free (encoding);
158   return CMD_CASCADING_FAILURE;
159 }