1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2009, 2010, 2011, 2012 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/>. */
19 #include "libpspp/message.h"
20 #include "libpspp/misc.h"
22 #include "gl/minmax.h"
23 #include "gl/c-strtod.h"
26 #define _(msgid) gettext (msgid)
27 #define N_(msgid) (msgid)
29 #include "spreadsheet-reader.h"
34 gnumeric_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
36 msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "Gnumeric");
43 #include "data/gnumeric-reader.h"
48 #include <libxml/xmlreader.h>
51 #include "data/case.h"
52 #include "data/casereader-provider.h"
53 #include "data/dictionary.h"
54 #include "data/identifier.h"
55 #include "data/value.h"
56 #include "data/variable.h"
57 #include "libpspp/i18n.h"
58 #include "libpspp/str.h"
60 #include "gl/xalloc.h"
62 static void gnm_file_casereader_destroy (struct casereader *, void *);
64 static struct ccase *gnm_file_casereader_read (struct casereader *, void *);
66 static const struct casereader_class gnm_file_casereader_class =
68 gnm_file_casereader_read,
69 gnm_file_casereader_destroy,
76 STATE_INIT = 0, /* Initial state */
77 STATE_SHEET_START, /* Found the start of a sheet */
78 STATE_SHEET_NAME, /* Found the sheet name */
80 STATE_SHEET_FOUND, /* Found the sheet that we actually want */
81 STATE_CELLS_START, /* Found the start of the cell array */
82 STATE_CELL /* Found a cell */
86 struct gnumeric_reader
90 enum reader_state state;
97 const xmlChar *target_sheet;
98 int target_sheet_index;
105 struct caseproto *proto;
106 struct dictionary *dict;
107 struct ccase *first_case;
108 bool used_first_case;
111 static void process_node (struct gnumeric_reader *r);
115 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
117 struct gnumeric_reader *r = r_;
122 xmlFreeTextReader (r->xtr);
124 if ( ! r->used_first_case )
125 case_unref (r->first_case);
127 caseproto_unref (r->proto);
133 process_node (struct gnumeric_reader *r)
135 xmlChar *name = xmlTextReaderName (r->xtr);
137 name = xmlStrdup (_xml ("--"));
140 r->node_type = xmlTextReaderNodeType (r->xtr);
145 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
146 XML_READER_TYPE_ELEMENT == r->node_type)
149 r->state = STATE_SHEET_START;
152 case STATE_SHEET_START:
153 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
154 XML_READER_TYPE_ELEMENT == r->node_type)
156 r->state = STATE_SHEET_NAME;
159 case STATE_SHEET_NAME:
160 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
161 XML_READER_TYPE_END_ELEMENT == r->node_type)
163 r->state = STATE_INIT;
165 else if (XML_READER_TYPE_TEXT == r->node_type)
167 if ( r->target_sheet != NULL)
169 xmlChar *value = xmlTextReaderValue (r->xtr);
170 if ( 0 == xmlStrcmp (value, r->target_sheet))
171 r->state = STATE_SHEET_FOUND;
174 else if (r->target_sheet_index == r->sheet_index)
176 r->state = STATE_SHEET_FOUND;
180 case STATE_SHEET_FOUND:
181 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
182 XML_READER_TYPE_ELEMENT == r->node_type)
184 if (! xmlTextReaderIsEmptyElement (r->xtr))
185 r->state = STATE_CELLS_START;
187 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
188 XML_READER_TYPE_ELEMENT == r->node_type)
190 r->state = STATE_MAXROW;
192 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
193 XML_READER_TYPE_END_ELEMENT == r->node_type)
195 r->state = STATE_INIT;
199 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
200 XML_READER_TYPE_END_ELEMENT == r->node_type)
202 r->state = STATE_SHEET_FOUND;
204 case STATE_CELLS_START:
205 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
206 XML_READER_TYPE_ELEMENT == r->node_type)
208 xmlChar *attr = NULL;
209 r->state = STATE_CELL;
211 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
212 r->col = _xmlchar_to_int (attr);
215 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
216 r->row = _xmlchar_to_int (attr);
219 else if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
220 XML_READER_TYPE_END_ELEMENT == r->node_type)
221 r->state = STATE_SHEET_NAME;
225 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) &&
226 XML_READER_TYPE_END_ELEMENT == r->node_type)
228 r->state = STATE_CELLS_START;
240 Sets the VAR of case C, to the value corresponding to the xml string XV
243 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
246 union value *v = case_data_rw (c, var);
249 value_set_missing (v, var_get_width (var));
250 else if ( var_is_alpha (var))
251 value_copy_str_rpad (v, var_get_width (var), xv, ' ');
254 const char *text = CHAR_CAST (const char *, xv);
258 v->f = c_strtod (text, &endptr);
259 if ( errno != 0 || endptr == text)
268 xmlChar *first_value;
272 gnumeric_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
274 unsigned long int vstart = 0;
276 casenumber n_cases = CASENUMBER_MAX;
278 struct var_spec *var_spec = NULL;
281 struct gnumeric_reader *r = NULL;
283 gzFile gz = gzopen (gri->file_name, "r");
287 msg (ME, _("Error opening `%s' for reading as a Gnumeric file: %s."),
288 gri->file_name, strerror (errno));
293 r = xzalloc (sizeof *r);
295 r->xtr = xmlReaderForIO ((xmlInputReadCallback) gzread,
296 (xmlInputCloseCallback) gzclose, gz,
299 if ( r->xtr == NULL )
302 if ( gri->cell_range )
304 if ( ! convert_cell_ref (gri->cell_range,
305 &r->start_col, &r->start_row,
306 &r->stop_col, &r->stop_row))
308 msg (SE, _("Invalid cell range `%s'"),
321 r->state = STATE_INIT;
322 r->target_sheet = BAD_CAST gri->sheet_name;
323 r->target_sheet_index = gri->sheet_index;
324 r->row = r->col = -1;
327 /* Advance to the start of the cells for the target sheet */
328 while ( (r->state != STATE_CELL || r->row < r->start_row )
329 && 1 == (ret = xmlTextReaderRead (r->xtr)))
333 value = xmlTextReaderValue (r->xtr);
335 if ( r->state == STATE_MAXROW && r->node_type == XML_READER_TYPE_TEXT)
337 n_cases = 1 + _xmlchar_to_int (value) ;
343 /* If a range has been given, then use that to calculate the number
345 if ( gri->cell_range)
347 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
350 if ( gri->read_names )
356 /* Read in the first row of cells,
357 including the headers if read_names was set */
359 (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
360 && (ret = xmlTextReaderRead (r->xtr))
366 if ( r->row > r->start_row ) break;
368 if ( r->col < r->start_col ||
369 (r->stop_col != -1 && r->col > r->stop_col))
372 idx = r->col - r->start_col;
374 if ( idx >= n_var_specs )
376 n_var_specs = idx + 1 ;
377 var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
378 var_spec [idx].name = NULL;
379 var_spec [idx].width = -1;
380 var_spec [idx].first_value = NULL;
383 if ( r->node_type == XML_READER_TYPE_TEXT )
385 xmlChar *value = xmlTextReaderValue (r->xtr);
386 const char *text = CHAR_CAST (const char *, value);
388 if ( r->row < r->start_row)
390 if ( gri->read_names )
392 var_spec [idx].name = xstrdup (text);
397 var_spec [idx].first_value = xmlStrdup (value);
399 if (-1 == var_spec [idx].width )
400 var_spec [idx].width = (gri->asw == -1) ?
401 ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : gri->asw;
406 else if ( r->node_type == XML_READER_TYPE_ELEMENT
407 && r->state == STATE_CELL)
409 if ( r->row == r->start_row )
412 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
414 if ( NULL == attr || 60 != _xmlchar_to_int (attr))
415 var_spec [idx].width = 0;
423 const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
426 /* Create the dictionary and populate it */
427 *dict = r->dict = dict_create (CHAR_CAST (const char *, enc));
430 for (i = 0 ; i < n_var_specs ; ++i )
434 /* Probably no data exists for this variable, so allocate a
436 if ( var_spec[i].width == -1 )
437 var_spec[i].width = SPREADSHEET_DEFAULT_WIDTH;
439 name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
440 dict_create_var (r->dict, name, var_spec[i].width);
444 /* Create the first case, and cache it */
445 r->used_first_case = false;
447 if ( n_var_specs == 0 )
449 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
454 r->proto = caseproto_ref (dict_get_proto (r->dict));
455 r->first_case = case_create (r->proto);
456 case_set_missing (r->first_case);
458 for ( i = 0 ; i < n_var_specs ; ++i )
460 const struct variable *var = dict_get_var (r->dict, i);
462 convert_xml_string_to_value (r->first_case, var,
463 var_spec[i].first_value);
466 for ( i = 0 ; i < n_var_specs ; ++i )
468 free (var_spec[i].first_value);
469 free (var_spec[i].name);
474 return casereader_create_sequential
478 &gnm_file_casereader_class, r);
482 for ( i = 0 ; i < n_var_specs ; ++i )
484 free (var_spec[i].first_value);
485 free (var_spec[i].name);
489 dict_destroy (*dict);
491 gnm_file_casereader_destroy (NULL, r);
497 /* Reads and returns one case from READER's file. Returns a null
498 pointer on failure. */
499 static struct ccase *
500 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
505 struct gnumeric_reader *r = r_;
506 int current_row = r->row;
508 if ( !r->used_first_case )
510 r->used_first_case = true;
511 return r->first_case;
514 c = case_create (r->proto);
515 case_set_missing (c);
517 while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
518 && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
522 if ( r->col < r->start_col || (r->stop_col != -1 &&
523 r->col > r->stop_col))
526 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
529 if ( r->stop_row != -1 && r->row > r->stop_row)
532 if ( r->node_type == XML_READER_TYPE_TEXT )
534 xmlChar *value = xmlTextReaderValue (r->xtr);
536 const int idx = r->col - r->start_col;
538 const struct variable *var = dict_get_var (r->dict, idx);
540 convert_xml_string_to_value (c, var, value);
557 #endif /* GNM_SUPPORT */