GET: Add an ENCODING subcommand.
[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 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   char *encoding = NULL;
77
78   for (;;)
79     {
80       lex_match (lexer, T_SLASH);
81
82       if (lex_match_id (lexer, "FILE") || lex_is_string (lexer))
83         {
84           lex_match (lexer, T_EQUALS);
85
86           fh_unref (fh);
87           fh = fh_parse (lexer, FH_REF_FILE, NULL);
88           if (fh == NULL)
89             goto error;
90         }
91       else if (command == GET_CMD && lex_match_id (lexer, "ENCODING"))
92         {
93           lex_match (lexer, T_EQUALS);
94
95           if (!lex_force_string (lexer))
96             goto error;
97
98           free (encoding);
99           encoding = ss_xstrdup (lex_tokss (lexer));
100
101           lex_get (lexer);
102         }
103       else if (command == IMPORT_CMD && lex_match_id (lexer, "TYPE"))
104         {
105           lex_match (lexer, T_EQUALS);
106
107           if (!lex_match_id (lexer, "COMM")
108               && !lex_match_id (lexer, "TAPE"))
109             {
110               lex_error_expecting (lexer, "COMM", "TAPE", NULL_SENTINEL);
111               goto error;
112             }
113         }
114       else
115         break;
116     }
117
118   if (fh == NULL)
119     {
120       lex_sbc_missing ("FILE");
121       goto error;
122     }
123
124   reader = any_reader_open (fh, encoding, &dict);
125   if (reader == NULL)
126     goto error;
127
128   case_map_prepare_dict (dict);
129
130   while (lex_token (lexer) != T_ENDCMD)
131     {
132       lex_match (lexer, T_SLASH);
133       if (!parse_dict_trim (lexer, dict))
134         goto error;
135     }
136   dict_compact_values (dict);
137
138   map = case_map_from_dict (dict);
139   if (map != NULL)
140     reader = case_map_create_input_translator (map, reader);
141
142   dataset_set_dict (ds, dict);
143   dataset_set_source (ds, reader);
144
145   fh_unref (fh);
146   free (encoding);
147   return CMD_SUCCESS;
148
149  error:
150   fh_unref (fh);
151   casereader_destroy (reader);
152   if (dict != NULL)
153     dict_destroy (dict);
154   free (encoding);
155   return CMD_CASCADING_FAILURE;
156 }