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