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/format.h"
52 #include "data/data-in.h"
53 #include "data/case.h"
54 #include "data/casereader-provider.h"
55 #include "data/dictionary.h"
56 #include "data/identifier.h"
57 #include "data/value.h"
58 #include "data/variable.h"
59 #include "libpspp/i18n.h"
60 #include "libpspp/str.h"
62 #include "gl/xalloc.h"
65 /* Shamelessly lifted from the Gnumeric sources:
66 https://git.gnome.org/browse/gnumeric/tree/src/value.h
72 VALUE_INTEGER = 30, /* Note, this was removed from gnumeric in 2006 - old versions may of
73 course still be around. New ones are supposed to use float.*/
83 static void gnm_file_casereader_destroy (struct casereader *, void *);
85 static struct ccase *gnm_file_casereader_read (struct casereader *, void *);
88 static const struct casereader_class gnm_file_casereader_class =
90 gnm_file_casereader_read,
91 gnm_file_casereader_destroy,
98 STATE_PRE_INIT = 0, /* Initial state */
99 STATE_SHEET_COUNT, /* Found the sheet index */
100 STATE_INIT , /* Other Initial state */
101 STATE_SHEET_START, /* Found the start of a sheet */
102 STATE_SHEET_NAME, /* Found the sheet name */
105 STATE_SHEET_FOUND, /* Found the sheet that we actually want */
106 STATE_CELLS_START, /* Found the start of the cell array */
107 STATE_CELL /* Found a cell */
112 /* The name of the sheet (utf8 encoding) */
126 /* The libxml reader for this instance */
127 xmlTextReaderPtr xtr;
129 /* An internal state variable */
130 enum reader_state state;
143 state_data_destroy (struct state_data *sd)
145 xmlFreeTextReader (sd->xtr);
149 struct gnumeric_reader
151 struct spreadsheet spreadsheet;
154 struct state_data rsd;
155 struct state_data msd;
162 struct sheet_detail *sheets;
164 const xmlChar *target_sheet;
165 int target_sheet_index;
167 struct caseproto *proto;
168 struct dictionary *dict;
169 struct ccase *first_case;
170 bool used_first_case;
172 enum gnm_value_type vtype;
177 gnumeric_destroy (struct spreadsheet *s)
179 struct gnumeric_reader *r = (struct gnumeric_reader *) s;
181 if (0 == --r->ref_cnt)
185 for (i = 0; i < s->n_sheets; ++i)
187 xmlFree (r->sheets[i].name);
191 state_data_destroy (&r->msd);
199 gnumeric_get_sheet_name (struct spreadsheet *s, int n)
201 struct gnumeric_reader *gr = (struct gnumeric_reader *) s;
202 assert (n < s->n_sheets);
204 return gr->sheets[n].name;
208 static void process_node (struct gnumeric_reader *r, struct state_data *sd);
213 gnumeric_get_sheet_range (struct spreadsheet *s, int n)
216 struct gnumeric_reader *gr = (struct gnumeric_reader *) s;
218 assert (n < s->n_sheets);
221 (gr->sheets[n].stop_col == -1)
223 (1 == (ret = xmlTextReaderRead (gr->msd.xtr)))
226 process_node (gr, &gr->msd);
229 return create_cell_range (
230 gr->sheets[n].start_col,
231 gr->sheets[n].start_row,
232 gr->sheets[n].stop_col,
233 gr->sheets[n].stop_row);
238 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
240 struct gnumeric_reader *r = r_;
245 state_data_destroy (&r->rsd);
247 if (r->first_case && ! r->used_first_case )
248 case_unref (r->first_case);
251 caseproto_unref (r->proto);
253 gnumeric_destroy (&r->spreadsheet);
258 process_node (struct gnumeric_reader *r, struct state_data *sd)
260 xmlChar *name = xmlTextReaderName (sd->xtr);
262 name = xmlStrdup (_xml ("--"));
264 sd->node_type = xmlTextReaderNodeType (sd->xtr);
269 sd->current_sheet = -1;
270 if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
271 XML_READER_TYPE_ELEMENT == sd->node_type)
273 sd->state = STATE_SHEET_COUNT;
277 case STATE_SHEET_COUNT:
278 if (0 == xmlStrcasecmp (name, _xml("gnm:SheetName")) &&
279 XML_READER_TYPE_ELEMENT == sd->node_type)
282 if (sd->current_sheet + 1 > r->spreadsheet.n_sheets)
284 struct sheet_detail *detail ;
285 r->sheets = xrealloc (r->sheets, (sd->current_sheet + 1) * sizeof *r->sheets);
286 detail = &r->sheets[sd->current_sheet];
287 detail->start_col = detail->stop_col = detail->start_row = detail->stop_row = -1;
289 r->spreadsheet.n_sheets = sd->current_sheet + 1;
292 else if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
293 XML_READER_TYPE_END_ELEMENT == sd->node_type)
295 sd->state = STATE_INIT;
296 sd->current_sheet = -1;
298 else if (XML_READER_TYPE_TEXT == sd->node_type)
300 if ( r->sheets [r->spreadsheet.n_sheets - 1].name == NULL)
301 r->sheets [r->spreadsheet.n_sheets - 1].name = CHAR_CAST (char *, xmlTextReaderValue (sd->xtr));
306 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
307 XML_READER_TYPE_ELEMENT == sd->node_type)
310 sd->state = STATE_SHEET_START;
313 case STATE_SHEET_START:
314 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
315 XML_READER_TYPE_ELEMENT == sd->node_type)
317 sd->state = STATE_SHEET_NAME;
320 case STATE_SHEET_NAME:
321 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
322 XML_READER_TYPE_END_ELEMENT == sd->node_type)
324 sd->state = STATE_INIT;
326 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
327 XML_READER_TYPE_END_ELEMENT == sd->node_type)
329 sd->state = STATE_INIT;
331 else if (XML_READER_TYPE_TEXT == sd->node_type)
333 if ( r->target_sheet != NULL)
335 xmlChar *value = xmlTextReaderValue (sd->xtr);
336 if ( 0 == xmlStrcmp (value, r->target_sheet))
337 sd->state = STATE_SHEET_FOUND;
340 else if (r->target_sheet_index == sd->current_sheet + 1)
342 sd->state = STATE_SHEET_FOUND;
344 else if (r->target_sheet_index == -1)
346 sd->state = STATE_SHEET_FOUND;
350 case STATE_SHEET_FOUND:
351 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
352 XML_READER_TYPE_ELEMENT == sd->node_type)
354 sd->min_col = INT_MAX;
355 if (! xmlTextReaderIsEmptyElement (sd->xtr))
356 sd->state = STATE_CELLS_START;
358 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
359 XML_READER_TYPE_ELEMENT == sd->node_type)
361 sd->state = STATE_MAXROW;
363 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol")) &&
364 XML_READER_TYPE_ELEMENT == sd->node_type)
366 sd->state = STATE_MAXCOL;
368 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
369 XML_READER_TYPE_END_ELEMENT == sd->node_type)
371 sd->state = STATE_INIT;
375 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
376 XML_READER_TYPE_END_ELEMENT == sd->node_type)
378 sd->state = STATE_SHEET_FOUND;
380 else if (sd->node_type == XML_READER_TYPE_TEXT)
382 xmlChar *value = xmlTextReaderValue (sd->xtr);
383 r->sheets[sd->current_sheet].maxrow = _xmlchar_to_int (value);
388 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol")) &&
389 XML_READER_TYPE_END_ELEMENT == sd->node_type)
391 sd->state = STATE_SHEET_FOUND;
393 else if (sd->node_type == XML_READER_TYPE_TEXT)
395 xmlChar *value = xmlTextReaderValue (sd->xtr);
396 r->sheets[sd->current_sheet].maxcol = _xmlchar_to_int (value);
400 case STATE_CELLS_START:
401 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
402 XML_READER_TYPE_ELEMENT == sd->node_type)
404 xmlChar *attr = NULL;
406 attr = xmlTextReaderGetAttribute (sd->xtr, _xml ("Col"));
407 sd->col = _xmlchar_to_int (attr);
410 if (sd->col < sd->min_col)
411 sd->min_col = sd->col;
413 attr = xmlTextReaderGetAttribute (sd->xtr, _xml ("Row"));
414 sd->row = _xmlchar_to_int (attr);
417 if (r->sheets[sd->current_sheet].start_row == -1)
419 r->sheets[sd->current_sheet].start_row = sd->row;
422 if (r->sheets[sd->current_sheet].start_col == -1)
424 r->sheets[sd->current_sheet].start_col = sd->col;
426 if (! xmlTextReaderIsEmptyElement (sd->xtr))
427 sd->state = STATE_CELL;
429 else if ( (0 == xmlStrcasecmp (name, _xml("gnm:Cells"))) && (XML_READER_TYPE_END_ELEMENT == sd->node_type) )
431 r->sheets[sd->current_sheet].stop_col = sd->col;
432 r->sheets[sd->current_sheet].stop_row = sd->row;
433 sd->state = STATE_SHEET_NAME;
437 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) && XML_READER_TYPE_END_ELEMENT == sd->node_type)
439 sd->state = STATE_CELLS_START;
451 Sets the VAR of case C, to the value corresponding to the xml string XV
454 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
455 const xmlChar *xv, enum gnm_value_type type, int col, int row)
457 union value *v = case_data_rw (c, var);
460 value_set_missing (v, var_get_width (var));
461 else if ( var_is_alpha (var))
462 value_copy_str_rpad (v, var_get_width (var), xv, ' ');
463 else if (type == VALUE_FLOAT || type == VALUE_INTEGER)
465 const char *text = CHAR_CAST (const char *, xv);
469 v->f = c_strtod (text, &endptr);
470 if ( errno != 0 || endptr == text)
475 const char *text = CHAR_CAST (const char *, xv);
477 const struct fmt_spec *fmt = var_get_write_format (var);
479 char *m = data_in (ss_cstr (text), "UTF-8",
487 char buf [FMT_STRING_LEN_MAX + 1];
488 char *cell = create_cell_ref (col, row);
490 msg (MW, _("Cannot convert the value in the spreadsheet cell %s to format (%s): %s"),
491 cell, fmt_to_string (fmt, buf), m);
502 xmlChar *first_value;
508 gnumeric_error_handler (void *ctx, const char *mesg,
509 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
511 struct gnumeric_reader *r = ctx;
513 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
515 r->spreadsheet.file_name,
516 xmlTextReaderLocatorLineNumber (loc),
520 static struct gnumeric_reader *
521 gnumeric_reopen (struct gnumeric_reader *r, const char *filename, bool show_errors)
524 struct state_data *sd;
526 xmlTextReaderPtr xtr;
529 assert (r == NULL || filename == NULL);
533 gz = gzopen (filename, "r");
537 gz = gzopen (r->spreadsheet.file_name, "r");
544 xtr = xmlReaderForIO ((xmlInputReadCallback) gzread,
545 (xmlInputCloseCallback) gzclose, gz,
547 show_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
557 r = xzalloc (sizeof *r);
558 r->spreadsheet.n_sheets = -1;
559 r->spreadsheet.file_name = filename;
568 xmlTextReaderSetErrorHandler (xtr, gnumeric_error_handler, r);
570 r->target_sheet = NULL;
571 r->target_sheet_index = -1;
573 sd->row = sd->col = -1;
574 sd->state = STATE_PRE_INIT;
578 /* Advance to the start of the workbook.
579 This gives us some confidence that we are actually dealing with a gnumeric
582 while ( (sd->state != STATE_INIT )
583 && 1 == (ret = xmlTextReaderRead (sd->xtr)))
585 process_node (r, sd);
591 /* Does not seem to be a gnumeric file */
592 xmlFreeTextReader (sd->xtr);
597 r->spreadsheet.type = SPREADSHEET_GNUMERIC;
601 const xmlChar *enc = xmlTextReaderConstEncoding (sd->xtr);
602 xmlCharEncoding xce = xmlParseCharEncoding (CHAR_CAST (const char *, enc));
604 if ( XML_CHAR_ENCODING_UTF8 != xce)
606 /* I have been told that ALL gnumeric files are UTF8 encoded. If that is correct, this
608 msg (MW, _("The gnumeric file `%s' is encoded as %s instead of the usual UTF-8 encoding. "
609 "Any non-ascii characters will be incorrectly imported."),
610 r->spreadsheet.file_name,
620 gnumeric_probe (const char *filename, bool report_errors)
622 struct gnumeric_reader *r = gnumeric_reopen (NULL, filename, report_errors);
624 return &r->spreadsheet;
629 gnumeric_make_reader (struct spreadsheet *spreadsheet,
630 const struct spreadsheet_read_options *opts)
634 struct gnumeric_reader *r = NULL;
635 unsigned long int vstart = 0;
637 casenumber n_cases = CASENUMBER_MAX;
639 struct var_spec *var_spec = NULL;
642 r = (struct gnumeric_reader *) (spreadsheet);
644 r = gnumeric_reopen (r, NULL, true);
646 if ( opts->cell_range )
648 if ( ! convert_cell_ref (opts->cell_range,
649 &r->start_col, &r->start_row,
650 &r->stop_col, &r->stop_row))
652 msg (SE, _("Invalid cell range `%s'"),
665 r->target_sheet = BAD_CAST opts->sheet_name;
666 r->target_sheet_index = opts->sheet_index;
667 r->rsd.row = r->rsd.col = -1;
668 r->rsd.current_sheet = -1;
669 r->first_case = NULL;
672 /* Advance to the start of the cells for the target sheet */
673 while ( (r->rsd.state != STATE_CELL || r->rsd.row < r->start_row )
674 && 1 == (ret = xmlTextReaderRead (r->rsd.xtr)))
677 process_node (r, &r->rsd);
678 value = xmlTextReaderValue (r->rsd.xtr);
680 if ( r->rsd.state == STATE_MAXROW && r->rsd.node_type == XML_READER_TYPE_TEXT)
682 n_cases = 1 + _xmlchar_to_int (value) ;
687 /* If a range has been given, then use that to calculate the number
689 if ( opts->cell_range)
691 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
694 if ( opts->read_names )
701 /* Read in the first row of cells,
702 including the headers if read_names was set */
704 (( r->rsd.state == STATE_CELLS_START && r->rsd.row <= r->start_row) || r->rsd.state == STATE_CELL )
705 && (ret = xmlTextReaderRead (r->rsd.xtr))
710 if (r->rsd.state == STATE_CELL && r->rsd.node_type == XML_READER_TYPE_TEXT)
713 xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("ValueType"));
715 type = _xmlchar_to_int (attr);
720 process_node (r, &r->rsd);
722 if ( r->rsd.row > r->start_row )
725 xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("ValueType"));
727 r->vtype = _xmlchar_to_int (attr);
733 if ( r->rsd.col < r->start_col ||
734 (r->stop_col != -1 && r->rsd.col > r->stop_col))
737 idx = r->rsd.col - r->start_col;
739 if ( idx >= n_var_specs )
742 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
743 for (i = n_var_specs; i <= idx; ++i)
745 var_spec [i].name = NULL;
746 var_spec [i].width = -1;
747 var_spec [i].first_value = NULL;
748 var_spec [i].first_type = -1;
750 n_var_specs = idx + 1 ;
753 var_spec [idx].first_type = type;
755 if ( r->rsd.node_type == XML_READER_TYPE_TEXT )
757 xmlChar *value = xmlTextReaderValue (r->rsd.xtr);
758 const char *text = CHAR_CAST (const char *, value);
760 if ( r->rsd.row < r->start_row)
762 if ( opts->read_names )
764 var_spec [idx].name = xstrdup (text);
769 var_spec [idx].first_value = xmlStrdup (value);
771 if (-1 == var_spec [idx].width )
772 var_spec [idx].width = (opts->asw == -1) ?
773 ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : opts->asw;
778 else if ( r->rsd.node_type == XML_READER_TYPE_ELEMENT
779 && r->rsd.state == STATE_CELL)
781 if ( r->rsd.row == r->start_row )
784 xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("ValueType"));
786 if ( NULL == attr || VALUE_STRING != _xmlchar_to_int (attr))
787 var_spec [idx].width = 0;
795 const xmlChar *enc = xmlTextReaderConstEncoding (r->rsd.xtr);
798 /* Create the dictionary and populate it */
799 spreadsheet->dict = r->dict = dict_create (CHAR_CAST (const char *, enc));
802 for (i = 0 ; i < n_var_specs ; ++i )
806 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
809 /* Probably no data exists for this variable, so allocate a
811 if ( var_spec[i].width == -1 )
812 var_spec[i].width = SPREADSHEET_DEFAULT_WIDTH;
814 name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
815 dict_create_var (r->dict, name, var_spec[i].width);
819 /* Create the first case, and cache it */
820 r->used_first_case = false;
822 if ( n_var_specs == 0 )
824 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
825 spreadsheet->file_name);
829 r->proto = caseproto_ref (dict_get_proto (r->dict));
830 r->first_case = case_create (r->proto);
831 case_set_missing (r->first_case);
834 for ( i = 0 ; i < n_var_specs ; ++i )
836 const struct variable *var;
838 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
841 var = dict_get_var (r->dict, x++);
843 convert_xml_string_to_value (r->first_case, var,
844 var_spec[i].first_value,
845 var_spec[i].first_type,
850 for ( i = 0 ; i < n_var_specs ; ++i )
852 free (var_spec[i].first_value);
853 free (var_spec[i].name);
859 return casereader_create_sequential
863 &gnm_file_casereader_class, r);
867 for ( i = 0 ; i < n_var_specs ; ++i )
869 free (var_spec[i].first_value);
870 free (var_spec[i].name);
874 dict_destroy (spreadsheet->dict);
875 spreadsheet->dict = NULL;
877 gnm_file_casereader_destroy (NULL, r);
883 /* Reads and returns one case from READER's file. Returns a null
884 pointer on failure. */
885 static struct ccase *
886 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
891 struct gnumeric_reader *r = r_;
892 int current_row = r->rsd.row;
894 if ( !r->used_first_case )
896 r->used_first_case = true;
897 return r->first_case;
900 c = case_create (r->proto);
901 case_set_missing (c);
903 if (r->start_col == -1)
904 r->start_col = r->rsd.min_col;
907 while ((r->rsd.state == STATE_CELL || r->rsd.state == STATE_CELLS_START )
908 && r->rsd.row == current_row && (ret = xmlTextReaderRead (r->rsd.xtr)))
910 process_node (r, &r->rsd);
912 if (r->rsd.state == STATE_CELL && r->rsd.node_type == XML_READER_TYPE_ELEMENT)
915 xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("ValueType"));
917 r->vtype = _xmlchar_to_int (attr);
922 if ( r->rsd.col < r->start_col || (r->stop_col != -1 &&
923 r->rsd.col > r->stop_col))
926 if ( r->rsd.col - r->start_col >= caseproto_get_n_widths (r->proto))
929 if ( r->stop_row != -1 && r->rsd.row > r->stop_row)
933 if ( r->rsd.node_type == XML_READER_TYPE_TEXT )
935 xmlChar *value = xmlTextReaderValue (r->rsd.xtr);
936 const int idx = r->rsd.col - r->start_col;
937 const struct variable *var = dict_get_var (r->dict, idx);
939 convert_xml_string_to_value (c, var, value, r->vtype,
940 r->rsd.col, r->rsd.row);
956 #endif /* GNM_READ_SUPPORT */