Make sure that ODS reading works even when not in C locale
[pspp] / src / data / ods-reader.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2011, 2012, 2013 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "libpspp/message.h"
20 #include "libpspp/misc.h"
21 #include "libpspp/assertion.h"
22
23 #include "data/data-in.h"
24
25 #include "gl/c-strtod.h"
26 #include "gl/minmax.h"
27
28 #include "gettext.h"
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) (msgid)
31
32 #include "ods-reader.h"
33 #include "spreadsheet-reader.h"
34
35 #if !ODF_READ_SUPPORT
36
37 struct casereader *
38 ods_open_reader (const struct spreadsheet_read_options *opts, 
39                  struct dictionary **dict)
40 {
41   msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "OpenDocument");
42
43   return NULL;
44 }
45
46 #else
47
48 #include "libpspp/zip-reader.h"
49
50
51 #include <assert.h>
52 #include <stdbool.h>
53 #include <errno.h>
54 #include <libxml/xmlreader.h>
55 #include <zlib.h>
56
57 #include "data/format.h"
58 #include "data/case.h"
59 #include "data/casereader-provider.h"
60 #include "data/dictionary.h"
61 #include "data/identifier.h"
62 #include "data/value.h"
63 #include "data/variable.h"
64 #include "libpspp/i18n.h"
65 #include "libpspp/str.h"
66
67 #include "gl/xalloc.h"
68
69 static void ods_file_casereader_destroy (struct casereader *, void *);
70
71 static struct ccase *ods_file_casereader_read (struct casereader *, void *);
72
73 static const struct casereader_class ods_file_casereader_class =
74   {
75     ods_file_casereader_read,
76     ods_file_casereader_destroy,
77     NULL,
78     NULL,
79   };
80
81 struct sheet_detail
82 {
83   /* The name of the sheet (utf8 encoding) */
84   char *name;
85
86   int start_col;
87   int stop_col;
88   int start_row;
89   int stop_row;
90 };
91
92
93 enum reader_state
94   {
95     STATE_INIT = 0,        /* Initial state */
96     STATE_SPREADSHEET,     /* Found the start of the spreadsheet doc */
97     STATE_TABLE,           /* Found the sheet that we actually want */
98     STATE_ROW,             /* Found the start of the cell array */
99     STATE_CELL,            /* Found a cell */
100     STATE_CELL_CONTENT     /* Found a the text within a cell */
101   };
102
103 struct ods_reader
104 {
105   struct spreadsheet spreadsheet;
106   struct zip_reader *zreader;
107   xmlTextReaderPtr xtr;
108
109   enum reader_state state;
110   int row;
111   int col;
112   int node_type;
113   int current_sheet;
114   xmlChar *current_sheet_name;
115
116   const xmlChar *target_sheet_name;
117   int target_sheet_index;
118
119
120   int wanted_row_start;
121   int wanted_col_start;
122
123 #if 0
124   int start_row;
125   int start_col;
126   int stop_row;
127   int stop_col;
128 #endif
129
130   int col_span;
131
132   struct sheet_detail *sheets;
133   int n_allocated_sheets;
134
135   struct caseproto *proto;
136   struct dictionary *dict;
137   struct ccase *first_case;
138   bool used_first_case;
139   bool read_names;
140
141   struct string ods_errs;
142 };
143
144
145 static bool
146 reading_target_sheet (const struct ods_reader *r)
147 {
148   if (r->target_sheet_name != NULL)
149     {
150       if ( 0 == xmlStrcmp (r->target_sheet_name, r->current_sheet_name))
151         return true;
152     }
153   
154   if (r->target_sheet_index == r->current_sheet + 1)
155     return true;
156
157   return false;
158 }
159
160
161 static void process_node (struct ods_reader *r);
162
163
164 const char *
165 ods_get_sheet_name (struct spreadsheet *s, int n)
166 {
167   int ret;
168   struct ods_reader *or = (struct ods_reader *) s;
169   
170   assert (n < s->n_sheets);
171
172   while ( 
173          (or->n_allocated_sheets <= n)
174          && 
175          (1 == (ret = xmlTextReaderRead (or->xtr)))
176           )
177     {
178       process_node (or);
179     }
180
181   return or->sheets[n].name;
182 }
183
184 char *
185 ods_get_sheet_range (struct spreadsheet *s, int n)
186 {
187   int ret = -1;
188   struct ods_reader *or = (struct ods_reader *) s;
189   
190   assert (n < s->n_sheets);
191
192   while ( 
193          (
194           (or->n_allocated_sheets <= n)
195           || (or->sheets[n].stop_row == -1) )
196          && 
197          (1 == (ret = xmlTextReaderRead (or->xtr)))
198           )
199     {
200       process_node (or);
201     }
202
203
204   return create_cell_ref (
205                           or->sheets[n].start_col,
206                           or->sheets[n].start_row,
207                           or->sheets[n].stop_col,
208                           or->sheets[n].stop_row);
209 }
210
211
212 static void
213 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
214 {
215   struct ods_reader *r = r_;
216   if ( r == NULL)
217     return ;
218
219   if (r->xtr)
220     xmlFreeTextReader (r->xtr);
221   r->xtr = NULL;
222
223   if ( ! ds_is_empty (&r->ods_errs))
224     msg (ME, "%s", ds_cstr (&r->ods_errs));
225
226   ds_destroy (&r->ods_errs);
227
228   if ( ! r->used_first_case )
229     case_unref (r->first_case);
230
231   caseproto_unref (r->proto);
232
233   //  free (r);
234 }
235
236 static void
237 process_node (struct ods_reader *r)
238 {
239   xmlChar *name = xmlTextReaderName (r->xtr);
240   if (name == NULL)
241     name = xmlStrdup (_xml ("--"));
242
243
244   r->node_type = xmlTextReaderNodeType (r->xtr);
245
246   switch (r->state)
247     {
248     case STATE_INIT:
249       if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
250           XML_READER_TYPE_ELEMENT  == r->node_type)
251         {
252           r->state = STATE_SPREADSHEET;
253           r->current_sheet = -1;
254           r->current_sheet_name = NULL;
255         }
256       break;
257     case STATE_SPREADSHEET:
258       if (0 == xmlStrcasecmp (name, _xml("table:table"))
259           && 
260           (XML_READER_TYPE_ELEMENT == r->node_type))
261         {
262           xmlFree (r->current_sheet_name);
263           r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
264
265           ++r->current_sheet;
266
267           if (r->current_sheet >= r->n_allocated_sheets)
268             {
269               r->sheets = xrealloc (r->sheets, sizeof (*r->sheets) * ++r->n_allocated_sheets);
270               r->sheets[r->n_allocated_sheets - 1].start_col = -1;
271               r->sheets[r->n_allocated_sheets - 1].stop_col = -1;
272               r->sheets[r->n_allocated_sheets - 1].start_row = -1;
273               r->sheets[r->n_allocated_sheets - 1].stop_row = -1;
274               r->sheets[r->n_allocated_sheets - 1].name = xmlStrdup (r->current_sheet_name);
275             }
276
277           r->col = 0;
278           r->row = 0;
279
280           r->state = STATE_TABLE;
281         }
282       else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
283                XML_READER_TYPE_ELEMENT  == r->node_type)
284         {
285           r->state = STATE_INIT;
286         }
287       break;
288     case STATE_TABLE:
289       if (0 == xmlStrcasecmp (name, _xml("table:table-row")) && 
290           (XML_READER_TYPE_ELEMENT  == r->node_type))
291         {
292           xmlChar *value =
293             xmlTextReaderGetAttribute (r->xtr,
294                                        _xml ("table:number-rows-repeated"));
295           
296           int row_span = value ? _xmlchar_to_int (value) : 1;
297
298           r->row += row_span;
299           r->col = 0;
300           
301           if (! xmlTextReaderIsEmptyElement (r->xtr))
302             r->state = STATE_ROW;
303         }
304       else if (0 == xmlStrcasecmp (name, _xml("table:table")) && 
305                (XML_READER_TYPE_END_ELEMENT  == r->node_type))
306         {
307           r->state = STATE_SPREADSHEET;
308         }
309       break;
310     case STATE_ROW:
311       //      printf ("%s:%d Name is %s\n", __FILE__, __LINE__, name);
312       if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
313            && 
314            (XML_READER_TYPE_ELEMENT  == r->node_type))
315         {
316           xmlChar *value =
317             xmlTextReaderGetAttribute (r->xtr,
318                                        _xml ("table:number-columns-repeated"));
319           
320           r->col_span = value ? _xmlchar_to_int (value) : 1;
321           r->col += r->col_span;
322
323           //      printf ("%s:%d %s\n", __FILE__, __LINE__, value);
324
325           if (! xmlTextReaderIsEmptyElement (r->xtr))
326             r->state = STATE_CELL;
327         }
328       else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
329                 &&
330                 (XML_READER_TYPE_END_ELEMENT  == r->node_type))
331         {
332           r->state = STATE_TABLE;
333         }
334       break;
335     case STATE_CELL:
336       if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
337             &&
338            ( XML_READER_TYPE_ELEMENT  == r->node_type))
339         {
340           if (! xmlTextReaderIsEmptyElement (r->xtr))
341             r->state = STATE_CELL_CONTENT;
342         }
343       else if
344         ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
345           &&
346           (XML_READER_TYPE_END_ELEMENT  == r->node_type)
347           )
348         {
349           r->state = STATE_ROW;
350         }
351       break;
352     case STATE_CELL_CONTENT:
353       assert (r->current_sheet >= 0);
354       assert (r->current_sheet < r->n_allocated_sheets);
355
356       if (r->sheets[r->current_sheet].start_row == -1)
357         r->sheets[r->current_sheet].start_row = r->row - 1;
358
359       if ( 
360           (r->sheets[r->current_sheet].start_col == -1)
361           ||
362           (r->sheets[r->current_sheet].start_col >= r->col - 1)
363            )
364         r->sheets[r->current_sheet].start_col = r->col - 1;
365
366       r->sheets[r->current_sheet].stop_row = r->row - 1;
367
368       if ( r->sheets[r->current_sheet].stop_col <  r->col - 1)
369         r->sheets[r->current_sheet].stop_col = r->col - 1;
370
371       if (XML_READER_TYPE_END_ELEMENT  == r->node_type)
372         r->state = STATE_CELL;
373       break;
374     default:
375       NOT_REACHED ();
376       break;
377     };
378
379   xmlFree (name);
380 }
381
382 /* 
383    A struct containing the parameters of a cell's value 
384    parsed from the xml
385 */
386 struct xml_value
387 {
388   xmlChar *type;
389   xmlChar *value;
390   xmlChar *text;
391 };
392
393 struct var_spec
394 {
395   char *name;
396   struct xml_value firstval;
397 };
398
399
400 /* Determine the width that a xmv should probably have */
401 static int
402 xmv_to_width (const struct xml_value *xmv, int fallback)
403 {
404   int width = SPREADSHEET_DEFAULT_WIDTH;
405
406   /* Non-strings always have zero width */
407   if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
408     return 0;
409
410   if ( fallback != -1)
411     return fallback;
412
413   if ( xmv->value )
414     width = ROUND_UP (xmlStrlen (xmv->value),
415                       SPREADSHEET_DEFAULT_WIDTH);
416   else if ( xmv->text)
417     width = ROUND_UP (xmlStrlen (xmv->text),
418                       SPREADSHEET_DEFAULT_WIDTH);
419
420   return width;
421 }
422
423 /*
424    Sets the VAR of case C, to the value corresponding to the xml data
425  */
426 static void
427 convert_xml_to_value (struct ccase *c, const struct variable *var,
428                       const struct xml_value *xmv)
429 {
430   union value *v = case_data_rw (c, var);
431
432   if (xmv->value == NULL && xmv->text == NULL)
433     value_set_missing (v, var_get_width (var));
434   else if ( var_is_alpha (var))
435     /* Use the text field, because it seems that there is no
436        value field for strings */
437     value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
438   else
439     {
440       const struct fmt_spec *fmt = var_get_write_format (var);
441       enum fmt_category fc  = fmt_get_category (fmt->type);
442
443       assert ( fc != FMT_CAT_STRING);
444
445       if ( 0 == xmlStrcmp (xmv->type, _xml("float")))
446         {
447           v->f = c_strtod (xmv->value, NULL);
448         }
449       else
450         {
451           const char *text = xmv->value ?
452             CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
453
454
455           free (data_in (ss_cstr (text), "UTF-8",
456                          fmt->type,
457                          v,
458                          var_get_width (var),
459                          "UTF-8"));
460         }
461     }
462 }
463
464
465 /* Try to find out how many sheets there are in the "workbook" */
466 static int
467 get_sheet_count (struct zip_reader *zreader)
468 {
469   xmlTextReaderPtr mxtr;
470   struct zip_member *meta = NULL;
471   meta = zip_member_open (zreader, "meta.xml");
472
473   if ( meta == NULL)
474     return -1;
475
476   mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
477                          (xmlInputCloseCallback) zip_member_finish,
478                          meta,   NULL, NULL, 0);
479
480   while (1 == xmlTextReaderRead (mxtr))
481     {
482       xmlChar *name = xmlTextReaderName (mxtr);
483       if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
484         {
485           xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
486             
487           if ( attr != NULL)
488             {
489               int s = _xmlchar_to_int (attr);
490               return s;
491             }
492         }
493     }
494   return -1;
495 }
496
497 static void
498 ods_error_handler (void *ctx, const char *mesg,
499                         UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
500 {
501   struct ods_reader *r = ctx;
502        
503   msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
504        "ODF",
505        r->spreadsheet.file_name,
506        xmlTextReaderLocatorLineNumber (loc),
507        mesg);
508 }
509
510
511 static bool
512 init_reader (struct ods_reader *r, bool report_errors)
513 {
514   struct zip_member *content = zip_member_open (r->zreader, "content.xml");
515   xmlTextReaderPtr xtr;
516
517   if ( content == NULL)
518     return false;
519
520   zip_member_ref (content);
521
522
523   xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
524                         (xmlInputCloseCallback) zip_member_finish,
525                         content,   NULL, NULL,
526                         report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
527
528   if ( xtr == NULL)
529     return false;
530
531   r->xtr = xtr;
532   r->spreadsheet.type = SPREADSHEET_ODS;
533
534   if (report_errors) 
535     xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
536
537   return true;
538 }
539
540
541 struct spreadsheet *
542 ods_probe (const char *filename, bool report_errors)
543 {
544   struct ods_reader *r;
545   struct string errs = DS_EMPTY_INITIALIZER;
546   int sheet_count;
547   struct zip_reader *zr = zip_reader_create (filename, &errs);
548
549   if (zr == NULL)
550     {
551       if (report_errors)
552         {
553           msg (ME, _("Cannot open %s as a OpenDocument file: %s"),
554                filename, ds_cstr (&errs));
555         }
556       return NULL;
557     }
558
559   sheet_count = get_sheet_count (zr);
560
561   r = xzalloc (sizeof *r);
562   r->zreader = zr;
563
564   if (! init_reader (r, report_errors))
565     {
566       goto error;
567     }
568
569   r->spreadsheet.n_sheets = sheet_count;
570   r->n_allocated_sheets = 0;
571   r->sheets = NULL;
572
573   ds_destroy (&errs);
574
575   r->spreadsheet.file_name = filename;
576   return &r->spreadsheet;
577
578  error:
579   zip_reader_destroy (r->zreader);
580   ds_destroy (&errs);
581   free (r);
582   return NULL;
583 }
584
585 struct casereader *
586 ods_make_reader (struct spreadsheet *spreadsheet, 
587                  const struct spreadsheet_read_options *opts)
588 {
589   intf ret = 0;
590   xmlChar *type = NULL;
591   unsigned long int vstart = 0;
592   casenumber n_cases = CASENUMBER_MAX;
593   int i;
594   struct var_spec *var_spec = NULL;
595   int n_var_specs = 0;
596
597   struct ods_reader *r = (struct ods_reader *) spreadsheet;
598   xmlChar *val_string = NULL;
599
600   assert (r);
601   r->read_names = opts->read_names;
602   ds_init_empty (&r->ods_errs);
603
604
605   if ( !init_reader (r, true))
606     goto error;
607
608 #if 0
609   if ( opts->cell_range )
610     {
611       if ( ! convert_cell_ref (opts->cell_range,
612                                &r->start_col, &r->start_row,
613                                &r->stop_col, &r->stop_row))
614         {
615           msg (SE, _("Invalid cell range `%s'"),
616                opts->cell_range);
617           goto error;
618         }
619     }
620   else
621     {
622       r->start_col = 0;
623       r->start_row = 0;
624       r->stop_col = -1;
625       r->stop_row = -1;
626     }
627 #endif
628
629   r->state = STATE_INIT;
630   r->target_sheet_name = BAD_CAST opts->sheet_name;
631   r->target_sheet_index = opts->sheet_index;
632   r->row = r->col = 0;
633
634 #if 0
635   /* If CELLRANGE was given, then we know how many variables should be read */
636   if ( r->stop_col != -1 )
637     {
638       assert (var_spec == NULL);
639       n_var_specs =  r->stop_col - r->start_col + 1;
640       var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
641       memset (var_spec, '\0', sizeof (*var_spec) * n_var_specs);
642     }
643 #endif
644
645   /* Advance to the start of the cells for the target sheet */
646   while ( ! reading_target_sheet (r)  || r->state != STATE_ROW  )
647     {
648       if (1 != (ret = xmlTextReaderRead (r->xtr)))
649            break;
650
651       process_node (r);
652     }
653
654
655   if (ret < 1)
656     {
657       msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
658            spreadsheet->file_name);
659       goto error;
660     }
661
662   if ( opts->read_names)
663     {
664       while (1 == (ret = xmlTextReaderRead (r->xtr)))
665         {
666           int idx;
667
668           process_node (r);
669
670           /* If the row is finished then stop for now */
671           if (r->state == STATE_TABLE && r->row > r->wanted_row_start)
672             break;
673
674           idx = r->col - r->wanted_col_start - 1;
675
676           if (r->state == STATE_CELL_CONTENT 
677               &&
678               XML_READER_TYPE_TEXT  == r->node_type)
679             {
680               xmlChar *value = xmlTextReaderValue (r->xtr);
681
682               if ( idx >= n_var_specs)
683                 {
684                   var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
685
686                   /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
687                   memset (var_spec + n_var_specs,
688                           0, 
689                           (n_var_specs - idx + 1) * sizeof (*var_spec));
690                   n_var_specs = idx + 1;
691                 }
692               var_spec[idx].firstval.text = 0;
693               var_spec[idx].firstval.value = 0;
694               var_spec[idx].firstval.type = 0;
695
696               var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
697               xmlFree (value);
698             }
699         }
700     }
701
702   /* Read in the first row of data */
703   while (1 == xmlTextReaderRead (r->xtr))
704     {
705       int idx;
706       process_node (r);
707
708       /* If the row is finished then stop for now */
709       if (r->state == STATE_TABLE && r->row > r->wanted_row_start + (opts->read_names ? 1 : 0))
710         break;
711
712       idx = r->col - r->wanted_col_start - 1;
713
714       if ( r->state == STATE_CELL &&
715            XML_READER_TYPE_ELEMENT  == r->node_type)
716         {
717           type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
718           val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
719         }
720
721       if ( r->state == STATE_CELL_CONTENT &&
722            XML_READER_TYPE_TEXT  == r->node_type)
723         {
724           if ( idx >= n_var_specs) 
725             {
726               var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
727               var_spec [idx].name = NULL;
728               n_var_specs = idx + 1;
729             }
730             
731           var_spec [idx].firstval.type = type;
732           var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr);
733           var_spec [idx].firstval.value = val_string;
734
735           val_string = NULL;
736           type = NULL;
737         }
738     }
739
740
741   /* Create the dictionary and populate it */
742   r->spreadsheet.dict = r->dict = dict_create (
743     CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
744
745   for (i = 0; i < n_var_specs ; ++i )
746     {
747       struct fmt_spec fmt;
748       struct variable *var = NULL;
749       char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
750       int width  = xmv_to_width (&var_spec[i].firstval, opts->asw);
751       dict_create_var (r->dict, name, width);
752       free (name);
753
754       var = dict_get_var (r->dict, i);
755
756       if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
757         {
758           fmt.type = FMT_DATE;
759           fmt.d = 0;
760           fmt.w = 20;
761         }
762       else
763         fmt = fmt_default_for_width (width);
764
765       var_set_both_formats (var, &fmt);
766     }
767
768   /* Create the first case, and cache it */
769   r->used_first_case = false;
770
771   if ( n_var_specs ==  0 )
772     {
773       msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
774            spreadsheet->file_name);
775       goto error;
776     }
777
778   r->proto = caseproto_ref (dict_get_proto (r->dict));
779   r->first_case = case_create (r->proto);
780   case_set_missing (r->first_case);
781
782   for (i = 0 ; i < n_var_specs; ++i)
783     {
784       const struct variable *var = dict_get_var (r->dict, i);
785
786       convert_xml_to_value (r->first_case, var,  &var_spec[i].firstval);
787     }
788
789   /* Read in the first row of data */
790   while (1 == xmlTextReaderRead (r->xtr))
791     {
792       process_node (r);
793
794       if (r->state == STATE_ROW)
795         break;
796     }
797
798   //  zip_reader_destroy (zreader);
799
800 #if 0
801   for ( i = 0 ; i < n_var_specs ; ++i )
802     {
803       free (var_spec[i].firstval.type);
804       free (var_spec[i].firstval.value);
805       free (var_spec[i].firstval.text);
806       free (var_spec[i].name);
807     }
808
809   free (var_spec);
810 #endif
811
812   return casereader_create_sequential
813     (NULL,
814      r->proto,
815      n_cases,
816      &ods_file_casereader_class, r);
817
818  error:
819   
820   // zip_reader_destroy (zreader);
821
822   for ( i = 0 ; i < n_var_specs ; ++i )
823     {
824       free (var_spec[i].firstval.type);
825       free (var_spec[i].firstval.value);
826       free (var_spec[i].firstval.text);
827       free (var_spec[i].name);
828     }
829
830   free (var_spec);
831
832   dict_destroy (r->spreadsheet.dict);
833   r->spreadsheet.dict = NULL;
834   ods_file_casereader_destroy (NULL, r);
835
836
837   return NULL;
838 }
839
840
841 /* Reads and returns one case from READER's file.  Returns a null
842    pointer on failure. */
843 static struct ccase *
844 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
845 {
846   struct ccase *c = NULL;
847   xmlChar *val_string = NULL;
848   xmlChar *type = NULL;
849   struct ods_reader *r = r_;
850   int current_row = r->row;
851
852   if (!r->used_first_case)
853     {
854       r->used_first_case = true;
855       return r->first_case;
856     }
857
858
859   /* Advance to the start of a row. (If there is one) */
860   while (r->state != STATE_ROW && 1 == xmlTextReaderRead (r->xtr))
861     {
862       process_node (r);
863     }
864
865
866   if ( ! reading_target_sheet (r)  ||  r->state < STATE_TABLE)
867     {
868       return NULL;
869     }
870
871   c = case_create (r->proto);
872   case_set_missing (c);
873   
874   while (1 == xmlTextReaderRead (r->xtr))
875     {
876       process_node (r);
877 #if 0
878       if (r->row > current_row && r->state == STATE_ROW)
879         break;
880 #endif
881       //      printf ("%s:%d\n", __FILE__, __LINE__);
882       if (r->state == STATE_CELL &&
883            r->node_type == XML_READER_TYPE_ELEMENT)
884         {
885           type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
886           val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
887         }
888
889       if (r->state == STATE_CELL_CONTENT && 
890            r->node_type == XML_READER_TYPE_TEXT)
891         {
892           int col;
893           struct xml_value *xmv = xzalloc (sizeof *xmv);
894           xmv->text = xmlTextReaderValue (r->xtr);
895           xmv->value = val_string;       
896           xmv->type = type;
897           val_string = NULL;
898
899           for (col = 0; col < r->col_span; ++col)
900             {
901               const int idx = r->col + col - r->wanted_col_start - 1;
902               const struct variable *var = dict_get_var (r->dict, idx);
903               convert_xml_to_value (c, var, xmv);
904             }
905
906           free (xmv->text);
907           free (xmv->value);
908           free (xmv);
909         }
910       if ( r->state <= STATE_TABLE)
911         break;
912     }
913
914   return c;
915 }
916 #endif