ox 2.14.14 → 2.14.15

Sign up to get free protection for your applications and to get access to all the features.
data/ext/ox/obj_load.c CHANGED
@@ -3,112 +3,107 @@
3
3
  * All rights reserved.
4
4
  */
5
5
 
6
- #include <stdlib.h>
7
6
  #include <errno.h>
7
+ #include <stdarg.h>
8
8
  #include <stdio.h>
9
+ #include <stdlib.h>
9
10
  #include <string.h>
10
- #include <stdarg.h>
11
11
  #include <time.h>
12
12
 
13
- #include "ruby.h"
14
- #include "ruby/encoding.h"
15
-
16
13
  #include "base64.h"
17
- #include "ox.h"
18
14
  #include "intern.h"
15
+ #include "ox.h"
16
+ #include "ruby.h"
17
+ #include "ruby/encoding.h"
19
18
 
20
- static void instruct(PInfo pi, const char *target, Attr attrs, const char *content);
21
- static void add_text(PInfo pi, char *text, int closed);
22
- static void add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren);
23
- static void end_element(PInfo pi, const char *ename);
24
-
25
- static VALUE parse_time(const char *text, VALUE clas);
26
- static VALUE parse_xsd_time(const char *text, VALUE clas);
27
- static VALUE parse_double_time(const char *text, VALUE clas);
28
- static VALUE parse_regexp(const char *text);
29
-
30
- static ID get_var_sym_from_attrs(Attr a, void *encoding);
31
- static VALUE get_obj_from_attrs(Attr a, PInfo pi, VALUE base_class);
32
- static VALUE get_class_from_attrs(Attr a, PInfo pi, VALUE base_class);
33
- static VALUE classname2class(const char *name, PInfo pi, VALUE base_class);
34
- static unsigned long get_id_from_attrs(PInfo pi, Attr a);
35
- static CircArray circ_array_new(void);
36
- static void circ_array_free(CircArray ca);
37
- static void circ_array_set(CircArray ca, VALUE obj, unsigned long id);
38
- static VALUE circ_array_get(CircArray ca, unsigned long id);
39
-
40
- static void debug_stack(PInfo pi, const char *comment);
41
- static void fill_indent(PInfo pi, char *buf, size_t size);
42
-
43
-
44
- struct _parseCallbacks _ox_obj_callbacks = {
45
- instruct, // instruct,
46
- 0, // add_doctype,
47
- 0, // add_comment,
48
- 0, // add_cdata,
19
+ static void instruct(PInfo pi, const char *target, Attr attrs, const char *content);
20
+ static void add_text(PInfo pi, char *text, int closed);
21
+ static void add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren);
22
+ static void end_element(PInfo pi, const char *ename);
23
+
24
+ static VALUE parse_time(const char *text, VALUE clas);
25
+ static VALUE parse_xsd_time(const char *text, VALUE clas);
26
+ static VALUE parse_double_time(const char *text, VALUE clas);
27
+ static VALUE parse_regexp(const char *text);
28
+
29
+ static ID get_var_sym_from_attrs(Attr a, void *encoding);
30
+ static VALUE get_obj_from_attrs(Attr a, PInfo pi, VALUE base_class);
31
+ static VALUE get_class_from_attrs(Attr a, PInfo pi, VALUE base_class);
32
+ static VALUE classname2class(const char *name, PInfo pi, VALUE base_class);
33
+ static unsigned long get_id_from_attrs(PInfo pi, Attr a);
34
+ static CircArray circ_array_new(void);
35
+ static void circ_array_free(CircArray ca);
36
+ static void circ_array_set(CircArray ca, VALUE obj, unsigned long id);
37
+ static VALUE circ_array_get(CircArray ca, unsigned long id);
38
+
39
+ static void debug_stack(PInfo pi, const char *comment);
40
+ static void fill_indent(PInfo pi, char *buf, size_t size);
41
+
42
+ struct _parseCallbacks _ox_obj_callbacks = {
43
+ instruct, // instruct,
44
+ 0, // add_doctype,
45
+ 0, // add_comment,
46
+ 0, // add_cdata,
49
47
  add_text,
50
48
  add_element,
51
49
  end_element,
52
50
  NULL,
53
51
  };
54
52
 
55
- ParseCallbacks ox_obj_callbacks = &_ox_obj_callbacks;
53
+ ParseCallbacks ox_obj_callbacks = &_ox_obj_callbacks;
56
54
 
57
- extern ParseCallbacks ox_gen_callbacks;
55
+ extern ParseCallbacks ox_gen_callbacks;
58
56
 
59
- inline static VALUE
60
- resolve_classname(VALUE mod, const char *class_name, Effort effort, VALUE base_class) {
61
- VALUE clas;
62
- ID ci = rb_intern(class_name);
57
+ inline static VALUE resolve_classname(VALUE mod, const char *class_name, Effort effort, VALUE base_class) {
58
+ VALUE clas;
59
+ ID ci = rb_intern(class_name);
63
60
 
64
61
  switch (effort) {
65
62
  case TolerantEffort:
66
- if (rb_const_defined_at(mod, ci)) {
67
- clas = rb_const_get_at(mod, ci);
68
- } else {
69
- clas = Qundef;
70
- }
71
- break;
63
+ if (rb_const_defined_at(mod, ci)) {
64
+ clas = rb_const_get_at(mod, ci);
65
+ } else {
66
+ clas = Qundef;
67
+ }
68
+ break;
72
69
  case AutoEffort:
73
- if (rb_const_defined_at(mod, ci)) {
74
- clas = rb_const_get_at(mod, ci);
75
- } else {
76
- clas = rb_define_class_under(mod, class_name, base_class);
77
- }
78
- break;
70
+ if (rb_const_defined_at(mod, ci)) {
71
+ clas = rb_const_get_at(mod, ci);
72
+ } else {
73
+ clas = rb_define_class_under(mod, class_name, base_class);
74
+ }
75
+ break;
79
76
  case StrictEffort:
80
77
  default:
81
- // raise an error if name is not defined
82
- clas = rb_const_get_at(mod, ci);
83
- break;
78
+ // raise an error if name is not defined
79
+ clas = rb_const_get_at(mod, ci);
80
+ break;
84
81
  }
85
82
  return clas;
86
83
  }
87
84
 
88
- inline static VALUE
89
- classname2obj(const char *name, PInfo pi, VALUE base_class) {
90
- VALUE clas = classname2class(name, pi, base_class);
85
+ inline static VALUE classname2obj(const char *name, PInfo pi, VALUE base_class) {
86
+ VALUE clas = classname2class(name, pi, base_class);
91
87
 
92
88
  if (Qundef == clas) {
93
- return Qnil;
89
+ return Qnil;
94
90
  } else {
95
- return rb_obj_alloc(clas);
91
+ return rb_obj_alloc(clas);
96
92
  }
97
93
  }
98
94
 
99
- inline static VALUE
100
- structname2obj(const char *name) {
101
- VALUE ost;
102
- const char *s = name;
95
+ inline static VALUE structname2obj(const char *name) {
96
+ VALUE ost;
97
+ const char *s = name;
103
98
 
104
99
  for (; 1; s++) {
105
- if ('\0' == *s) {
106
- s = name;
107
- break;
108
- } else if (':' == *s) {
109
- s += 2;
110
- break;
111
- }
100
+ if ('\0' == *s) {
101
+ s = name;
102
+ break;
103
+ } else if (':' == *s) {
104
+ s += 2;
105
+ break;
106
+ }
112
107
  }
113
108
  ost = rb_const_get(ox_struct_class, rb_intern(s));
114
109
  #if HAVE_RB_STRUCT_ALLOC_NOINIT
@@ -118,403 +113,364 @@ structname2obj(const char *name) {
118
113
  #endif
119
114
  }
120
115
 
121
- inline static VALUE
122
- parse_ulong(const char *s, PInfo pi) {
123
- unsigned long n = 0;
116
+ inline static VALUE parse_ulong(const char *s, PInfo pi) {
117
+ unsigned long n = 0;
124
118
 
125
119
  for (; '\0' != *s; s++) {
126
- if ('0' <= *s && *s <= '9') {
127
- n = n * 10 + (*s - '0');
128
- } else {
129
- set_error(&pi->err, "Invalid number for a julian day", pi->str, pi->s);
130
- return Qundef;
131
- }
120
+ if ('0' <= *s && *s <= '9') {
121
+ n = n * 10 + (*s - '0');
122
+ } else {
123
+ set_error(&pi->err, "Invalid number for a julian day", pi->str, pi->s);
124
+ return Qundef;
125
+ }
132
126
  }
133
127
  return ULONG2NUM(n);
134
128
  }
135
129
 
136
130
  // 2010-07-09T10:47:45.895826162+09:00
137
- inline static VALUE
138
- parse_time(const char *text, VALUE clas) {
139
- VALUE t;
131
+ inline static VALUE parse_time(const char *text, VALUE clas) {
132
+ VALUE t;
140
133
 
141
- if (Qnil == (t = parse_double_time(text, clas)) &&
142
- Qnil == (t = parse_xsd_time(text, clas))) {
143
- VALUE args[1];
134
+ if (Qnil == (t = parse_double_time(text, clas)) && Qnil == (t = parse_xsd_time(text, clas))) {
135
+ VALUE args[1];
144
136
 
145
- *args = rb_str_new2(text);
146
- t = rb_funcall2(ox_time_class, ox_parse_id, 1, args);
137
+ *args = rb_str_new2(text);
138
+ t = rb_funcall2(ox_time_class, ox_parse_id, 1, args);
147
139
  }
148
140
  return t;
149
141
  }
150
142
 
151
- static VALUE
152
- classname2class(const char *name, PInfo pi, VALUE base_class) {
153
- VALUE *slot;
154
- VALUE clas;
143
+ static VALUE classname2class(const char *name, PInfo pi, VALUE base_class) {
144
+ VALUE *slot;
145
+ VALUE clas;
155
146
 
156
147
  if (Qundef == (clas = slot_cache_get(ox_class_cache, name, &slot, 0))) {
157
- char class_name[1024];
158
- char *s;
159
- const char *n = name;
160
-
161
- clas = rb_cObject;
162
- for (s = class_name; '\0' != *n; n++) {
163
- if (':' == *n) {
164
- *s = '\0';
165
- n++;
166
- if (':' != *n) {
167
- set_error(&pi->err, "Invalid classname, expected another ':'", pi->str, pi->s);
168
- return Qundef;
169
- }
170
- if (Qundef == (clas = resolve_classname(clas, class_name, pi->options->effort, base_class))) {
171
- return Qundef;
172
- }
173
- s = class_name;
174
- } else {
175
- *s++ = *n;
176
- }
177
- }
178
- *s = '\0';
179
- if (Qundef != (clas = resolve_classname(clas, class_name, pi->options->effort, base_class))) {
180
- *slot = clas;
181
- rb_gc_register_address(slot);
182
- }
148
+ char class_name[1024];
149
+ char *s;
150
+ const char *n = name;
151
+
152
+ clas = rb_cObject;
153
+ for (s = class_name; '\0' != *n; n++) {
154
+ if (':' == *n) {
155
+ *s = '\0';
156
+ n++;
157
+ if (':' != *n) {
158
+ set_error(&pi->err, "Invalid classname, expected another ':'", pi->str, pi->s);
159
+ return Qundef;
160
+ }
161
+ if (Qundef == (clas = resolve_classname(clas, class_name, pi->options->effort, base_class))) {
162
+ return Qundef;
163
+ }
164
+ s = class_name;
165
+ } else {
166
+ *s++ = *n;
167
+ }
168
+ }
169
+ *s = '\0';
170
+ if (Qundef != (clas = resolve_classname(clas, class_name, pi->options->effort, base_class))) {
171
+ *slot = clas;
172
+ rb_gc_register_address(slot);
173
+ }
183
174
  }
184
175
  return clas;
185
176
  }
186
177
 
187
- static ID
188
- get_var_sym_from_attrs(Attr a, void *encoding) {
178
+ static ID get_var_sym_from_attrs(Attr a, void *encoding) {
189
179
  for (; 0 != a->name; a++) {
190
- if ('a' == *a->name && '\0' == *(a->name + 1)) {
191
- const char *val = a->value;
180
+ if ('a' == *a->name && '\0' == *(a->name + 1)) {
181
+ const char *val = a->value;
192
182
 
193
- if ('0' <= *val && *val <= '9') {
194
- return INT2NUM(atoi(val));
195
- }
196
- return ox_id_intern(val, strlen(val));
197
- }
183
+ if ('0' <= *val && *val <= '9') {
184
+ return INT2NUM(atoi(val));
185
+ }
186
+ return ox_id_intern(val, strlen(val));
187
+ }
198
188
  }
199
189
  return 0;
200
190
  }
201
191
 
202
- static VALUE
203
- get_obj_from_attrs(Attr a, PInfo pi, VALUE base_class) {
192
+ static VALUE get_obj_from_attrs(Attr a, PInfo pi, VALUE base_class) {
204
193
  for (; 0 != a->name; a++) {
205
- if ('c' == *a->name && '\0' == *(a->name + 1)) {
206
- return classname2obj(a->value, pi, base_class);
207
- }
194
+ if ('c' == *a->name && '\0' == *(a->name + 1)) {
195
+ return classname2obj(a->value, pi, base_class);
196
+ }
208
197
  }
209
198
  return Qundef;
210
199
  }
211
200
 
212
- static VALUE
213
- get_struct_from_attrs(Attr a) {
201
+ static VALUE get_struct_from_attrs(Attr a) {
214
202
  for (; 0 != a->name; a++) {
215
- if ('c' == *a->name && '\0' == *(a->name + 1)) {
216
- return structname2obj(a->value);
217
- }
203
+ if ('c' == *a->name && '\0' == *(a->name + 1)) {
204
+ return structname2obj(a->value);
205
+ }
218
206
  }
219
207
  return Qundef;
220
208
  }
221
209
 
222
- static VALUE
223
- get_class_from_attrs(Attr a, PInfo pi, VALUE base_class) {
210
+ static VALUE get_class_from_attrs(Attr a, PInfo pi, VALUE base_class) {
224
211
  for (; 0 != a->name; a++) {
225
- if ('c' == *a->name && '\0' == *(a->name + 1)) {
226
- return classname2class(a->value, pi, base_class);
227
- }
212
+ if ('c' == *a->name && '\0' == *(a->name + 1)) {
213
+ return classname2class(a->value, pi, base_class);
214
+ }
228
215
  }
229
216
  return Qundef;
230
217
  }
231
218
 
232
- static unsigned long
233
- get_id_from_attrs(PInfo pi, Attr a) {
219
+ static unsigned long get_id_from_attrs(PInfo pi, Attr a) {
234
220
  for (; 0 != a->name; a++) {
235
- if ('i' == *a->name && '\0' == *(a->name + 1)) {
236
- unsigned long id = 0;
237
- const char *text = a->value;
238
- char c;
239
-
240
- for (; '\0' != *text; text++) {
241
- c = *text;
242
- if ('0' <= c && c <= '9') {
243
- id = id * 10 + (c - '0');
244
- } else {
245
- set_error(&pi->err, "bad number format", pi->str, pi->s);
246
- return 0;
247
- }
248
- }
249
- return id;
250
- }
221
+ if ('i' == *a->name && '\0' == *(a->name + 1)) {
222
+ unsigned long id = 0;
223
+ const char *text = a->value;
224
+ char c;
225
+
226
+ for (; '\0' != *text; text++) {
227
+ c = *text;
228
+ if ('0' <= c && c <= '9') {
229
+ id = id * 10 + (c - '0');
230
+ } else {
231
+ set_error(&pi->err, "bad number format", pi->str, pi->s);
232
+ return 0;
233
+ }
234
+ }
235
+ return id;
236
+ }
251
237
  }
252
238
  return 0;
253
239
  }
254
240
 
255
- static CircArray
256
- circ_array_new() {
257
- CircArray ca;
241
+ static CircArray circ_array_new() {
242
+ CircArray ca;
258
243
 
259
- ca = ALLOC(struct _circArray);
244
+ ca = ALLOC(struct _circArray);
260
245
  ca->objs = ca->obj_array;
261
246
  ca->size = sizeof(ca->obj_array) / sizeof(VALUE);
262
- ca->cnt = 0;
247
+ ca->cnt = 0;
263
248
 
264
249
  return ca;
265
250
  }
266
251
 
267
- static void
268
- circ_array_free(CircArray ca) {
252
+ static void circ_array_free(CircArray ca) {
269
253
  if (ca->objs != ca->obj_array) {
270
- xfree(ca->objs);
254
+ xfree(ca->objs);
271
255
  }
272
256
  xfree(ca);
273
257
  }
274
258
 
275
- static void
276
- circ_array_set(CircArray ca, VALUE obj, unsigned long id) {
259
+ static void circ_array_set(CircArray ca, VALUE obj, unsigned long id) {
277
260
  if (0 < id) {
278
- unsigned long i;
279
-
280
- if (ca->size < id) {
281
- unsigned long cnt = id + 512;
282
-
283
- if (ca->objs == ca->obj_array) {
284
- ca->objs = ALLOC_N(VALUE, cnt);
285
- memcpy(ca->objs, ca->obj_array, sizeof(VALUE) * ca->cnt);
286
- } else {
287
- REALLOC_N(ca->objs, VALUE, cnt);
288
- }
289
- ca->size = cnt;
290
- }
291
- id--;
292
- for (i = ca->cnt; i < id; i++) {
293
- ca->objs[i] = Qundef;
294
- }
295
- ca->objs[id] = obj;
296
- if (ca->cnt <= id) {
297
- ca->cnt = id + 1;
298
- }
261
+ unsigned long i;
262
+
263
+ if (ca->size < id) {
264
+ unsigned long cnt = id + 512;
265
+
266
+ if (ca->objs == ca->obj_array) {
267
+ ca->objs = ALLOC_N(VALUE, cnt);
268
+ memcpy(ca->objs, ca->obj_array, sizeof(VALUE) * ca->cnt);
269
+ } else {
270
+ REALLOC_N(ca->objs, VALUE, cnt);
271
+ }
272
+ ca->size = cnt;
273
+ }
274
+ id--;
275
+ for (i = ca->cnt; i < id; i++) {
276
+ ca->objs[i] = Qundef;
277
+ }
278
+ ca->objs[id] = obj;
279
+ if (ca->cnt <= id) {
280
+ ca->cnt = id + 1;
281
+ }
299
282
  }
300
283
  }
301
284
 
302
- static VALUE
303
- circ_array_get(CircArray ca, unsigned long id) {
304
- VALUE obj = Qundef;
285
+ static VALUE circ_array_get(CircArray ca, unsigned long id) {
286
+ VALUE obj = Qundef;
305
287
 
306
288
  if (id <= ca->cnt) {
307
- obj = ca->objs[id - 1];
289
+ obj = ca->objs[id - 1];
308
290
  }
309
291
  return obj;
310
292
  }
311
293
 
312
- static VALUE
313
- parse_regexp(const char *text) {
314
- const char *te;
315
- int options = 0;
294
+ static VALUE parse_regexp(const char *text) {
295
+ const char *te;
296
+ int options = 0;
316
297
 
317
298
  te = text + strlen(text) - 1;
318
299
  #ifdef ONIG_OPTION_IGNORECASE
319
300
  for (; text < te && '/' != *te; te--) {
320
- switch (*te) {
321
- case 'i': options |= ONIG_OPTION_IGNORECASE; break;
322
- case 'm': options |= ONIG_OPTION_MULTILINE; break;
323
- case 'x': options |= ONIG_OPTION_EXTEND; break;
324
- default: break;
325
- }
301
+ switch (*te) {
302
+ case 'i': options |= ONIG_OPTION_IGNORECASE; break;
303
+ case 'm': options |= ONIG_OPTION_MULTILINE; break;
304
+ case 'x': options |= ONIG_OPTION_EXTEND; break;
305
+ default: break;
306
+ }
326
307
  }
327
308
  #endif
328
309
  return rb_reg_new(text + 1, te - text - 1, options);
329
310
  }
330
311
 
331
- static void
332
- instruct(PInfo pi, const char *target, Attr attrs, const char *content) {
312
+ static void instruct(PInfo pi, const char *target, Attr attrs, const char *content) {
333
313
  if (0 == strcmp("xml", target)) {
334
- for (; 0 != attrs->name; attrs++) {
335
- if (0 == strcmp("encoding", attrs->name)) {
336
- pi->options->rb_enc = rb_enc_find(attrs->value);
337
- }
338
- }
314
+ for (; 0 != attrs->name; attrs++) {
315
+ if (0 == strcmp("encoding", attrs->name)) {
316
+ pi->options->rb_enc = rb_enc_find(attrs->value);
317
+ }
318
+ }
339
319
  }
340
320
  }
341
321
 
342
- static void
343
- add_text(PInfo pi, char *text, int closed) {
344
- Helper h = helper_stack_peek(&pi->helpers);
322
+ static void add_text(PInfo pi, char *text, int closed) {
323
+ Helper h = helper_stack_peek(&pi->helpers);
345
324
 
346
325
  if (!closed) {
347
- set_error(&pi->err, "Text not closed", pi->str, pi->s);
348
- return;
326
+ set_error(&pi->err, "Text not closed", pi->str, pi->s);
327
+ return;
349
328
  }
350
329
  if (0 == h) {
351
- set_error(&pi->err, "Unexpected text", pi->str, pi->s);
352
- return;
330
+ set_error(&pi->err, "Unexpected text", pi->str, pi->s);
331
+ return;
353
332
  }
354
333
  if (DEBUG <= pi->options->trace) {
355
- char indent[128];
334
+ char indent[128];
356
335
 
357
- fill_indent(pi, indent, sizeof(indent));
358
- printf("%s '%s' to type %c\n", indent, text, h->type);
336
+ fill_indent(pi, indent, sizeof(indent));
337
+ printf("%s '%s' to type %c\n", indent, text, h->type);
359
338
  }
360
339
  switch (h->type) {
361
340
  case NoCode:
362
341
  case StringCode:
363
- h->obj = rb_str_new2(text);
364
- if (0 != pi->options->rb_enc) {
365
- rb_enc_associate(h->obj, pi->options->rb_enc);
366
- }
367
- if (0 != pi->circ_array) {
368
- circ_array_set(pi->circ_array, h->obj, (unsigned long)pi->id);
369
- }
370
- break;
371
- case FixnumCode:
372
- {
373
- long n = 0;
374
- char c;
375
- int neg = 0;
376
-
377
- if ('-' == *text) {
378
- neg = 1;
379
- text++;
380
- }
381
- for (; '\0' != *text; text++) {
382
- c = *text;
383
- if ('0' <= c && c <= '9') {
384
- n = n * 10 + (c - '0');
385
- } else {
386
- set_error(&pi->err, "bad number format", pi->str, pi->s);
387
- return;
388
- }
389
- }
390
- if (neg) {
391
- n = -n;
392
- }
393
- h->obj = LONG2NUM(n);
394
- break;
395
- }
396
- case FloatCode:
397
- h->obj = rb_float_new(strtod(text, 0));
398
- break;
399
- case SymbolCode:
400
- h->obj = ox_sym_intern(text, strlen(text), NULL);
401
- break;
402
- case DateCode:
403
- {
404
- VALUE args[1];
405
-
406
- if (Qundef == (*args = parse_ulong(text, pi))) {
407
- return;
408
- }
409
- h->obj = rb_funcall2(ox_date_class, ox_jd_id, 1, args);
410
- break;
411
- }
412
- case TimeCode:
413
- h->obj = parse_time(text, ox_time_class);
414
- break;
415
- case String64Code:
416
- {
417
- unsigned long str_size = b64_orig_size(text);
418
- VALUE v;
419
- char *str = ALLOCA_N(char, str_size + 1);
420
-
421
- from_base64(text, (uchar*)str);
422
- v = rb_str_new(str, str_size);
423
- if (0 != pi->options->rb_enc) {
424
- rb_enc_associate(v, pi->options->rb_enc);
425
- }
426
- if (0 != pi->circ_array) {
427
- circ_array_set(pi->circ_array, v, (unsigned long)h->obj);
428
- }
429
- h->obj = v;
430
- break;
431
- }
432
- case Symbol64Code:
433
- {
434
- unsigned long str_size = b64_orig_size(text);
435
- char *str = ALLOCA_N(char, str_size + 1);
436
-
437
- from_base64(text, (uchar*)str);
438
- h->obj = ox_sym_intern(str, strlen(str), NULL);
439
- break;
342
+ h->obj = rb_str_new2(text);
343
+ if (0 != pi->options->rb_enc) {
344
+ rb_enc_associate(h->obj, pi->options->rb_enc);
345
+ }
346
+ if (0 != pi->circ_array) {
347
+ circ_array_set(pi->circ_array, h->obj, (unsigned long)pi->id);
348
+ }
349
+ break;
350
+ case FixnumCode: {
351
+ long n = 0;
352
+ char c;
353
+ int neg = 0;
354
+
355
+ if ('-' == *text) {
356
+ neg = 1;
357
+ text++;
358
+ }
359
+ for (; '\0' != *text; text++) {
360
+ c = *text;
361
+ if ('0' <= c && c <= '9') {
362
+ n = n * 10 + (c - '0');
363
+ } else {
364
+ set_error(&pi->err, "bad number format", pi->str, pi->s);
365
+ return;
366
+ }
367
+ }
368
+ if (neg) {
369
+ n = -n;
370
+ }
371
+ h->obj = LONG2NUM(n);
372
+ break;
373
+ }
374
+ case FloatCode: h->obj = rb_float_new(strtod(text, 0)); break;
375
+ case SymbolCode: h->obj = ox_sym_intern(text, strlen(text), NULL); break;
376
+ case DateCode: {
377
+ VALUE args[1];
378
+
379
+ if (Qundef == (*args = parse_ulong(text, pi))) {
380
+ return;
381
+ }
382
+ h->obj = rb_funcall2(ox_date_class, ox_jd_id, 1, args);
383
+ break;
384
+ }
385
+ case TimeCode: h->obj = parse_time(text, ox_time_class); break;
386
+ case String64Code: {
387
+ unsigned long str_size = b64_orig_size(text);
388
+ VALUE v;
389
+ char *str = ALLOCA_N(char, str_size + 1);
390
+
391
+ from_base64(text, (uchar *)str);
392
+ v = rb_str_new(str, str_size);
393
+ if (0 != pi->options->rb_enc) {
394
+ rb_enc_associate(v, pi->options->rb_enc);
395
+ }
396
+ if (0 != pi->circ_array) {
397
+ circ_array_set(pi->circ_array, v, (unsigned long)h->obj);
398
+ }
399
+ h->obj = v;
400
+ break;
401
+ }
402
+ case Symbol64Code: {
403
+ unsigned long str_size = b64_orig_size(text);
404
+ char *str = ALLOCA_N(char, str_size + 1);
405
+
406
+ from_base64(text, (uchar *)str);
407
+ h->obj = ox_sym_intern(str, strlen(str), NULL);
408
+ break;
440
409
  }
441
410
  case RegexpCode:
442
- if ('/' == *text) {
443
- h->obj = parse_regexp(text);
444
- } else {
445
- unsigned long str_size = b64_orig_size(text);
446
- char *str = ALLOCA_N(char, str_size + 1);
447
-
448
- from_base64(text, (uchar*)str);
449
- h->obj = parse_regexp(str);
450
- }
451
- break;
452
- case BignumCode:
453
- h->obj = rb_cstr_to_inum(text, 10, 1);
454
- break;
455
- case BigDecimalCode:
456
- h->obj = rb_funcall(rb_cObject, ox_bigdecimal_id, 1, rb_str_new2(text));
457
- break;
458
- default:
459
- h->obj = Qnil;
460
- break;
411
+ if ('/' == *text) {
412
+ h->obj = parse_regexp(text);
413
+ } else {
414
+ unsigned long str_size = b64_orig_size(text);
415
+ char *str = ALLOCA_N(char, str_size + 1);
416
+
417
+ from_base64(text, (uchar *)str);
418
+ h->obj = parse_regexp(str);
419
+ }
420
+ break;
421
+ case BignumCode: h->obj = rb_cstr_to_inum(text, 10, 1); break;
422
+ case BigDecimalCode: h->obj = rb_funcall(rb_cObject, ox_bigdecimal_id, 1, rb_str_new2(text)); break;
423
+ default: h->obj = Qnil; break;
461
424
  }
462
425
  }
463
426
 
464
- static void
465
- add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
466
- Attr a;
467
- Helper h;
468
- unsigned long id;
427
+ static void add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
428
+ Attr a;
429
+ Helper h;
430
+ unsigned long id;
469
431
 
470
432
  if (TRACE <= pi->options->trace) {
471
- char buf[1024];
472
- char indent[128];
473
- char *s = buf;
474
- char *end = buf + sizeof(buf) - 2;
475
-
476
- s += snprintf(s, end - s, " <%s%s", (hasChildren) ? "" : "/", ename);
477
- for (a = attrs; 0 != a->name; a++) {
478
- s += snprintf(s, end - s, " %s=%s", a->name, a->value);
479
- }
480
- *s++ = '>';
481
- *s++ = '\0';
482
- if (DEBUG <= pi->options->trace) {
483
- printf("===== add element stack(%d) =====\n", helper_stack_depth(&pi->helpers));
484
- debug_stack(pi, buf);
485
- } else {
486
- fill_indent(pi, indent, sizeof(indent));
487
- printf("%s%s\n", indent, buf);
488
- }
489
- }
490
- if (helper_stack_empty(&pi->helpers)) { // top level object
491
- if (0 != (id = get_id_from_attrs(pi, attrs))) {
492
- pi->circ_array = circ_array_new();
493
- }
433
+ char buf[1024];
434
+ char indent[128];
435
+ char *s = buf;
436
+ char *end = buf + sizeof(buf) - 2;
437
+
438
+ s += snprintf(s, end - s, " <%s%s", (hasChildren) ? "" : "/", ename);
439
+ for (a = attrs; 0 != a->name; a++) {
440
+ s += snprintf(s, end - s, " %s=%s", a->name, a->value);
441
+ }
442
+ *s++ = '>';
443
+ *s++ = '\0';
444
+ if (DEBUG <= pi->options->trace) {
445
+ printf("===== add element stack(%d) =====\n", helper_stack_depth(&pi->helpers));
446
+ debug_stack(pi, buf);
447
+ } else {
448
+ fill_indent(pi, indent, sizeof(indent));
449
+ printf("%s%s\n", indent, buf);
450
+ }
451
+ }
452
+ if (helper_stack_empty(&pi->helpers)) { // top level object
453
+ if (0 != (id = get_id_from_attrs(pi, attrs))) {
454
+ pi->circ_array = circ_array_new();
455
+ }
494
456
  }
495
457
  if ('\0' != ename[1]) {
496
- set_error(&pi->err, "Invalid element name", pi->str, pi->s);
497
- return;
458
+ set_error(&pi->err, "Invalid element name", pi->str, pi->s);
459
+ return;
498
460
  }
499
- h = helper_stack_push(&pi->helpers, get_var_sym_from_attrs(attrs, (void*)pi->options->rb_enc), Qundef, *ename);
461
+ h = helper_stack_push(&pi->helpers, get_var_sym_from_attrs(attrs, (void *)pi->options->rb_enc), Qundef, *ename);
500
462
  switch (h->type) {
501
- case NilClassCode:
502
- h->obj = Qnil;
503
- break;
504
- case TrueClassCode:
505
- h->obj = Qtrue;
506
- break;
507
- case FalseClassCode:
508
- h->obj = Qfalse;
509
- break;
463
+ case NilClassCode: h->obj = Qnil; break;
464
+ case TrueClassCode: h->obj = Qtrue; break;
465
+ case FalseClassCode: h->obj = Qfalse; break;
510
466
  case StringCode:
511
- // h->obj will be replaced by add_text if it is called
512
- h->obj = ox_empty_string;
513
- if (0 != pi->circ_array) {
514
- pi->id = get_id_from_attrs(pi, attrs);
515
- circ_array_set(pi->circ_array, h->obj, pi->id);
516
- }
517
- break;
467
+ // h->obj will be replaced by add_text if it is called
468
+ h->obj = ox_empty_string;
469
+ if (0 != pi->circ_array) {
470
+ pi->id = get_id_from_attrs(pi, attrs);
471
+ circ_array_set(pi->circ_array, h->obj, pi->id);
472
+ }
473
+ break;
518
474
  case FixnumCode:
519
475
  case FloatCode:
520
476
  case SymbolCode:
@@ -525,251 +481,243 @@ add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
525
481
  case ComplexCode:
526
482
  case DateCode:
527
483
  case TimeCode:
528
- case RationalCode: // sub elements read next
529
- // value will be read in the following add_text
530
- h->obj = Qundef;
531
- break;
484
+ case RationalCode: // sub elements read next
485
+ // value will be read in the following add_text
486
+ h->obj = Qundef;
487
+ break;
532
488
  case String64Code:
533
- h->obj = Qundef;
534
- if (0 != pi->circ_array) {
535
- pi->id = get_id_from_attrs(pi, attrs);
536
- }
537
- break;
489
+ h->obj = Qundef;
490
+ if (0 != pi->circ_array) {
491
+ pi->id = get_id_from_attrs(pi, attrs);
492
+ }
493
+ break;
538
494
  case ArrayCode:
539
- h->obj = rb_ary_new();
540
- if (0 != pi->circ_array) {
541
- circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
542
- }
543
- break;
495
+ h->obj = rb_ary_new();
496
+ if (0 != pi->circ_array) {
497
+ circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
498
+ }
499
+ break;
544
500
  case HashCode:
545
- h->obj = rb_hash_new();
546
- if (0 != pi->circ_array) {
547
- circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
548
- }
549
- break;
550
- case RangeCode:
551
- h->obj = rb_ary_new_from_args(3, Qnil, Qnil, Qfalse);
552
- break;
501
+ h->obj = rb_hash_new();
502
+ if (0 != pi->circ_array) {
503
+ circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
504
+ }
505
+ break;
506
+ case RangeCode: h->obj = rb_ary_new_from_args(3, Qnil, Qnil, Qfalse); break;
553
507
  case RawCode:
554
- if (hasChildren) {
555
- h->obj = ox_parse(pi->s, pi->end - pi->s, ox_gen_callbacks, &pi->s, pi->options, &pi->err);
556
- if (0 != pi->circ_array) {
557
- circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
558
- }
559
- } else {
560
- h->obj = Qnil;
561
- }
562
- break;
508
+ if (hasChildren) {
509
+ h->obj = ox_parse(pi->s, pi->end - pi->s, ox_gen_callbacks, &pi->s, pi->options, &pi->err);
510
+ if (0 != pi->circ_array) {
511
+ circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
512
+ }
513
+ } else {
514
+ h->obj = Qnil;
515
+ }
516
+ break;
563
517
  case ExceptionCode:
564
- if (Qundef == (h->obj = get_obj_from_attrs(attrs, pi, rb_eException))) {
565
- return;
566
- }
567
- if (0 != pi->circ_array && Qnil != h->obj) {
568
- circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
569
- }
570
- break;
518
+ if (Qundef == (h->obj = get_obj_from_attrs(attrs, pi, rb_eException))) {
519
+ return;
520
+ }
521
+ if (0 != pi->circ_array && Qnil != h->obj) {
522
+ circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
523
+ }
524
+ break;
571
525
  case ObjectCode:
572
- if (Qundef == (h->obj = get_obj_from_attrs(attrs, pi, ox_bag_clas))) {
573
- return;
574
- }
575
- if (0 != pi->circ_array && Qnil != h->obj) {
576
- circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
577
- }
578
- break;
526
+ if (Qundef == (h->obj = get_obj_from_attrs(attrs, pi, ox_bag_clas))) {
527
+ return;
528
+ }
529
+ if (0 != pi->circ_array && Qnil != h->obj) {
530
+ circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
531
+ }
532
+ break;
579
533
  case StructCode:
580
- h->obj = get_struct_from_attrs(attrs);
581
- if (0 != pi->circ_array) {
582
- circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
583
- }
584
- break;
534
+ h->obj = get_struct_from_attrs(attrs);
535
+ if (0 != pi->circ_array) {
536
+ circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
537
+ }
538
+ break;
585
539
  case ClassCode:
586
- if (Qundef == (h->obj = get_class_from_attrs(attrs, pi, ox_bag_clas))) {
587
- return;
588
- }
589
- break;
540
+ if (Qundef == (h->obj = get_class_from_attrs(attrs, pi, ox_bag_clas))) {
541
+ return;
542
+ }
543
+ break;
590
544
  case RefCode:
591
- h->obj = Qundef;
592
- if (0 != pi->circ_array) {
593
- h->obj = circ_array_get(pi->circ_array, get_id_from_attrs(pi, attrs));
594
- }
595
- if (Qundef == h->obj) {
596
- set_error(&pi->err, "Invalid circular reference", pi->str, pi->s);
597
- return;
598
- }
599
- break;
545
+ h->obj = Qundef;
546
+ if (0 != pi->circ_array) {
547
+ h->obj = circ_array_get(pi->circ_array, get_id_from_attrs(pi, attrs));
548
+ }
549
+ if (Qundef == h->obj) {
550
+ set_error(&pi->err, "Invalid circular reference", pi->str, pi->s);
551
+ return;
552
+ }
553
+ break;
600
554
  default:
601
- set_error(&pi->err, "Invalid element name", pi->str, pi->s);
602
- return;
603
- break;
555
+ set_error(&pi->err, "Invalid element name", pi->str, pi->s);
556
+ return;
557
+ break;
604
558
  }
605
559
  if (DEBUG <= pi->options->trace) {
606
- debug_stack(pi, " -----------");
560
+ debug_stack(pi, " -----------");
607
561
  }
608
562
  }
609
563
 
610
- static void
611
- end_element(PInfo pi, const char *ename) {
564
+ static void end_element(PInfo pi, const char *ename) {
612
565
  if (TRACE <= pi->options->trace) {
613
- char indent[128];
566
+ char indent[128];
614
567
 
615
- if (DEBUG <= pi->options->trace) {
616
- char buf[1024];
568
+ if (DEBUG <= pi->options->trace) {
569
+ char buf[1024];
617
570
 
618
- printf("===== end element stack(%d) =====\n", helper_stack_depth(&pi->helpers));
619
- snprintf(buf, sizeof(buf) - 1, "</%s>", ename);
620
- debug_stack(pi, buf);
621
- } else {
622
- fill_indent(pi, indent, sizeof(indent));
623
- printf("%s</%s>\n", indent, ename);
624
- }
571
+ printf("===== end element stack(%d) =====\n", helper_stack_depth(&pi->helpers));
572
+ snprintf(buf, sizeof(buf) - 1, "</%s>", ename);
573
+ debug_stack(pi, buf);
574
+ } else {
575
+ fill_indent(pi, indent, sizeof(indent));
576
+ printf("%s</%s>\n", indent, ename);
577
+ }
625
578
  }
626
579
  if (!helper_stack_empty(&pi->helpers)) {
627
- Helper h = helper_stack_pop(&pi->helpers);
628
- Helper ph = helper_stack_peek(&pi->helpers);
629
-
630
- if (ox_empty_string == h->obj) {
631
- // special catch for empty strings
632
- h->obj = rb_str_new2("");
633
- } else if (Qundef == h->obj) {
634
- set_error(&pi->err, "Invalid element for object mode", pi->str, pi->s);
635
- return;
636
- } else if (RangeCode == h->type) { // Expect an array of 3 elements.
637
- const VALUE *ap = RARRAY_PTR(h->obj);
638
-
639
- h->obj = rb_range_new(*ap, *(ap + 1), Qtrue == *(ap + 2));
640
- }
641
- pi->obj = h->obj;
642
- if (0 != ph) {
643
- switch (ph->type) {
644
- case ArrayCode:
645
- rb_ary_push(ph->obj, h->obj);
646
- break;
647
- case ExceptionCode:
648
- case ObjectCode:
649
- if (Qnil != ph->obj) {
650
- if (0 == h->var || NULL == rb_id2name(h->var )) {
651
- set_error(&pi->err, "Invalid element for object mode", pi->str, pi->s);
652
- return;
653
- }
654
- if (RUBY_T_OBJECT != rb_type(ph->obj)) {
655
- set_error(&pi->err, "Corrupt object encoding", pi->str, pi->s);
656
- return;
657
- }
658
- rb_ivar_set(ph->obj, h->var, h->obj);
659
- }
660
- break;
661
- case StructCode:
662
- if (0 == h->var) {
663
- set_error(&pi->err, "Invalid element for object mode", pi->str, pi->s);
664
- return;
665
- }
666
- rb_struct_aset(ph->obj, h->var, h->obj);
667
- break;
668
- case HashCode:
669
- // put back h
670
- helper_stack_push(&pi->helpers, h->var, h->obj, KeyCode);
671
- break;
672
- case RangeCode:
673
- if (ox_beg_id == h->var) {
674
- rb_ary_store(ph->obj, 0, h->obj);
675
- } else if (ox_end_id == h->var) {
676
- rb_ary_store(ph->obj, 1, h->obj);
677
- } else if (ox_excl_id == h->var) {
678
- rb_ary_store(ph->obj, 2, h->obj);
679
- } else {
680
- set_error(&pi->err, "Invalid range attribute", pi->str, pi->s);
681
- return;
682
- }
683
- break;
684
- case KeyCode:
685
- {
686
- Helper gh;
687
-
688
- helper_stack_pop(&pi->helpers);
689
- if (NULL == (gh = helper_stack_peek(&pi->helpers)) || Qundef == ph->obj || Qundef == h->obj) {
690
- set_error(&pi->err, "Corrupt parse stack, container is wrong type", pi->str, pi->s);
691
- return;
692
- }
693
- rb_hash_aset(gh->obj, ph->obj, h->obj);
694
- }
695
- break;
696
- case ComplexCode:
580
+ Helper h = helper_stack_pop(&pi->helpers);
581
+ Helper ph = helper_stack_peek(&pi->helpers);
582
+
583
+ if (ox_empty_string == h->obj) {
584
+ // special catch for empty strings
585
+ h->obj = rb_str_new2("");
586
+ } else if (Qundef == h->obj) {
587
+ set_error(&pi->err, "Invalid element for object mode", pi->str, pi->s);
588
+ return;
589
+ } else if (RangeCode == h->type) { // Expect an array of 3 elements.
590
+ const VALUE *ap = RARRAY_PTR(h->obj);
591
+
592
+ h->obj = rb_range_new(*ap, *(ap + 1), Qtrue == *(ap + 2));
593
+ }
594
+ pi->obj = h->obj;
595
+ if (0 != ph) {
596
+ switch (ph->type) {
597
+ case ArrayCode: rb_ary_push(ph->obj, h->obj); break;
598
+ case ExceptionCode:
599
+ case ObjectCode:
600
+ if (Qnil != ph->obj) {
601
+ if (0 == h->var || NULL == rb_id2name(h->var)) {
602
+ set_error(&pi->err, "Invalid element for object mode", pi->str, pi->s);
603
+ return;
604
+ }
605
+ if (RUBY_T_OBJECT != rb_type(ph->obj)) {
606
+ set_error(&pi->err, "Corrupt object encoding", pi->str, pi->s);
607
+ return;
608
+ }
609
+ rb_ivar_set(ph->obj, h->var, h->obj);
610
+ }
611
+ break;
612
+ case StructCode:
613
+ if (0 == h->var) {
614
+ set_error(&pi->err, "Invalid element for object mode", pi->str, pi->s);
615
+ return;
616
+ }
617
+ rb_struct_aset(ph->obj, h->var, h->obj);
618
+ break;
619
+ case HashCode:
620
+ // put back h
621
+ helper_stack_push(&pi->helpers, h->var, h->obj, KeyCode);
622
+ break;
623
+ case RangeCode:
624
+ if (ox_beg_id == h->var) {
625
+ rb_ary_store(ph->obj, 0, h->obj);
626
+ } else if (ox_end_id == h->var) {
627
+ rb_ary_store(ph->obj, 1, h->obj);
628
+ } else if (ox_excl_id == h->var) {
629
+ rb_ary_store(ph->obj, 2, h->obj);
630
+ } else {
631
+ set_error(&pi->err, "Invalid range attribute", pi->str, pi->s);
632
+ return;
633
+ }
634
+ break;
635
+ case KeyCode: {
636
+ Helper gh;
637
+
638
+ helper_stack_pop(&pi->helpers);
639
+ if (NULL == (gh = helper_stack_peek(&pi->helpers)) || Qundef == ph->obj || Qundef == h->obj) {
640
+ set_error(&pi->err, "Corrupt parse stack, container is wrong type", pi->str, pi->s);
641
+ return;
642
+ }
643
+ rb_hash_aset(gh->obj, ph->obj, h->obj);
644
+ } break;
645
+ case ComplexCode:
697
646
  #ifdef T_COMPLEX
698
- if (Qundef == ph->obj) {
699
- ph->obj = h->obj;
700
- } else {
701
- ph->obj = rb_complex_new(ph->obj, h->obj);
702
- }
647
+ if (Qundef == ph->obj) {
648
+ ph->obj = h->obj;
649
+ } else {
650
+ ph->obj = rb_complex_new(ph->obj, h->obj);
651
+ }
703
652
  #else
704
- set_error(&pi->err, "Complex Objects not implemented in Ruby 1.8.7", pi->str, pi->s);
705
- return;
653
+ set_error(&pi->err, "Complex Objects not implemented in Ruby 1.8.7", pi->str, pi->s);
654
+ return;
706
655
  #endif
707
- break;
708
- case RationalCode: {
709
- if (Qundef == h->obj || RUBY_T_FIXNUM != rb_type(h->obj)) {
710
- set_error(&pi->err, "Invalid object format", pi->str, pi->s);
711
- return;
712
- }
656
+ break;
657
+ case RationalCode: {
658
+ if (Qundef == h->obj || RUBY_T_FIXNUM != rb_type(h->obj)) {
659
+ set_error(&pi->err, "Invalid object format", pi->str, pi->s);
660
+ return;
661
+ }
713
662
  #ifdef T_RATIONAL
714
- if (Qundef == ph->obj) {
715
- ph->obj = h->obj;
716
- } else {
717
- if (Qundef == ph->obj || RUBY_T_FIXNUM != rb_type(h->obj)) {
718
- set_error(&pi->err, "Corrupt parse stack, container is wrong type", pi->str, pi->s);
719
- return;
720
- }
663
+ if (Qundef == ph->obj) {
664
+ ph->obj = h->obj;
665
+ } else {
666
+ if (Qundef == ph->obj || RUBY_T_FIXNUM != rb_type(h->obj)) {
667
+ set_error(&pi->err, "Corrupt parse stack, container is wrong type", pi->str, pi->s);
668
+ return;
669
+ }
721
670
  #ifdef RUBINIUS_RUBY
722
- ph->obj = rb_Rational(ph->obj, h->obj);
671
+ ph->obj = rb_Rational(ph->obj, h->obj);
723
672
  #else
724
- ph->obj = rb_rational_new(ph->obj, h->obj);
673
+ ph->obj = rb_rational_new(ph->obj, h->obj);
725
674
  #endif
726
- }
675
+ }
727
676
  #else
728
- set_error(&pi->err, "Rational Objects not implemented in Ruby 1.8.7", pi->str, pi->s);
729
- return;
677
+ set_error(&pi->err, "Rational Objects not implemented in Ruby 1.8.7", pi->str, pi->s);
678
+ return;
730
679
  #endif
731
- break;
732
- }
733
- default:
734
- set_error(&pi->err, "Corrupt parse stack, container is wrong type", pi->str, pi->s);
735
- return;
736
- break;
737
- }
738
- }
680
+ break;
681
+ }
682
+ default:
683
+ set_error(&pi->err, "Corrupt parse stack, container is wrong type", pi->str, pi->s);
684
+ return;
685
+ break;
686
+ }
687
+ }
739
688
  }
740
689
  if (0 != pi->circ_array && helper_stack_empty(&pi->helpers)) {
741
- circ_array_free(pi->circ_array);
742
- pi->circ_array = 0;
690
+ circ_array_free(pi->circ_array);
691
+ pi->circ_array = 0;
743
692
  }
744
693
  if (DEBUG <= pi->options->trace) {
745
- debug_stack(pi, " ----------");
694
+ debug_stack(pi, " ----------");
746
695
  }
747
696
  }
748
697
 
749
- static VALUE
750
- parse_double_time(const char *text, VALUE clas) {
751
- long v = 0;
752
- long v2 = 0;
753
- const char *dot = 0;
754
- char c;
698
+ static VALUE parse_double_time(const char *text, VALUE clas) {
699
+ long v = 0;
700
+ long v2 = 0;
701
+ const char *dot = 0;
702
+ char c;
755
703
 
756
704
  for (; '.' != *text; text++) {
757
- c = *text;
758
- if (c < '0' || '9' < c) {
759
- return Qnil;
760
- }
761
- v = 10 * v + (long)(c - '0');
705
+ c = *text;
706
+ if (c < '0' || '9' < c) {
707
+ return Qnil;
708
+ }
709
+ v = 10 * v + (long)(c - '0');
762
710
  }
763
711
  dot = text++;
764
712
  for (; '\0' != *text && text - dot <= 6; text++) {
765
- c = *text;
766
- if (c < '0' || '9' < c) {
767
- return Qnil;
768
- }
769
- v2 = 10 * v2 + (long)(c - '0');
713
+ c = *text;
714
+ if (c < '0' || '9' < c) {
715
+ return Qnil;
716
+ }
717
+ v2 = 10 * v2 + (long)(c - '0');
770
718
  }
771
719
  for (; text - dot <= 9; text++) {
772
- v2 *= 10;
720
+ v2 *= 10;
773
721
  }
774
722
  #if HAVE_RB_TIME_NANO_NEW
775
723
  return rb_time_nano_new(v, v2);
@@ -779,54 +727,53 @@ parse_double_time(const char *text, VALUE clas) {
779
727
  }
780
728
 
781
729
  typedef struct _tp {
782
- int cnt;
783
- char end;
784
- char alt;
730
+ int cnt;
731
+ char end;
732
+ char alt;
785
733
  } *Tp;
786
734
 
787
- static VALUE
788
- parse_xsd_time(const char *text, VALUE clas) {
789
- long cargs[10];
790
- long *cp = cargs;
791
- long v;
792
- int i;
793
- char c;
794
- struct _tp tpa[10] = { { 4, '-', '-' },
795
- { 2, '-', '-' },
796
- { 2, 'T', 'T' },
797
- { 2, ':', ':' },
798
- { 2, ':', ':' },
799
- { 2, '.', '.' },
800
- { 9, '+', '-' },
801
- { 2, ':', ':' },
802
- { 2, '\0', '\0' },
803
- { 0, '\0', '\0' } };
804
- Tp tp = tpa;
805
- struct tm tm;
735
+ static VALUE parse_xsd_time(const char *text, VALUE clas) {
736
+ long cargs[10];
737
+ long *cp = cargs;
738
+ long v;
739
+ int i;
740
+ char c;
741
+ struct _tp tpa[10] = {{4, '-', '-'},
742
+ {2, '-', '-'},
743
+ {2, 'T', 'T'},
744
+ {2, ':', ':'},
745
+ {2, ':', ':'},
746
+ {2, '.', '.'},
747
+ {9, '+', '-'},
748
+ {2, ':', ':'},
749
+ {2, '\0', '\0'},
750
+ {0, '\0', '\0'}};
751
+ Tp tp = tpa;
752
+ struct tm tm;
806
753
 
807
754
  for (; 0 != tp->cnt; tp++) {
808
- for (i = tp->cnt, v = 0; 0 < i ; text++, i--) {
809
- c = *text;
810
- if (c < '0' || '9' < c) {
811
- if (tp->end == c || tp->alt == c) {
812
- break;
813
- }
814
- return Qnil;
815
- }
816
- v = 10 * v + (long)(c - '0');
817
- }
818
- c = *text++;
819
- if (tp->end != c && tp->alt != c) {
820
- return Qnil;
821
- }
822
- *cp++ = v;
755
+ for (i = tp->cnt, v = 0; 0 < i; text++, i--) {
756
+ c = *text;
757
+ if (c < '0' || '9' < c) {
758
+ if (tp->end == c || tp->alt == c) {
759
+ break;
760
+ }
761
+ return Qnil;
762
+ }
763
+ v = 10 * v + (long)(c - '0');
764
+ }
765
+ c = *text++;
766
+ if (tp->end != c && tp->alt != c) {
767
+ return Qnil;
768
+ }
769
+ *cp++ = v;
823
770
  }
824
771
  tm.tm_year = (int)cargs[0] - 1900;
825
- tm.tm_mon = (int)cargs[1] - 1;
772
+ tm.tm_mon = (int)cargs[1] - 1;
826
773
  tm.tm_mday = (int)cargs[2];
827
774
  tm.tm_hour = (int)cargs[3];
828
- tm.tm_min = (int)cargs[4];
829
- tm.tm_sec = (int)cargs[5];
775
+ tm.tm_min = (int)cargs[4];
776
+ tm.tm_sec = (int)cargs[5];
830
777
  #if HAVE_RB_TIME_NANO_NEW
831
778
  return rb_time_nano_new(mktime(&tm), cargs[6]);
832
779
  #else
@@ -835,51 +782,50 @@ parse_xsd_time(const char *text, VALUE clas) {
835
782
  }
836
783
 
837
784
  // debug functions
838
- static void
839
- fill_indent(PInfo pi, char *buf, size_t size) {
840
- size_t cnt;
785
+ static void fill_indent(PInfo pi, char *buf, size_t size) {
786
+ size_t cnt;
841
787
 
842
788
  if (0 < (cnt = helper_stack_depth(&pi->helpers))) {
843
- cnt *= 2;
844
- if (size < cnt + 1) {
845
- cnt = size - 1;
846
- }
847
- memset(buf, ' ', cnt);
848
- buf += cnt;
789
+ cnt *= 2;
790
+ if (size < cnt + 1) {
791
+ cnt = size - 1;
792
+ }
793
+ memset(buf, ' ', cnt);
794
+ buf += cnt;
849
795
  }
850
796
  *buf = '\0';
851
797
  }
852
798
 
853
- static void
854
- debug_stack(PInfo pi, const char *comment) {
855
- char indent[128];
856
- Helper h;
799
+ static void debug_stack(PInfo pi, const char *comment) {
800
+ char indent[128];
801
+ Helper h;
857
802
 
858
803
  fill_indent(pi, indent, sizeof(indent));
859
804
  printf("%s%s\n", indent, comment);
860
805
  if (!helper_stack_empty(&pi->helpers)) {
861
- for (h = pi->helpers.head; h < pi->helpers.tail; h++) {
862
- const char *clas = "---";
863
- const char *key = "---";
864
-
865
- if (Qundef != h->obj) {
866
- VALUE c = rb_obj_class(h->obj);
867
-
868
- clas = rb_class2name(c);
869
- }
870
- if (0 != h->var) {
871
- if (HashCode == h->type) {
872
- VALUE v;
873
-
874
- v = rb_funcall2(h->var, rb_intern("to_s"), 0, 0);
875
- key = StringValuePtr(v);
876
- } else if (ObjectCode == (h - 1)->type || ExceptionCode == (h - 1)->type || RangeCode == (h - 1)->type || StructCode == (h - 1)->type) {
877
- key = rb_id2name(h->var);
878
- } else {
879
- printf("%s*** corrupt stack ***\n", indent);
880
- }
881
- }
882
- printf("%s [%c] %s : %s\n", indent, h->type, clas, key);
883
- }
806
+ for (h = pi->helpers.head; h < pi->helpers.tail; h++) {
807
+ const char *clas = "---";
808
+ const char *key = "---";
809
+
810
+ if (Qundef != h->obj) {
811
+ VALUE c = rb_obj_class(h->obj);
812
+
813
+ clas = rb_class2name(c);
814
+ }
815
+ if (0 != h->var) {
816
+ if (HashCode == h->type) {
817
+ VALUE v;
818
+
819
+ v = rb_funcall2(h->var, rb_intern("to_s"), 0, 0);
820
+ key = StringValuePtr(v);
821
+ } else if (ObjectCode == (h - 1)->type || ExceptionCode == (h - 1)->type ||
822
+ RangeCode == (h - 1)->type || StructCode == (h - 1)->type) {
823
+ key = rb_id2name(h->var);
824
+ } else {
825
+ printf("%s*** corrupt stack ***\n", indent);
826
+ }
827
+ }
828
+ printf("%s [%c] %s : %s\n", indent, h->type, clas, key);
829
+ }
884
830
  }
885
831
  }