1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2009, 2010, 2011 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/>. */
21 #include "libpspp/message.h"
22 #include "libpspp/misc.h"
24 #include "gl/minmax.h"
27 #define _(msgid) gettext (msgid)
28 #define N_(msgid) (msgid)
34 gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict)
36 msg (ME, _("Support for Gnumeric files was not compiled into this installation of PSPP"));
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 /* Default width of string variables. */
63 #define GNUMERIC_DEFAULT_WIDTH 8
65 static void gnm_file_casereader_destroy (struct casereader *, void *);
67 static struct ccase *gnm_file_casereader_read (struct casereader *, void *);
69 static const struct casereader_class gnm_file_casereader_class =
71 gnm_file_casereader_read,
72 gnm_file_casereader_destroy,
77 /* Convert a string, which is an integer encoded in base26
78 IE, A=0, B=1, ... Z=25 to the integer it represents.
79 ... except that in this scheme, digits with an exponent
80 greater than 1 are implicitly incremented by 1, so
81 AA = 0 + 1*26, AB = 1 + 1*26,
82 ABC = 2 + 2*26 + 1*26^2 ....
85 pseudo_base26 (const char *str)
90 int len = strlen (str);
92 for ( i = len - 1 ; i >= 0; --i)
94 int mantissa = (str[i] - 'A');
96 if ( mantissa < 0 || mantissa > 25 )
102 result += mantissa * multiplier;
112 /* Convert a cell reference in the form "A1:B2", to
113 integers. A1 means column zero, row zero.
114 B1 means column 1 row 0. AA1 means column 26, row 0.
117 convert_cell_ref (const char *ref,
118 int *col0, int *row0,
119 int *coli, int *rowi)
127 int n = sscanf (ref, "%4[a-zA-Z]%d:%4[a-zA-Z]%d",
133 str_uppercase (startcol);
134 *col0 = pseudo_base26 (startcol);
135 str_uppercase (stopcol);
136 *coli = pseudo_base26 (stopcol);
137 *row0 = startrow - 1;
138 *rowi = stoprow - 1 ;
146 STATE_INIT = 0, /* Initial state */
147 STATE_SHEET_START, /* Found the start of a sheet */
148 STATE_SHEET_NAME, /* Found the sheet name */
150 STATE_SHEET_FOUND, /* Found the sheet that we actually want */
151 STATE_CELLS_START, /* Found the start of the cell array */
152 STATE_CELL /* Found a cell */
156 struct gnumeric_reader
158 xmlTextReaderPtr xtr;
160 enum reader_state state;
167 const xmlChar *target_sheet;
168 int target_sheet_index;
175 struct caseproto *proto;
176 struct dictionary *dict;
177 struct ccase *first_case;
178 bool used_first_case;
181 static void process_node (struct gnumeric_reader *r);
183 #define _xml(X) (CHAR_CAST (const xmlChar *, X))
185 #define _xmlchar_to_int(X) (atoi(CHAR_CAST (const char *, X)))
188 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
190 struct gnumeric_reader *r = r_;
195 xmlFreeTextReader (r->xtr);
197 if ( ! r->used_first_case )
198 case_unref (r->first_case);
200 caseproto_unref (r->proto);
206 process_node (struct gnumeric_reader *r)
208 xmlChar *name = xmlTextReaderName (r->xtr);
210 name = xmlStrdup (_xml ("--"));
213 r->node_type = xmlTextReaderNodeType (r->xtr);
218 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
219 XML_READER_TYPE_ELEMENT == r->node_type)
221 r->state = STATE_SHEET_START;
224 case STATE_SHEET_START:
225 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
226 XML_READER_TYPE_ELEMENT == r->node_type)
228 r->state = STATE_SHEET_NAME;
230 else if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
231 XML_READER_TYPE_END_ELEMENT == r->node_type)
233 r->state = STATE_INIT;
236 case STATE_SHEET_NAME:
237 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
238 XML_READER_TYPE_END_ELEMENT == r->node_type)
240 r->state = STATE_SHEET_START;
242 else if (XML_READER_TYPE_TEXT == r->node_type)
245 if ( r->target_sheet != NULL)
247 xmlChar *value = xmlTextReaderValue (r->xtr);
248 if ( 0 == xmlStrcmp (value, r->target_sheet))
249 r->state = STATE_SHEET_FOUND;
252 else if (r->target_sheet_index == r->sheet_index)
254 r->state = STATE_SHEET_FOUND;
258 case STATE_SHEET_FOUND:
259 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
260 XML_READER_TYPE_ELEMENT == r->node_type)
262 if (! xmlTextReaderIsEmptyElement (r->xtr))
263 r->state = STATE_CELLS_START;
265 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
266 XML_READER_TYPE_ELEMENT == r->node_type)
268 r->state = STATE_MAXROW;
270 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
271 XML_READER_TYPE_END_ELEMENT == r->node_type)
273 r->state = STATE_INIT;
277 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
278 XML_READER_TYPE_END_ELEMENT == r->node_type)
280 r->state = STATE_SHEET_FOUND;
282 case STATE_CELLS_START:
283 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
284 XML_READER_TYPE_ELEMENT == r->node_type)
286 xmlChar *attr = NULL;
287 r->state = STATE_CELL;
289 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
290 r->col = _xmlchar_to_int (attr);
293 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
294 r->row = _xmlchar_to_int (attr);
297 else if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
298 XML_READER_TYPE_END_ELEMENT == r->node_type)
299 r->state = STATE_SHEET_NAME;
303 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) &&
304 XML_READER_TYPE_END_ELEMENT == r->node_type)
305 r->state = STATE_CELLS_START;
316 Sets the VAR of case C, to the value corresponding to the xml string XV
319 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
322 union value *v = case_data_rw (c, var);
325 value_set_missing (v, var_get_width (var));
326 else if ( var_is_alpha (var))
327 value_copy_str_rpad (v, var_get_width (var), xv, ' ');
330 const char *text = CHAR_CAST (const char *, xv);
334 v->f = strtod (text, &endptr);
335 if ( errno != 0 || endptr == text)
344 xmlChar *first_value;
348 gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict)
350 unsigned long int vstart = 0;
352 casenumber n_cases = CASENUMBER_MAX;
354 struct var_spec *var_spec = NULL;
357 struct gnumeric_reader *r = NULL;
359 gzFile gz = gzopen (gri->file_name, "r");
363 msg (ME, _("Error opening `%s' for reading as a Gnumeric file: %s."),
364 gri->file_name, strerror (errno));
369 r = xzalloc (sizeof *r);
371 r->xtr = xmlReaderForIO ((xmlInputReadCallback) gzread, gzclose, gz,
377 if ( gri->cell_range )
379 if ( ! convert_cell_ref (gri->cell_range,
380 &r->start_col, &r->start_row,
381 &r->stop_col, &r->stop_row))
383 msg (SE, _("Invalid cell range `%s'"),
396 r->state = STATE_INIT;
397 r->target_sheet = BAD_CAST gri->sheet_name;
398 r->target_sheet_index = gri->sheet_index;
399 r->row = r->col = -1;
402 /* Advance to the start of the cells for the target sheet */
403 while ( (r->state != STATE_CELL || r->row < r->start_row )
404 && 1 == (ret = xmlTextReaderRead (r->xtr)))
408 value = xmlTextReaderValue (r->xtr);
410 if ( r->state == STATE_MAXROW && r->node_type == XML_READER_TYPE_TEXT)
412 n_cases = 1 + _xmlchar_to_int (value) ;
418 /* If a range has been given, then use that to calculate the number
420 if ( gri->cell_range)
422 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
425 if ( gri->read_names )
431 /* Read in the first row of cells,
432 including the headers if read_names was set */
434 (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
435 && (ret = xmlTextReaderRead (r->xtr))
441 if ( r->row > r->start_row ) break;
443 if ( r->col < r->start_col ||
444 (r->stop_col != -1 && r->col > r->stop_col))
447 idx = r->col - r->start_col;
449 if ( idx >= n_var_specs )
451 n_var_specs = idx + 1 ;
452 var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
453 var_spec [idx].name = NULL;
454 var_spec [idx].width = -1;
455 var_spec [idx].first_value = NULL;
458 if ( r->node_type == XML_READER_TYPE_TEXT )
460 xmlChar *value = xmlTextReaderValue (r->xtr);
461 const char *text = CHAR_CAST (const char *, value);
463 if ( r->row < r->start_row)
465 if ( gri->read_names )
467 var_spec [idx].name = xstrdup (text);
472 var_spec [idx].first_value = xmlStrdup (value);
474 if (-1 == var_spec [idx].width )
475 var_spec [idx].width = (gri->asw == -1) ?
476 ROUND_UP (strlen(text), GNUMERIC_DEFAULT_WIDTH) : gri->asw;
481 else if ( r->node_type == XML_READER_TYPE_ELEMENT
482 && r->state == STATE_CELL)
484 if ( r->row == r->start_row )
487 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
489 if ( NULL == attr || 60 != _xmlchar_to_int (attr))
490 var_spec [idx].width = 0;
498 /* Create the dictionary and populate it */
499 *dict = r->dict = dict_create (
500 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
502 for (i = 0 ; i < n_var_specs ; ++i )
506 /* Probably no data exists for this variable, so allocate a
508 if ( var_spec[i].width == -1 )
509 var_spec[i].width = GNUMERIC_DEFAULT_WIDTH;
511 name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
512 dict_create_var (r->dict, name, var_spec[i].width);
516 /* Create the first case, and cache it */
517 r->used_first_case = false;
519 if ( n_var_specs == 0 )
521 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
526 r->proto = caseproto_ref (dict_get_proto (r->dict));
527 r->first_case = case_create (r->proto);
528 case_set_missing (r->first_case);
530 for ( i = 0 ; i < n_var_specs ; ++i )
532 const struct variable *var = dict_get_var (r->dict, i);
534 convert_xml_string_to_value (r->first_case, var,
535 var_spec[i].first_value);
538 for ( i = 0 ; i < n_var_specs ; ++i )
540 free (var_spec[i].first_value);
541 free (var_spec[i].name);
546 return casereader_create_sequential
550 &gnm_file_casereader_class, r);
554 for ( i = 0 ; i < n_var_specs ; ++i )
556 free (var_spec[i].first_value);
557 free (var_spec[i].name);
561 dict_destroy (*dict);
563 gnm_file_casereader_destroy (NULL, r);
569 /* Reads and returns one case from READER's file. Returns a null
570 pointer on failure. */
571 static struct ccase *
572 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
577 struct gnumeric_reader *r = r_;
578 int current_row = r->row;
580 if ( !r->used_first_case )
582 r->used_first_case = true;
583 return r->first_case;
586 c = case_create (r->proto);
587 case_set_missing (c);
589 while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
590 && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
594 if ( r->col < r->start_col || (r->stop_col != -1 &&
595 r->col > r->stop_col))
598 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
601 if ( r->stop_row != -1 && r->row > r->stop_row)
604 if ( r->node_type == XML_READER_TYPE_TEXT )
606 xmlChar *value = xmlTextReaderValue (r->xtr);
608 const int idx = r->col - r->start_col;
610 const struct variable *var = dict_get_var (r->dict, idx);
612 convert_xml_string_to_value (c, var, value);
629 #endif /* GNM_SUPPORT */