1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007 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>
24 #define _(msgid) gettext (msgid)
25 #define N_(msgid) (msgid)
31 gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict)
33 msg (ME, _("Support for Gnumeric files was not compiled into this installation of PSPP"));
40 #include <data/casereader-provider.h>
42 #include <libpspp/str.h>
43 #include <libpspp/i18n.h>
44 #include <data/dictionary.h>
45 #include <data/variable.h>
49 #include <libxml/xmlreader.h>
53 #include <data/case.h>
54 #include <data/value.h>
56 #include "gnumeric-reader.h"
57 #include <data/identifier.h>
61 static void gnm_file_casereader_destroy (struct casereader *, void *);
63 static bool 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,
74 /* Convert a string, which is an integer encoded in base26
75 IE, A=0, B=1, ... Z=25 to the integer it represents.
76 ... except that in this scheme, digits with an exponent
77 greater than 1 are implicitly incremented by 1, so
78 AA = 0 + 1*26, AB = 1 + 1*26,
79 ABC = 2 + 2*26 + 1*26^2 ....
82 pseudo_base26 (const char *str)
87 int len = strlen (str);
89 for ( i = len - 1 ; i >= 0; --i)
91 int mantissa = (str[i] - 'A');
93 if ( mantissa < 0 || mantissa > 25 )
99 result += mantissa * multiplier;
109 /* Convert a cell reference in the form "A1:B2", to
110 integers. A1 means column zero, row zero.
111 B1 means column 1 row 0. AA1 means column 26, row 0.
114 convert_cell_ref (const char *ref,
115 int *col0, int *row0,
116 int *coli, int *rowi)
124 int n = sscanf (ref, "%4[a-zA-Z]%d:%4[a-zA-Z]%d",
130 str_uppercase (startcol);
131 *col0 = pseudo_base26 (startcol);
132 str_uppercase (stopcol);
133 *coli = pseudo_base26 (stopcol);
134 *row0 = startrow - 1;
135 *rowi = stoprow - 1 ;
143 STATE_INIT = 0, /* Initial state */
144 STATE_SHEET_START, /* Found the start of a sheet */
145 STATE_SHEET_NAME, /* Found the sheet name */
147 STATE_SHEET_FOUND, /* Found the sheet that we actually want */
148 STATE_CELLS_START, /* Found the start of the cell array */
149 STATE_CELL /* Found a cell */
153 struct gnumeric_reader
155 xmlTextReaderPtr xtr;
157 enum reader_state state;
164 const xmlChar *target_sheet;
165 int target_sheet_index;
174 struct dictionary *dict;
175 struct ccase first_case;
176 bool used_first_case;
179 static void process_node (struct gnumeric_reader *r);
181 #define _xml(X) (const xmlChar *)(X)
183 #define _xmlchar_to_int(X) atoi((const char *)X)
186 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
188 struct gnumeric_reader *r = r_;
193 xmlFreeTextReader (r->xtr);
195 if ( ! r->used_first_case )
196 case_destroy (&r->first_case);
202 process_node (struct gnumeric_reader *r)
204 xmlChar *name = xmlTextReaderName (r->xtr);
206 name = xmlStrdup (_xml ("--"));
209 r->node_type = xmlTextReaderNodeType (r->xtr);
214 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
215 XML_READER_TYPE_ELEMENT == r->node_type)
217 r->state = STATE_SHEET_START;
220 case STATE_SHEET_START:
221 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
222 XML_READER_TYPE_ELEMENT == r->node_type)
224 r->state = STATE_SHEET_NAME;
226 else if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
227 XML_READER_TYPE_END_ELEMENT == r->node_type)
229 r->state = STATE_INIT;
232 case STATE_SHEET_NAME:
233 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
234 XML_READER_TYPE_END_ELEMENT == r->node_type)
236 r->state = STATE_SHEET_START;
238 else if (XML_READER_TYPE_TEXT == r->node_type)
241 if ( r->target_sheet != NULL)
243 xmlChar *value = xmlTextReaderValue (r->xtr);
244 if ( 0 == xmlStrcmp (value, r->target_sheet))
245 r->state = STATE_SHEET_FOUND;
248 else if (r->target_sheet_index == r->sheet_index)
250 r->state = STATE_SHEET_FOUND;
254 case STATE_SHEET_FOUND:
255 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
256 XML_READER_TYPE_ELEMENT == r->node_type)
258 if (! xmlTextReaderIsEmptyElement (r->xtr))
259 r->state = STATE_CELLS_START;
261 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
262 XML_READER_TYPE_ELEMENT == r->node_type)
264 r->state = STATE_MAXROW;
266 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
267 XML_READER_TYPE_END_ELEMENT == r->node_type)
269 r->state = STATE_INIT;
273 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
274 XML_READER_TYPE_END_ELEMENT == r->node_type)
276 r->state = STATE_SHEET_FOUND;
278 case STATE_CELLS_START:
279 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
280 XML_READER_TYPE_ELEMENT == r->node_type)
282 xmlChar *attr = NULL;
283 r->state = STATE_CELL;
285 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
286 r->col = _xmlchar_to_int (attr);
289 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
290 r->row = _xmlchar_to_int (attr);
293 else if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
294 XML_READER_TYPE_END_ELEMENT == r->node_type)
295 r->state = STATE_SHEET_NAME;
299 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) &&
300 XML_READER_TYPE_END_ELEMENT == r->node_type)
301 r->state = STATE_CELLS_START;
312 Sets the VAR of case C, to the value corresponding to the xml string XV
315 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
320 union value *v = case_data_rw (c, var);
322 text = recode_string (CONV_UTF8_TO_PSPP, (const char *) xv, -1);
325 n_bytes = MIN (var_get_width (var), strlen (text));
327 if ( var_is_alpha (var))
329 memcpy (v->s, text, n_bytes);
335 v->f = strtod (text, &endptr);
336 if ( errno != 0 || endptr == text)
347 xmlChar *first_value;
351 gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict)
353 unsigned long int vstart = 0;
355 casenumber n_cases = CASENUMBER_MAX;
357 struct var_spec *var_spec = NULL;
360 struct gnumeric_reader *r = NULL;
362 gzFile gz = gzopen (gri->file_name, "r");
366 msg (ME, _("Error opening \"%s\" for reading as a gnumeric file: %s."),
367 gri->file_name, strerror (errno));
372 r = xzalloc (sizeof *r);
374 r->xtr = xmlReaderForIO ((xmlInputReadCallback) gzread, gzclose, gz,
380 if ( gri->cell_range )
382 if ( ! convert_cell_ref (gri->cell_range,
383 &r->start_col, &r->start_row,
384 &r->stop_col, &r->stop_row))
386 msg (SE, _("Invalid cell range \"%s\""),
399 r->state = STATE_INIT;
400 r->target_sheet = BAD_CAST gri->sheet_name;
401 r->target_sheet_index = gri->sheet_index;
402 r->row = r->col = -1;
405 /* Advance to the start of the cells for the target sheet */
406 while ( (r->state != STATE_CELL || r->row < r->start_row )
407 && 1 == (ret = xmlTextReaderRead (r->xtr)))
411 value = xmlTextReaderValue (r->xtr);
413 if ( r->state == STATE_MAXROW && r->node_type == XML_READER_TYPE_TEXT)
415 n_cases = 1 + _xmlchar_to_int (value) ;
421 /* If a range has been given, then use that to calculate the number
423 if ( gri->cell_range)
425 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
428 if ( gri->read_names )
434 /* Read in the first row of cells,
435 including the headers if read_names was set */
437 (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
438 && (ret = xmlTextReaderRead (r->xtr))
444 if ( r->row > r->start_row ) break;
446 if ( r->col < r->start_col ||
447 (r->stop_col != -1 && r->col > r->stop_col))
450 idx = r->col - r->start_col;
452 if ( idx >= n_var_specs )
454 n_var_specs = idx + 1 ;
455 var_spec = realloc (var_spec, sizeof (*var_spec) * n_var_specs);
456 var_spec [idx].name = NULL;
457 var_spec [idx].width = -1;
458 var_spec [idx].first_value = NULL;
461 if ( r->node_type == XML_READER_TYPE_TEXT )
464 xmlChar *value = xmlTextReaderValue (r->xtr);
466 text = recode_string (CONV_UTF8_TO_PSPP, (const char *) value, -1);
468 if ( r->row < r->start_row)
470 if ( gri->read_names )
472 var_spec [idx].name = strdup (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), MAX_SHORT_STRING) : gri->asw;
487 else if ( r->node_type == XML_READER_TYPE_ELEMENT
488 && r->state == STATE_CELL)
490 if ( r->row == r->start_row )
493 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
495 if ( NULL == attr || 60 != _xmlchar_to_int (attr))
496 var_spec [idx].width = 0;
504 /* Create the dictionary and populate it */
505 *dict = r->dict = dict_create ();
509 for (i = 0 ; i < n_var_specs ; ++i )
511 char name[VAR_NAME_LEN + 1];
513 /* Probably no data exists for this variable, so allocate a
515 if ( var_spec[i].width == -1 )
516 var_spec[i].width = MAX_SHORT_STRING;
518 r->value_cnt += value_cnt_from_width (var_spec[i].width);
520 if ( ! dict_make_unique_var_name (r->dict, var_spec[i].name,
523 msg (ME, _("Cannot create variable name from %s"), var_spec[i].name);
527 dict_create_var (r->dict, name, var_spec[i].width);
530 /* Create the first case, and cache it */
531 r->used_first_case = false;
533 if ( n_var_specs == 0 )
535 msg (MW, _("Selected sheet or range of spreadsheet \"%s\" is empty."),
540 case_create (&r->first_case, r->value_cnt);
541 memset (case_data_rw_idx (&r->first_case, 0)->s,
542 ' ', MAX_SHORT_STRING * r->value_cnt);
544 for ( i = 0 ; i < n_var_specs ; ++i )
546 const struct variable *var = dict_get_var (r->dict, i);
548 convert_xml_string_to_value (&r->first_case, var,
549 var_spec[i].first_value);
552 for ( i = 0 ; i < n_var_specs ; ++i )
554 free (var_spec[i].first_value);
555 free (var_spec[i].name);
560 return casereader_create_sequential
564 &gnm_file_casereader_class, r);
568 for ( i = 0 ; i < n_var_specs ; ++i )
570 free (var_spec[i].first_value);
571 free (var_spec[i].name);
575 dict_destroy (*dict);
577 gnm_file_casereader_destroy (NULL, r);
583 /* Reads one case from READER's file into C. Returns true only
586 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_,
591 struct gnumeric_reader *r = r_;
592 int current_row = r->row;
594 if ( !r->used_first_case )
597 r->used_first_case = true;
601 case_create (c, r->value_cnt);
603 memset (case_data_rw_idx (c, 0)->s, ' ', MAX_SHORT_STRING * r->value_cnt);
605 while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
606 && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
610 if ( r->col < r->start_col || (r->stop_col != -1 &&
611 r->col > r->stop_col))
614 if ( r->col - r->start_col >= r->value_cnt)
617 if ( r->stop_row != -1 && r->row > r->stop_row)
620 if ( r->node_type == XML_READER_TYPE_TEXT )
622 xmlChar *value = xmlTextReaderValue (r->xtr);
624 const int idx = r->col - r->start_col;
626 const struct variable *var = dict_get_var (r->dict, idx);
628 convert_xml_string_to_value (c, var, value);
639 #endif /* GNM_SUPPORT */