Merge two option structs
[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 Gnumeric file `%s' (near line %d): `%s'"),
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   if ( ret != 1)
508     {
509       /* Does not seem to be a gnumeric file */
510       xmlFreeTextReader (r->xtr);
511       free (r);
512       return NULL;
513     }
514
515   r->spreadsheet.type = SPREADSHEET_GNUMERIC;
516
517   if (show_errors)
518     {
519       const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
520       xmlCharEncoding xce = xmlParseCharEncoding (CHAR_CAST (const char *, enc));
521
522       if ( XML_CHAR_ENCODING_UTF8 != xce)
523         {
524           /* I have been told that ALL gnumeric files are UTF8 encoded.  If that is correct, this 
525              can never happen. */
526           msg (MW, _("The gnumeric file `%s' is encoded as %s instead of the usual UTF-8 encoding. "
527                      "Any non-ascii characters will be incorrectly imported."),
528                r->spreadsheet.file_name,
529                enc);
530         }
531     }
532
533   return r;
534 }
535
536
537 struct spreadsheet *
538 gnumeric_probe (const char *filename, bool report_errors)
539 {
540   struct gnumeric_reader *r = gnumeric_reopen (NULL, filename, report_errors);
541
542   return &r->spreadsheet;
543 }
544
545
546 struct casereader *
547 gnumeric_make_reader (struct spreadsheet *spreadsheet,
548                       const struct spreadsheet_read_options *opts)
549 {
550   int x = 0;
551   struct gnumeric_reader *r = NULL;
552   unsigned long int vstart = 0;
553   int ret;
554   casenumber n_cases = CASENUMBER_MAX;
555   int i;
556   struct var_spec *var_spec = NULL;
557   int n_var_specs = 0;
558
559   r = (struct gnumeric_reader *) (spreadsheet);
560
561   if (r->row != -1)
562     r = gnumeric_reopen (r, NULL, true);
563
564   if ( opts->cell_range )
565     {
566       if ( ! convert_cell_ref (opts->cell_range,
567                                &r->start_col, &r->start_row,
568                                &r->stop_col, &r->stop_row))
569         {
570           msg (SE, _("Invalid cell range `%s'"),
571                opts->cell_range);
572           goto error;
573         }
574     }
575   else
576     {
577       r->start_col = -1;
578       r->start_row = 0;
579       r->stop_col = -1;
580       r->stop_row = -1;
581     }
582
583   r->target_sheet = BAD_CAST opts->sheet_name;
584   r->target_sheet_index = opts->sheet_index;
585   r->row = r->col = -1;
586   r->current_sheet = -1;
587
588   /* Advance to the start of the cells for the target sheet */
589   while ( (r->state != STATE_CELL || r->row < r->start_row )
590           && 1 == (ret = xmlTextReaderRead (r->xtr)))
591     {
592       xmlChar *value ;
593       process_node (r);
594       value = xmlTextReaderValue (r->xtr);
595
596       if ( r->state == STATE_MAXROW  && r->node_type == XML_READER_TYPE_TEXT)
597         {
598           n_cases = 1 + _xmlchar_to_int (value) ;
599         }
600       free (value);
601     }
602
603   /* If a range has been given, then  use that to calculate the number
604      of cases */
605   if ( opts->cell_range)
606     {
607       n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
608     }
609
610   if ( opts->read_names )
611     {
612       r->start_row++;
613       n_cases --;
614     }
615
616   /* Read in the first row of cells,
617      including the headers if read_names was set */
618   while (
619          (( r->state == STATE_CELLS_START && r->row <= r->start_row) || r->state == STATE_CELL )
620          && (ret = xmlTextReaderRead (r->xtr))
621          )
622     {
623       int idx;
624       process_node (r);
625
626       if ( r->row > r->start_row ) break;
627
628       if ( r->col < r->start_col ||
629            (r->stop_col != -1 && r->col > r->stop_col))
630         continue;
631
632       idx = r->col - r->start_col;
633
634       if ( idx  >= n_var_specs )
635         {
636           int i;
637           var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
638           for (i = n_var_specs; i <= idx; ++i)
639           {
640             var_spec [i].name = NULL;
641             var_spec [i].width = -1;
642             var_spec [i].first_value = NULL;
643           }
644           n_var_specs =  idx + 1 ;
645         }
646
647       if ( r->node_type == XML_READER_TYPE_TEXT )
648         {
649           xmlChar *value = xmlTextReaderValue (r->xtr);
650           const char *text  = CHAR_CAST (const char *, value);
651
652           if ( r->row < r->start_row)
653             {
654               if ( opts->read_names )
655                 {
656                   var_spec [idx].name = xstrdup (text);
657                 }
658             }
659           else
660             {
661               var_spec [idx].first_value = xmlStrdup (value);
662
663               if (-1 ==  var_spec [idx].width )
664                 var_spec [idx].width = (opts->asw == -1) ?
665                   ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : opts->asw;
666             }
667
668           free (value);
669         }
670       else if ( r->node_type == XML_READER_TYPE_ELEMENT
671                 && r->state == STATE_CELL)
672         {
673           if ( r->row == r->start_row )
674             {
675               xmlChar *attr =
676                 xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
677
678               if ( NULL == attr || 60 !=  _xmlchar_to_int (attr))
679                 var_spec [idx].width = 0;
680
681               free (attr);
682             }
683         }
684     }
685
686   {
687     const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
688     if ( enc == NULL)
689       goto error;
690     /* Create the dictionary and populate it */
691     spreadsheet->dict = r->dict = dict_create (CHAR_CAST (const char *, enc));
692   }
693
694   for (i = 0 ; i < n_var_specs ; ++i )
695     {
696       char *name;
697
698       if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
699         continue;
700
701       /* Probably no data exists for this variable, so allocate a
702          default width */
703       if ( var_spec[i].width == -1 )
704         var_spec[i].width = SPREADSHEET_DEFAULT_WIDTH;
705
706       name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
707       dict_create_var (r->dict, name, var_spec[i].width);
708       free (name);
709     }
710
711   /* Create the first case, and cache it */
712   r->used_first_case = false;
713
714   if ( n_var_specs ==  0 )
715     {
716       msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
717            spreadsheet->file_name);
718       goto error;
719     }
720
721   r->proto = caseproto_ref (dict_get_proto (r->dict));
722   r->first_case = case_create (r->proto);
723   case_set_missing (r->first_case);
724
725
726   for ( i = 0 ; i < n_var_specs ; ++i )
727     {
728       const struct variable *var;
729
730       if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
731         continue;
732
733       var = dict_get_var (r->dict, x++);
734
735       convert_xml_string_to_value (r->first_case, var,
736                                    var_spec[i].first_value);
737     }
738
739   for ( i = 0 ; i < n_var_specs ; ++i )
740     {
741       free (var_spec[i].first_value);
742       free (var_spec[i].name);
743     }
744
745   free (var_spec);
746   
747
748   return casereader_create_sequential
749     (NULL,
750      r->proto,
751      n_cases,
752      &gnm_file_casereader_class, r);
753
754
755  error:
756   for ( i = 0 ; i < n_var_specs ; ++i )
757     {
758       free (var_spec[i].first_value);
759       free (var_spec[i].name);
760     }
761
762   free (var_spec);
763   dict_destroy (spreadsheet->dict);
764   spreadsheet->dict = NULL;
765
766   gnm_file_casereader_destroy (NULL, r);
767
768   return NULL;
769 };
770
771
772 /* Reads and returns one case from READER's file.  Returns a null
773    pointer on failure. */
774 static struct ccase *
775 gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
776 {
777   struct ccase *c;
778   int ret = 0;
779
780   struct gnumeric_reader *r = r_;
781   int current_row = r->row;
782
783   if ( !r->used_first_case )
784     {
785       r->used_first_case = true;
786       return r->first_case;
787     }
788
789   c = case_create (r->proto);
790   case_set_missing (c);
791
792   if (r->start_col == -1)
793     r->start_col = r->min_col;
794
795   while ((r->state == STATE_CELL || r->state == STATE_CELLS_START )
796          && r->row == current_row && (ret = xmlTextReaderRead (r->xtr)))
797     {
798       process_node (r);
799
800       if ( r->col < r->start_col || (r->stop_col != -1 &&
801                                      r->col > r->stop_col))
802         continue;
803
804       if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
805         continue;
806
807       if ( r->stop_row != -1 && r->row > r->stop_row)
808         break;
809
810       if ( r->node_type == XML_READER_TYPE_TEXT )
811         {
812           xmlChar *value = xmlTextReaderValue (r->xtr);
813
814           const int idx = r->col - r->start_col;
815
816           const struct variable *var = dict_get_var (r->dict, idx);
817
818           convert_xml_string_to_value (c, var, value);
819
820           free (value);
821         }
822
823     }
824
825   if (ret == 1)
826     return c;
827   else
828     {
829       case_unref (c);
830       return NULL;
831     }
832 }
833
834
835 #endif /* GNM_SUPPORT */