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