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