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