Make GET DATA a separate command, instead of something invoked
[pspp-builds.git] / src / language / data-io / get-data.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007 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
20 #include <libpspp/message.h>
21 #include <data/gnumeric-reader.h>
22
23 #include <language/command.h>
24 #include <language/lexer/lexer.h>
25 #include <stdlib.h>
26 #include <data/procedure.h>
27
28 #include "gettext.h"
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) (msgid)
31
32 static int parse_get_gnm (struct lexer *lexer, struct dataset *);
33
34 int
35 cmd_get_data (struct lexer *lexer, struct dataset *ds)
36 {
37   lex_force_match (lexer, '/');
38
39   if (!lex_force_match_id (lexer, "TYPE"))
40     return CMD_FAILURE;
41
42   lex_force_match (lexer, '=');
43
44   if (lex_match_id (lexer, "GNM"))
45     return parse_get_gnm (lexer, ds);
46
47   msg (SE, _("Unsupported TYPE %s"), lex_tokid (lexer));
48   return CMD_FAILURE;
49 }
50
51
52 static int
53 parse_get_gnm (struct lexer *lexer, struct dataset *ds)
54 {
55   struct gnumeric_read_info gri  = {NULL, NULL, NULL, 1, true, -1};
56
57   lex_force_match (lexer, '/');
58
59   if (!lex_force_match_id (lexer, "FILE"))
60     goto error;
61
62   lex_force_match (lexer, '=');
63
64   if (!lex_force_string (lexer))
65     goto error;
66
67   gri.file_name = strdup (ds_cstr (lex_tokstr (lexer)));
68
69   lex_get (lexer);
70
71   while (lex_match (lexer, '/') )
72     {
73       if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
74         {
75           lex_match (lexer, '=');
76           gri.asw = lex_integer (lexer);
77         }
78       else if (lex_match_id (lexer, "SHEET"))
79         {
80           lex_match (lexer, '=');
81           if (lex_match_id (lexer, "NAME"))
82             {
83               if ( ! lex_force_string (lexer) )
84                 goto error;
85
86               gri.sheet_name = strdup (ds_cstr (lex_tokstr (lexer)));
87               gri.sheet_index = -1;
88             }
89           else if (lex_match_id (lexer, "INDEX"))
90             {
91               gri.sheet_index = lex_integer (lexer);
92             }
93           else
94             goto error;
95         }
96       else if (lex_match_id (lexer, "CELLRANGE"))
97         {
98           lex_match (lexer, '=');
99
100           if (lex_match_id (lexer, "FULL"))
101             {
102               gri.cell_range = NULL;
103               lex_put_back (lexer, T_ID);
104             }
105           else if (lex_match_id (lexer, "RANGE"))
106             {
107               if ( ! lex_force_string (lexer) )
108                 goto error;
109
110               gri.cell_range = strdup (ds_cstr (lex_tokstr (lexer)));
111             }
112           else
113             goto error;
114         }
115       else if (lex_match_id (lexer, "READNAMES"))
116         {
117           lex_match (lexer, '=');
118
119           if ( lex_match_id (lexer, "ON"))
120             {
121               gri.read_names = true;
122             }
123           else if (lex_match_id (lexer, "OFF"))
124             {
125               gri.read_names = false;
126             }
127           else
128             goto error;
129           lex_put_back (lexer, T_ID);
130         }
131       else
132         {
133           printf ("Unknown data file type \"\%s\"\n", lex_tokid (lexer));
134           goto error;
135         }
136       lex_get (lexer);
137     }
138
139   {
140     struct dictionary *dict = NULL;
141     struct casereader *reader = gnumeric_open_reader (&gri, &dict);
142
143     if ( reader )
144       proc_set_active_file (ds, reader, dict);
145   }
146
147   free (gri.file_name);
148   free (gri.sheet_name);
149   free (gri.cell_range);
150   return CMD_SUCCESS;
151
152  error:
153
154   free (gri.file_name);
155   free (gri.sheet_name);
156   free (gri.cell_range);
157   return CMD_FAILURE;
158 }