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>
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 struct ccase *gnm_file_casereader_read (struct casereader *, void *);
65 static const struct casereader_class gnm_file_casereader_class =
67 gnm_file_casereader_read,
68 gnm_file_casereader_destroy,
73 /* Convert a string, which is an integer encoded in base26
74 IE, A=0, B=1, ... Z=25 to the integer it represents.
75 ... except that in this scheme, digits with an exponent
76 greater than 1 are implicitly incremented by 1, so
77 AA = 0 + 1*26, AB = 1 + 1*26,
78 ABC = 2 + 2*26 + 1*26^2 ....
81 pseudo_base26 (const char *str)
86 int len = strlen (str);
88 for ( i = len - 1 ; i >= 0; --i)
90 int mantissa = (str[i] - 'A');
92 if ( mantissa < 0 || mantissa > 25 )
98 result += mantissa * multiplier;
108 /* Convert a cell reference in the form "A1:B2", to
109 integers. A1 means column zero, row zero.
110 B1 means column 1 row 0. AA1 means column 26, row 0.
113 convert_cell_ref (const char *ref,
114 int *col0, int *row0,
115 int *coli, int *rowi)
123 int n = sscanf (ref, "%4[a-zA-Z]%d:%4[a-zA-Z]%d",
129 str_uppercase (startcol);
130 *col0 = pseudo_base26 (startcol);
131 str_uppercase (stopcol);
132 *coli = pseudo_base26 (stopcol);
133 *row0 = startrow - 1;
134 *rowi = stoprow - 1 ;
142 STATE_INIT = 0, /* Initial state */
143 STATE_SHEET_START, /* Found the start of a sheet */
144 STATE_SHEET_NAME, /* Found the sheet name */
146 STATE_SHEET_FOUND, /* Found the sheet that we actually want */
147 STATE_CELLS_START, /* Found the start of the cell array */
148 STATE_CELL /* Found a cell */
152 struct gnumeric_reader
154 xmlTextReaderPtr xtr;
156 enum reader_state state;
163 const xmlChar *target_sheet;
164 int target_sheet_index;
173 struct dictionary *dict;
174 struct ccase *first_case;
175 bool used_first_case;
178 static void process_node (struct gnumeric_reader *r);
180 #define _xml(X) (const xmlChar *)(X)
182 #define _xmlchar_to_int(X) atoi((const char *)X)
185 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
187 struct gnumeric_reader *r = r_;
192 xmlFreeTextReader (r->xtr);
194 if ( ! r->used_first_case )
195 case_unref (r->first_case);
201 process_node (struct gnumeric_reader *r)
203 xmlChar *name = xmlTextReaderName (r->xtr);
205 name = xmlStrdup (_xml ("--"));
208 r->node_type = xmlTextReaderNodeType (r->xtr);
213 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
214 XML_READER_TYPE_ELEMENT == r->node_type)
216 r->state = STATE_SHEET_START;
219 case STATE_SHEET_START:
220 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
221 XML_READER_TYPE_ELEMENT == r->node_type)
223 r->state = STATE_SHEET_NAME;
225 else if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
226 XML_READER_TYPE_END_ELEMENT == r->node_type)
228 r->state = STATE_INIT;
231 case STATE_SHEET_NAME:
232 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
233 XML_READER_TYPE_END_ELEMENT == r->node_type)
235 r->state = STATE_SHEET_START;
237 else if (XML_READER_TYPE_TEXT == r->node_type)
240 if ( r->target_sheet != NULL)
242 xmlChar *value = xmlTextReaderValue (r->xtr);
243 if ( 0 == xmlStrcmp (value, r->target_sheet))
244 r->state = STATE_SHEET_FOUND;
247 else if (r->target_sheet_index == r->sheet_index)
249 r->state = STATE_SHEET_FOUND;
253 case STATE_SHEET_FOUND:
254 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
255 XML_READER_TYPE_ELEMENT == r->node_type)
257 if (! xmlTextReaderIsEmptyElement (r->xtr))
258 r->state = STATE_CELLS_START;
260 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
261 XML_READER_TYPE_ELEMENT == r->node_type)
263 r->state = STATE_MAXROW;
265 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
266 XML_READER_TYPE_END_ELEMENT == r->node_type)
268 r->state = STATE_INIT;
272 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
273 XML_READER_TYPE_END_ELEMENT == r->node_type)
275 r->state = STATE_SHEET_FOUND;
277 case STATE_CELLS_START:
278 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
279 XML_READER_TYPE_ELEMENT == r->node_type)
281 xmlChar *attr = NULL;
282 r->state = STATE_CELL;
284 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
285 r->col = _xmlchar_to_int (attr);
288 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
289 r->row = _xmlchar_to_int (attr);
292 else if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
293 XML_READER_TYPE_END_ELEMENT == r->node_type)
294 r->state = STATE_SHEET_NAME;
298 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) &&
299 XML_READER_TYPE_END_ELEMENT == r->node_type)
300 r->state = STATE_CELLS_START;
311 Sets the VAR of case C, to the value corresponding to the xml string XV
314 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
319 union value *v = case_data_rw (c, var);
321 text = recode_string (CONV_UTF8_TO_PSPP, (const char *) xv, -1);
324 n_bytes = MIN (var_get_width (var), strlen (text));
326 if ( var_is_alpha (var))
328 memcpy (v->s, text, n_bytes);
334 v->f = strtod (text, &endptr);
335 if ( errno != 0 || endptr == text)
346 xmlChar *first_value;
350 gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict)
352 unsigned long int vstart = 0;
354 casenumber n_cases = CASENUMBER_MAX;
356 struct var_spec *var_spec = NULL;
359 struct gnumeric_reader *r = NULL;
361 gzFile gz = gzopen (gri->file_name, "r");
365 msg (ME, _("Error opening \"%s\" for reading as a gnumeric file: %s."),
366 gri->file_name, strerror (errno));
371 r = xzalloc (sizeof *r);
373 r->xtr = xmlReaderForIO ((xmlInputReadCallback) gzread, gzclose, gz,
379 if ( gri->cell_range )
381 if ( ! convert_cell_ref (gri->cell_range,
382 &r->start_col, &r->start_row,
383 &r->stop_col, &r->stop_row))
385 msg (SE, _("Invalid cell range \"%s\""),
398 r->state = STATE_INIT;
399 r->target_sheet = BAD_CAST gri->sheet_name;
400 r->target_sheet_index = gri->sheet_index;
401 r->row = r->col = -1;
404 /* Advance to the start of the cells for the target sheet */
405 while ( (r->state != STATE_CELL || r->row < r->start_row )
406 && 1 == (ret = xmlTextReaderRead (r->xtr)))
410 value = xmlTextReaderValue (r->xtr);
412 if ( r->state == STATE_MAXROW && r->node_type == XML_READER_TYPE_TEXT)
414 n_cases = 1 + _xmlchar_to_int (value) ;
420 /* If a range has been given, then use that to calculate the number
422 if ( gri->cell_range)
424 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
427 if ( gri->read_names )
433 /* Read in the first row of cells,
434 including the headers if read_names was set */
436 (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
437 && (ret = xmlTextReaderRead (r->xtr))
443 if ( r->row > r->start_row ) break;
445 if ( r->col < r->start_col ||
446 (r->stop_col != -1 && r->col > r->stop_col))
449 idx = r->col - r->start_col;
451 if ( idx >= n_var_specs )
453 n_var_specs = idx + 1 ;
454 var_spec = realloc (var_spec, sizeof (*var_spec) * n_var_specs);
455 var_spec [idx].name = NULL;
456 var_spec [idx].width = -1;
457 var_spec [idx].first_value = NULL;
460 if ( r->node_type == XML_READER_TYPE_TEXT )
463 xmlChar *value = xmlTextReaderValue (r->xtr);
465 text = recode_string (CONV_UTF8_TO_PSPP, (const char *) value, -1);
467 if ( r->row < r->start_row)
469 if ( gri->read_names )
471 var_spec [idx].name = strdup (text);
476 var_spec [idx].first_value = xmlStrdup (value);
478 if (-1 == var_spec [idx].width )
479 var_spec [idx].width = (gri->asw == -1) ?
480 ROUND_UP (strlen(text), MAX_SHORT_STRING) : 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 ();
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 = MAX_SHORT_STRING;
517 r->value_cnt += value_cnt_from_width (var_spec[i].width);
519 if ( ! dict_make_unique_var_name (r->dict, var_spec[i].name,
522 msg (ME, _("Cannot create variable name from %s"), var_spec[i].name);
526 dict_create_var (r->dict, name, var_spec[i].width);
529 /* Create the first case, and cache it */
530 r->used_first_case = false;
532 if ( n_var_specs == 0 )
534 msg (MW, _("Selected sheet or range of spreadsheet \"%s\" is empty."),
539 r->first_case = case_create (r->value_cnt);
540 memset (case_data_rw_idx (r->first_case, 0)->s,
541 ' ', MAX_SHORT_STRING * r->value_cnt);
543 for ( i = 0 ; i < n_var_specs ; ++i )
545 const struct variable *var = dict_get_var (r->dict, i);
547 convert_xml_string_to_value (r->first_case, var,
548 var_spec[i].first_value);
551 for ( i = 0 ; i < n_var_specs ; ++i )
553 free (var_spec[i].first_value);
554 free (var_spec[i].name);
559 return casereader_create_sequential
563 &gnm_file_casereader_class, r);
567 for ( i = 0 ; i < n_var_specs ; ++i )
569 free (var_spec[i].first_value);
570 free (var_spec[i].name);
574 dict_destroy (*dict);
576 gnm_file_casereader_destroy (NULL, r);
582 /* Reads and returns one case from READER's file. Returns a null
583 pointer on failure. */
584 static struct ccase *
585 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
590 struct gnumeric_reader *r = r_;
591 int current_row = r->row;
593 if ( !r->used_first_case )
595 r->used_first_case = true;
596 return r->first_case;
599 c = case_create (r->value_cnt);
601 memset (case_data_rw_idx (c, 0)->s, ' ', MAX_SHORT_STRING * r->value_cnt);
603 while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
604 && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
608 if ( r->col < r->start_col || (r->stop_col != -1 &&
609 r->col > r->stop_col))
612 if ( r->col - r->start_col >= r->value_cnt)
615 if ( r->stop_row != -1 && r->row > r->stop_row)
618 if ( r->node_type == XML_READER_TYPE_TEXT )
620 xmlChar *value = xmlTextReaderValue (r->xtr);
622 const int idx = r->col - r->start_col;
624 const struct variable *var = dict_get_var (r->dict, idx);
626 convert_xml_string_to_value (c, var, value);
643 #endif /* GNM_SUPPORT */