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 *);
66 static const struct casereader_class gnm_file_casereader_class =
68 gnm_file_casereader_read,
69 gnm_file_casereader_destroy,
76 STATE_PRE_INIT = 0, /* Initial state */
77 STATE_SHEET_COUNT, /* Found the sheet index */
78 STATE_INIT , /* Other Initial state */
79 STATE_SHEET_START, /* Found the start of a sheet */
80 STATE_SHEET_NAME, /* Found the sheet name */
83 STATE_SHEET_FOUND, /* Found the sheet that we actually want */
84 STATE_CELLS_START, /* Found the start of the cell array */
85 STATE_CELL /* Found a cell */
90 /* The name of the sheet (utf8 encoding) */
103 struct gnumeric_reader
105 struct spreadsheet spreadsheet;
107 /* The libxml reader for this instance */
108 xmlTextReaderPtr xtr;
110 /* An internal state variable */
111 enum reader_state state;
124 struct sheet_detail *sheets;
126 const xmlChar *target_sheet;
127 int target_sheet_index;
129 struct caseproto *proto;
130 struct dictionary *dict;
131 struct ccase *first_case;
132 bool used_first_case;
137 gnumeric_get_sheet_name (struct spreadsheet *s, int n)
139 struct gnumeric_reader *gr = (struct gnumeric_reader *) s;
140 assert (n < s->n_sheets);
142 return gr->sheets[n].name;
146 static void process_node (struct gnumeric_reader *r);
151 gnumeric_get_sheet_range (struct spreadsheet *s, int n)
154 struct gnumeric_reader *gr = (struct gnumeric_reader *) s;
156 assert (n < s->n_sheets);
159 (gr->sheets[n].stop_col == -1)
161 (1 == (ret = xmlTextReaderRead (gr->xtr)))
167 return create_cell_ref (
168 gr->sheets[n].start_col,
169 gr->sheets[n].start_row,
170 gr->sheets[n].stop_col,
171 gr->sheets[n].stop_row);
176 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
178 struct gnumeric_reader *r = r_;
183 xmlFreeTextReader (r->xtr);
186 if ( ! r->used_first_case )
187 case_unref (r->first_case);
189 caseproto_unref (r->proto);
192 for (i = 0; i < r->spreadsheet.n_sheets; ++i)
194 xmlFree (r->sheets[i].name);
205 process_node (struct gnumeric_reader *r)
207 xmlChar *name = xmlTextReaderName (r->xtr);
209 name = xmlStrdup (_xml ("--"));
211 r->node_type = xmlTextReaderNodeType (r->xtr);
216 r->current_sheet = -1;
217 if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
218 XML_READER_TYPE_ELEMENT == r->node_type)
220 r->state = STATE_SHEET_COUNT;
224 case STATE_SHEET_COUNT:
225 if (0 == xmlStrcasecmp (name, _xml("gnm:SheetName")) &&
226 XML_READER_TYPE_ELEMENT == r->node_type)
229 if (r->current_sheet + 1 > r->spreadsheet.n_sheets)
231 struct sheet_detail *sd ;
232 r->sheets = xrealloc (r->sheets, (r->current_sheet + 1) * sizeof *r->sheets);
233 sd = &r->sheets[r->current_sheet];
234 sd->start_col = sd->stop_col = sd->start_row = sd->stop_row = -1;
235 r->spreadsheet.n_sheets = r->current_sheet + 1;
238 else if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
239 XML_READER_TYPE_END_ELEMENT == r->node_type)
241 r->state = STATE_INIT;
242 r->current_sheet = -1;
244 else if (XML_READER_TYPE_TEXT == r->node_type)
246 r->sheets [r->spreadsheet.n_sheets - 1].name = CHAR_CAST (char *, xmlTextReaderValue (r->xtr));
251 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
252 XML_READER_TYPE_ELEMENT == r->node_type)
255 r->state = STATE_SHEET_START;
258 case STATE_SHEET_START:
259 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
260 XML_READER_TYPE_ELEMENT == r->node_type)
262 r->state = STATE_SHEET_NAME;
265 case STATE_SHEET_NAME:
266 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
267 XML_READER_TYPE_END_ELEMENT == r->node_type)
269 r->state = STATE_INIT;
271 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
272 XML_READER_TYPE_END_ELEMENT == r->node_type)
274 r->state = STATE_INIT;
276 else if (XML_READER_TYPE_TEXT == r->node_type)
278 if ( r->target_sheet != NULL)
280 xmlChar *value = xmlTextReaderValue (r->xtr);
281 if ( 0 == xmlStrcmp (value, r->target_sheet))
282 r->state = STATE_SHEET_FOUND;
285 else if (r->target_sheet_index == r->current_sheet + 1)
287 r->state = STATE_SHEET_FOUND;
289 else if (r->target_sheet_index == -1)
291 r->state = STATE_SHEET_FOUND;
295 case STATE_SHEET_FOUND:
296 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
297 XML_READER_TYPE_ELEMENT == r->node_type)
299 r->min_col = INT_MAX;
300 if (! xmlTextReaderIsEmptyElement (r->xtr))
301 r->state = STATE_CELLS_START;
303 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
304 XML_READER_TYPE_ELEMENT == r->node_type)
306 r->state = STATE_MAXROW;
308 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol")) &&
309 XML_READER_TYPE_ELEMENT == r->node_type)
311 r->state = STATE_MAXCOL;
313 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
314 XML_READER_TYPE_END_ELEMENT == r->node_type)
316 r->state = STATE_INIT;
320 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
321 XML_READER_TYPE_END_ELEMENT == r->node_type)
323 r->state = STATE_SHEET_FOUND;
325 else if (r->node_type == XML_READER_TYPE_TEXT)
327 xmlChar *value = xmlTextReaderValue (r->xtr);
328 r->sheets[r->current_sheet].maxrow = _xmlchar_to_int (value);
333 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol")) &&
334 XML_READER_TYPE_END_ELEMENT == r->node_type)
336 r->state = STATE_SHEET_FOUND;
338 else if (r->node_type == XML_READER_TYPE_TEXT)
340 xmlChar *value = xmlTextReaderValue (r->xtr);
341 r->sheets[r->current_sheet].maxcol = _xmlchar_to_int (value);
345 case STATE_CELLS_START:
346 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
347 XML_READER_TYPE_ELEMENT == r->node_type)
349 xmlChar *attr = NULL;
351 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
352 r->col = _xmlchar_to_int (attr);
355 if (r->col < r->min_col)
358 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
359 r->row = _xmlchar_to_int (attr);
362 if (r->sheets[r->current_sheet].start_row == -1)
364 r->sheets[r->current_sheet].start_row = r->row;
367 if (r->sheets[r->current_sheet].start_col == -1)
369 r->sheets[r->current_sheet].start_col = r->col;
371 if (! xmlTextReaderIsEmptyElement (r->xtr))
372 r->state = STATE_CELL;
374 else if ( (0 == xmlStrcasecmp (name, _xml("gnm:Cells"))) && (XML_READER_TYPE_END_ELEMENT == r->node_type) )
376 r->sheets[r->current_sheet].stop_col = r->col;
377 r->sheets[r->current_sheet].stop_row = r->row;
378 r->state = STATE_SHEET_NAME;
382 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) && XML_READER_TYPE_END_ELEMENT == r->node_type)
384 r->state = STATE_CELLS_START;
396 Sets the VAR of case C, to the value corresponding to the xml string XV
399 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
402 union value *v = case_data_rw (c, var);
405 value_set_missing (v, var_get_width (var));
406 else if ( var_is_alpha (var))
407 value_copy_str_rpad (v, var_get_width (var), xv, ' ');
410 const char *text = CHAR_CAST (const char *, xv);
414 v->f = c_strtod (text, &endptr);
415 if ( errno != 0 || endptr == text)
424 xmlChar *first_value;
429 gnumeric_destroy (struct spreadsheet *s)
431 gnm_file_casereader_destroy (NULL, s);
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 Gnumeric file `%s' (near line %d): `%s'"),
442 r->spreadsheet.file_name,
443 xmlTextReaderLocatorLineNumber (loc),
447 static struct gnumeric_reader *
448 gnumeric_reopen (struct gnumeric_reader *r, const char *filename, bool show_errors)
452 xmlTextReaderPtr xtr;
455 assert (r == NULL || filename == NULL);
458 xmlFreeTextReader (r->xtr);
461 gz = gzopen (filename, "r");
463 gz = gzopen ( r->spreadsheet.file_name, "r");
469 xtr = xmlReaderForIO ((xmlInputReadCallback) gzread,
470 (xmlInputCloseCallback) gzclose, gz,
472 show_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
482 r = xzalloc (sizeof *r);
483 r->spreadsheet.n_sheets = -1;
484 r->spreadsheet.file_name = filename;
488 xmlTextReaderSetErrorHandler (xtr, gnumeric_error_handler, r);
490 r->target_sheet = NULL;
491 r->target_sheet_index = -1;
493 r->row = r->col = -1;
494 r->state = STATE_PRE_INIT;
497 /* Advance to the start of the workbook.
498 This gives us some confidence that we are actually dealing with a gnumeric
501 while ( (r->state != STATE_INIT )
502 && 1 == (ret = xmlTextReaderRead (r->xtr)))
509 /* Does not seem to be a gnumeric file */
510 xmlFreeTextReader (r->xtr);
515 r->spreadsheet.type = SPREADSHEET_GNUMERIC;
519 const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
520 xmlCharEncoding xce = xmlParseCharEncoding (CHAR_CAST (const char *, enc));
522 if ( XML_CHAR_ENCODING_UTF8 != xce)
524 /* I have been told that ALL gnumeric files are UTF8 encoded. If that is correct, this
526 msg (MW, _("The gnumeric file `%s' is encoded as %s instead of the usual UTF-8 encoding. "
527 "Any non-ascii characters will be incorrectly imported."),
528 r->spreadsheet.file_name,
538 gnumeric_probe (const char *filename, bool report_errors)
540 struct gnumeric_reader *r = gnumeric_reopen (NULL, filename, report_errors);
542 return &r->spreadsheet;
547 gnumeric_make_reader (struct spreadsheet *spreadsheet,
548 const struct spreadsheet_read_options *opts)
551 struct gnumeric_reader *r = NULL;
552 unsigned long int vstart = 0;
554 casenumber n_cases = CASENUMBER_MAX;
556 struct var_spec *var_spec = NULL;
559 r = (struct gnumeric_reader *) (spreadsheet);
562 r = gnumeric_reopen (r, NULL, true);
564 if ( opts->cell_range )
566 if ( ! convert_cell_ref (opts->cell_range,
567 &r->start_col, &r->start_row,
568 &r->stop_col, &r->stop_row))
570 msg (SE, _("Invalid cell range `%s'"),
583 r->target_sheet = BAD_CAST opts->sheet_name;
584 r->target_sheet_index = opts->sheet_index;
585 r->row = r->col = -1;
586 r->current_sheet = -1;
588 /* Advance to the start of the cells for the target sheet */
589 while ( (r->state != STATE_CELL || r->row < r->start_row )
590 && 1 == (ret = xmlTextReaderRead (r->xtr)))
594 value = xmlTextReaderValue (r->xtr);
596 if ( r->state == STATE_MAXROW && r->node_type == XML_READER_TYPE_TEXT)
598 n_cases = 1 + _xmlchar_to_int (value) ;
603 /* If a range has been given, then use that to calculate the number
605 if ( opts->cell_range)
607 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
610 if ( opts->read_names )
616 /* Read in the first row of cells,
617 including the headers if read_names was set */
619 (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
620 && (ret = xmlTextReaderRead (r->xtr))
626 if ( r->row > r->start_row ) break;
628 if ( r->col < r->start_col ||
629 (r->stop_col != -1 && r->col > r->stop_col))
632 idx = r->col - r->start_col;
634 if ( idx >= n_var_specs )
637 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
638 for (i = n_var_specs; i <= idx; ++i)
640 var_spec [i].name = NULL;
641 var_spec [i].width = -1;
642 var_spec [i].first_value = NULL;
644 n_var_specs = idx + 1 ;
647 if ( r->node_type == XML_READER_TYPE_TEXT )
649 xmlChar *value = xmlTextReaderValue (r->xtr);
650 const char *text = CHAR_CAST (const char *, value);
652 if ( r->row < r->start_row)
654 if ( opts->read_names )
656 var_spec [idx].name = xstrdup (text);
661 var_spec [idx].first_value = xmlStrdup (value);
663 if (-1 == var_spec [idx].width )
664 var_spec [idx].width = (opts->asw == -1) ?
665 ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : opts->asw;
670 else if ( r->node_type == XML_READER_TYPE_ELEMENT
671 && r->state == STATE_CELL)
673 if ( r->row == r->start_row )
676 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
678 if ( NULL == attr || 60 != _xmlchar_to_int (attr))
679 var_spec [idx].width = 0;
687 const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
690 /* Create the dictionary and populate it */
691 spreadsheet->dict = r->dict = dict_create (CHAR_CAST (const char *, enc));
694 for (i = 0 ; i < n_var_specs ; ++i )
698 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
701 /* Probably no data exists for this variable, so allocate a
703 if ( var_spec[i].width == -1 )
704 var_spec[i].width = SPREADSHEET_DEFAULT_WIDTH;
706 name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
707 dict_create_var (r->dict, name, var_spec[i].width);
711 /* Create the first case, and cache it */
712 r->used_first_case = false;
714 if ( n_var_specs == 0 )
716 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
717 spreadsheet->file_name);
721 r->proto = caseproto_ref (dict_get_proto (r->dict));
722 r->first_case = case_create (r->proto);
723 case_set_missing (r->first_case);
726 for ( i = 0 ; i < n_var_specs ; ++i )
728 const struct variable *var;
730 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
733 var = dict_get_var (r->dict, x++);
735 convert_xml_string_to_value (r->first_case, var,
736 var_spec[i].first_value);
739 for ( i = 0 ; i < n_var_specs ; ++i )
741 free (var_spec[i].first_value);
742 free (var_spec[i].name);
748 return casereader_create_sequential
752 &gnm_file_casereader_class, r);
756 for ( i = 0 ; i < n_var_specs ; ++i )
758 free (var_spec[i].first_value);
759 free (var_spec[i].name);
763 dict_destroy (spreadsheet->dict);
764 spreadsheet->dict = NULL;
766 gnm_file_casereader_destroy (NULL, r);
772 /* Reads and returns one case from READER's file. Returns a null
773 pointer on failure. */
774 static struct ccase *
775 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
780 struct gnumeric_reader *r = r_;
781 int current_row = r->row;
783 if ( !r->used_first_case )
785 r->used_first_case = true;
786 return r->first_case;
789 c = case_create (r->proto);
790 case_set_missing (c);
792 if (r->start_col == -1)
793 r->start_col = r->min_col;
795 while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
796 && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
800 if ( r->col < r->start_col || (r->stop_col != -1 &&
801 r->col > r->stop_col))
804 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
807 if ( r->stop_row != -1 && r->row > r->stop_row)
810 if ( r->node_type == XML_READER_TYPE_TEXT )
812 xmlChar *value = xmlTextReaderValue (r->xtr);
814 const int idx = r->col - r->start_col;
816 const struct variable *var = dict_get_var (r->dict, idx);
818 convert_xml_string_to_value (c, var, value);
835 #endif /* GNM_SUPPORT */