1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2009, 2010, 2011, 2012 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"
23 #include "gl/c-strtod.h"
26 #define _(msgid) gettext (msgid)
27 #define N_(msgid) (msgid)
29 #include "spreadsheet-reader.h"
31 #include "c-xvasprintf.h"
36 gnumeric_open_reader (struct spreadsheet_read_info *gri, struct spreadsheet_read_options *opts, struct dictionary **dict)
38 msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "Gnumeric");
45 #include "data/gnumeric-reader.h"
50 #include <libxml/xmlreader.h>
53 #include "data/case.h"
54 #include "data/casereader-provider.h"
55 #include "data/dictionary.h"
56 #include "data/identifier.h"
57 #include "data/value.h"
58 #include "data/variable.h"
59 #include "libpspp/i18n.h"
60 #include "libpspp/str.h"
62 #include "gl/xalloc.h"
64 static void gnm_file_casereader_destroy (struct casereader *, void *);
66 static struct ccase *gnm_file_casereader_read (struct casereader *, void *);
68 static const struct casereader_class gnm_file_casereader_class =
70 gnm_file_casereader_read,
71 gnm_file_casereader_destroy,
78 STATE_INIT = 0, /* Initial state */
79 STATE_SHEET_START, /* Found the start of a sheet */
80 STATE_SHEET_NAME, /* Found the sheet name */
82 STATE_SHEET_FOUND, /* Found the sheet that we actually want */
83 STATE_CELLS_START, /* Found the start of the cell array */
84 STATE_CELL /* Found a cell */
88 struct gnumeric_reader
92 enum reader_state state;
100 const xmlChar *target_sheet;
101 int target_sheet_index;
108 struct caseproto *proto;
109 struct dictionary *dict;
110 struct ccase *first_case;
111 bool used_first_case;
114 static void process_node (struct gnumeric_reader *r);
118 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
120 struct gnumeric_reader *r = r_;
125 xmlFreeTextReader (r->xtr);
127 if ( ! r->used_first_case )
128 case_unref (r->first_case);
130 caseproto_unref (r->proto);
136 process_node (struct gnumeric_reader *r)
138 xmlChar *name = xmlTextReaderName (r->xtr);
140 name = xmlStrdup (_xml ("--"));
143 r->node_type = xmlTextReaderNodeType (r->xtr);
148 if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
149 XML_READER_TYPE_ELEMENT == r->node_type)
152 r->state = STATE_SHEET_START;
155 case STATE_SHEET_START:
156 if (0 == xmlStrcasecmp (name, _xml("gnm:Name")) &&
157 XML_READER_TYPE_ELEMENT == r->node_type)
159 r->state = STATE_SHEET_NAME;
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_INIT;
168 else if (XML_READER_TYPE_TEXT == r->node_type)
170 if ( r->target_sheet != NULL)
172 printf ("%s:%d\n", __FILE__, __LINE__);
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 printf ("%s:%d %d\n", __FILE__, __LINE__, r->sheet_index);
181 r->state = STATE_SHEET_FOUND;
185 case STATE_SHEET_FOUND:
186 if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
187 XML_READER_TYPE_ELEMENT == r->node_type)
189 r->min_col = INT_MAX;
190 if (! xmlTextReaderIsEmptyElement (r->xtr))
191 r->state = STATE_CELLS_START;
193 else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
194 XML_READER_TYPE_ELEMENT == r->node_type)
196 r->state = STATE_MAXROW;
198 else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
199 XML_READER_TYPE_END_ELEMENT == r->node_type)
201 r->state = STATE_INIT;
205 if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow")) &&
206 XML_READER_TYPE_END_ELEMENT == r->node_type)
208 r->state = STATE_SHEET_FOUND;
210 case STATE_CELLS_START:
211 if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell")) &&
212 XML_READER_TYPE_ELEMENT == r->node_type)
214 xmlChar *attr = NULL;
215 r->state = STATE_CELL;
217 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
218 r->col = _xmlchar_to_int (attr);
221 if (r->col < r->min_col)
224 attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
225 r->row = _xmlchar_to_int (attr);
228 else if (0 == xmlStrcasecmp (name, _xml("gnm:Cells")) &&
229 XML_READER_TYPE_END_ELEMENT == r->node_type)
230 r->state = STATE_SHEET_NAME;
234 if (0 == xmlStrcasecmp (name, _xml("gnm:Cell")) &&
235 XML_READER_TYPE_END_ELEMENT == r->node_type)
237 r->state = STATE_CELLS_START;
249 Sets the VAR of case C, to the value corresponding to the xml string XV
252 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
255 union value *v = case_data_rw (c, var);
258 value_set_missing (v, var_get_width (var));
259 else if ( var_is_alpha (var))
260 value_copy_str_rpad (v, var_get_width (var), xv, ' ');
263 const char *text = CHAR_CAST (const char *, xv);
267 v->f = c_strtod (text, &endptr);
268 if ( errno != 0 || endptr == text)
277 xmlChar *first_value;
281 gnumeric_open_reader (const struct spreadsheet_read_info *gri,
282 struct spreadsheet_read_options *opts,
283 struct dictionary **dict)
285 unsigned long int vstart = 0;
287 casenumber n_cases = CASENUMBER_MAX;
289 struct var_spec *var_spec = NULL;
292 struct gnumeric_reader *r = NULL;
294 gzFile gz = gzopen (gri->file_name, "r");
298 msg (ME, _("Error opening `%s' for reading as a Gnumeric file: %s."),
299 gri->file_name, strerror (errno));
304 r = xzalloc (sizeof *r);
306 r->xtr = xmlReaderForIO ((xmlInputReadCallback) gzread,
307 (xmlInputCloseCallback) gzclose, gz,
310 if ( r->xtr == NULL )
313 if ( opts->cell_range )
315 if ( ! convert_cell_ref (opts->cell_range,
316 &r->start_col, &r->start_row,
317 &r->stop_col, &r->stop_row))
319 msg (SE, _("Invalid cell range `%s'"),
332 r->state = STATE_INIT;
333 r->target_sheet = BAD_CAST opts->sheet_name;
334 r->target_sheet_index = opts->sheet_index;
335 r->row = r->col = -1;
338 /* Advance to the start of the cells for the target sheet */
339 while ( (r->state != STATE_CELL || r->row < r->start_row )
340 && 1 == (ret = xmlTextReaderRead (r->xtr)))
344 value = xmlTextReaderValue (r->xtr);
346 if ( r->state == STATE_MAXROW && r->node_type == XML_READER_TYPE_TEXT)
348 n_cases = 1 + _xmlchar_to_int (value) ;
354 /* If a range has been given, then use that to calculate the number
356 if ( opts->cell_range)
358 n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
361 if ( gri->read_names )
367 /* Read in the first row of cells,
368 including the headers if read_names was set */
370 (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
371 && (ret = xmlTextReaderRead (r->xtr))
377 if ( r->row > r->start_row ) break;
379 if ( r->col < r->start_col ||
380 (r->stop_col != -1 && r->col > r->stop_col))
383 idx = r->col - r->start_col;
385 if ( idx >= n_var_specs )
388 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
389 for (i = n_var_specs; i <= idx; ++i)
391 var_spec [i].name = NULL;
392 var_spec [i].width = -1;
393 var_spec [i].first_value = NULL;
395 n_var_specs = idx + 1 ;
398 if ( r->node_type == XML_READER_TYPE_TEXT )
400 xmlChar *value = xmlTextReaderValue (r->xtr);
401 const char *text = CHAR_CAST (const char *, value);
403 if ( r->row < r->start_row)
405 if ( gri->read_names )
407 var_spec [idx].name = xstrdup (text);
412 var_spec [idx].first_value = xmlStrdup (value);
414 if (-1 == var_spec [idx].width )
415 var_spec [idx].width = (gri->asw == -1) ?
416 ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : gri->asw;
421 else if ( r->node_type == XML_READER_TYPE_ELEMENT
422 && r->state == STATE_CELL)
424 if ( r->row == r->start_row )
427 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
429 if ( NULL == attr || 60 != _xmlchar_to_int (attr))
430 var_spec [idx].width = 0;
438 const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
441 /* Create the dictionary and populate it */
442 *dict = r->dict = dict_create (CHAR_CAST (const char *, enc));
445 for (i = 0 ; i < n_var_specs ; ++i )
449 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
452 /* Probably no data exists for this variable, so allocate a
454 if ( var_spec[i].width == -1 )
455 var_spec[i].width = SPREADSHEET_DEFAULT_WIDTH;
457 name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
458 dict_create_var (r->dict, name, var_spec[i].width);
462 /* Create the first case, and cache it */
463 r->used_first_case = false;
465 if ( n_var_specs == 0 )
467 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
472 r->proto = caseproto_ref (dict_get_proto (r->dict));
473 r->first_case = case_create (r->proto);
474 case_set_missing (r->first_case);
477 for ( i = 0 ; i < n_var_specs ; ++i )
479 if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
482 const struct variable *var = dict_get_var (r->dict, x++);
484 convert_xml_string_to_value (r->first_case, var,
485 var_spec[i].first_value);
488 for ( i = 0 ; i < n_var_specs ; ++i )
490 free (var_spec[i].first_value);
491 free (var_spec[i].name);
497 if (opts->cell_range == NULL)
499 opts->cell_range = c_xasprintf ("%c%d:%c%d",
502 r->stop_col + 'A' + caseproto_get_n_widths (r->proto),
503 r->start_row + n_cases);
506 return casereader_create_sequential
510 &gnm_file_casereader_class, r);
514 for ( i = 0 ; i < n_var_specs ; ++i )
516 free (var_spec[i].first_value);
517 free (var_spec[i].name);
521 dict_destroy (*dict);
524 gnm_file_casereader_destroy (NULL, r);
530 /* Reads and returns one case from READER's file. Returns a null
531 pointer on failure. */
532 static struct ccase *
533 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
538 struct gnumeric_reader *r = r_;
539 int current_row = r->row;
541 if ( !r->used_first_case )
543 r->used_first_case = true;
544 return r->first_case;
547 c = case_create (r->proto);
548 case_set_missing (c);
550 if (r->start_col == -1)
551 r->start_col = r->min_col;
553 while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
554 && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
558 if ( r->col < r->start_col || (r->stop_col != -1 &&
559 r->col > r->stop_col))
562 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
565 if ( r->stop_row != -1 && r->row > r->stop_row)
568 if ( r->node_type == XML_READER_TYPE_TEXT )
570 xmlChar *value = xmlTextReaderValue (r->xtr);
572 const int idx = r->col - r->start_col;
574 const struct variable *var = dict_get_var (r->dict, idx);
576 convert_xml_string_to_value (c, var, value);
593 #endif /* GNM_SUPPORT */