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