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;
109 /* The libxml reader for this instance */
110 xmlTextReaderPtr xtr;
112 /* An internal state variable */
113 enum reader_state state;
126 struct sheet_detail *sheets;
128 const xmlChar *target_sheet;
129 int target_sheet_index;
131 struct caseproto *proto;
132 struct dictionary *dict;
133 struct ccase *first_case;
134 bool used_first_case;
139 gnumeric_destroy (struct spreadsheet *s)
141 struct gnumeric_reader *r = (struct gnumeric_reader *) s;
145 if (0 == --r->ref_cnt)
149 for (i = 0; i < s->n_sheets; ++i)
151 xmlFree (r->sheets[i].name);
163 gnumeric_get_sheet_name (struct spreadsheet *s, int n)
165 struct gnumeric_reader *gr = (struct gnumeric_reader *) s;
166 assert (n < s->n_sheets);
168 return gr->sheets[n].name;
172 static void process_node (struct gnumeric_reader *r);
177 gnumeric_get_sheet_range (struct spreadsheet *s, int n)
180 struct gnumeric_reader *gr = (struct gnumeric_reader *) s;
182 assert (n < s->n_sheets);
185 (gr->sheets[n].stop_col == -1)
187 (1 == (ret = xmlTextReaderRead (gr->xtr)))
193 return create_cell_ref (
194 gr->sheets[n].start_col,
195 gr->sheets[n].start_row,
196 gr->sheets[n].stop_col,
197 gr->sheets[n].stop_row);
202 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
204 struct gnumeric_reader *r = r_;
211 xmlFreeTextReader (r->xtr);
214 if ( ! r->used_first_case )
215 case_unref (r->first_case);
217 caseproto_unref (r->proto);
219 gnumeric_destroy (&r->spreadsheet);
225 process_node (struct gnumeric_reader *r)
227 xmlChar *name = xmlTextReaderName (r->xtr);
229 name = xmlStrdup (_xml ("--"));
231 r->node_type = xmlTextReaderNodeType (r->xtr);
236 r->current_sheet = -1;
237 if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
238 XML_READER_TYPE_ELEMENT == r->node_type)
240 r->state = STATE_SHEET_COUNT;
244 case STATE_SHEET_COUNT:
245 if (0 == xmlStrcasecmp (name, _xml("gnm:SheetName")) &&
246 XML_READER_TYPE_ELEMENT == r->node_type)
249 if (r->current_sheet + 1 > r->spreadsheet.n_sheets)
251 struct sheet_detail *sd ;
252 r->sheets = xrealloc (r->sheets, (r->current_sheet + 1) * sizeof *r->sheets);
253 sd = &r->sheets[r->current_sheet];
254 sd->start_col = sd->stop_col = sd->start_row = sd->stop_row = -1;
255 r->spreadsheet.n_sheets = r->current_sheet + 1;
258 else if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
259 XML_READER_TYPE_END_ELEMENT == r->node_type)
261 r->state = STATE_INIT;
262 r->current_sheet = -1;
264 else if (XML_READER_TYPE_TEXT == r->node_type)
266 r->sheets [r->spreadsheet.n_sheets - 1].name = CHAR_CAST (char *, xmlTextReaderValue (r->xtr));
271 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
272 XML_READER_TYPE_ELEMENT == r->node_type)
275 r->state = STATE_SHEET_START;
278 case STATE_SHEET_START:
279 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
280 XML_READER_TYPE_ELEMENT == r->node_type)
282 r->state = STATE_SHEET_NAME;
285 case STATE_SHEET_NAME:
286 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
287 XML_READER_TYPE_END_ELEMENT == r->node_type)
289 r->state = STATE_INIT;
291 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
292 XML_READER_TYPE_END_ELEMENT == r->node_type)
294 r->state = STATE_INIT;
296 else if (XML_READER_TYPE_TEXT == r->node_type)
298 if ( r->target_sheet != NULL)
300 xmlChar *value = xmlTextReaderValue (r->xtr);
301 if ( 0 == xmlStrcmp (value, r->target_sheet))
302 r->state = STATE_SHEET_FOUND;
305 else if (r->target_sheet_index == r->current_sheet + 1)
307 r->state = STATE_SHEET_FOUND;
309 else if (r->target_sheet_index == -1)
311 r->state = STATE_SHEET_FOUND;
315 case STATE_SHEET_FOUND:
316 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
317 XML_READER_TYPE_ELEMENT == r->node_type)
319 r->min_col = INT_MAX;
320 if (! xmlTextReaderIsEmptyElement (r->xtr))
321 r->state = STATE_CELLS_START;
323 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
324 XML_READER_TYPE_ELEMENT == r->node_type)
326 r->state = STATE_MAXROW;
328 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol")) &&
329 XML_READER_TYPE_ELEMENT == r->node_type)
331 r->state = STATE_MAXCOL;
333 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
334 XML_READER_TYPE_END_ELEMENT == r->node_type)
336 r->state = STATE_INIT;
340 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
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].maxrow = _xmlchar_to_int (value);
353 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol")) &&
354 XML_READER_TYPE_END_ELEMENT == r->node_type)
356 r->state = STATE_SHEET_FOUND;
358 else if (r->node_type == XML_READER_TYPE_TEXT)
360 xmlChar *value = xmlTextReaderValue (r->xtr);
361 r->sheets[r->current_sheet].maxcol = _xmlchar_to_int (value);
365 case STATE_CELLS_START:
366 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
367 XML_READER_TYPE_ELEMENT == r->node_type)
369 xmlChar *attr = NULL;
371 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
372 r->col = _xmlchar_to_int (attr);
375 if (r->col < r->min_col)
378 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
379 r->row = _xmlchar_to_int (attr);
382 if (r->sheets[r->current_sheet].start_row == -1)
384 r->sheets[r->current_sheet].start_row = r->row;
387 if (r->sheets[r->current_sheet].start_col == -1)
389 r->sheets[r->current_sheet].start_col = r->col;
391 if (! xmlTextReaderIsEmptyElement (r->xtr))
392 r->state = STATE_CELL;
394 else if ( (0 == xmlStrcasecmp (name, _xml("gnm:Cells"))) && (XML_READER_TYPE_END_ELEMENT == r->node_type) )
396 r->sheets[r->current_sheet].stop_col = r->col;
397 r->sheets[r->current_sheet].stop_row = r->row;
398 r->state = STATE_SHEET_NAME;
402 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) && XML_READER_TYPE_END_ELEMENT == r->node_type)
404 r->state = STATE_CELLS_START;
416 Sets the VAR of case C, to the value corresponding to the xml string XV
419 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
422 union value *v = case_data_rw (c, var);
425 value_set_missing (v, var_get_width (var));
426 else if ( var_is_alpha (var))
427 value_copy_str_rpad (v, var_get_width (var), xv, ' ');
430 const char *text = CHAR_CAST (const char *, xv);
434 v->f = c_strtod (text, &endptr);
435 if ( errno != 0 || endptr == text)
444 xmlChar *first_value;
449 gnumeric_error_handler (void *ctx, const char *mesg,
450 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
452 struct gnumeric_reader *r = ctx;
454 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
456 r->spreadsheet.file_name,
457 xmlTextReaderLocatorLineNumber (loc),
461 static struct gnumeric_reader *
462 gnumeric_reopen (struct gnumeric_reader *r, const char *filename, bool show_errors)
466 xmlTextReaderPtr xtr;
469 assert (r == NULL || filename == NULL);
472 xmlFreeTextReader (r->xtr);
475 gz = gzopen (filename, "r");
477 gz = gzopen ( r->spreadsheet.file_name, "r");
483 xtr = xmlReaderForIO ((xmlInputReadCallback) gzread,
484 (xmlInputCloseCallback) gzclose, gz,
486 show_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
496 r = xzalloc (sizeof *r);
497 r->spreadsheet.n_sheets = -1;
498 r->spreadsheet.file_name = filename;
502 xmlTextReaderSetErrorHandler (xtr, gnumeric_error_handler, r);
504 r->target_sheet = NULL;
505 r->target_sheet_index = -1;
507 r->row = r->col = -1;
508 r->state = STATE_PRE_INIT;
512 /* Advance to the start of the workbook.
513 This gives us some confidence that we are actually dealing with a gnumeric
516 while ( (r->state != STATE_INIT )
517 && 1 == (ret = xmlTextReaderRead (r->xtr)))
525 /* Does not seem to be a gnumeric file */
526 xmlFreeTextReader (r->xtr);
531 r->spreadsheet.type = SPREADSHEET_GNUMERIC;
535 const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
536 xmlCharEncoding xce = xmlParseCharEncoding (CHAR_CAST (const char *, enc));
538 if ( XML_CHAR_ENCODING_UTF8 != xce)
540 /* I have been told that ALL gnumeric files are UTF8 encoded. If that is correct, this
542 msg (MW, _("The gnumeric file `%s' is encoded as %s instead of the usual UTF-8 encoding. "
543 "Any non-ascii characters will be incorrectly imported."),
544 r->spreadsheet.file_name,
554 gnumeric_probe (const char *filename, bool report_errors)
556 struct gnumeric_reader *r = gnumeric_reopen (NULL, filename, report_errors);
558 return &r->spreadsheet;
563 gnumeric_make_reader (struct spreadsheet *spreadsheet,
564 const struct spreadsheet_read_options *opts)
567 struct gnumeric_reader *r = NULL;
568 unsigned long int vstart = 0;
570 casenumber n_cases = CASENUMBER_MAX;
572 struct var_spec *var_spec = NULL;
575 r = (struct gnumeric_reader *) (spreadsheet);
578 r = gnumeric_reopen (r, NULL, true);
582 if ( opts->cell_range )
584 if ( ! convert_cell_ref (opts->cell_range,
585 &r->start_col, &r->start_row,
586 &r->stop_col, &r->stop_row))
588 msg (SE, _("Invalid cell range `%s'"),
601 r->target_sheet = BAD_CAST opts->sheet_name;
602 r->target_sheet_index = opts->sheet_index;
603 r->row = r->col = -1;
604 r->current_sheet = -1;
606 /* Advance to the start of the cells for the target sheet */
607 while ( (r->state != STATE_CELL || r->row < r->start_row )
608 && 1 == (ret = xmlTextReaderRead (r->xtr)))
612 value = xmlTextReaderValue (r->xtr);
614 if ( r->state == STATE_MAXROW && r->node_type == XML_READER_TYPE_TEXT)
616 n_cases = 1 + _xmlchar_to_int (value) ;
621 /* If a range has been given, then use that to calculate the number
623 if ( opts->cell_range)
625 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
628 if ( opts->read_names )
634 /* Read in the first row of cells,
635 including the headers if read_names was set */
637 (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
638 && (ret = xmlTextReaderRead (r->xtr))
644 if ( r->row > r->start_row ) break;
646 if ( r->col < r->start_col ||
647 (r->stop_col != -1 && r->col > r->stop_col))
650 idx = r->col - r->start_col;
652 if ( idx >= n_var_specs )
655 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
656 for (i = n_var_specs; i <= idx; ++i)
658 var_spec [i].name = NULL;
659 var_spec [i].width = -1;
660 var_spec [i].first_value = NULL;
662 n_var_specs = idx + 1 ;
665 if ( r->node_type == XML_READER_TYPE_TEXT )
667 xmlChar *value = xmlTextReaderValue (r->xtr);
668 const char *text = CHAR_CAST (const char *, value);
670 if ( r->row < r->start_row)
672 if ( opts->read_names )
674 var_spec [idx].name = xstrdup (text);
679 var_spec [idx].first_value = xmlStrdup (value);
681 if (-1 == var_spec [idx].width )
682 var_spec [idx].width = (opts->asw == -1) ?
683 ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : opts->asw;
688 else if ( r->node_type == XML_READER_TYPE_ELEMENT
689 && r->state == STATE_CELL)
691 if ( r->row == r->start_row )
694 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
696 if ( NULL == attr || 60 != _xmlchar_to_int (attr))
697 var_spec [idx].width = 0;
705 const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
708 /* Create the dictionary and populate it */
709 spreadsheet->dict = r->dict = dict_create (CHAR_CAST (const char *, enc));
712 for (i = 0 ; i < n_var_specs ; ++i )
716 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
719 /* Probably no data exists for this variable, so allocate a
721 if ( var_spec[i].width == -1 )
722 var_spec[i].width = SPREADSHEET_DEFAULT_WIDTH;
724 name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
725 dict_create_var (r->dict, name, var_spec[i].width);
729 /* Create the first case, and cache it */
730 r->used_first_case = false;
732 if ( n_var_specs == 0 )
734 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
735 spreadsheet->file_name);
739 r->proto = caseproto_ref (dict_get_proto (r->dict));
740 r->first_case = case_create (r->proto);
741 case_set_missing (r->first_case);
744 for ( i = 0 ; i < n_var_specs ; ++i )
746 const struct variable *var;
748 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
751 var = dict_get_var (r->dict, x++);
753 convert_xml_string_to_value (r->first_case, var,
754 var_spec[i].first_value);
757 for ( i = 0 ; i < n_var_specs ; ++i )
759 free (var_spec[i].first_value);
760 free (var_spec[i].name);
766 return casereader_create_sequential
770 &gnm_file_casereader_class, r);
774 for ( i = 0 ; i < n_var_specs ; ++i )
776 free (var_spec[i].first_value);
777 free (var_spec[i].name);
781 dict_destroy (spreadsheet->dict);
782 spreadsheet->dict = NULL;
784 gnm_file_casereader_destroy (NULL, r);
790 /* Reads and returns one case from READER's file. Returns a null
791 pointer on failure. */
792 static struct ccase *
793 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
798 struct gnumeric_reader *r = r_;
799 int current_row = r->row;
801 if ( !r->used_first_case )
803 r->used_first_case = true;
804 return r->first_case;
807 c = case_create (r->proto);
808 case_set_missing (c);
810 if (r->start_col == -1)
811 r->start_col = r->min_col;
813 while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
814 && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
818 if ( r->col < r->start_col || (r->stop_col != -1 &&
819 r->col > r->stop_col))
822 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
825 if ( r->stop_row != -1 && r->row > r->stop_row)
828 if ( r->node_type == XML_READER_TYPE_TEXT )
830 xmlChar *value = xmlTextReaderValue (r->xtr);
832 const int idx = r->col - r->start_col;
834 const struct variable *var = dict_get_var (r->dict, idx);
836 convert_xml_string_to_value (c, var, value);
853 #endif /* GNM_SUPPORT */