Make the gnumeric reader behave the same as the odsreader
[pspp] / src / data / gnumeric-reader.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2009, 2010, 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
22 #include "gl/minmax.h"
23 #include "gl/c-strtod.h"
24
25 #include "gettext.h"
26 #define _(msgid) gettext (msgid)
27 #define N_(msgid) (msgid)
28
29 #include "spreadsheet-reader.h"
30
31 #if !GNM_SUPPORT
32
33 struct casereader *
34 gnumeric_open_reader (const struct spreadsheet_read_options *opts, struct dictionary **dict)
35 {
36   msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "Gnumeric");
37
38   return NULL;
39 }
40
41 #else
42
43 #include "data/gnumeric-reader.h"
44
45 #include <assert.h>
46 #include <stdbool.h>
47 #include <errno.h>
48 #include <libxml/xmlreader.h>
49 #include <zlib.h>
50
51 #include "data/case.h"
52 #include "data/casereader-provider.h"
53 #include "data/dictionary.h"
54 #include "data/identifier.h"
55 #include "data/value.h"
56 #include "data/variable.h"
57 #include "libpspp/i18n.h"
58 #include "libpspp/str.h"
59
60 #include "gl/xalloc.h"
61
62 static void gnm_file_casereader_destroy (struct casereader *, void *);
63
64 static struct ccase *gnm_file_casereader_read (struct casereader *, void *);
65
66
67 static const struct casereader_class gnm_file_casereader_class =
68   {
69     gnm_file_casereader_read,
70     gnm_file_casereader_destroy,
71     NULL,
72     NULL,
73   };
74
75 enum reader_state
76   {
77     STATE_PRE_INIT = 0,        /* Initial state */
78     STATE_SHEET_COUNT,      /* Found the sheet index */
79     STATE_INIT ,           /* Other Initial state */
80     STATE_SHEET_START,     /* Found the start of a sheet */
81     STATE_SHEET_NAME,      /* Found the sheet name */
82     STATE_MAXROW,
83     STATE_MAXCOL,
84     STATE_SHEET_FOUND,     /* Found the sheet that we actually want */
85     STATE_CELLS_START,     /* Found the start of the cell array */
86     STATE_CELL             /* Found a cell */
87   };
88
89 struct sheet_detail
90 {
91   /* The name of the sheet (utf8 encoding) */
92   char *name;
93
94   int start_col;
95   int stop_col;
96   int start_row;
97   int stop_row;
98
99   int maxcol;
100   int maxrow;
101 };
102
103
104 struct gnumeric_reader
105 {
106   struct spreadsheet spreadsheet;
107   int ref_cnt;
108
109   /* The libxml reader for this instance */
110   xmlTextReaderPtr xtr;
111
112   /* An internal state variable */
113   enum reader_state state;
114
115   int row;
116   int col;
117   int min_col;
118   int node_type;
119   int current_sheet;
120
121   int start_col;
122   int stop_col;
123   int start_row;
124   int stop_row;
125   
126   struct sheet_detail *sheets;
127
128   const xmlChar *target_sheet;
129   int target_sheet_index;
130
131   struct caseproto *proto;
132   struct dictionary *dict;
133   struct ccase *first_case;
134   bool used_first_case;
135 };
136
137
138 void
139 gnumeric_destroy (struct spreadsheet *s)
140 {
141   struct gnumeric_reader *r = (struct gnumeric_reader *) s;
142
143
144 #if 0
145   if (0 == --r->ref_cnt)
146     {
147       int i;
148
149       for (i = 0; i < s->n_sheets; ++i)
150         {
151           xmlFree (r->sheets[i].name);
152         }
153     
154       free (r->sheets);
155
156       free (r);
157     }
158 #endif
159 }
160
161
162 const char *
163 gnumeric_get_sheet_name (struct spreadsheet *s, int n)
164 {
165   struct gnumeric_reader *gr = (struct gnumeric_reader *) s;
166   assert (n < s->n_sheets);
167
168   return gr->sheets[n].name; 
169 }
170
171
172 static void process_node (struct gnumeric_reader *r);
173
174
175
176 char *
177 gnumeric_get_sheet_range (struct spreadsheet *s, int n)
178 {
179   int ret;
180   struct gnumeric_reader *gr = (struct gnumeric_reader *) s;
181   
182   assert (n < s->n_sheets);
183
184   while ( 
185          (gr->sheets[n].stop_col == -1)
186          && 
187          (1 == (ret = xmlTextReaderRead (gr->xtr)))
188           )
189     {
190       process_node (gr);
191     }
192
193   return create_cell_ref (
194                           gr->sheets[n].start_col,
195                           gr->sheets[n].start_row,
196                           gr->sheets[n].stop_col,
197                           gr->sheets[n].stop_row);
198 }
199
200
201 static void
202 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
203 {
204   struct gnumeric_reader *r = r_;
205
206   if ( r == NULL)
207         return ;
208
209 #if 0
210   if ( r->xtr)
211     xmlFreeTextReader (r->xtr);
212   r->xtr = NULL;
213
214   if ( ! r->used_first_case )
215     case_unref (r->first_case);
216
217   caseproto_unref (r->proto);
218
219   gnumeric_destroy (&r->spreadsheet);
220 #endif
221 }
222
223
224 static void
225 process_node (struct gnumeric_reader *r)
226 {
227   xmlChar *name = xmlTextReaderName (r->xtr);
228   if (name == NULL)
229     name = xmlStrdup (_xml ("--"));
230
231   r->node_type = xmlTextReaderNodeType (r->xtr);
232
233   switch (r->state)
234     {
235     case STATE_PRE_INIT:
236       r->current_sheet = -1;
237       if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
238           XML_READER_TYPE_ELEMENT  == r->node_type)
239         {
240           r->state = STATE_SHEET_COUNT;
241         }
242       break;
243
244     case STATE_SHEET_COUNT:
245       if (0 == xmlStrcasecmp (name, _xml("gnm:SheetName")) &&
246           XML_READER_TYPE_ELEMENT  == r->node_type)
247         {
248           ++r->current_sheet;
249           if (r->current_sheet + 1 > r->spreadsheet.n_sheets)
250             {
251               struct sheet_detail *sd ;
252               r->sheets = xrealloc (r->sheets, (r->current_sheet + 1) * sizeof *r->sheets);
253               sd = &r->sheets[r->current_sheet];
254               sd->start_col = sd->stop_col = sd->start_row = sd->stop_row = -1;
255               r->spreadsheet.n_sheets = r->current_sheet + 1;
256             }
257         }
258       else if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
259           XML_READER_TYPE_END_ELEMENT  == r->node_type)
260         {
261           r->state = STATE_INIT;
262           r->current_sheet = -1;
263         }
264       else if (XML_READER_TYPE_TEXT == r->node_type)
265         {
266           r->sheets [r->spreadsheet.n_sheets - 1].name = CHAR_CAST (char *, xmlTextReaderValue (r->xtr));
267         }
268       break;
269
270     case STATE_INIT:
271       if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
272           XML_READER_TYPE_ELEMENT  == r->node_type)
273         {
274           ++r->current_sheet;
275           r->state = STATE_SHEET_START;
276         }
277       break;
278     case STATE_SHEET_START:
279       if (0 == xmlStrcasecmp (name, _xml("gnm:Name"))  &&
280           XML_READER_TYPE_ELEMENT  == r->node_type)
281         {
282           r->state = STATE_SHEET_NAME;
283         }
284       break;
285     case STATE_SHEET_NAME:
286       if (0 == xmlStrcasecmp (name, _xml("gnm:Name"))  &&
287           XML_READER_TYPE_END_ELEMENT  == r->node_type)
288         {
289           r->state = STATE_INIT;
290         }
291       else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet"))  &&
292           XML_READER_TYPE_END_ELEMENT  == r->node_type)
293         {
294           r->state = STATE_INIT;
295         }
296       else if (XML_READER_TYPE_TEXT == r->node_type)
297         {
298           if ( r->target_sheet != NULL)
299             {
300               xmlChar *value = xmlTextReaderValue (r->xtr);
301               if ( 0 == xmlStrcmp (value, r->target_sheet))
302                 r->state = STATE_SHEET_FOUND;
303               free (value);
304             }
305           else if (r->target_sheet_index == r->current_sheet + 1)
306             {
307               r->state = STATE_SHEET_FOUND;
308             }
309           else if (r->target_sheet_index == -1)
310             {
311               r->state = STATE_SHEET_FOUND;
312             }
313         }
314       break;
315     case STATE_SHEET_FOUND:
316       if (0 == xmlStrcasecmp (name, _xml("gnm:Cells"))  &&
317           XML_READER_TYPE_ELEMENT  == r->node_type)
318         {
319           r->min_col = INT_MAX;
320           if (! xmlTextReaderIsEmptyElement (r->xtr))
321             r->state = STATE_CELLS_START;
322         }
323       else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow"))  &&
324           XML_READER_TYPE_ELEMENT  == r->node_type)
325         {
326           r->state = STATE_MAXROW;
327         }
328       else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol"))  &&
329           XML_READER_TYPE_ELEMENT  == r->node_type)
330         {
331           r->state = STATE_MAXCOL;
332         }
333       else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet"))  &&
334           XML_READER_TYPE_END_ELEMENT  == r->node_type)
335         {
336           r->state = STATE_INIT;
337         }
338       break;
339     case STATE_MAXROW:
340       if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow"))  &&
341           XML_READER_TYPE_END_ELEMENT  == r->node_type)
342         {
343           r->state = STATE_SHEET_FOUND;
344         }
345       else if (r->node_type == XML_READER_TYPE_TEXT)
346         {
347           xmlChar *value = xmlTextReaderValue (r->xtr);
348           r->sheets[r->current_sheet].maxrow = _xmlchar_to_int (value);
349           xmlFree (value);
350         }
351       break;
352     case STATE_MAXCOL:
353       if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol"))  &&
354           XML_READER_TYPE_END_ELEMENT  == r->node_type)
355         {
356           r->state = STATE_SHEET_FOUND;
357         }
358       else if (r->node_type == XML_READER_TYPE_TEXT)
359         {
360           xmlChar *value = xmlTextReaderValue (r->xtr);
361           r->sheets[r->current_sheet].maxcol = _xmlchar_to_int (value);
362           xmlFree (value);
363         }
364       break;
365     case STATE_CELLS_START:
366       if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell"))  &&
367           XML_READER_TYPE_ELEMENT  == r->node_type)
368         {
369           xmlChar *attr = NULL;
370
371           attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
372           r->col =  _xmlchar_to_int (attr);
373           free (attr);
374
375           if (r->col < r->min_col)
376             r->min_col = r->col;
377
378           attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
379           r->row = _xmlchar_to_int (attr);
380           free (attr);
381
382           if (r->sheets[r->current_sheet].start_row == -1)
383             {
384               r->sheets[r->current_sheet].start_row = r->row;
385             }
386
387           if (r->sheets[r->current_sheet].start_col == -1)
388             {
389               r->sheets[r->current_sheet].start_col = r->col;
390             }
391           if (! xmlTextReaderIsEmptyElement (r->xtr))
392             r->state = STATE_CELL;
393         }
394       else if ( (0 == xmlStrcasecmp (name, _xml("gnm:Cells")))  &&  (XML_READER_TYPE_END_ELEMENT  == r->node_type) )
395         {
396           r->sheets[r->current_sheet].stop_col = r->col;
397           r->sheets[r->current_sheet].stop_row = r->row;
398           r->state = STATE_SHEET_NAME;
399         }
400       break;
401     case STATE_CELL:
402       if (0 == xmlStrcasecmp (name, _xml("gnm:Cell"))  && XML_READER_TYPE_END_ELEMENT  == r->node_type)
403         {
404           r->state = STATE_CELLS_START;
405         }
406       break;
407     default:
408       break;
409     };
410
411   xmlFree (name);
412 }
413
414
415 /*
416    Sets the VAR of case C, to the value corresponding to the xml string XV
417  */
418 static void
419 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
420                              const xmlChar *xv)
421 {
422   union value *v = case_data_rw (c, var);
423
424   if (xv == NULL)
425     value_set_missing (v, var_get_width (var));
426   else if ( var_is_alpha (var))
427     value_copy_str_rpad (v, var_get_width (var), xv, ' ');
428   else
429     {
430       const char *text = CHAR_CAST (const char *, xv);
431       char *endptr;
432
433       errno = 0;
434       v->f = c_strtod (text, &endptr);
435       if ( errno != 0 || endptr == text)
436         v->f = SYSMIS;
437     }
438 }
439
440 struct var_spec
441 {
442   char *name;
443   int width;
444   xmlChar *first_value;
445 };
446
447
448 static void
449 gnumeric_error_handler (void *ctx, const char *mesg,
450                         UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
451 {
452   struct gnumeric_reader *r = ctx;
453        
454   msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
455        "Gnumeric",
456        r->spreadsheet.file_name,
457        xmlTextReaderLocatorLineNumber (loc),
458        mesg);
459 }
460
461 static struct gnumeric_reader *
462 gnumeric_reopen (struct gnumeric_reader *r, const char *filename, bool show_errors)
463 {  
464   int ret;
465
466   xmlTextReaderPtr xtr;
467   gzFile gz;
468
469   assert (r == NULL || filename == NULL);
470
471   if (r && r->xtr)
472     xmlFreeTextReader (r->xtr);
473
474   if (filename)
475     gz = gzopen (filename, "r");
476   else
477     gz = gzopen ( r->spreadsheet.file_name, "r");
478
479   if (NULL == gz)
480     return NULL;
481
482
483   xtr = xmlReaderForIO ((xmlInputReadCallback) gzread,
484                         (xmlInputCloseCallback) gzclose, gz,
485                         NULL, NULL,
486                         show_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
487
488   if (xtr == NULL)
489     {
490       gzclose (gz);
491       return NULL;
492     }
493
494   if (r == NULL)
495     {
496       r = xzalloc (sizeof *r);
497       r->spreadsheet.n_sheets = -1;
498       r->spreadsheet.file_name = filename;
499     }
500   
501   if (show_errors) 
502     xmlTextReaderSetErrorHandler (xtr, gnumeric_error_handler, r);
503
504   r->target_sheet = NULL;
505   r->target_sheet_index = -1;
506
507   r->row = r->col = -1;
508   r->state = STATE_PRE_INIT;
509   r->xtr = xtr;
510   r->ref_cnt++;
511
512   /* Advance to the start of the workbook.
513      This gives us some confidence that we are actually dealing with a gnumeric
514      spreadsheet.
515    */
516   while ( (r->state != STATE_INIT )
517           && 1 == (ret = xmlTextReaderRead (r->xtr)))
518     {
519       process_node (r);
520     }
521
522
523   if ( ret != 1)
524     {
525       /* Does not seem to be a gnumeric file */
526       xmlFreeTextReader (r->xtr);
527       free (r);
528       return NULL;
529     }
530
531   r->spreadsheet.type = SPREADSHEET_GNUMERIC;
532
533   if (show_errors)
534     {
535       const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
536       xmlCharEncoding xce = xmlParseCharEncoding (CHAR_CAST (const char *, enc));
537
538       if ( XML_CHAR_ENCODING_UTF8 != xce)
539         {
540           /* I have been told that ALL gnumeric files are UTF8 encoded.  If that is correct, this 
541              can never happen. */
542           msg (MW, _("The gnumeric file `%s' is encoded as %s instead of the usual UTF-8 encoding. "
543                      "Any non-ascii characters will be incorrectly imported."),
544                r->spreadsheet.file_name,
545                enc);
546         }
547     }
548
549   return r;
550 }
551
552
553 struct spreadsheet *
554 gnumeric_probe (const char *filename, bool report_errors)
555 {
556   struct gnumeric_reader *r = gnumeric_reopen (NULL, filename, report_errors);
557
558   return &r->spreadsheet;
559 }
560
561
562 struct casereader *
563 gnumeric_make_reader (struct spreadsheet *spreadsheet,
564                       const struct spreadsheet_read_options *opts)
565 {
566   int x = 0;
567   struct gnumeric_reader *r = NULL;
568   unsigned long int vstart = 0;
569   int ret;
570   casenumber n_cases = CASENUMBER_MAX;
571   int i;
572   struct var_spec *var_spec = NULL;
573   int n_var_specs = 0;
574
575   r = (struct gnumeric_reader *) (spreadsheet);
576
577   if (r->row != -1)
578     r = gnumeric_reopen (r, NULL, true);
579
580
581
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 = -1;
596       r->start_row = 0;
597       r->stop_col = -1;
598       r->stop_row = -1;
599     }
600
601   r->target_sheet = BAD_CAST opts->sheet_name;
602   r->target_sheet_index = opts->sheet_index;
603   r->row = r->col = -1;
604   r->current_sheet = -1;
605
606   /* Advance to the start of the cells for the target sheet */
607   while ( (r->state != STATE_CELL || r->row < r->start_row )
608           && 1 == (ret = xmlTextReaderRead (r->xtr)))
609     {
610       xmlChar *value ;
611       process_node (r);
612       value = xmlTextReaderValue (r->xtr);
613
614       if ( r->state == STATE_MAXROW  && r->node_type == XML_READER_TYPE_TEXT)
615         {
616           n_cases = 1 + _xmlchar_to_int (value) ;
617         }
618       free (value);
619     }
620
621   /* If a range has been given, then  use that to calculate the number
622      of cases */
623   if ( opts->cell_range)
624     {
625       n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
626     }
627
628   if ( opts->read_names )
629     {
630       r->start_row++;
631       n_cases --;
632     }
633
634   /* Read in the first row of cells,
635      including the headers if read_names was set */
636   while (
637          (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
638          && (ret = xmlTextReaderRead (r->xtr))
639          )
640     {
641       int idx;
642       process_node (r);
643
644       if ( r->row > r->start_row ) break;
645
646       if ( r->col < r->start_col ||
647            (r->stop_col != -1 && r->col > r->stop_col))
648         continue;
649
650       idx = r->col - r->start_col;
651
652       if ( idx  >= n_var_specs )
653         {
654           int i;
655           var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
656           for (i = n_var_specs; i <= idx; ++i)
657           {
658             var_spec [i].name = NULL;
659             var_spec [i].width = -1;
660             var_spec [i].first_value = NULL;
661           }
662           n_var_specs =  idx + 1 ;
663         }
664
665       if ( r->node_type == XML_READER_TYPE_TEXT )
666         {
667           xmlChar *value = xmlTextReaderValue (r->xtr);
668           const char *text  = CHAR_CAST (const char *, value);
669
670           if ( r->row < r->start_row)
671             {
672               if ( opts->read_names )
673                 {
674                   var_spec [idx].name = xstrdup (text);
675                 }
676             }
677           else
678             {
679               var_spec [idx].first_value = xmlStrdup (value);
680
681               if (-1 ==  var_spec [idx].width )
682                 var_spec [idx].width = (opts->asw == -1) ?
683                   ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : opts->asw;
684             }
685
686           free (value);
687         }
688       else if ( r->node_type == XML_READER_TYPE_ELEMENT
689                 && r->state == STATE_CELL)
690         {
691           if ( r->row == r->start_row )
692             {
693               xmlChar *attr =
694                 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
695
696               if ( NULL == attr || 60 !=  _xmlchar_to_int (attr))
697                 var_spec [idx].width = 0;
698
699               free (attr);
700             }
701         }
702     }
703
704   {
705     const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
706     if ( enc == NULL)
707       goto error;
708     /* Create the dictionary and populate it */
709     spreadsheet->dict = r->dict = dict_create (CHAR_CAST (const char *, enc));
710   }
711
712   for (i = 0 ; i < n_var_specs ; ++i )
713     {
714       char *name;
715
716       if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
717         continue;
718
719       /* Probably no data exists for this variable, so allocate a
720          default width */
721       if ( var_spec[i].width == -1 )
722         var_spec[i].width = SPREADSHEET_DEFAULT_WIDTH;
723
724       name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
725       dict_create_var (r->dict, name, var_spec[i].width);
726       free (name);
727     }
728
729   /* Create the first case, and cache it */
730   r->used_first_case = false;
731
732   if ( n_var_specs ==  0 )
733     {
734       msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
735            spreadsheet->file_name);
736       goto error;
737     }
738
739   r->proto = caseproto_ref (dict_get_proto (r->dict));
740   r->first_case = case_create (r->proto);
741   case_set_missing (r->first_case);
742
743
744   for ( i = 0 ; i < n_var_specs ; ++i )
745     {
746       const struct variable *var;
747
748       if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
749         continue;
750
751       var = dict_get_var (r->dict, x++);
752
753       convert_xml_string_to_value (r->first_case, var,
754                                    var_spec[i].first_value);
755     }
756
757   for ( i = 0 ; i < n_var_specs ; ++i )
758     {
759       free (var_spec[i].first_value);
760       free (var_spec[i].name);
761     }
762
763   free (var_spec);
764   
765
766   return casereader_create_sequential
767     (NULL,
768      r->proto,
769      n_cases,
770      &gnm_file_casereader_class, r);
771
772
773  error:
774   for ( i = 0 ; i < n_var_specs ; ++i )
775     {
776       free (var_spec[i].first_value);
777       free (var_spec[i].name);
778     }
779
780   free (var_spec);
781   dict_destroy (spreadsheet->dict);
782   spreadsheet->dict = NULL;
783
784   gnm_file_casereader_destroy (NULL, r);
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 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
794 {
795   struct ccase *c;
796   int ret = 0;
797
798   struct gnumeric_reader *r = r_;
799   int current_row = r->row;
800
801   if ( !r->used_first_case )
802     {
803       r->used_first_case = true;
804       return r->first_case;
805     }
806
807   c = case_create (r->proto);
808   case_set_missing (c);
809
810   if (r->start_col == -1)
811     r->start_col = r->min_col;
812
813   while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
814          && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
815     {
816       process_node (r);
817
818       if ( r->col < r->start_col || (r->stop_col != -1 &&
819                                      r->col > r->stop_col))
820         continue;
821
822       if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
823         continue;
824
825       if ( r->stop_row != -1 && r->row > r->stop_row)
826         break;
827
828       if ( r->node_type == XML_READER_TYPE_TEXT )
829         {
830           xmlChar *value = xmlTextReaderValue (r->xtr);
831
832           const int idx = r->col - r->start_col;
833
834           const struct variable *var = dict_get_var (r->dict, idx);
835
836           convert_xml_string_to_value (c, var, value);
837
838           free (value);
839         }
840
841     }
842
843   if (ret == 1)
844     return c;
845   else
846     {
847       case_unref (c);
848       return NULL;
849     }
850 }
851
852
853 #endif /* GNM_SUPPORT */