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