1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2009, 2010, 2011, 2012, 2013 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 (const struct spreadsheet_read_options *opts, 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 *);
67 static const struct casereader_class gnm_file_casereader_class =
69 gnm_file_casereader_read,
70 gnm_file_casereader_destroy,
77 STATE_PRE_INIT = 0, /* Initial state */
78 STATE_SHEET_COUNT, /* Found the sheet index */
79 STATE_INIT , /* Other Initial state */
80 STATE_SHEET_START, /* Found the start of a sheet */
81 STATE_SHEET_NAME, /* Found the sheet name */
84 STATE_SHEET_FOUND, /* Found the sheet that we actually want */
85 STATE_CELLS_START, /* Found the start of the cell array */
86 STATE_CELL /* Found a cell */
91 /* The name of the sheet (utf8 encoding) */
104 struct gnumeric_reader
106 struct spreadsheet spreadsheet;
108 /* The libxml reader for this instance */
109 xmlTextReaderPtr xtr;
111 /* An internal state variable */
112 enum reader_state state;
125 struct sheet_detail *sheets;
127 const xmlChar *target_sheet;
128 int target_sheet_index;
130 struct caseproto *proto;
131 struct dictionary *dict;
132 struct ccase *first_case;
133 bool used_first_case;
138 gnumeric_destroy (struct spreadsheet *s)
140 struct gnumeric_reader *r = s;
143 for (i = 0; i < s->n_sheets; ++i)
145 xmlFree (r->sheets[i].name);
155 gnumeric_get_sheet_name (struct spreadsheet *s, int n)
157 struct gnumeric_reader *gr = (struct gnumeric_reader *) s;
158 assert (n < s->n_sheets);
160 return gr->sheets[n].name;
164 static void process_node (struct gnumeric_reader *r);
169 gnumeric_get_sheet_range (struct spreadsheet *s, int n)
172 struct gnumeric_reader *gr = (struct gnumeric_reader *) s;
174 assert (n < s->n_sheets);
177 (gr->sheets[n].stop_col == -1)
179 (1 == (ret = xmlTextReaderRead (gr->xtr)))
185 return create_cell_ref (
186 gr->sheets[n].start_col,
187 gr->sheets[n].start_row,
188 gr->sheets[n].stop_col,
189 gr->sheets[n].stop_row);
194 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
196 struct gnumeric_reader *r = r_;
201 xmlFreeTextReader (r->xtr);
204 if ( ! r->used_first_case )
205 case_unref (r->first_case);
207 caseproto_unref (r->proto);
212 process_node (struct gnumeric_reader *r)
214 xmlChar *name = xmlTextReaderName (r->xtr);
216 name = xmlStrdup (_xml ("--"));
218 r->node_type = xmlTextReaderNodeType (r->xtr);
223 r->current_sheet = -1;
224 if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
225 XML_READER_TYPE_ELEMENT == r->node_type)
227 r->state = STATE_SHEET_COUNT;
231 case STATE_SHEET_COUNT:
232 if (0 == xmlStrcasecmp (name, _xml("gnm:SheetName")) &&
233 XML_READER_TYPE_ELEMENT == r->node_type)
236 if (r->current_sheet + 1 > r->spreadsheet.n_sheets)
238 struct sheet_detail *sd ;
239 r->sheets = xrealloc (r->sheets, (r->current_sheet + 1) * sizeof *r->sheets);
240 sd = &r->sheets[r->current_sheet];
241 sd->start_col = sd->stop_col = sd->start_row = sd->stop_row = -1;
242 r->spreadsheet.n_sheets = r->current_sheet + 1;
245 else if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
246 XML_READER_TYPE_END_ELEMENT == r->node_type)
248 r->state = STATE_INIT;
249 r->current_sheet = -1;
251 else if (XML_READER_TYPE_TEXT == r->node_type)
253 r->sheets [r->spreadsheet.n_sheets - 1].name = CHAR_CAST (char *, xmlTextReaderValue (r->xtr));
258 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
259 XML_READER_TYPE_ELEMENT == r->node_type)
262 r->state = STATE_SHEET_START;
265 case STATE_SHEET_START:
266 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
267 XML_READER_TYPE_ELEMENT == r->node_type)
269 r->state = STATE_SHEET_NAME;
272 case STATE_SHEET_NAME:
273 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
274 XML_READER_TYPE_END_ELEMENT == r->node_type)
276 r->state = STATE_INIT;
278 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
279 XML_READER_TYPE_END_ELEMENT == r->node_type)
281 r->state = STATE_INIT;
283 else if (XML_READER_TYPE_TEXT == r->node_type)
285 if ( r->target_sheet != NULL)
287 xmlChar *value = xmlTextReaderValue (r->xtr);
288 if ( 0 == xmlStrcmp (value, r->target_sheet))
289 r->state = STATE_SHEET_FOUND;
292 else if (r->target_sheet_index == r->current_sheet + 1)
294 r->state = STATE_SHEET_FOUND;
296 else if (r->target_sheet_index == -1)
298 r->state = STATE_SHEET_FOUND;
302 case STATE_SHEET_FOUND:
303 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
304 XML_READER_TYPE_ELEMENT == r->node_type)
306 r->min_col = INT_MAX;
307 if (! xmlTextReaderIsEmptyElement (r->xtr))
308 r->state = STATE_CELLS_START;
310 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
311 XML_READER_TYPE_ELEMENT == r->node_type)
313 r->state = STATE_MAXROW;
315 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol")) &&
316 XML_READER_TYPE_ELEMENT == r->node_type)
318 r->state = STATE_MAXCOL;
320 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
321 XML_READER_TYPE_END_ELEMENT == r->node_type)
323 r->state = STATE_INIT;
327 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
328 XML_READER_TYPE_END_ELEMENT == r->node_type)
330 r->state = STATE_SHEET_FOUND;
332 else if (r->node_type == XML_READER_TYPE_TEXT)
334 xmlChar *value = xmlTextReaderValue (r->xtr);
335 r->sheets[r->current_sheet].maxrow = _xmlchar_to_int (value);
340 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol")) &&
341 XML_READER_TYPE_END_ELEMENT == r->node_type)
343 r->state = STATE_SHEET_FOUND;
345 else if (r->node_type == XML_READER_TYPE_TEXT)
347 xmlChar *value = xmlTextReaderValue (r->xtr);
348 r->sheets[r->current_sheet].maxcol = _xmlchar_to_int (value);
352 case STATE_CELLS_START:
353 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
354 XML_READER_TYPE_ELEMENT == r->node_type)
356 xmlChar *attr = NULL;
358 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
359 r->col = _xmlchar_to_int (attr);
362 if (r->col < r->min_col)
365 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
366 r->row = _xmlchar_to_int (attr);
369 if (r->sheets[r->current_sheet].start_row == -1)
371 r->sheets[r->current_sheet].start_row = r->row;
374 if (r->sheets[r->current_sheet].start_col == -1)
376 r->sheets[r->current_sheet].start_col = r->col;
378 if (! xmlTextReaderIsEmptyElement (r->xtr))
379 r->state = STATE_CELL;
381 else if ( (0 == xmlStrcasecmp (name, _xml("gnm:Cells"))) && (XML_READER_TYPE_END_ELEMENT == r->node_type) )
383 r->sheets[r->current_sheet].stop_col = r->col;
384 r->sheets[r->current_sheet].stop_row = r->row;
385 r->state = STATE_SHEET_NAME;
389 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) && XML_READER_TYPE_END_ELEMENT == r->node_type)
391 r->state = STATE_CELLS_START;
403 Sets the VAR of case C, to the value corresponding to the xml string XV
406 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
409 union value *v = case_data_rw (c, var);
412 value_set_missing (v, var_get_width (var));
413 else if ( var_is_alpha (var))
414 value_copy_str_rpad (v, var_get_width (var), xv, ' ');
417 const char *text = CHAR_CAST (const char *, xv);
421 v->f = c_strtod (text, &endptr);
422 if ( errno != 0 || endptr == text)
431 xmlChar *first_value;
436 gnumeric_error_handler (void *ctx, const char *mesg,
437 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
439 struct gnumeric_reader *r = ctx;
441 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
443 r->spreadsheet.file_name,
444 xmlTextReaderLocatorLineNumber (loc),
448 static struct gnumeric_reader *
449 gnumeric_reopen (struct gnumeric_reader *r, const char *filename, bool show_errors)
453 xmlTextReaderPtr xtr;
456 assert (r == NULL || filename == NULL);
459 xmlFreeTextReader (r->xtr);
462 gz = gzopen (filename, "r");
464 gz = gzopen ( r->spreadsheet.file_name, "r");
470 xtr = xmlReaderForIO ((xmlInputReadCallback) gzread,
471 (xmlInputCloseCallback) gzclose, gz,
473 show_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
483 r = xzalloc (sizeof *r);
484 r->spreadsheet.n_sheets = -1;
485 r->spreadsheet.file_name = filename;
489 xmlTextReaderSetErrorHandler (xtr, gnumeric_error_handler, r);
491 r->target_sheet = NULL;
492 r->target_sheet_index = -1;
494 r->row = r->col = -1;
495 r->state = STATE_PRE_INIT;
498 /* Advance to the start of the workbook.
499 This gives us some confidence that we are actually dealing with a gnumeric
502 while ( (r->state != STATE_INIT )
503 && 1 == (ret = xmlTextReaderRead (r->xtr)))
511 /* Does not seem to be a gnumeric file */
512 xmlFreeTextReader (r->xtr);
517 r->spreadsheet.type = SPREADSHEET_GNUMERIC;
521 const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
522 xmlCharEncoding xce = xmlParseCharEncoding (CHAR_CAST (const char *, enc));
524 if ( XML_CHAR_ENCODING_UTF8 != xce)
526 /* I have been told that ALL gnumeric files are UTF8 encoded. If that is correct, this
528 msg (MW, _("The gnumeric file `%s' is encoded as %s instead of the usual UTF-8 encoding. "
529 "Any non-ascii characters will be incorrectly imported."),
530 r->spreadsheet.file_name,
540 gnumeric_probe (const char *filename, bool report_errors)
542 struct gnumeric_reader *r = gnumeric_reopen (NULL, filename, report_errors);
544 return &r->spreadsheet;
549 gnumeric_make_reader (struct spreadsheet *spreadsheet,
550 const struct spreadsheet_read_options *opts)
553 struct gnumeric_reader *r = NULL;
554 unsigned long int vstart = 0;
556 casenumber n_cases = CASENUMBER_MAX;
558 struct var_spec *var_spec = NULL;
561 r = (struct gnumeric_reader *) (spreadsheet);
564 r = gnumeric_reopen (r, NULL, true);
566 if ( opts->cell_range )
568 if ( ! convert_cell_ref (opts->cell_range,
569 &r->start_col, &r->start_row,
570 &r->stop_col, &r->stop_row))
572 msg (SE, _("Invalid cell range `%s'"),
585 r->target_sheet = BAD_CAST opts->sheet_name;
586 r->target_sheet_index = opts->sheet_index;
587 r->row = r->col = -1;
588 r->current_sheet = -1;
590 /* Advance to the start of the cells for the target sheet */
591 while ( (r->state != STATE_CELL || r->row < r->start_row )
592 && 1 == (ret = xmlTextReaderRead (r->xtr)))
596 value = xmlTextReaderValue (r->xtr);
598 if ( r->state == STATE_MAXROW && r->node_type == XML_READER_TYPE_TEXT)
600 n_cases = 1 + _xmlchar_to_int (value) ;
605 /* If a range has been given, then use that to calculate the number
607 if ( opts->cell_range)
609 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
612 if ( opts->read_names )
618 /* Read in the first row of cells,
619 including the headers if read_names was set */
621 (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
622 && (ret = xmlTextReaderRead (r->xtr))
628 if ( r->row > r->start_row ) break;
630 if ( r->col < r->start_col ||
631 (r->stop_col != -1 && r->col > r->stop_col))
634 idx = r->col - r->start_col;
636 if ( idx >= n_var_specs )
639 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
640 for (i = n_var_specs; i <= idx; ++i)
642 var_spec [i].name = NULL;
643 var_spec [i].width = -1;
644 var_spec [i].first_value = NULL;
646 n_var_specs = idx + 1 ;
649 if ( r->node_type == XML_READER_TYPE_TEXT )
651 xmlChar *value = xmlTextReaderValue (r->xtr);
652 const char *text = CHAR_CAST (const char *, value);
654 if ( r->row < r->start_row)
656 if ( opts->read_names )
658 var_spec [idx].name = xstrdup (text);
663 var_spec [idx].first_value = xmlStrdup (value);
665 if (-1 == var_spec [idx].width )
666 var_spec [idx].width = (opts->asw == -1) ?
667 ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : opts->asw;
672 else if ( r->node_type == XML_READER_TYPE_ELEMENT
673 && r->state == STATE_CELL)
675 if ( r->row == r->start_row )
678 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
680 if ( NULL == attr || 60 != _xmlchar_to_int (attr))
681 var_spec [idx].width = 0;
689 const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
692 /* Create the dictionary and populate it */
693 spreadsheet->dict = r->dict = dict_create (CHAR_CAST (const char *, enc));
696 for (i = 0 ; i < n_var_specs ; ++i )
700 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
703 /* Probably no data exists for this variable, so allocate a
705 if ( var_spec[i].width == -1 )
706 var_spec[i].width = SPREADSHEET_DEFAULT_WIDTH;
708 name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
709 dict_create_var (r->dict, name, var_spec[i].width);
713 /* Create the first case, and cache it */
714 r->used_first_case = false;
716 if ( n_var_specs == 0 )
718 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
719 spreadsheet->file_name);
723 r->proto = caseproto_ref (dict_get_proto (r->dict));
724 r->first_case = case_create (r->proto);
725 case_set_missing (r->first_case);
728 for ( i = 0 ; i < n_var_specs ; ++i )
730 const struct variable *var;
732 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
735 var = dict_get_var (r->dict, x++);
737 convert_xml_string_to_value (r->first_case, var,
738 var_spec[i].first_value);
741 for ( i = 0 ; i < n_var_specs ; ++i )
743 free (var_spec[i].first_value);
744 free (var_spec[i].name);
750 return casereader_create_sequential
754 &gnm_file_casereader_class, r);
758 for ( i = 0 ; i < n_var_specs ; ++i )
760 free (var_spec[i].first_value);
761 free (var_spec[i].name);
765 dict_destroy (spreadsheet->dict);
766 spreadsheet->dict = NULL;
768 gnm_file_casereader_destroy (NULL, r);
774 /* Reads and returns one case from READER's file. Returns a null
775 pointer on failure. */
776 static struct ccase *
777 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
782 struct gnumeric_reader *r = r_;
783 int current_row = r->row;
785 if ( !r->used_first_case )
787 r->used_first_case = true;
788 return r->first_case;
791 c = case_create (r->proto);
792 case_set_missing (c);
794 if (r->start_col == -1)
795 r->start_col = r->min_col;
797 while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
798 && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
802 if ( r->col < r->start_col || (r->stop_col != -1 &&
803 r->col > r->stop_col))
806 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
809 if ( r->stop_row != -1 && r->row > r->stop_row)
812 if ( r->node_type == XML_READER_TYPE_TEXT )
814 xmlChar *value = xmlTextReaderValue (r->xtr);
816 const int idx = r->col - r->start_col;
818 const struct variable *var = dict_get_var (r->dict, idx);
820 convert_xml_string_to_value (c, var, value);
837 #endif /* GNM_SUPPORT */