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