1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2009 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>
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/casereader-provider.h>
45 #include <libpspp/str.h>
46 #include <libpspp/i18n.h>
47 #include <data/dictionary.h>
48 #include <data/variable.h>
52 #include <libxml/xmlreader.h>
56 #include <data/case.h>
57 #include <data/value.h>
59 #include "gnumeric-reader.h"
60 #include <data/identifier.h>
63 /* Default width of string variables. */
64 #define GNUMERIC_DEFAULT_WIDTH 8
66 static void gnm_file_casereader_destroy (struct casereader *, void *);
68 static struct ccase *gnm_file_casereader_read (struct casereader *, void *);
70 static const struct casereader_class gnm_file_casereader_class =
72 gnm_file_casereader_read,
73 gnm_file_casereader_destroy,
78 /* Convert a string, which is an integer encoded in base26
79 IE, A=0, B=1, ... Z=25 to the integer it represents.
80 ... except that in this scheme, digits with an exponent
81 greater than 1 are implicitly incremented by 1, so
82 AA = 0 + 1*26, AB = 1 + 1*26,
83 ABC = 2 + 2*26 + 1*26^2 ....
86 pseudo_base26 (const char *str)
91 int len = strlen (str);
93 for ( i = len - 1 ; i >= 0; --i)
95 int mantissa = (str[i] - 'A');
97 if ( mantissa < 0 || mantissa > 25 )
103 result += mantissa * multiplier;
113 /* Convert a cell reference in the form "A1:B2", to
114 integers. A1 means column zero, row zero.
115 B1 means column 1 row 0. AA1 means column 26, row 0.
118 convert_cell_ref (const char *ref,
119 int *col0, int *row0,
120 int *coli, int *rowi)
128 int n = sscanf (ref, "%4[a-zA-Z]%d:%4[a-zA-Z]%d",
134 str_uppercase (startcol);
135 *col0 = pseudo_base26 (startcol);
136 str_uppercase (stopcol);
137 *coli = pseudo_base26 (stopcol);
138 *row0 = startrow - 1;
139 *rowi = stoprow - 1 ;
147 STATE_INIT = 0, /* Initial state */
148 STATE_SHEET_START, /* Found the start of a sheet */
149 STATE_SHEET_NAME, /* Found the sheet name */
151 STATE_SHEET_FOUND, /* Found the sheet that we actually want */
152 STATE_CELLS_START, /* Found the start of the cell array */
153 STATE_CELL /* Found a cell */
157 struct gnumeric_reader
159 xmlTextReaderPtr xtr;
161 enum reader_state state;
168 const xmlChar *target_sheet;
169 int target_sheet_index;
176 struct caseproto *proto;
177 struct dictionary *dict;
178 struct ccase *first_case;
179 bool used_first_case;
182 static void process_node (struct gnumeric_reader *r);
184 #define _xml(X) (const xmlChar *)(X)
186 #define _xmlchar_to_int(X) atoi((const char *)X)
189 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
191 struct gnumeric_reader *r = r_;
196 xmlFreeTextReader (r->xtr);
198 if ( ! r->used_first_case )
199 case_unref (r->first_case);
201 caseproto_unref (r->proto);
207 process_node (struct gnumeric_reader *r)
209 xmlChar *name = xmlTextReaderName (r->xtr);
211 name = xmlStrdup (_xml ("--"));
214 r->node_type = xmlTextReaderNodeType (r->xtr);
219 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
220 XML_READER_TYPE_ELEMENT == r->node_type)
222 r->state = STATE_SHEET_START;
225 case STATE_SHEET_START:
226 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
227 XML_READER_TYPE_ELEMENT == r->node_type)
229 r->state = STATE_SHEET_NAME;
231 else if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
232 XML_READER_TYPE_END_ELEMENT == r->node_type)
234 r->state = STATE_INIT;
237 case STATE_SHEET_NAME:
238 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
239 XML_READER_TYPE_END_ELEMENT == r->node_type)
241 r->state = STATE_SHEET_START;
243 else if (XML_READER_TYPE_TEXT == r->node_type)
246 if ( r->target_sheet != NULL)
248 xmlChar *value = xmlTextReaderValue (r->xtr);
249 if ( 0 == xmlStrcmp (value, r->target_sheet))
250 r->state = STATE_SHEET_FOUND;
253 else if (r->target_sheet_index == r->sheet_index)
255 r->state = STATE_SHEET_FOUND;
259 case STATE_SHEET_FOUND:
260 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
261 XML_READER_TYPE_ELEMENT == r->node_type)
263 if (! xmlTextReaderIsEmptyElement (r->xtr))
264 r->state = STATE_CELLS_START;
266 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
267 XML_READER_TYPE_ELEMENT == r->node_type)
269 r->state = STATE_MAXROW;
271 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
272 XML_READER_TYPE_END_ELEMENT == r->node_type)
274 r->state = STATE_INIT;
278 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
279 XML_READER_TYPE_END_ELEMENT == r->node_type)
281 r->state = STATE_SHEET_FOUND;
283 case STATE_CELLS_START:
284 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
285 XML_READER_TYPE_ELEMENT == r->node_type)
287 xmlChar *attr = NULL;
288 r->state = STATE_CELL;
290 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
291 r->col = _xmlchar_to_int (attr);
294 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
295 r->row = _xmlchar_to_int (attr);
298 else if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
299 XML_READER_TYPE_END_ELEMENT == r->node_type)
300 r->state = STATE_SHEET_NAME;
304 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) &&
305 XML_READER_TYPE_END_ELEMENT == r->node_type)
306 r->state = STATE_CELLS_START;
317 Sets the VAR of case C, to the value corresponding to the xml string XV
320 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
324 union value *v = case_data_rw (c, var);
326 const char *text = (const char *) xv;
329 n_bytes = MIN (var_get_width (var), strlen (text));
331 if ( var_is_alpha (var))
333 memcpy (value_str_rw (v, var_get_width (var)), text, n_bytes);
339 v->f = strtod (text, &endptr);
340 if ( errno != 0 || endptr == text)
349 xmlChar *first_value;
353 gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict)
355 unsigned long int vstart = 0;
357 casenumber n_cases = CASENUMBER_MAX;
359 struct var_spec *var_spec = NULL;
362 struct gnumeric_reader *r = NULL;
364 gzFile gz = gzopen (gri->file_name, "r");
368 msg (ME, _("Error opening \"%s\" for reading as a Gnumeric file: %s."),
369 gri->file_name, strerror (errno));
374 r = xzalloc (sizeof *r);
376 r->xtr = xmlReaderForIO ((xmlInputReadCallback) gzread, gzclose, gz,
382 if ( gri->cell_range )
384 if ( ! convert_cell_ref (gri->cell_range,
385 &r->start_col, &r->start_row,
386 &r->stop_col, &r->stop_row))
388 msg (SE, _("Invalid cell range \"%s\""),
401 r->state = STATE_INIT;
402 r->target_sheet = BAD_CAST gri->sheet_name;
403 r->target_sheet_index = gri->sheet_index;
404 r->row = r->col = -1;
407 /* Advance to the start of the cells for the target sheet */
408 while ( (r->state != STATE_CELL || r->row < r->start_row )
409 && 1 == (ret = xmlTextReaderRead (r->xtr)))
413 value = xmlTextReaderValue (r->xtr);
415 if ( r->state == STATE_MAXROW && r->node_type == XML_READER_TYPE_TEXT)
417 n_cases = 1 + _xmlchar_to_int (value) ;
423 /* If a range has been given, then use that to calculate the number
425 if ( gri->cell_range)
427 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
430 if ( gri->read_names )
436 /* Read in the first row of cells,
437 including the headers if read_names was set */
439 (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
440 && (ret = xmlTextReaderRead (r->xtr))
446 if ( r->row > r->start_row ) break;
448 if ( r->col < r->start_col ||
449 (r->stop_col != -1 && r->col > r->stop_col))
452 idx = r->col - r->start_col;
454 if ( idx >= n_var_specs )
456 n_var_specs = idx + 1 ;
457 var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
458 var_spec [idx].name = NULL;
459 var_spec [idx].width = -1;
460 var_spec [idx].first_value = NULL;
463 if ( r->node_type == XML_READER_TYPE_TEXT )
465 xmlChar *value = xmlTextReaderValue (r->xtr);
466 const char *text = (const char *) value;
468 if ( r->row < r->start_row)
470 if ( gri->read_names )
472 var_spec [idx].name = xstrdup (text);
477 var_spec [idx].first_value = xmlStrdup (value);
479 if (-1 == var_spec [idx].width )
480 var_spec [idx].width = (gri->asw == -1) ?
481 ROUND_UP (strlen(text), GNUMERIC_DEFAULT_WIDTH) : gri->asw;
486 else if ( r->node_type == XML_READER_TYPE_ELEMENT
487 && r->state == STATE_CELL)
489 if ( r->row == r->start_row )
492 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
494 if ( NULL == attr || 60 != _xmlchar_to_int (attr))
495 var_spec [idx].width = 0;
503 /* Create the dictionary and populate it */
504 *dict = r->dict = dict_create ();
506 dict_set_encoding (r->dict, (const char *) xmlTextReaderConstEncoding (r->xtr));
508 for (i = 0 ; i < n_var_specs ; ++i )
510 char name[VAR_NAME_LEN + 1];
512 /* Probably no data exists for this variable, so allocate a
514 if ( var_spec[i].width == -1 )
515 var_spec[i].width = GNUMERIC_DEFAULT_WIDTH;
517 if ( ! dict_make_unique_var_name (r->dict, var_spec[i].name,
520 msg (ME, _("Cannot create variable name from %s"), var_spec[i].name);
524 dict_create_var (r->dict, name, var_spec[i].width);
527 /* Create the first case, and cache it */
528 r->used_first_case = false;
530 if ( n_var_specs == 0 )
532 msg (MW, _("Selected sheet or range of spreadsheet \"%s\" is empty."),
537 r->proto = caseproto_ref (dict_get_proto (r->dict));
538 r->first_case = case_create (r->proto);
539 case_set_missing (r->first_case);
541 for ( i = 0 ; i < n_var_specs ; ++i )
543 const struct variable *var = dict_get_var (r->dict, i);
545 convert_xml_string_to_value (r->first_case, var,
546 var_spec[i].first_value);
549 for ( i = 0 ; i < n_var_specs ; ++i )
551 free (var_spec[i].first_value);
552 free (var_spec[i].name);
557 return casereader_create_sequential
561 &gnm_file_casereader_class, r);
565 for ( i = 0 ; i < n_var_specs ; ++i )
567 free (var_spec[i].first_value);
568 free (var_spec[i].name);
572 dict_destroy (*dict);
574 gnm_file_casereader_destroy (NULL, r);
580 /* Reads and returns one case from READER's file. Returns a null
581 pointer on failure. */
582 static struct ccase *
583 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
588 struct gnumeric_reader *r = r_;
589 int current_row = r->row;
591 if ( !r->used_first_case )
593 r->used_first_case = true;
594 return r->first_case;
597 c = case_create (r->proto);
598 case_set_missing (c);
600 while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
601 && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
605 if ( r->col < r->start_col || (r->stop_col != -1 &&
606 r->col > r->stop_col))
609 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
612 if ( r->stop_row != -1 && r->row > r->stop_row)
615 if ( r->node_type == XML_READER_TYPE_TEXT )
617 xmlChar *value = xmlTextReaderValue (r->xtr);
619 const int idx = r->col - r->start_col;
621 const struct variable *var = dict_get_var (r->dict, idx);
623 convert_xml_string_to_value (c, var, value);
640 #endif /* GNM_SUPPORT */