Merge remote branch 'origin/master' into import-gui
[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
210
211 static void
212 process_node (struct gnumeric_reader *r)
213 {
214   xmlChar *name = xmlTextReaderName (r->xtr);
215   if (name == NULL)
216     name = xmlStrdup (_xml ("--"));
217
218   r->node_type = xmlTextReaderNodeType (r->xtr);
219
220   switch (r->state)
221     {
222     case STATE_PRE_INIT:
223       r->current_sheet = -1;
224       if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
225           XML_READER_TYPE_ELEMENT  == r->node_type)
226         {
227           r->state = STATE_SHEET_COUNT;
228         }
229       break;
230
231     case STATE_SHEET_COUNT:
232       if (0 == xmlStrcasecmp (name, _xml("gnm:SheetName")) &&
233           XML_READER_TYPE_ELEMENT  == r->node_type)
234         {
235           ++r->current_sheet;
236           if (r->current_sheet + 1 > r->spreadsheet.n_sheets)
237             {
238               struct sheet_detail *sd ;
239               r->sheets = xrealloc (r->sheets, (r->current_sheet + 1) * sizeof *r->sheets);
240               sd = &r->sheets[r->current_sheet];
241               sd->start_col = sd->stop_col = sd->start_row = sd->stop_row = -1;
242               r->spreadsheet.n_sheets = r->current_sheet + 1;
243             }
244         }
245       else if (0 == xmlStrcasecmp (name, _xml("gnm:SheetNameIndex")) &&
246           XML_READER_TYPE_END_ELEMENT  == r->node_type)
247         {
248           r->state = STATE_INIT;
249           r->current_sheet = -1;
250         }
251       else if (XML_READER_TYPE_TEXT == r->node_type)
252         {
253           r->sheets [r->spreadsheet.n_sheets - 1].name = CHAR_CAST (char *, xmlTextReaderValue (r->xtr));
254         }
255       break;
256
257     case STATE_INIT:
258       if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet")) &&
259           XML_READER_TYPE_ELEMENT  == r->node_type)
260         {
261           ++r->current_sheet;
262           r->state = STATE_SHEET_START;
263         }
264       break;
265     case STATE_SHEET_START:
266       if (0 == xmlStrcasecmp (name, _xml("gnm:Name"))  &&
267           XML_READER_TYPE_ELEMENT  == r->node_type)
268         {
269           r->state = STATE_SHEET_NAME;
270         }
271       break;
272     case STATE_SHEET_NAME:
273       if (0 == xmlStrcasecmp (name, _xml("gnm:Name"))  &&
274           XML_READER_TYPE_END_ELEMENT  == r->node_type)
275         {
276           r->state = STATE_INIT;
277         }
278       else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet"))  &&
279           XML_READER_TYPE_END_ELEMENT  == r->node_type)
280         {
281           r->state = STATE_INIT;
282         }
283       else if (XML_READER_TYPE_TEXT == r->node_type)
284         {
285           if ( r->target_sheet != NULL)
286             {
287               xmlChar *value = xmlTextReaderValue (r->xtr);
288               if ( 0 == xmlStrcmp (value, r->target_sheet))
289                 r->state = STATE_SHEET_FOUND;
290               free (value);
291             }
292           else if (r->target_sheet_index == r->current_sheet + 1)
293             {
294               r->state = STATE_SHEET_FOUND;
295             }
296           else if (r->target_sheet_index == -1)
297             {
298               r->state = STATE_SHEET_FOUND;
299             }
300         }
301       break;
302     case STATE_SHEET_FOUND:
303       if (0 == xmlStrcasecmp (name, _xml("gnm:Cells"))  &&
304           XML_READER_TYPE_ELEMENT  == r->node_type)
305         {
306           r->min_col = INT_MAX;
307           if (! xmlTextReaderIsEmptyElement (r->xtr))
308             r->state = STATE_CELLS_START;
309         }
310       else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow"))  &&
311           XML_READER_TYPE_ELEMENT  == r->node_type)
312         {
313           r->state = STATE_MAXROW;
314         }
315       else if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol"))  &&
316           XML_READER_TYPE_ELEMENT  == r->node_type)
317         {
318           r->state = STATE_MAXCOL;
319         }
320       else if (0 == xmlStrcasecmp (name, _xml("gnm:Sheet"))  &&
321           XML_READER_TYPE_END_ELEMENT  == r->node_type)
322         {
323           r->state = STATE_INIT;
324         }
325       break;
326     case STATE_MAXROW:
327       if (0 == xmlStrcasecmp (name, _xml("gnm:MaxRow"))  &&
328           XML_READER_TYPE_END_ELEMENT  == r->node_type)
329         {
330           r->state = STATE_SHEET_FOUND;
331         }
332       else if (r->node_type == XML_READER_TYPE_TEXT)
333         {
334           xmlChar *value = xmlTextReaderValue (r->xtr);
335           r->sheets[r->current_sheet].maxrow = _xmlchar_to_int (value);
336           xmlFree (value);
337         }
338       break;
339     case STATE_MAXCOL:
340       if (0 == xmlStrcasecmp (name, _xml("gnm:MaxCol"))  &&
341           XML_READER_TYPE_END_ELEMENT  == r->node_type)
342         {
343           r->state = STATE_SHEET_FOUND;
344         }
345       else if (r->node_type == XML_READER_TYPE_TEXT)
346         {
347           xmlChar *value = xmlTextReaderValue (r->xtr);
348           r->sheets[r->current_sheet].maxcol = _xmlchar_to_int (value);
349           xmlFree (value);
350         }
351       break;
352     case STATE_CELLS_START:
353       if (0 == xmlStrcasecmp (name, _xml ("gnm:Cell"))  &&
354           XML_READER_TYPE_ELEMENT  == r->node_type)
355         {
356           xmlChar *attr = NULL;
357
358           attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Col"));
359           r->col =  _xmlchar_to_int (attr);
360           free (attr);
361
362           if (r->col < r->min_col)
363             r->min_col = r->col;
364
365           attr = xmlTextReaderGetAttribute (r->xtr, _xml ("Row"));
366           r->row = _xmlchar_to_int (attr);
367           free (attr);
368
369           if (r->sheets[r->current_sheet].start_row == -1)
370             {
371               r->sheets[r->current_sheet].start_row = r->row;
372             }
373
374           if (r->sheets[r->current_sheet].start_col == -1)
375             {
376               r->sheets[r->current_sheet].start_col = r->col;
377             }
378           if (! xmlTextReaderIsEmptyElement (r->xtr))
379             r->state = STATE_CELL;
380         }
381       else if ( (0 == xmlStrcasecmp (name, _xml("gnm:Cells")))  &&  (XML_READER_TYPE_END_ELEMENT  == r->node_type) )
382         {
383           r->sheets[r->current_sheet].stop_col = r->col;
384           r->sheets[r->current_sheet].stop_row = r->row;
385           r->state = STATE_SHEET_NAME;
386         }
387       break;
388     case STATE_CELL:
389       if (0 == xmlStrcasecmp (name, _xml("gnm:Cell"))  && XML_READER_TYPE_END_ELEMENT  == r->node_type)
390         {
391           r->state = STATE_CELLS_START;
392         }
393       break;
394     default:
395       break;
396     };
397
398   xmlFree (name);
399 }
400
401
402 /*
403    Sets the VAR of case C, to the value corresponding to the xml string XV
404  */
405 static void
406 convert_xml_string_to_value (struct ccase *c, const struct variable *var,
407                              const xmlChar *xv)
408 {
409   union value *v = case_data_rw (c, var);
410
411   if (xv == NULL)
412     value_set_missing (v, var_get_width (var));
413   else if ( var_is_alpha (var))
414     value_copy_str_rpad (v, var_get_width (var), xv, ' ');
415   else
416     {
417       const char *text = CHAR_CAST (const char *, xv);
418       char *endptr;
419
420       errno = 0;
421       v->f = c_strtod (text, &endptr);
422       if ( errno != 0 || endptr == text)
423         v->f = SYSMIS;
424     }
425 }
426
427 struct var_spec
428 {
429   char *name;
430   int width;
431   xmlChar *first_value;
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 */