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