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