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;
98 const xmlChar *target_sheet;
99 int target_sheet_index;
106 struct caseproto *proto;
107 struct dictionary *dict;
108 struct ccase *first_case;
109 bool used_first_case;
112 static void process_node (struct gnumeric_reader *r);
116 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
118 struct gnumeric_reader *r = r_;
123 xmlFreeTextReader (r->xtr);
125 if ( ! r->used_first_case )
126 case_unref (r->first_case);
128 caseproto_unref (r->proto);
134 process_node (struct gnumeric_reader *r)
136 xmlChar *name = xmlTextReaderName (r->xtr);
138 name = xmlStrdup (_xml ("--"));
141 r->node_type = xmlTextReaderNodeType (r->xtr);
146 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
147 XML_READER_TYPE_ELEMENT == r->node_type)
150 r->state = STATE_SHEET_START;
153 case STATE_SHEET_START:
154 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
155 XML_READER_TYPE_ELEMENT == r->node_type)
157 r->state = STATE_SHEET_NAME;
160 case STATE_SHEET_NAME:
161 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
162 XML_READER_TYPE_END_ELEMENT == r->node_type)
164 r->state = STATE_INIT;
166 else if (XML_READER_TYPE_TEXT == r->node_type)
168 if ( r->target_sheet != NULL)
170 xmlChar *value = xmlTextReaderValue (r->xtr);
171 if ( 0 == xmlStrcmp (value, r->target_sheet))
172 r->state = STATE_SHEET_FOUND;
175 else if (r->target_sheet_index == r->sheet_index)
177 r->state = STATE_SHEET_FOUND;
181 case STATE_SHEET_FOUND:
182 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
183 XML_READER_TYPE_ELEMENT == r->node_type)
185 r->min_col = INT_MAX;
186 if (! xmlTextReaderIsEmptyElement (r->xtr))
187 r->state = STATE_CELLS_START;
189 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
190 XML_READER_TYPE_ELEMENT == r->node_type)
192 r->state = STATE_MAXROW;
194 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
195 XML_READER_TYPE_END_ELEMENT == r->node_type)
197 r->state = STATE_INIT;
201 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
202 XML_READER_TYPE_END_ELEMENT == r->node_type)
204 r->state = STATE_SHEET_FOUND;
206 case STATE_CELLS_START:
207 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
208 XML_READER_TYPE_ELEMENT == r->node_type)
210 xmlChar *attr = NULL;
211 r->state = STATE_CELL;
213 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
214 r->col = _xmlchar_to_int (attr);
217 if (r->col < r->min_col)
220 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
221 r->row = _xmlchar_to_int (attr);
224 else if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
225 XML_READER_TYPE_END_ELEMENT == r->node_type)
226 r->state = STATE_SHEET_NAME;
230 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) &&
231 XML_READER_TYPE_END_ELEMENT == r->node_type)
233 r->state = STATE_CELLS_START;
245 Sets the VAR of case C, to the value corresponding to the xml string XV
248 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
251 union value *v = case_data_rw (c, var);
254 value_set_missing (v, var_get_width (var));
255 else if ( var_is_alpha (var))
256 value_copy_str_rpad (v, var_get_width (var), xv, ' ');
259 const char *text = CHAR_CAST (const char *, xv);
263 v->f = c_strtod (text, &endptr);
264 if ( errno != 0 || endptr == text)
273 xmlChar *first_value;
277 gnumeric_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
279 unsigned long int vstart = 0;
281 casenumber n_cases = CASENUMBER_MAX;
283 struct var_spec *var_spec = NULL;
286 struct gnumeric_reader *r = NULL;
288 gzFile gz = gzopen (gri->file_name, "r");
292 msg (ME, _("Error opening `%s' for reading as a Gnumeric file: %s."),
293 gri->file_name, strerror (errno));
298 r = xzalloc (sizeof *r);
300 r->xtr = xmlReaderForIO ((xmlInputReadCallback) gzread,
301 (xmlInputCloseCallback) gzclose, gz,
304 if ( r->xtr == NULL )
307 if ( gri->cell_range )
309 if ( ! convert_cell_ref (gri->cell_range,
310 &r->start_col, &r->start_row,
311 &r->stop_col, &r->stop_row))
313 msg (SE, _("Invalid cell range `%s'"),
326 r->state = STATE_INIT;
327 r->target_sheet = BAD_CAST gri->sheet_name;
328 r->target_sheet_index = gri->sheet_index;
329 r->row = r->col = -1;
332 /* Advance to the start of the cells for the target sheet */
333 while ( (r->state != STATE_CELL || r->row < r->start_row )
334 && 1 == (ret = xmlTextReaderRead (r->xtr)))
338 value = xmlTextReaderValue (r->xtr);
340 if ( r->state == STATE_MAXROW && r->node_type == XML_READER_TYPE_TEXT)
342 n_cases = 1 + _xmlchar_to_int (value) ;
348 /* If a range has been given, then use that to calculate the number
350 if ( gri->cell_range)
352 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
355 if ( gri->read_names )
361 /* Read in the first row of cells,
362 including the headers if read_names was set */
364 (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
365 && (ret = xmlTextReaderRead (r->xtr))
371 if ( r->row > r->start_row ) break;
373 if ( r->col < r->start_col ||
374 (r->stop_col != -1 && r->col > r->stop_col))
377 idx = r->col - r->start_col;
379 if ( idx >= n_var_specs )
382 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
383 for (i = n_var_specs; i <= idx; ++i)
385 var_spec [i].name = NULL;
386 var_spec [i].width = -1;
387 var_spec [i].first_value = NULL;
389 n_var_specs = idx + 1 ;
392 if ( r->node_type == XML_READER_TYPE_TEXT )
394 xmlChar *value = xmlTextReaderValue (r->xtr);
395 const char *text = CHAR_CAST (const char *, value);
397 if ( r->row < r->start_row)
399 if ( gri->read_names )
401 var_spec [idx].name = xstrdup (text);
406 var_spec [idx].first_value = xmlStrdup (value);
408 if (-1 == var_spec [idx].width )
409 var_spec [idx].width = (gri->asw == -1) ?
410 ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : gri->asw;
415 else if ( r->node_type == XML_READER_TYPE_ELEMENT
416 && r->state == STATE_CELL)
418 if ( r->row == r->start_row )
421 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
423 if ( NULL == attr || 60 != _xmlchar_to_int (attr))
424 var_spec [idx].width = 0;
432 const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
435 /* Create the dictionary and populate it */
436 *dict = r->dict = dict_create (CHAR_CAST (const char *, enc));
439 for (i = 0 ; i < n_var_specs ; ++i )
443 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
446 /* Probably no data exists for this variable, so allocate a
448 if ( var_spec[i].width == -1 )
449 var_spec[i].width = SPREADSHEET_DEFAULT_WIDTH;
451 name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
452 dict_create_var (r->dict, name, var_spec[i].width);
456 /* Create the first case, and cache it */
457 r->used_first_case = false;
459 if ( n_var_specs == 0 )
461 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
466 r->proto = caseproto_ref (dict_get_proto (r->dict));
467 r->first_case = case_create (r->proto);
468 case_set_missing (r->first_case);
471 for ( i = 0 ; i < n_var_specs ; ++i )
473 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
476 const struct variable *var = dict_get_var (r->dict, x++);
478 convert_xml_string_to_value (r->first_case, var,
479 var_spec[i].first_value);
482 for ( i = 0 ; i < n_var_specs ; ++i )
484 free (var_spec[i].first_value);
485 free (var_spec[i].name);
490 return casereader_create_sequential
494 &gnm_file_casereader_class, r);
498 for ( i = 0 ; i < n_var_specs ; ++i )
500 free (var_spec[i].first_value);
501 free (var_spec[i].name);
505 dict_destroy (*dict);
508 gnm_file_casereader_destroy (NULL, r);
514 /* Reads and returns one case from READER's file. Returns a null
515 pointer on failure. */
516 static struct ccase *
517 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
522 struct gnumeric_reader *r = r_;
523 int current_row = r->row;
525 if ( !r->used_first_case )
527 r->used_first_case = true;
528 return r->first_case;
531 c = case_create (r->proto);
532 case_set_missing (c);
534 if (r->start_col == -1)
535 r->start_col = r->min_col;
537 while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
538 && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
542 if ( r->col < r->start_col || (r->stop_col != -1 &&
543 r->col > r->stop_col))
546 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
549 if ( r->stop_row != -1 && r->row > r->stop_row)
552 if ( r->node_type == XML_READER_TYPE_TEXT )
554 xmlChar *value = xmlTextReaderValue (r->xtr);
556 const int idx = r->col - r->start_col;
558 const struct variable *var = dict_get_var (r->dict, idx);
560 convert_xml_string_to_value (c, var, value);
577 #endif /* GNM_SUPPORT */