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/>. */
19 #include "libpspp/message.h"
20 #include "libpspp/misc.h"
22 #include "gl/minmax.h"
25 #define _(msgid) gettext (msgid)
26 #define N_(msgid) (msgid)
28 #include "spreadsheet-reader.h"
33 gnumeric_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
35 msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "Gnumeric");
42 #include "data/gnumeric-reader.h"
47 #include <libxml/xmlreader.h>
50 #include "data/case.h"
51 #include "data/casereader-provider.h"
52 #include "data/dictionary.h"
53 #include "data/identifier.h"
54 #include "data/value.h"
55 #include "data/variable.h"
56 #include "libpspp/i18n.h"
57 #include "libpspp/str.h"
59 #include "gl/xalloc.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,
75 STATE_INIT = 0, /* Initial state */
76 STATE_SHEET_START, /* Found the start of a sheet */
77 STATE_SHEET_NAME, /* Found the sheet name */
79 STATE_SHEET_FOUND, /* Found the sheet that we actually want */
80 STATE_CELLS_START, /* Found the start of the cell array */
81 STATE_CELL /* Found a cell */
85 struct gnumeric_reader
89 enum reader_state state;
96 const xmlChar *target_sheet;
97 int target_sheet_index;
104 struct caseproto *proto;
105 struct dictionary *dict;
106 struct ccase *first_case;
107 bool used_first_case;
110 static void process_node (struct gnumeric_reader *r);
114 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
116 struct gnumeric_reader *r = r_;
121 xmlFreeTextReader (r->xtr);
123 if ( ! r->used_first_case )
124 case_unref (r->first_case);
126 caseproto_unref (r->proto);
132 process_node (struct gnumeric_reader *r)
134 xmlChar *name = xmlTextReaderName (r->xtr);
136 name = xmlStrdup (_xml ("--"));
139 r->node_type = xmlTextReaderNodeType (r->xtr);
144 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
145 XML_READER_TYPE_ELEMENT == r->node_type)
147 r->state = STATE_SHEET_START;
150 case STATE_SHEET_START:
151 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
152 XML_READER_TYPE_ELEMENT == r->node_type)
154 r->state = STATE_SHEET_NAME;
156 else if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
157 XML_READER_TYPE_END_ELEMENT == r->node_type)
159 r->state = STATE_INIT;
162 case STATE_SHEET_NAME:
163 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
164 XML_READER_TYPE_END_ELEMENT == r->node_type)
166 r->state = STATE_SHEET_START;
168 else if (XML_READER_TYPE_TEXT == r->node_type)
171 if ( r->target_sheet != NULL)
173 xmlChar *value = xmlTextReaderValue (r->xtr);
174 if ( 0 == xmlStrcmp (value, r->target_sheet))
175 r->state = STATE_SHEET_FOUND;
178 else if (r->target_sheet_index == r->sheet_index)
180 r->state = STATE_SHEET_FOUND;
184 case STATE_SHEET_FOUND:
185 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
186 XML_READER_TYPE_ELEMENT == r->node_type)
188 if (! xmlTextReaderIsEmptyElement (r->xtr))
189 r->state = STATE_CELLS_START;
191 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
192 XML_READER_TYPE_ELEMENT == r->node_type)
194 r->state = STATE_MAXROW;
196 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
197 XML_READER_TYPE_END_ELEMENT == r->node_type)
199 r->state = STATE_INIT;
203 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
204 XML_READER_TYPE_END_ELEMENT == r->node_type)
206 r->state = STATE_SHEET_FOUND;
208 case STATE_CELLS_START:
209 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
210 XML_READER_TYPE_ELEMENT == r->node_type)
212 xmlChar *attr = NULL;
213 r->state = STATE_CELL;
215 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
216 r->col = _xmlchar_to_int (attr);
219 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
220 r->row = _xmlchar_to_int (attr);
223 else if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
224 XML_READER_TYPE_END_ELEMENT == r->node_type)
225 r->state = STATE_SHEET_NAME;
229 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) &&
230 XML_READER_TYPE_END_ELEMENT == r->node_type)
231 r->state = STATE_CELLS_START;
242 Sets the VAR of case C, to the value corresponding to the xml string XV
245 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
248 union value *v = case_data_rw (c, var);
251 value_set_missing (v, var_get_width (var));
252 else if ( var_is_alpha (var))
253 value_copy_str_rpad (v, var_get_width (var), xv, ' ');
256 const char *text = CHAR_CAST (const char *, xv);
260 v->f = strtod (text, &endptr);
261 if ( errno != 0 || endptr == text)
270 xmlChar *first_value;
274 gnumeric_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
276 unsigned long int vstart = 0;
278 casenumber n_cases = CASENUMBER_MAX;
280 struct var_spec *var_spec = NULL;
283 struct gnumeric_reader *r = NULL;
285 gzFile gz = gzopen (gri->file_name, "r");
289 msg (ME, _("Error opening `%s' for reading as a Gnumeric file: %s."),
290 gri->file_name, strerror (errno));
295 r = xzalloc (sizeof *r);
297 r->xtr = xmlReaderForIO ((xmlInputReadCallback) gzread, gzclose, gz,
303 if ( gri->cell_range )
305 if ( ! convert_cell_ref (gri->cell_range,
306 &r->start_col, &r->start_row,
307 &r->stop_col, &r->stop_row))
309 msg (SE, _("Invalid cell range `%s'"),
322 r->state = STATE_INIT;
323 r->target_sheet = BAD_CAST gri->sheet_name;
324 r->target_sheet_index = gri->sheet_index;
325 r->row = r->col = -1;
328 /* Advance to the start of the cells for the target sheet */
329 while ( (r->state != STATE_CELL || r->row < r->start_row )
330 && 1 == (ret = xmlTextReaderRead (r->xtr)))
334 value = xmlTextReaderValue (r->xtr);
336 if ( r->state == STATE_MAXROW && r->node_type == XML_READER_TYPE_TEXT)
338 n_cases = 1 + _xmlchar_to_int (value) ;
344 /* If a range has been given, then use that to calculate the number
346 if ( gri->cell_range)
348 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
351 if ( gri->read_names )
357 /* Read in the first row of cells,
358 including the headers if read_names was set */
360 (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
361 && (ret = xmlTextReaderRead (r->xtr))
367 if ( r->row > r->start_row ) break;
369 if ( r->col < r->start_col ||
370 (r->stop_col != -1 && r->col > r->stop_col))
373 idx = r->col - r->start_col;
375 if ( idx >= n_var_specs )
377 n_var_specs = idx + 1 ;
378 var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
379 var_spec [idx].name = NULL;
380 var_spec [idx].width = -1;
381 var_spec [idx].first_value = NULL;
384 if ( r->node_type == XML_READER_TYPE_TEXT )
386 xmlChar *value = xmlTextReaderValue (r->xtr);
387 const char *text = CHAR_CAST (const char *, value);
389 if ( r->row < r->start_row)
391 if ( gri->read_names )
393 var_spec [idx].name = xstrdup (text);
398 var_spec [idx].first_value = xmlStrdup (value);
400 if (-1 == var_spec [idx].width )
401 var_spec [idx].width = (gri->asw == -1) ?
402 ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : gri->asw;
407 else if ( r->node_type == XML_READER_TYPE_ELEMENT
408 && r->state == STATE_CELL)
410 if ( r->row == r->start_row )
413 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
415 if ( NULL == attr || 60 != _xmlchar_to_int (attr))
416 var_spec [idx].width = 0;
424 /* Create the dictionary and populate it */
425 *dict = r->dict = dict_create (
426 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
428 for (i = 0 ; i < n_var_specs ; ++i )
432 /* Probably no data exists for this variable, so allocate a
434 if ( var_spec[i].width == -1 )
435 var_spec[i].width = SPREADSHEET_DEFAULT_WIDTH;
437 name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
438 dict_create_var (r->dict, name, var_spec[i].width);
442 /* Create the first case, and cache it */
443 r->used_first_case = false;
445 if ( n_var_specs == 0 )
447 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
452 r->proto = caseproto_ref (dict_get_proto (r->dict));
453 r->first_case = case_create (r->proto);
454 case_set_missing (r->first_case);
456 for ( i = 0 ; i < n_var_specs ; ++i )
458 const struct variable *var = dict_get_var (r->dict, i);
460 convert_xml_string_to_value (r->first_case, var,
461 var_spec[i].first_value);
464 for ( i = 0 ; i < n_var_specs ; ++i )
466 free (var_spec[i].first_value);
467 free (var_spec[i].name);
472 return casereader_create_sequential
476 &gnm_file_casereader_class, r);
480 for ( i = 0 ; i < n_var_specs ; ++i )
482 free (var_spec[i].first_value);
483 free (var_spec[i].name);
487 dict_destroy (*dict);
489 gnm_file_casereader_destroy (NULL, r);
495 /* Reads and returns one case from READER's file. Returns a null
496 pointer on failure. */
497 static struct ccase *
498 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
503 struct gnumeric_reader *r = r_;
504 int current_row = r->row;
506 if ( !r->used_first_case )
508 r->used_first_case = true;
509 return r->first_case;
512 c = case_create (r->proto);
513 case_set_missing (c);
515 while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
516 && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
520 if ( r->col < r->start_col || (r->stop_col != -1 &&
521 r->col > r->stop_col))
524 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
527 if ( r->stop_row != -1 && r->row > r->stop_row)
530 if ( r->node_type == XML_READER_TYPE_TEXT )
532 xmlChar *value = xmlTextReaderValue (r->xtr);
534 const int idx = r->col - r->start_col;
536 const struct variable *var = dict_get_var (r->dict, idx);
538 convert_xml_string_to_value (c, var, value);
555 #endif /* GNM_SUPPORT */