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"
31 #include "c-xvasprintf.h"
36 gnumeric_open_reader (struct spreadsheet_read_info *gri, struct spreadsheet_read_options *opts, struct dictionary **dict)
38 msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "Gnumeric");
45 #include "data/gnumeric-reader.h"
50 #include <libxml/xmlreader.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"
64 static void gnm_file_casereader_destroy (struct casereader *, void *);
66 static struct ccase *gnm_file_casereader_read (struct casereader *, void *);
68 static const struct casereader_class gnm_file_casereader_class =
70 gnm_file_casereader_read,
71 gnm_file_casereader_destroy,
78 STATE_PRE_INIT = 0, /* Initial state */
79 STATE_SHEET_COUNT, /* Found the sheet index */
80 STATE_INIT , /* Other Initial state */
81 STATE_SHEET_START, /* Found the start of a sheet */
82 STATE_SHEET_NAME, /* Found the sheet name */
85 STATE_SHEET_FOUND, /* Found the sheet that we actually want */
86 STATE_CELLS_START, /* Found the start of the cell array */
87 STATE_CELL /* Found a cell */
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_get_sheet_name (struct spreadsheet *s, int n)
140 struct gnumeric_reader *gr = (struct gnumeric_reader *) s;
141 assert (n < s->n_sheets);
143 return gr->sheets[n].name; // Kludge: Assumes the encoding is utf8
147 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 ("--"));
212 r->node_type = xmlTextReaderNodeType (r->xtr);
217 r->current_sheet = -1;
218 if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
219 XML_READER_TYPE_ELEMENT == r->node_type)
221 r->state = STATE_SHEET_COUNT;
225 case STATE_SHEET_COUNT:
226 if (0 == xmlStrcasecmp (name, _xml("gnm:SheetName")) &&
227 XML_READER_TYPE_ELEMENT == r->node_type)
230 if (r->current_sheet + 1 > r->spreadsheet.n_sheets)
232 struct sheet_detail *sd ;
233 r->sheets = xrealloc (r->sheets, (r->current_sheet + 1) * sizeof *r->sheets);
234 sd = &r->sheets[r->current_sheet];
235 sd->start_col = sd->stop_col = sd->start_row = sd->stop_row = -1;
236 r->spreadsheet.n_sheets = r->current_sheet + 1;
239 else if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
240 XML_READER_TYPE_END_ELEMENT == r->node_type)
242 r->state = STATE_INIT;
243 r->current_sheet = -1;
245 else if (XML_READER_TYPE_TEXT == r->node_type)
247 r->sheets [r->spreadsheet.n_sheets - 1].name = xmlTextReaderValue (r->xtr);
252 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
253 XML_READER_TYPE_ELEMENT == r->node_type)
256 r->state = STATE_SHEET_START;
259 case STATE_SHEET_START:
260 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
261 XML_READER_TYPE_ELEMENT == r->node_type)
263 r->state = STATE_SHEET_NAME;
266 case STATE_SHEET_NAME:
267 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
268 XML_READER_TYPE_END_ELEMENT == r->node_type)
270 r->state = STATE_INIT;
272 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
273 XML_READER_TYPE_END_ELEMENT == r->node_type)
275 r->state = STATE_INIT;
277 else if (XML_READER_TYPE_TEXT == r->node_type)
279 if ( r->target_sheet != NULL)
281 xmlChar *value = xmlTextReaderValue (r->xtr);
282 if ( 0 == xmlStrcmp (value, r->target_sheet))
283 r->state = STATE_SHEET_FOUND;
286 else if (r->target_sheet_index == r->current_sheet + 1)
288 r->state = STATE_SHEET_FOUND;
290 else if (r->target_sheet_index == -1)
292 r->state = STATE_SHEET_FOUND;
296 case STATE_SHEET_FOUND:
297 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
298 XML_READER_TYPE_ELEMENT == r->node_type)
300 r->min_col = INT_MAX;
301 if (! xmlTextReaderIsEmptyElement (r->xtr))
302 r->state = STATE_CELLS_START;
304 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
305 XML_READER_TYPE_ELEMENT == r->node_type)
307 r->state = STATE_MAXROW;
309 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol")) &&
310 XML_READER_TYPE_ELEMENT == r->node_type)
312 r->state = STATE_MAXCOL;
314 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
315 XML_READER_TYPE_END_ELEMENT == r->node_type)
317 r->state = STATE_INIT;
321 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
322 XML_READER_TYPE_END_ELEMENT == r->node_type)
324 r->state = STATE_SHEET_FOUND;
326 else if (r->node_type == XML_READER_TYPE_TEXT)
328 xmlChar *value = xmlTextReaderValue (r->xtr);
329 r->sheets[r->current_sheet].maxrow = _xmlchar_to_int (value);
334 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol")) &&
335 XML_READER_TYPE_END_ELEMENT == r->node_type)
337 r->state = STATE_SHEET_FOUND;
339 else if (r->node_type == XML_READER_TYPE_TEXT)
341 xmlChar *value = xmlTextReaderValue (r->xtr);
342 r->sheets[r->current_sheet].maxcol = _xmlchar_to_int (value);
346 case STATE_CELLS_START:
347 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
348 XML_READER_TYPE_ELEMENT == r->node_type)
350 xmlChar *attr = NULL;
352 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
353 r->col = _xmlchar_to_int (attr);
356 if (r->col < r->min_col)
359 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
360 r->row = _xmlchar_to_int (attr);
363 if (r->sheets[r->current_sheet].start_row == -1)
365 r->sheets[r->current_sheet].start_row = r->row;
368 if (r->sheets[r->current_sheet].start_col == -1)
370 r->sheets[r->current_sheet].start_col = r->col;
372 if (! xmlTextReaderIsEmptyElement (r->xtr))
373 r->state = STATE_CELL;
375 else if ( (0 == xmlStrcasecmp (name, _xml("gnm:Cells"))) && (XML_READER_TYPE_END_ELEMENT == r->node_type) )
377 r->sheets[r->current_sheet].stop_col = r->col;
378 r->sheets[r->current_sheet].stop_row = r->row;
379 r->state = STATE_SHEET_NAME;
383 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) && XML_READER_TYPE_END_ELEMENT == r->node_type)
385 r->state = STATE_CELLS_START;
397 Sets the VAR of case C, to the value corresponding to the xml string XV
400 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
403 union value *v = case_data_rw (c, var);
406 value_set_missing (v, var_get_width (var));
407 else if ( var_is_alpha (var))
408 value_copy_str_rpad (v, var_get_width (var), xv, ' ');
411 const char *text = CHAR_CAST (const char *, xv);
415 v->f = c_strtod (text, &endptr);
416 if ( errno != 0 || endptr == text)
425 xmlChar *first_value;
430 gnumeric_destroy (struct spreadsheet *s)
432 gnm_file_casereader_destroy (NULL, s);
437 gnumeric_error_handler (void *ctx, const char *mesg,
438 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
440 struct gnumeric_reader *r = ctx;
442 msg (MW, _("There was a problem whilst reading the Gnumeric 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)))
510 /* Does not seem to be a gnumeric file */
511 xmlFreeTextReader (r->xtr);
516 r->spreadsheet.type = SPREADSHEET_GNUMERIC;
523 gnumeric_probe (const char *filename, bool report_errors)
525 struct gnumeric_reader *r = gnumeric_reopen (NULL, filename, report_errors);
527 return &r->spreadsheet;
532 gnumeric_make_reader (struct spreadsheet *spreadsheet,
533 const struct spreadsheet_read_info *gri,
534 struct spreadsheet_read_options *opts)
536 struct gnumeric_reader *r = NULL;
537 unsigned long int vstart = 0;
539 casenumber n_cases = CASENUMBER_MAX;
541 struct var_spec *var_spec = NULL;
544 r = (struct gnumeric_reader *) (spreadsheet);
547 r = gnumeric_reopen (r, NULL, true);
549 if ( opts->cell_range )
551 if ( ! convert_cell_ref (opts->cell_range,
552 &r->start_col, &r->start_row,
553 &r->stop_col, &r->stop_row))
555 msg (SE, _("Invalid cell range `%s'"),
568 r->target_sheet = BAD_CAST opts->sheet_name;
569 r->target_sheet_index = opts->sheet_index;
570 r->row = r->col = -1;
571 r->current_sheet = -1;
573 /* Advance to the start of the cells for the target sheet */
574 while ( (r->state != STATE_CELL || r->row < r->start_row )
575 && 1 == (ret = xmlTextReaderRead (r->xtr)))
579 value = xmlTextReaderValue (r->xtr);
581 if ( r->state == STATE_MAXROW && r->node_type == XML_READER_TYPE_TEXT)
583 n_cases = 1 + _xmlchar_to_int (value) ;
588 /* If a range has been given, then use that to calculate the number
590 if ( opts->cell_range)
592 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
595 if ( gri->read_names )
601 /* Read in the first row of cells,
602 including the headers if read_names was set */
604 (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
605 && (ret = xmlTextReaderRead (r->xtr))
611 if ( r->row > r->start_row ) break;
613 if ( r->col < r->start_col ||
614 (r->stop_col != -1 && r->col > r->stop_col))
617 idx = r->col - r->start_col;
619 if ( idx >= n_var_specs )
622 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
623 for (i = n_var_specs; i <= idx; ++i)
625 var_spec [i].name = NULL;
626 var_spec [i].width = -1;
627 var_spec [i].first_value = NULL;
629 n_var_specs = idx + 1 ;
632 if ( r->node_type == XML_READER_TYPE_TEXT )
634 xmlChar *value = xmlTextReaderValue (r->xtr);
635 const char *text = CHAR_CAST (const char *, value);
637 if ( r->row < r->start_row)
639 if ( gri->read_names )
641 var_spec [idx].name = xstrdup (text);
646 var_spec [idx].first_value = xmlStrdup (value);
648 if (-1 == var_spec [idx].width )
649 var_spec [idx].width = (gri->asw == -1) ?
650 ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : gri->asw;
655 else if ( r->node_type == XML_READER_TYPE_ELEMENT
656 && r->state == STATE_CELL)
658 if ( r->row == r->start_row )
661 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
663 if ( NULL == attr || 60 != _xmlchar_to_int (attr))
664 var_spec [idx].width = 0;
672 const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
675 /* Create the dictionary and populate it */
676 spreadsheet->dict = r->dict = dict_create (CHAR_CAST (const char *, enc));
679 for (i = 0 ; i < n_var_specs ; ++i )
683 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
686 /* Probably no data exists for this variable, so allocate a
688 if ( var_spec[i].width == -1 )
689 var_spec[i].width = SPREADSHEET_DEFAULT_WIDTH;
691 name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
692 dict_create_var (r->dict, name, var_spec[i].width);
696 /* Create the first case, and cache it */
697 r->used_first_case = false;
699 if ( n_var_specs == 0 )
701 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
702 spreadsheet->file_name);
706 r->proto = caseproto_ref (dict_get_proto (r->dict));
707 r->first_case = case_create (r->proto);
708 case_set_missing (r->first_case);
711 for ( i = 0 ; i < n_var_specs ; ++i )
713 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
716 const struct variable *var = dict_get_var (r->dict, x++);
718 convert_xml_string_to_value (r->first_case, var,
719 var_spec[i].first_value);
722 for ( i = 0 ; i < n_var_specs ; ++i )
724 free (var_spec[i].first_value);
725 free (var_spec[i].name);
731 if (opts->cell_range == NULL)
733 opts->cell_range = c_xasprintf ("%c%d:%c%ld",
736 r->stop_col + 'A' + caseproto_get_n_widths (r->proto),
737 r->start_row + n_cases);
740 return casereader_create_sequential
744 &gnm_file_casereader_class, r);
748 for ( i = 0 ; i < n_var_specs ; ++i )
750 free (var_spec[i].first_value);
751 free (var_spec[i].name);
755 dict_destroy (spreadsheet->dict);
756 spreadsheet->dict = NULL;
758 gnm_file_casereader_destroy (NULL, r);
764 /* Reads and returns one case from READER's file. Returns a null
765 pointer on failure. */
766 static struct ccase *
767 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
772 struct gnumeric_reader *r = r_;
773 int current_row = r->row;
775 if ( !r->used_first_case )
777 r->used_first_case = true;
778 return r->first_case;
781 c = case_create (r->proto);
782 case_set_missing (c);
784 if (r->start_col == -1)
785 r->start_col = r->min_col;
787 while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
788 && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
792 if ( r->col < r->start_col || (r->stop_col != -1 &&
793 r->col > r->stop_col))
796 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
799 if ( r->stop_row != -1 && r->row > r->stop_row)
802 if ( r->node_type == XML_READER_TYPE_TEXT )
804 xmlChar *value = xmlTextReaderValue (r->xtr);
806 const int idx = r->col - r->start_col;
808 const struct variable *var = dict_get_var (r->dict, idx);
810 convert_xml_string_to_value (c, var, value);
827 #endif /* GNM_SUPPORT */