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 <gl/mbswidth.h>
58 #include "gnumeric-reader.h"
59 #include <data/identifier.h>
63 static void gnm_file_casereader_destroy (struct casereader *, void *);
65 static bool gnm_file_casereader_read (struct casereader *, void *,
68 static struct casereader_class gnm_file_casereader_class =
70 gnm_file_casereader_read,
71 gnm_file_casereader_destroy,
76 /* Convert a string, which is an integer encoded in base26
77 IE, A=0, B=1, ... Z=25 to the integer it represents.
78 ... except that in this scheme, digits with an exponent
79 greater than 1 are implicitly incremented by 1, so
80 AA = 0 + 1*26, AB = 1 + 1*26,
81 ABC = 2 + 2*26 + 1*26^2 ....
84 pseudo_base26 (const char *str)
89 int len = strlen (str);
91 for ( i = len - 1 ; i >= 0; --i)
93 int mantissa = (str[i] - 'A');
95 if ( mantissa < 0 || mantissa > 25 )
101 result += mantissa * multiplier;
111 /* Convert a cell reference in the form "A1:B2", to
112 integers. A1 means column zero, row zero.
113 B1 means column 1 row 0. AA1 means column 26, row 0.
116 convert_cell_ref (const char *ref,
117 int *col0, int *row0,
118 int *coli, int *rowi)
126 int n = sscanf (ref, "%4[a-zA-Z]%d:%4[a-zA-Z]%d",
132 str_uppercase (startcol);
133 *col0 = pseudo_base26 (startcol);
134 str_uppercase (stopcol);
135 *coli = pseudo_base26 (stopcol);
136 *row0 = startrow - 1;
137 *rowi = stoprow - 1 ;
145 STATE_INIT = 0, /* Initial state */
146 STATE_SHEET_START, /* Found the start of a sheet */
147 STATE_SHEET_NAME, /* Found the sheet name */
149 STATE_SHEET_FOUND, /* Found the sheet that we actually want */
150 STATE_CELLS_START, /* Found the start of the cell array */
151 STATE_CELL /* Found a cell */
155 struct gnumeric_reader
157 xmlTextReaderPtr xtr;
159 enum reader_state state;
166 const xmlChar *target_sheet;
167 int target_sheet_index;
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) (const xmlChar *)(X)
185 #define _xmlchar_to_int(X) atoi((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_destroy (&r->first_case);
204 process_node (struct gnumeric_reader *r)
206 xmlChar *name = xmlTextReaderName (r->xtr);
208 name = xmlStrdup (_xml ("--"));
211 r->node_type = xmlTextReaderNodeType (r->xtr);
216 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
217 XML_READER_TYPE_ELEMENT == r->node_type)
219 r->state = STATE_SHEET_START;
222 case STATE_SHEET_START:
223 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
224 XML_READER_TYPE_ELEMENT == r->node_type)
226 r->state = STATE_SHEET_NAME;
228 else if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
229 XML_READER_TYPE_END_ELEMENT == r->node_type)
231 r->state = STATE_INIT;
234 case STATE_SHEET_NAME:
235 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
236 XML_READER_TYPE_END_ELEMENT == r->node_type)
238 r->state = STATE_SHEET_START;
240 else if (XML_READER_TYPE_TEXT == r->node_type)
243 if ( r->target_sheet != NULL)
245 xmlChar *value = xmlTextReaderValue (r->xtr);
246 if ( 0 == xmlStrcmp (value, r->target_sheet))
247 r->state = STATE_SHEET_FOUND;
250 else if (r->target_sheet_index == r->sheet_index)
252 r->state = STATE_SHEET_FOUND;
256 case STATE_SHEET_FOUND:
257 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
258 XML_READER_TYPE_ELEMENT == r->node_type)
260 if (! xmlTextReaderIsEmptyElement (r->xtr))
261 r->state = STATE_CELLS_START;
263 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
264 XML_READER_TYPE_ELEMENT == r->node_type)
266 r->state = STATE_MAXROW;
268 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
269 XML_READER_TYPE_END_ELEMENT == r->node_type)
271 r->state = STATE_INIT;
275 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
276 XML_READER_TYPE_END_ELEMENT == r->node_type)
278 r->state = STATE_SHEET_FOUND;
280 case STATE_CELLS_START:
281 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
282 XML_READER_TYPE_ELEMENT == r->node_type)
284 xmlChar *attr = NULL;
285 r->state = STATE_CELL;
287 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
288 r->col = _xmlchar_to_int (attr);
291 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
292 r->row = _xmlchar_to_int (attr);
295 else if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
296 XML_READER_TYPE_END_ELEMENT == r->node_type)
297 r->state = STATE_SHEET_NAME;
301 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) &&
302 XML_READER_TYPE_END_ELEMENT == r->node_type)
303 r->state = STATE_CELLS_START;
315 Change SUGGESTION until it's a valid name that can be added to DICT.
318 devise_name (const struct dictionary *dict, struct string *name, int *x)
320 struct string basename;
321 if ( ds_is_empty (name))
322 ds_init_cstr (&basename, "var");
324 ds_init_string (&basename, name);
328 ds_put_format (name, "%s%d", ds_cstr (&basename), ++(*x));
330 while (NULL != dict_lookup_var (dict, ds_cstr (name)) );
332 ds_destroy (&basename);
336 Mutate NAME of a variable, which is gauranteed to be valid for the
340 munge_name (const struct dictionary *dict, struct string *name)
344 if (! ds_is_empty (name))
346 /* Change all the invalid characters to valid ones */
351 if ( !lex_is_id1 (*s))
356 while (s < ds_data (name) + ds_length (name))
358 if ( !lex_is_idn (*s))
363 assert (var_is_valid_name (ds_cstr (name), false));
366 while (ds_is_empty (name) || NULL != dict_lookup_var (dict, ds_cstr (name)) )
368 devise_name (dict, name, &x);
374 Sets the VAR of case C, to the value corresponding to the xml string XV
377 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
382 union value *v = case_data_rw (c, var);
384 text = recode_string (CONV_UTF8_TO_PSPP, (const char *) xv, -1);
387 n_bytes = MIN (var_get_width (var), strlen (text));
389 if ( var_is_alpha (var))
391 memcpy (v->s, text, n_bytes);
397 v->f = strtod (text, &endptr);
398 if ( errno != 0 || endptr == text)
409 xmlChar *first_value;
413 gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict)
416 casenumber n_cases = CASENUMBER_MAX;
418 struct var_spec *var_spec = NULL;
421 struct gnumeric_reader *r = NULL;
423 gzFile gz = gzopen (gri->file_name, "r");
427 msg (ME, _("Error opening \"%s\" for reading as a gnumeric file: %s."),
428 gri->file_name, strerror (errno));
433 r = xzalloc (sizeof *r);
435 r->xtr = xmlReaderForIO ((xmlInputReadCallback) gzread, gzclose, gz,
441 if ( gri->cell_range )
443 if ( ! convert_cell_ref (gri->cell_range,
444 &r->start_col, &r->start_row,
445 &r->stop_col, &r->stop_row))
447 msg (SE, _("Invalid cell range \"%s\""),
460 r->state = STATE_INIT;
461 r->target_sheet = BAD_CAST gri->sheet_name;
462 r->target_sheet_index = gri->sheet_index;
463 r->row = r->col = -1;
466 /* Advance to the start of the cells for the target sheet */
467 while ( (r->state != STATE_CELL || r->row < r->start_row )
468 && 1 == (ret = xmlTextReaderRead (r->xtr)))
472 value = xmlTextReaderValue (r->xtr);
474 if ( r->state == STATE_MAXROW && r->node_type == XML_READER_TYPE_TEXT)
476 n_cases = 1 + _xmlchar_to_int (value) ;
482 /* If a range has been given, then use that to calculate the number
484 if ( gri->cell_range)
486 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
489 if ( gri->read_names )
495 /* Read in the first row of cells,
496 including the headers if read_names was set */
498 (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
499 && (ret = xmlTextReaderRead (r->xtr))
505 if ( r->row > r->start_row ) break;
507 if ( r->col < r->start_col ||
508 (r->stop_col != -1 && r->col > r->stop_col))
511 idx = r->col - r->start_col;
513 if ( idx >= n_var_specs )
515 n_var_specs = idx + 1 ;
516 var_spec = realloc (var_spec, sizeof (*var_spec) * n_var_specs);
517 var_spec [idx].name = NULL;
518 var_spec [idx].width = -1;
519 var_spec [idx].first_value = NULL;
522 if ( r->node_type == XML_READER_TYPE_TEXT )
525 xmlChar *value = xmlTextReaderValue (r->xtr);
527 text = recode_string (CONV_UTF8_TO_PSPP, (const char *) value, -1);
529 if ( r->row < r->start_row)
531 if ( gri->read_names )
533 var_spec [idx].name = strdup (text);
538 var_spec [idx].first_value = xmlStrdup (value);
540 if (-1 == var_spec [idx].width )
541 var_spec [idx].width = (gri->asw == -1) ?
542 ROUND_UP (strlen(text), MAX_SHORT_STRING) : gri->asw;
548 else if ( r->node_type == XML_READER_TYPE_ELEMENT
549 && r->state == STATE_CELL)
551 if ( r->row == r->start_row )
554 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
556 if ( 60 != _xmlchar_to_int (attr))
557 var_spec [idx].width = 0;
565 /* Create the dictionary and populate it */
566 *dict = r->dict = dict_create ();
570 for (i = 0 ; i < n_var_specs ; ++i )
574 /* Probably no data exists for this variable, so allocate a default width */
575 if ( var_spec[i].width == -1 )
576 var_spec[i].width = MAX_SHORT_STRING;
578 r->value_cnt += value_cnt_from_width (var_spec[i].width);
580 if (var_spec[i].name)
581 ds_init_cstr (&name, var_spec[i].name);
583 ds_init_empty (&name);
585 munge_name (r->dict, &name);
588 dict_create_var (r->dict, ds_cstr (&name), var_spec[i].width);
593 /* Create the first case, and cache it */
594 r->used_first_case = false;
596 if ( n_var_specs == 0 )
598 msg (MW, _("Selected sheet or range of spreadsheet \"%s\" is empty."),
603 case_create (&r->first_case, r->value_cnt);
604 memset (case_data_rw_idx (&r->first_case, 0)->s,
605 ' ', MAX_SHORT_STRING * r->value_cnt);
607 for ( i = 0 ; i < n_var_specs ; ++i )
609 const struct variable *var = dict_get_var (r->dict, i);
611 convert_xml_string_to_value (&r->first_case, var,
612 var_spec[i].first_value);
615 for ( i = 0 ; i < n_var_specs ; ++i )
617 free (var_spec[i].first_value);
618 free (var_spec[i].name);
623 return casereader_create_sequential
627 &gnm_file_casereader_class, r);
631 for ( i = 0 ; i < n_var_specs ; ++i )
633 free (var_spec[i].first_value);
634 free (var_spec[i].name);
639 gnm_file_casereader_destroy (NULL, r);
645 /* Reads one case from READER's file into C. Returns true only
648 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_,
653 struct gnumeric_reader *r = r_;
654 int current_row = r->row;
656 if ( !r->used_first_case )
659 r->used_first_case = true;
663 case_create (c, r->value_cnt);
665 memset (case_data_rw_idx (c, 0)->s, ' ', MAX_SHORT_STRING * r->value_cnt);
667 while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
668 && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
672 if ( r->col < r->start_col || (r->stop_col != -1 &&
673 r->col > r->stop_col))
676 if ( r->col - r->start_col >= r->value_cnt)
679 if ( r->stop_row != -1 && r->row > r->stop_row)
682 if ( r->node_type == XML_READER_TYPE_TEXT )
684 xmlChar *value = xmlTextReaderValue (r->xtr);
686 const int idx = r->col - r->start_col;
688 const struct variable *var = dict_get_var (r->dict, idx);
690 convert_xml_string_to_value (c, var, value);
701 #endif /* GNM_SUPPORT */