File: obj_load.c

package info (click to toggle)
ruby-ox 2.1.1-2+deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 540 kB
  • sloc: ansic: 7,178; ruby: 415; makefile: 2
file content (981 lines) | stat: -rw-r--r-- 23,860 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
/* obj_load.c
 * Copyright (c) 2011, Peter Ohler
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *  - Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 *  - Neither the name of Peter Ohler nor the names of its contributors may be
 *    used to endorse or promote products derived from this software without
 *    specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>

#include "ruby.h"
#include "base64.h"
#include "ox.h"

static void	instruct(PInfo pi, const char *target, Attr attrs, const char *content);
static void	add_text(PInfo pi, char *text, int closed);
static void	add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren);
static void	end_element(PInfo pi, const char *ename);

static VALUE	parse_time(const char *text, VALUE clas);
static VALUE	parse_xsd_time(const char *text, VALUE clas);
static VALUE	parse_double_time(const char *text, VALUE clas);
static VALUE	parse_regexp(const char *text);

static VALUE		get_var_sym_from_attrs(Attr a, void *encoding);
static VALUE		get_obj_from_attrs(Attr a, PInfo pi, VALUE base_class);
static VALUE		get_class_from_attrs(Attr a, PInfo pi, VALUE base_class);
static VALUE		classname2class(const char *name, PInfo pi, VALUE base_class);
static unsigned long	get_id_from_attrs(PInfo pi, Attr a);
static CircArray	circ_array_new(void);
static void		circ_array_free(CircArray ca);
static void		circ_array_set(CircArray ca, VALUE obj, unsigned long id);
static VALUE		circ_array_get(CircArray ca, unsigned long id);

static void		debug_stack(PInfo pi, const char *comment);
static void		fill_indent(PInfo pi, char *buf, size_t size);


struct _ParseCallbacks	 _ox_obj_callbacks = {
    instruct, /* instruct, */
    0, /* add_doctype, */
    0, /* add_comment, */
    0, /* add_cdata, */
    add_text,
    add_element,
    end_element,
};

ParseCallbacks	 ox_obj_callbacks = &_ox_obj_callbacks;

extern ParseCallbacks	ox_gen_callbacks;


inline static VALUE
str2sym(const char *str, void *encoding) {
    VALUE	sym;
    
#ifdef HAVE_RUBY_ENCODING_H
    if (0 != encoding) {
	VALUE	rstr = rb_str_new2(str);

	rb_enc_associate(rstr, (rb_encoding*)encoding);
	sym = rb_funcall(rstr, ox_to_sym_id, 0);
    } else {
	sym = ID2SYM(rb_intern(str));
    }
#else
    sym = ID2SYM(rb_intern(str));
#endif
    return sym;
}

inline static ID
name2var(const char *name, void *encoding) {
    VALUE	*slot;
    ID		var_id;

    if ('0' <= *name && *name <= '9') {
	var_id = INT2NUM(atoi(name));
    } else if (Qundef == (var_id = ox_cache_get(ox_attr_cache, name, &slot, 0))) {
#ifdef HAVE_RUBY_ENCODING_H
	if (0 != encoding) {
	    VALUE	rstr = rb_str_new2(name);
	    VALUE	sym;
	    
	    rb_enc_associate(rstr, (rb_encoding*)encoding);
	    sym = rb_funcall(rstr, ox_to_sym_id, 0);
	    var_id = SYM2ID(sym);
	} else {
	    var_id = rb_intern(name);
	}
#else
	var_id = rb_intern(name);
#endif
	*slot = var_id;
    }
    return var_id;
}

inline static VALUE
resolve_classname(VALUE mod, const char *class_name, Effort effort, VALUE base_class) {
    VALUE	clas;
    ID		ci = rb_intern(class_name);

    switch (effort) {
    case TolerantEffort:
	if (rb_const_defined_at(mod, ci)) {
	    clas = rb_const_get_at(mod, ci);
	} else {
	    clas = Qundef;
	}
	break;
    case AutoEffort:
	if (rb_const_defined_at(mod, ci)) {
	    clas = rb_const_get_at(mod, ci);
	} else {
	    clas = rb_define_class_under(mod, class_name, base_class);
	}
	break;
    case StrictEffort:
    default:
	/* raise an error if name is not defined */
	clas = rb_const_get_at(mod, ci);
	break;
    }
    return clas;
}

inline static VALUE
classname2obj(const char *name, PInfo pi, VALUE base_class) {
    VALUE   clas = classname2class(name, pi, base_class);
    
    if (Qundef == clas) {
	return Qnil;
    } else {
	return rb_obj_alloc(clas);
    }
}

#if HAS_RSTRUCT
inline static VALUE
structname2obj(const char *name) {
    VALUE	ost;
    const char	*s = name;

    for (; 1; s++) {
	if ('\0' == *s) {
	    s = name;
	    break;
	} else if (':' == *s) {
	    s += 2;
	    break;
	}
    }
    ost = rb_const_get(ox_struct_class, rb_intern(s));
    /* use encoding as the indicator for Ruby 1.8.7 or 1.9.x */
#if HAS_ENCODING_SUPPORT
    return rb_struct_alloc_noinit(ost);
#elif HAS_PRIVATE_ENCODING
    return rb_struct_alloc_noinit(ost);
#else
    return rb_struct_new(ost);
#endif
}
#endif

inline static VALUE
parse_ulong(const char *s, PInfo pi) {
    unsigned long	n = 0;

    for (; '\0' != *s; s++) {
	if ('0' <= *s && *s <= '9') {
	    n = n * 10 + (*s - '0');
	} else {
	    set_error(&pi->err, "Invalid number for a julian day", pi->str, pi->s);
	    return Qundef;
	}
    }
    return ULONG2NUM(n);
}

/* 2010-07-09T10:47:45.895826162+09:00 */
inline static VALUE
parse_time(const char *text, VALUE clas) {
    VALUE	t;

    if (Qnil == (t = parse_double_time(text, clas)) &&
	Qnil == (t = parse_xsd_time(text, clas))) {
	VALUE	    args[1];

	/*printf("**** time parse\n"); */
	*args = rb_str_new2(text);
	t = rb_funcall2(ox_time_class, ox_parse_id, 1, args);
    }
    return t;
}

static VALUE
classname2class(const char *name, PInfo pi, VALUE base_class) {
    VALUE	*slot;
    VALUE	clas;
	    
    if (Qundef == (clas = ox_cache_get(ox_class_cache, name, &slot, 0))) {
	char		class_name[1024];
	char		*s;
	const char	*n = name;

	clas = rb_cObject;
	for (s = class_name; '\0' != *n; n++) {
	    if (':' == *n) {
		*s = '\0';
		n++;
		if (':' != *n) {
		    set_error(&pi->err, "Invalid classname, expected another ':'", pi->str, pi->s);
		    return Qundef;
		}
		if (Qundef == (clas = resolve_classname(clas, class_name, pi->options->effort, base_class))) {
		    return Qundef;
		}
		s = class_name;
	    } else {
		*s++ = *n;
	    }
	}
	*s = '\0';
	if (Qundef != (clas = resolve_classname(clas, class_name, pi->options->effort, base_class))) {
	    *slot = clas;
	}
    }
    return clas;
}

static VALUE
get_var_sym_from_attrs(Attr a, void *encoding) {
    for (; 0 != a->name; a++) {
	if ('a' == *a->name && '\0' == *(a->name + 1)) {
	    return name2var(a->value, encoding);
	}
    }
    return Qundef;
}

static VALUE
get_obj_from_attrs(Attr a, PInfo pi, VALUE base_class) {
    for (; 0 != a->name; a++) {
	if ('c' == *a->name && '\0' == *(a->name + 1)) {
	    return classname2obj(a->value, pi, base_class);
	}
    }
    return Qundef;
}

#if HAS_RSTRUCT
static VALUE
get_struct_from_attrs(Attr a) {
    for (; 0 != a->name; a++) {
	if ('c' == *a->name && '\0' == *(a->name + 1)) {
	    return structname2obj(a->value);
	}
    }
    return Qundef;
}
#endif

static VALUE
get_class_from_attrs(Attr a, PInfo pi, VALUE base_class) {
    for (; 0 != a->name; a++) {
	if ('c' == *a->name && '\0' == *(a->name + 1)) {
	    return classname2class(a->value, pi, base_class);
	}
    }
    return Qundef;
}

static unsigned long
get_id_from_attrs(PInfo pi, Attr a) {
    for (; 0 != a->name; a++) {
	if ('i' == *a->name && '\0' == *(a->name + 1)) {
	    unsigned long	id = 0;
	    const char		*text = a->value;
	    char		c;
	    
	    for (; '\0' != *text; text++) {
		c = *text;
		if ('0' <= c && c <= '9') {
		    id = id * 10 + (c - '0');
		} else {
		    set_error(&pi->err, "bad number format", pi->str, pi->s);
		    return 0;
		}
	    }
	    return id;
	}
    }
    return 0;
}

static CircArray
circ_array_new() {
    CircArray	ca;
    
    ca = ALLOC(struct _CircArray);
    ca->objs = ca->obj_array;
    ca->size = sizeof(ca->obj_array) / sizeof(VALUE);
    ca->cnt = 0;
    
    return ca;
}

static void
circ_array_free(CircArray ca) {
    if (ca->objs != ca->obj_array) {
	xfree(ca->objs);
    }
    xfree(ca);
}

static void
circ_array_set(CircArray ca, VALUE obj, unsigned long id) {
    if (0 < id) {
	unsigned long	i;

	if (ca->size < id) {
	    unsigned long	cnt = id + 512;

	    if (ca->objs == ca->obj_array) {
		ca->objs = ALLOC_N(VALUE, cnt);
		memcpy(ca->objs, ca->obj_array, sizeof(VALUE) * ca->cnt);
	    } else {
		REALLOC_N(ca->objs, VALUE, cnt);
	    }
	    ca->size = cnt;
	}
	id--;
	for (i = ca->cnt; i < id; i++) {
	    ca->objs[i] = Qundef;
	}
	ca->objs[id] = obj;
	if (ca->cnt <= id) {
	    ca->cnt = id + 1;
	}
    }
}

static VALUE
circ_array_get(CircArray ca, unsigned long id) {
    VALUE	obj = Qundef;

    if (id <= ca->cnt) {
	obj = ca->objs[id - 1];
    }
    return obj;
}

static VALUE
parse_regexp(const char *text) {
    const char	*te;
    int		options = 0;
	    
    te = text + strlen(text) - 1;
#if HAS_ONIG
    for (; text < te && '/' != *te; te--) {
	switch (*te) {
	case 'i':	options |= ONIG_OPTION_IGNORECASE;	break;
	case 'm':	options |= ONIG_OPTION_MULTILINE;	break;
	case 'x':	options |= ONIG_OPTION_EXTEND;		break;
	default:						break;
	}
    }
#endif
    return rb_reg_new(text + 1, te - text - 1, options);
}

static void
instruct(PInfo pi, const char *target, Attr attrs, const char *content) {
    if (0 == strcmp("xml", target)) {
#if HAS_ENCODING_SUPPORT
	for (; 0 != attrs->name; attrs++) {
	    if (0 == strcmp("encoding", attrs->name)) {
		pi->options->rb_enc = rb_enc_find(attrs->value);
	    }
	}
#elif HAS_PRIVATE_ENCODING
	for (; 0 != attrs->name; attrs++) {
	    if (0 == strcmp("encoding", attrs->name)) {
		pi->options->rb_enc = rb_str_new2(attrs->value);
	    }
	}
#endif
    }
}

static void
add_text(PInfo pi, char *text, int closed) {
    Helper	h = helper_stack_peek(&pi->helpers);

    if (!closed) {
	set_error(&pi->err, "Text not closed", pi->str, pi->s);
	return;
    }
    if (0 == h) {
	set_error(&pi->err, "Unexpected text", pi->str, pi->s);
	return;
    }
    if (DEBUG <= pi->options->trace) {
	char	indent[128];

	fill_indent(pi, indent, sizeof(indent));
	printf("%s '%s' to type %c\n", indent, text, h->type);
    }
    switch (h->type) {
    case NoCode:
    case StringCode:
	h->obj = rb_str_new2(text);
#if HAS_ENCODING_SUPPORT
	if (0 != pi->options->rb_enc) {
	    rb_enc_associate(h->obj, pi->options->rb_enc);
	}
#elif HAS_PRIVATE_ENCODING
	if (Qnil != pi->options->rb_enc) {
	    rb_funcall(h->obj, ox_force_encoding_id, 1, pi->options->rb_enc);
	}
#endif
	if (0 != pi->circ_array) {
	    circ_array_set(pi->circ_array, h->obj, (unsigned long)pi->id);
	}
	break;
    case FixnumCode:
    {
	long	n = 0;
	char	c;
	int	neg = 0;

	if ('-' == *text) {
	    neg = 1;
	    text++;
	}
	for (; '\0' != *text; text++) {
	    c = *text;
	    if ('0' <= c && c <= '9') {
		n = n * 10 + (c - '0');
	    } else {
		set_error(&pi->err, "bad number format", pi->str, pi->s);
		return;
	    }
	}
	if (neg) {
	    n = -n;
	}
	h->obj = LONG2NUM(n);
	break;
    }
    case FloatCode:
	h->obj = rb_float_new(strtod(text, 0));
	break;
    case SymbolCode:
    {
	VALUE	sym;
	VALUE	*slot;

	if (Qundef == (sym = ox_cache_get(ox_symbol_cache, text, &slot, 0))) {
	    sym = str2sym(text, (void*)pi->options->rb_enc);
	    *slot = sym;
	}
	h->obj = sym;
	break;
    }
    case DateCode:
    {
	VALUE	args[1];

	if (Qundef == (*args = parse_ulong(text, pi))) {
	    return;
	}
	h->obj = rb_funcall2(ox_date_class, ox_jd_id, 1, args);
	break;
    }
    case TimeCode:
	h->obj = parse_time(text, ox_time_class);
	break;
    case String64Code:
    {
	unsigned long	str_size = b64_orig_size(text);
	VALUE		v;
	char		*str = ALLOCA_N(char, str_size + 1);
	
	from_base64(text, (uchar*)str);
	v = rb_str_new(str, str_size);
#if HAS_ENCODING_SUPPORT
	if (0 != pi->options->rb_enc) {
	    rb_enc_associate(v, pi->options->rb_enc);
	}
#elif HAS_PRIVATE_ENCODING
	if (0 != pi->options->rb_enc) {
	    rb_funcall(v, ox_force_encoding_id, 1, pi->options->rb_enc);
	}
#endif
	if (0 != pi->circ_array) {
	    circ_array_set(pi->circ_array, v, (unsigned long)h->obj);
	}
	h->obj = v;
	break;
    }
    case Symbol64Code:
    {
	VALUE		sym;
	VALUE		*slot;
	unsigned long	str_size = b64_orig_size(text);
	char		*str = ALLOCA_N(char, str_size + 1);
	
	from_base64(text, (uchar*)str);
	if (Qundef == (sym = ox_cache_get(ox_symbol_cache, str, &slot, 0))) {
	    sym = str2sym(str, (void*)pi->options->rb_enc);
	    *slot = sym;
	}
	h->obj = sym;
	break;
    }
    case RegexpCode:
	if ('/' == *text) {
	    h->obj = parse_regexp(text);
	} else {
	    unsigned long	str_size = b64_orig_size(text);
	    char		*str = ALLOCA_N(char, str_size + 1);
	
	    from_base64(text, (uchar*)str);
	    h->obj = parse_regexp(str);
	}
	break;
    case BignumCode:
	h->obj = rb_cstr_to_inum(text, 10, 1);
	break;
    case BigDecimalCode:
#if HAS_BIGDECIMAL
	h->obj = rb_funcall(ox_bigdecimal_class, ox_new_id, 1, rb_str_new2(text));
#else
	h->obj = Qnil;
#endif
	break;
    default:
	h->obj = Qnil;
	break;
    }
}

static void
add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
    Attr		a;
    Helper		h;
    unsigned long	id;

    if (TRACE <= pi->options->trace) {
	char	buf[1024];
	char	indent[128];
	char	*s = buf;
	char	*end = buf + sizeof(buf) - 2;

	s += snprintf(s, end - s, " <%s%s", (hasChildren) ? "" : "/", ename);
	for (a = attrs; 0 != a->name; a++) {
	    s += snprintf(s, end - s, " %s=%s", a->name, a->value);
	}
	*s++ = '>';
	*s++ = '\0';
	if (DEBUG <= pi->options->trace) {
	    printf("===== add element stack(%d) =====\n", helper_stack_depth(&pi->helpers));
	    debug_stack(pi, buf);
	} else {
	    fill_indent(pi, indent, sizeof(indent));
	    printf("%s%s\n", indent, buf);
	}
    }
    if (helper_stack_empty(&pi->helpers)) { /* top level object */
	if (0 != (id = get_id_from_attrs(pi, attrs))) {
	    pi->circ_array = circ_array_new();
	}
    }
    if ('\0' != ename[1]) {
	set_error(&pi->err, "Invalid element name", pi->str, pi->s);
	return;
    }
    h = helper_stack_push(&pi->helpers, get_var_sym_from_attrs(attrs, (void*)pi->options->rb_enc), Qundef, *ename);
    switch (h->type) {
    case NilClassCode:
	h->obj = Qnil;
	break;
    case TrueClassCode:
	h->obj = Qtrue;
	break;
    case FalseClassCode:
	h->obj = Qfalse;
	break;
    case StringCode:
	/* h->obj will be replaced by add_text if it is called */
	h->obj = ox_empty_string;
	if (0 != pi->circ_array) {
	    pi->id = get_id_from_attrs(pi, attrs);
	    circ_array_set(pi->circ_array, h->obj, pi->id);
	}
	break;
    case FixnumCode:
    case FloatCode:
    case SymbolCode:
    case Symbol64Code:
    case RegexpCode:
    case BignumCode:
    case BigDecimalCode:
    case ComplexCode:
    case DateCode:
    case TimeCode:
    case RationalCode: /* sub elements read next */
	/* value will be read in the following add_text */
	h->obj = Qundef;
	break;
    case String64Code:
	h->obj = Qundef;
	if (0 != pi->circ_array) {
	    pi->id = get_id_from_attrs(pi, attrs);
	}
	break;
    case ArrayCode:
	h->obj = rb_ary_new();
	if (0 != pi->circ_array) {
	    circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
	}
	break;
    case HashCode:
	h->obj = rb_hash_new();
	if (0 != pi->circ_array) {
	    circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
	}
	break;
    case RangeCode:
	h->obj = rb_range_new(ox_zero_fixnum, ox_zero_fixnum, Qfalse);
	break;
    case RawCode:
	if (hasChildren) {
	    h->obj = ox_parse(pi->s, ox_gen_callbacks, &pi->s, pi->options, &pi->err);
	    if (0 != pi->circ_array) {
		circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
	    }
	} else {
	    h->obj = Qnil;
	}
	break;
    case ExceptionCode:
	if (Qundef == (h->obj = get_obj_from_attrs(attrs, pi, rb_eException))) {
	    return;
	}
	if (0 != pi->circ_array && Qnil != h->obj) {
	    circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
	}
	break;
    case ObjectCode:
	if (Qundef == (h->obj = get_obj_from_attrs(attrs, pi, ox_bag_clas))) {
	    return;
	}
	if (0 != pi->circ_array && Qnil != h->obj) {
	    circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
	}
	break;
    case StructCode:
#if HAS_RSTRUCT
	h->obj = get_struct_from_attrs(attrs);
	if (0 != pi->circ_array) {
	    circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
	}
#else
	set_error(&pi->err, "Ruby structs not supported with this verion of Ruby", pi->str, pi->s);
	return;
#endif
	break;
    case ClassCode:
	if (Qundef == (h->obj = get_class_from_attrs(attrs, pi, ox_bag_clas))) {
	    return;
	}
	break;
    case RefCode:
	h->obj = Qundef;
	if (0 != pi->circ_array) {
	    h->obj = circ_array_get(pi->circ_array, get_id_from_attrs(pi, attrs));
	}
	if (Qundef == h->obj) {
	    set_error(&pi->err, "Invalid circular reference", pi->str, pi->s);
	    return;
	}
	break;
    default:
	set_error(&pi->err, "Invalid element name", pi->str, pi->s);
	return;
	break;
    }
    if (DEBUG <= pi->options->trace) {
	debug_stack(pi, "   -----------");
    }
}

static void
end_element(PInfo pi, const char *ename) {
    if (TRACE <= pi->options->trace) {
	char	indent[128];
	
	if (DEBUG <= pi->options->trace) {
	    char    buf[1024];

	    printf("===== end element stack(%d) =====\n", helper_stack_depth(&pi->helpers));
	    snprintf(buf, sizeof(buf) - 1, "</%s>", ename);
	    debug_stack(pi, buf);
	} else {
	    fill_indent(pi, indent, sizeof(indent));
	    printf("%s</%s>\n", indent, ename);
	}
    }
    if (!helper_stack_empty(&pi->helpers)) {
	Helper	h = helper_stack_pop(&pi->helpers);
	Helper	ph = helper_stack_peek(&pi->helpers);

	if (ox_empty_string == h->obj) {
	    /* special catch for empty strings */
	    h->obj = rb_str_new2("");
	}
	pi->obj = h->obj;
	if (0 != ph) {
	    switch (ph->type) {
	    case ArrayCode:
		rb_ary_push(ph->obj, h->obj);
		break;
	    case ExceptionCode:
	    case ObjectCode:
		if (Qnil != ph->obj) {
		    rb_ivar_set(ph->obj, h->var, h->obj);
		}
		break;
	    case StructCode:
#if HAS_RSTRUCT
		rb_struct_aset(ph->obj, h->var, h->obj);
#else
		set_error(&pi->err, "Ruby structs not supported with this verion of Ruby", pi->str, pi->s);
		return;
#endif
		break;
	    case HashCode:
		// put back h
		helper_stack_push(&pi->helpers, h->var, h->obj, KeyCode);
		break;
	    case RangeCode:
#if HAS_RSTRUCT
		if (ox_beg_id == h->var) {
		    RSTRUCT_PTR(ph->obj)[0] = h->obj;
		} else if (ox_end_id == h->var) {
		    RSTRUCT_PTR(ph->obj)[1] = h->obj;
		} else if (ox_excl_id == h->var) {
		    RSTRUCT_PTR(ph->obj)[2] = h->obj;
		} else {
		    set_error(&pi->err, "Invalid range attribute", pi->str, pi->s);
		    return;
		}
#else
		set_error(&pi->err, "Ruby structs not supported with this verion of Ruby", pi->str, pi->s);
		return;
#endif
		break;
	    case KeyCode:
		{
		    Helper	gh;

		    helper_stack_pop(&pi->helpers);
		    if (NULL == (gh = helper_stack_peek(&pi->helpers))) {
			set_error(&pi->err, "Corrupt parse stack, container is wrong type", pi->str, pi->s);
			return;
		    }
		    rb_hash_aset(gh->obj, ph->obj, h->obj);
		}
		break;
	    case ComplexCode:
#ifdef T_COMPLEX
		if (Qundef == ph->obj) {
		    ph->obj = h->obj;
		} else {
		    ph->obj = rb_complex_new(ph->obj, h->obj);
		}
#else
		set_error(&pi->err, "Complex Objects not implemented in Ruby 1.8.7", pi->str, pi->s);
		return;
#endif
		break;
	    case RationalCode:
#ifdef T_RATIONAL
		if (Qundef == ph->obj) {
		    ph->obj = h->obj;
		} else {
#ifdef RUBINIUS_RUBY
		    ph->obj = rb_Rational(ph->obj, h->obj);
#else
		    ph->obj = rb_rational_new(ph->obj, h->obj);
#endif
		}
#else
		set_error(&pi->err, "Rational Objects not implemented in Ruby 1.8.7", pi->str, pi->s);
		return;
#endif
		break;
	    default:
		set_error(&pi->err, "Corrupt parse stack, container is wrong type", pi->str, pi->s);
		return;
		break;
	    }
	}
    }
    if (0 != pi->circ_array && helper_stack_empty(&pi->helpers)) {
	circ_array_free(pi->circ_array);
	pi->circ_array = 0;
    }
    if (DEBUG <= pi->options->trace) {
	debug_stack(pi, "   ----------");
    }
}

static VALUE
parse_double_time(const char *text, VALUE clas) {
    long	v = 0;
    long	v2 = 0;
    const char	*dot = 0;
    char	c;
    
    for (; '.' != *text; text++) {
	c = *text;
	if (c < '0' || '9' < c) {
	    return Qnil;
	}
	v = 10 * v + (long)(c - '0');
    }
    dot = text++;
    for (; '\0' != *text && text - dot <= 6; text++) {
	c = *text;
	if (c < '0' || '9' < c) {
	    return Qnil;
	}
	v2 = 10 * v2 + (long)(c - '0');
    }
    for (; text - dot <= 9; text++) {
	v2 *= 10;
    }
#if HAS_NANO_TIME
    return rb_time_nano_new(v, v2);
#else
    return rb_time_new(v, v2 / 1000);
#endif
}

typedef struct _Tp {
    int		cnt;
    char	end;
    char	alt;
} *Tp;

static VALUE
parse_xsd_time(const char *text, VALUE clas) {
    long	cargs[10];
    long	*cp = cargs;
    long	v;
    int		i;
    char	c;
    struct _Tp	tpa[10] = { { 4, '-', '-' },
			   { 2, '-', '-' },
			   { 2, 'T', 'T' },
			   { 2, ':', ':' },
			   { 2, ':', ':' },
			   { 2, '.', '.' },
			   { 9, '+', '-' },
			   { 2, ':', ':' },
			   { 2, '\0', '\0' },
			   { 0, '\0', '\0' } };
    Tp		tp = tpa;
    struct tm	tm;

    for (; 0 != tp->cnt; tp++) {
	for (i = tp->cnt, v = 0; 0 < i ; text++, i--) {
	    c = *text;
	    if (c < '0' || '9' < c) {
		if (tp->end == c || tp->alt == c) {
		    break;
		}
		return Qnil;
	    }
	    v = 10 * v + (long)(c - '0');
	}
	c = *text++;
	if (tp->end != c && tp->alt != c) {
	    return Qnil;
	}
	*cp++ = v;
    }
    tm.tm_year = (int)cargs[0] - 1900;
    tm.tm_mon = (int)cargs[1] - 1;
    tm.tm_mday = (int)cargs[2];
    tm.tm_hour = (int)cargs[3];
    tm.tm_min = (int)cargs[4];
    tm.tm_sec = (int)cargs[5];
#if HAS_NANO_TIME
    return rb_time_nano_new(mktime(&tm), cargs[6]);
#else
    return rb_time_new(mktime(&tm), cargs[6] / 1000);
#endif
}

/* debug functions */
static void
fill_indent(PInfo pi, char *buf, size_t size) {
    size_t	cnt;

    if (0 < (cnt = helper_stack_depth(&pi->helpers))) {
	cnt *= 2;
	if (size < cnt + 1) {
	    cnt = size - 1;
	}
	memset(buf, ' ', cnt);
	buf += cnt;
    }
    *buf = '\0';
}

static void
debug_stack(PInfo pi, const char *comment) {
    char	indent[128];
    Helper	h;

    fill_indent(pi, indent, sizeof(indent));
    printf("%s%s\n", indent, comment);
    if (!helper_stack_empty(&pi->helpers)) {
	for (h = pi->helpers.head; h < pi->helpers.tail; h++) {
	    const char	*clas = "---";
	    const char	*key = "---";

	    if (Qundef != h->obj) {
		VALUE	c =  rb_obj_class(h->obj);

		clas = rb_class2name(c);
	    }
	    if (Qundef != h->var) {
		if (HashCode == h->type) {
		    VALUE	v;
		    
		    v = rb_funcall2(h->var, rb_intern("to_s"), 0, 0);
		    key = StringValuePtr(v);
		} else if (ObjectCode == (h - 1)->type || ExceptionCode == (h - 1)->type || RangeCode == (h - 1)->type || StructCode == (h - 1)->type) {
		    key = rb_id2name(h->var);
		} else {
		    printf("%s*** corrupt stack ***\n", indent);
		}
	    }
	    printf("%s [%c] %s : %s\n", indent, h->type, clas, key);
	}
    }
}