ox 2.14.13 → 2.14.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/README.md +1 -1
- data/ext/ox/attr.h +33 -39
- data/ext/ox/base64.c +48 -42
- data/ext/ox/base64.h +4 -4
- data/ext/ox/buf.h +80 -86
- data/ext/ox/builder.c +378 -423
- data/ext/ox/cache.c +2 -2
- data/ext/ox/cache8.c +37 -40
- data/ext/ox/cache8.h +7 -7
- data/ext/ox/dump.c +838 -867
- data/ext/ox/err.c +16 -13
- data/ext/ox/err.h +11 -12
- data/ext/ox/extconf.rb +5 -5
- data/ext/ox/gen_load.c +135 -137
- data/ext/ox/hash_load.c +130 -148
- data/ext/ox/helper.h +32 -39
- data/ext/ox/intern.c +1 -2
- data/ext/ox/obj_load.c +590 -644
- data/ext/ox/ox.c +2 -2
- data/ext/ox/ox.h +5 -5
- data/ext/ox/parse.c +836 -874
- data/ext/ox/sax.c +38 -23
- data/ext/ox/sax.h +2 -2
- data/ext/ox/sax_as.c +78 -94
- data/ext/ox/sax_buf.c +85 -94
- data/ext/ox/sax_buf.h +101 -120
- data/ext/ox/sax_hint.c +175 -184
- data/ext/ox/sax_hint.h +19 -19
- data/ext/ox/sax_stack.h +59 -45
- data/ext/ox/slotcache.c +2 -2
- data/ext/ox/slotcache.h +4 -4
- data/ext/ox/special.c +320 -327
- data/ext/ox/special.h +2 -2
- data/ext/ox/type.h +19 -19
- data/lib/ox/bag.rb +13 -9
- data/lib/ox/cdata.rb +0 -2
- data/lib/ox/comment.rb +0 -2
- data/lib/ox/doctype.rb +0 -2
- data/lib/ox/document.rb +3 -5
- data/lib/ox/element.rb +41 -26
- data/lib/ox/error.rb +0 -3
- data/lib/ox/hasattrs.rb +7 -8
- data/lib/ox/instruct.rb +4 -6
- data/lib/ox/node.rb +3 -4
- data/lib/ox/raw.rb +0 -2
- data/lib/ox/sax.rb +20 -36
- data/lib/ox/version.rb +1 -2
- data/lib/ox/xmlrpc_adapter.rb +4 -6
- data/lib/ox.rb +15 -16
- metadata +6 -5
data/ext/ox/dump.c
CHANGED
@@ -3,84 +3,83 @@
|
|
3
3
|
* All rights reserved.
|
4
4
|
*/
|
5
5
|
|
6
|
-
#include <stdlib.h>
|
7
6
|
#include <errno.h>
|
8
|
-
#include <time.h>
|
9
7
|
#include <stdio.h>
|
8
|
+
#include <stdlib.h>
|
10
9
|
#include <string.h>
|
10
|
+
#include <time.h>
|
11
11
|
|
12
12
|
#include "base64.h"
|
13
13
|
#include "cache8.h"
|
14
14
|
#include "ox.h"
|
15
15
|
|
16
|
-
#define USE_B64
|
16
|
+
#define USE_B64 0
|
17
17
|
#define MAX_DEPTH 1000
|
18
18
|
|
19
|
-
typedef unsigned long
|
19
|
+
typedef unsigned long ulong;
|
20
20
|
|
21
21
|
typedef struct _str {
|
22
|
-
const char
|
23
|
-
size_t
|
22
|
+
const char *str;
|
23
|
+
size_t len;
|
24
24
|
} *Str;
|
25
25
|
|
26
26
|
typedef struct _element {
|
27
|
-
struct _str
|
28
|
-
struct _str
|
29
|
-
unsigned long
|
30
|
-
int
|
31
|
-
int
|
32
|
-
char
|
27
|
+
struct _str clas;
|
28
|
+
struct _str attr;
|
29
|
+
unsigned long id;
|
30
|
+
int indent; /* < 0 indicates no \n */
|
31
|
+
int closed;
|
32
|
+
char type;
|
33
33
|
} *Element;
|
34
34
|
|
35
35
|
typedef struct _out {
|
36
|
-
void
|
37
|
-
void
|
38
|
-
void
|
39
|
-
char
|
40
|
-
char
|
41
|
-
char
|
42
|
-
Cache8
|
43
|
-
unsigned long
|
44
|
-
int
|
45
|
-
int
|
46
|
-
Options
|
47
|
-
VALUE
|
36
|
+
void (*w_start)(struct _out *out, Element e);
|
37
|
+
void (*w_end)(struct _out *out, Element e);
|
38
|
+
void (*w_time)(struct _out *out, VALUE obj);
|
39
|
+
char *buf;
|
40
|
+
char *end;
|
41
|
+
char *cur;
|
42
|
+
Cache8 circ_cache;
|
43
|
+
unsigned long circ_cnt;
|
44
|
+
int indent;
|
45
|
+
int depth; /* used by dumpHash */
|
46
|
+
Options opts;
|
47
|
+
VALUE obj;
|
48
48
|
} *Out;
|
49
49
|
|
50
|
-
static void
|
50
|
+
static void dump_obj_to_xml(VALUE obj, Options copts, Out out);
|
51
51
|
|
52
|
-
static void
|
53
|
-
static void
|
54
|
-
static void
|
55
|
-
static void
|
56
|
-
static void
|
57
|
-
static int
|
58
|
-
static int
|
59
|
-
static void
|
60
|
-
|
61
|
-
const char *suf, size_t slen, Out out);
|
52
|
+
static void dump_first_obj(VALUE obj, Out out);
|
53
|
+
static void dump_obj(ID aid, VALUE obj, int depth, Out out);
|
54
|
+
static void dump_gen_doc(VALUE obj, int depth, Out out);
|
55
|
+
static void dump_gen_element(VALUE obj, int depth, Out out);
|
56
|
+
static void dump_gen_instruct(VALUE obj, int depth, Out out);
|
57
|
+
static int dump_gen_attr(VALUE key, VALUE value, VALUE ov);
|
58
|
+
static int dump_gen_nodes(VALUE obj, int depth, Out out);
|
59
|
+
static void
|
60
|
+
dump_gen_val_node(VALUE obj, int depth, const char *pre, size_t plen, const char *suf, size_t slen, Out out);
|
62
61
|
|
63
|
-
static void
|
64
|
-
static void
|
62
|
+
static void dump_start(Out out, Element e);
|
63
|
+
static void dump_end(Out out, Element e);
|
65
64
|
|
66
|
-
static void
|
65
|
+
static void grow(Out out, size_t len);
|
67
66
|
|
68
|
-
static void
|
69
|
-
static void
|
70
|
-
static int
|
71
|
-
static void
|
72
|
-
static void
|
73
|
-
static void
|
74
|
-
static void
|
75
|
-
static int
|
67
|
+
static void dump_value(Out out, const char *value, size_t size);
|
68
|
+
static void dump_str_value(Out out, const char *value, size_t size, const char *table);
|
69
|
+
static int dump_var(ID key, VALUE value, VALUE ov);
|
70
|
+
static void dump_num(Out out, VALUE obj);
|
71
|
+
static void dump_date(Out out, VALUE obj);
|
72
|
+
static void dump_time_thin(Out out, VALUE obj);
|
73
|
+
static void dump_time_xsd(Out out, VALUE obj);
|
74
|
+
static int dump_hash(VALUE key, VALUE value, VALUE ov);
|
76
75
|
|
77
|
-
static int
|
76
|
+
static int is_xml_friendly(const uchar *str, int len, const char *table);
|
78
77
|
|
79
|
-
static const char
|
78
|
+
static const char hex_chars[17] = "0123456789abcdef";
|
80
79
|
|
81
80
|
// The : character is equivalent to 10. Used for replacement characters up to 10
|
82
81
|
// characters long such as ''.
|
83
|
-
static const char
|
82
|
+
static const char xml_friendly_chars[257] = "\
|
84
83
|
:::::::::11::1::::::::::::::::::\
|
85
84
|
11611156111111111111111111114141\
|
86
85
|
11111111111111111111111111111111\
|
@@ -90,7 +89,7 @@ static const char xml_friendly_chars[257] = "\
|
|
90
89
|
11111111111111111111111111111111\
|
91
90
|
11111111111111111111111111111111";
|
92
91
|
|
93
|
-
static const char
|
92
|
+
static const char xml_quote_chars[257] = "\
|
94
93
|
:::::::::11::1::::::::::::::::::\
|
95
94
|
11611151111111111111111111114141\
|
96
95
|
11111111111111111111111111111111\
|
@@ -100,7 +99,7 @@ static const char xml_quote_chars[257] = "\
|
|
100
99
|
11111111111111111111111111111111\
|
101
100
|
11111111111111111111111111111111";
|
102
101
|
|
103
|
-
static const char
|
102
|
+
static const char xml_element_chars[257] = "\
|
104
103
|
:::::::::11::1::::::::::::::::::\
|
105
104
|
11111151111111111111111111114141\
|
106
105
|
11111111111111111111111111111111\
|
@@ -110,916 +109,902 @@ static const char xml_element_chars[257] = "\
|
|
110
109
|
11111111111111111111111111111111\
|
111
110
|
11111111111111111111111111111111";
|
112
111
|
|
113
|
-
inline static int
|
114
|
-
is_xml_friendly(const uchar *str, int len, const char *table) {
|
112
|
+
inline static int is_xml_friendly(const uchar *str, int len, const char *table) {
|
115
113
|
for (; 0 < len; str++, len--) {
|
116
|
-
|
117
|
-
|
118
|
-
|
114
|
+
if ('1' != table[*str]) {
|
115
|
+
return 0;
|
116
|
+
}
|
119
117
|
}
|
120
118
|
return 1;
|
121
119
|
}
|
122
120
|
|
123
|
-
inline static size_t
|
124
|
-
|
125
|
-
size_t size = 0;
|
121
|
+
inline static size_t xml_str_len(const uchar *str, size_t len, const char *table) {
|
122
|
+
size_t size = 0;
|
126
123
|
|
127
124
|
for (; 0 < len; str++, len--) {
|
128
|
-
|
125
|
+
size += xml_friendly_chars[*str];
|
129
126
|
}
|
130
127
|
return size - len * (size_t)'0';
|
131
128
|
}
|
132
129
|
|
133
|
-
inline static void
|
134
|
-
|
135
|
-
uchar d = (c >> 4) & 0x0F;
|
130
|
+
inline static void dump_hex(uchar c, Out out) {
|
131
|
+
uchar d = (c >> 4) & 0x0F;
|
136
132
|
|
137
133
|
*out->cur++ = hex_chars[d];
|
138
|
-
d
|
134
|
+
d = c & 0x0F;
|
139
135
|
*out->cur++ = hex_chars[d];
|
140
136
|
}
|
141
137
|
|
142
|
-
static Type
|
143
|
-
|
144
|
-
VALUE clas = rb_obj_class(obj);
|
138
|
+
static Type obj_class_code(VALUE obj) {
|
139
|
+
VALUE clas = rb_obj_class(obj);
|
145
140
|
|
146
141
|
switch (rb_type(obj)) {
|
147
|
-
case T_NIL:
|
148
|
-
case T_ARRAY:
|
149
|
-
case T_HASH:
|
150
|
-
case T_TRUE:
|
151
|
-
case T_FALSE:
|
152
|
-
case T_FIXNUM:
|
153
|
-
case T_FLOAT:
|
154
|
-
case T_STRING:
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
case
|
163
|
-
case
|
164
|
-
case
|
165
|
-
case
|
142
|
+
case T_NIL: return NilClassCode;
|
143
|
+
case T_ARRAY: return ArrayCode;
|
144
|
+
case T_HASH: return HashCode;
|
145
|
+
case T_TRUE: return TrueClassCode;
|
146
|
+
case T_FALSE: return FalseClassCode;
|
147
|
+
case T_FIXNUM: return FixnumCode;
|
148
|
+
case T_FLOAT: return FloatCode;
|
149
|
+
case T_STRING:
|
150
|
+
return (is_xml_friendly((uchar *)StringValuePtr(obj), (int)RSTRING_LEN(obj), xml_element_chars)) ? StringCode
|
151
|
+
: String64Code;
|
152
|
+
case T_SYMBOL: {
|
153
|
+
const char *sym = rb_id2name(SYM2ID(obj));
|
154
|
+
|
155
|
+
return (is_xml_friendly((uchar *)sym, (int)strlen(sym), xml_element_chars)) ? SymbolCode : Symbol64Code;
|
156
|
+
}
|
157
|
+
case T_DATA: return (rb_cTime == clas) ? TimeCode : ((ox_date_class == clas) ? DateCode : 0);
|
158
|
+
case T_STRUCT: return (rb_cRange == clas) ? RangeCode : StructCode;
|
159
|
+
case T_OBJECT: return (ox_document_clas == clas || ox_element_clas == clas) ? RawCode : ObjectCode;
|
160
|
+
case T_REGEXP: return RegexpCode;
|
161
|
+
case T_BIGNUM: return BignumCode;
|
166
162
|
#ifdef T_COMPLEX
|
167
|
-
case T_COMPLEX:
|
163
|
+
case T_COMPLEX: return ComplexCode;
|
168
164
|
#endif
|
169
165
|
#ifdef T_RATIONAL
|
170
|
-
case T_RATIONAL:
|
166
|
+
case T_RATIONAL: return RationalCode;
|
171
167
|
#endif
|
172
|
-
case T_CLASS:
|
173
|
-
default:
|
168
|
+
case T_CLASS: return ClassCode;
|
169
|
+
default: return 0;
|
174
170
|
}
|
175
171
|
}
|
176
172
|
|
177
|
-
inline static void
|
178
|
-
fill_indent(Out out, int cnt) {
|
173
|
+
inline static void fill_indent(Out out, int cnt) {
|
179
174
|
if (0 <= cnt) {
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
175
|
+
*out->cur++ = '\n';
|
176
|
+
if (0 < out->opts->margin_len) {
|
177
|
+
memcpy(out->cur, out->opts->margin, out->opts->margin_len);
|
178
|
+
out->cur += out->opts->margin_len;
|
179
|
+
}
|
180
|
+
for (; 0 < cnt; cnt--) {
|
181
|
+
*out->cur++ = ' ';
|
182
|
+
}
|
188
183
|
}
|
189
184
|
}
|
190
185
|
|
191
|
-
inline static void
|
192
|
-
fill_value(Out out, const char *value, size_t len) {
|
186
|
+
inline static void fill_value(Out out, const char *value, size_t len) {
|
193
187
|
if (6 < len) {
|
194
|
-
|
195
|
-
|
188
|
+
memcpy(out->cur, value, len);
|
189
|
+
out->cur += len;
|
196
190
|
} else {
|
197
|
-
|
198
|
-
|
199
|
-
|
191
|
+
for (; 0 < len; len--, value++) {
|
192
|
+
*out->cur++ = *value;
|
193
|
+
}
|
200
194
|
}
|
201
195
|
}
|
202
196
|
|
203
|
-
inline static void
|
204
|
-
fill_attr(Out out, char name, const char *value, size_t len) {
|
197
|
+
inline static void fill_attr(Out out, char name, const char *value, size_t len) {
|
205
198
|
*out->cur++ = ' ';
|
206
199
|
*out->cur++ = name;
|
207
200
|
*out->cur++ = '=';
|
208
201
|
*out->cur++ = '"';
|
209
202
|
if (6 < len) {
|
210
|
-
|
211
|
-
|
203
|
+
memcpy(out->cur, value, len);
|
204
|
+
out->cur += len;
|
212
205
|
} else {
|
213
|
-
|
214
|
-
|
215
|
-
|
206
|
+
for (; 0 < len; len--, value++) {
|
207
|
+
*out->cur++ = *value;
|
208
|
+
}
|
216
209
|
}
|
217
210
|
*out->cur++ = '"';
|
218
211
|
}
|
219
212
|
|
220
|
-
inline static const char*
|
221
|
-
|
222
|
-
char *b;
|
213
|
+
inline static const char *ulong2str(ulong num, char *end) {
|
214
|
+
char *b;
|
223
215
|
|
224
216
|
*end-- = '\0';
|
225
217
|
for (b = end; 0 < num || b == end; num /= 10, b--) {
|
226
|
-
|
218
|
+
*b = (num % 10) + '0';
|
227
219
|
}
|
228
220
|
b++;
|
229
221
|
|
230
222
|
return b;
|
231
223
|
}
|
232
224
|
|
233
|
-
static int
|
234
|
-
|
235
|
-
slot_t
|
236
|
-
|
237
|
-
int result;
|
225
|
+
static int check_circular(Out out, VALUE obj, Element e) {
|
226
|
+
slot_t *slot;
|
227
|
+
slot_t id;
|
228
|
+
int result;
|
238
229
|
|
239
230
|
if (0 == (id = ox_cache8_get(out->circ_cache, obj, &slot))) {
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
231
|
+
out->circ_cnt++;
|
232
|
+
id = out->circ_cnt;
|
233
|
+
*slot = id;
|
234
|
+
e->id = id;
|
235
|
+
result = 0;
|
245
236
|
} else {
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
237
|
+
e->type = RefCode;
|
238
|
+
e->clas.len = 0;
|
239
|
+
e->clas.str = 0;
|
240
|
+
e->closed = 1;
|
241
|
+
e->id = id;
|
242
|
+
out->w_start(out, e);
|
243
|
+
result = 1;
|
251
244
|
}
|
252
245
|
return result;
|
253
246
|
}
|
254
247
|
|
255
|
-
static void
|
256
|
-
|
257
|
-
|
258
|
-
long pos = out->cur - out->buf;
|
248
|
+
static void grow(Out out, size_t len) {
|
249
|
+
size_t size = out->end - out->buf;
|
250
|
+
long pos = out->cur - out->buf;
|
259
251
|
|
260
252
|
size *= 2;
|
261
253
|
if (size <= len * 2 + pos) {
|
262
|
-
|
254
|
+
size += len;
|
263
255
|
}
|
264
256
|
REALLOC_N(out->buf, char, size + 10); /* 10 extra for terminator character plus extra (paranoid) */
|
265
257
|
out->end = out->buf + size;
|
266
258
|
out->cur = out->buf + pos;
|
267
259
|
}
|
268
260
|
|
269
|
-
static void
|
270
|
-
|
271
|
-
size_t size = e->indent + 4 + out->opts->margin_len;
|
261
|
+
static void dump_start(Out out, Element e) {
|
262
|
+
size_t size = e->indent + 4 + out->opts->margin_len;
|
272
263
|
|
273
264
|
if (0 < e->attr.len) { /* a="attr" */
|
274
|
-
|
265
|
+
size += e->attr.len + 5;
|
275
266
|
}
|
276
267
|
if (0 < e->clas.len) { /* c="class" */
|
277
|
-
|
268
|
+
size += e->clas.len + 5;
|
278
269
|
}
|
279
270
|
if (0 < e->id) { /* i="id" */
|
280
|
-
|
271
|
+
size += 24; /* over estimate, 19 digits */
|
281
272
|
}
|
282
273
|
if (out->end - out->cur <= (long)size) {
|
283
|
-
|
274
|
+
grow(out, size);
|
284
275
|
}
|
285
276
|
if (out->buf + out->opts->margin_len < out->cur) {
|
286
|
-
|
277
|
+
fill_indent(out, e->indent);
|
287
278
|
}
|
288
279
|
*out->cur++ = '<';
|
289
280
|
*out->cur++ = e->type;
|
290
281
|
if (0 < e->attr.len) {
|
291
|
-
|
282
|
+
fill_attr(out, 'a', e->attr.str, e->attr.len);
|
292
283
|
}
|
293
|
-
if ((ObjectCode == e->type || ExceptionCode == e->type || StructCode == e->type || ClassCode == e->type) &&
|
294
|
-
|
284
|
+
if ((ObjectCode == e->type || ExceptionCode == e->type || StructCode == e->type || ClassCode == e->type) &&
|
285
|
+
0 < e->clas.len) {
|
286
|
+
fill_attr(out, 'c', e->clas.str, e->clas.len);
|
295
287
|
}
|
296
288
|
if (0 < e->id) {
|
297
|
-
|
298
|
-
|
299
|
-
|
289
|
+
char buf[32];
|
290
|
+
char *end = buf + sizeof(buf) - 1;
|
291
|
+
const char *s = ulong2str(e->id, end);
|
300
292
|
|
301
|
-
|
293
|
+
fill_attr(out, 'i', s, end - s);
|
302
294
|
}
|
303
295
|
if (e->closed) {
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
296
|
+
if (out->opts->no_empty) {
|
297
|
+
*out->cur++ = '>';
|
298
|
+
*out->cur++ = '<';
|
299
|
+
*out->cur++ = '/';
|
300
|
+
*out->cur++ = e->type;
|
301
|
+
} else {
|
302
|
+
*out->cur++ = '/';
|
303
|
+
}
|
312
304
|
}
|
313
305
|
*out->cur++ = '>';
|
314
|
-
*out->cur
|
306
|
+
*out->cur = '\0';
|
315
307
|
}
|
316
308
|
|
317
|
-
static void
|
318
|
-
|
319
|
-
size_t size = e->indent + 5 + out->opts->margin_len;
|
309
|
+
static void dump_end(Out out, Element e) {
|
310
|
+
size_t size = e->indent + 5 + out->opts->margin_len;
|
320
311
|
|
321
312
|
if (out->end - out->cur <= (long)size) {
|
322
|
-
|
313
|
+
grow(out, size);
|
323
314
|
}
|
324
315
|
fill_indent(out, e->indent);
|
325
316
|
*out->cur++ = '<';
|
326
317
|
*out->cur++ = '/';
|
327
318
|
*out->cur++ = e->type;
|
328
319
|
*out->cur++ = '>';
|
329
|
-
*out->cur
|
320
|
+
*out->cur = '\0';
|
330
321
|
}
|
331
322
|
|
332
|
-
inline static void
|
333
|
-
dump_value(Out out, const char *value, size_t size) {
|
323
|
+
inline static void dump_value(Out out, const char *value, size_t size) {
|
334
324
|
if (out->end - out->cur <= (long)size) {
|
335
|
-
|
325
|
+
grow(out, size);
|
336
326
|
}
|
337
327
|
if (6 < size) {
|
338
|
-
|
339
|
-
|
328
|
+
memcpy(out->cur, value, size);
|
329
|
+
out->cur += size;
|
340
330
|
} else {
|
341
|
-
|
342
|
-
|
343
|
-
|
331
|
+
for (; 0 < size; size--, value++) {
|
332
|
+
*out->cur++ = *value;
|
333
|
+
}
|
344
334
|
}
|
345
335
|
*out->cur = '\0';
|
346
336
|
}
|
347
337
|
|
348
|
-
inline static void
|
349
|
-
|
350
|
-
size_t xsize = xml_str_len((const uchar*)value, size, table);
|
338
|
+
inline static void dump_str_value(Out out, const char *value, size_t size, const char *table) {
|
339
|
+
size_t xsize = xml_str_len((const uchar *)value, size, table);
|
351
340
|
|
352
341
|
if (out->end - out->cur <= (long)xsize) {
|
353
|
-
|
342
|
+
grow(out, xsize);
|
354
343
|
}
|
355
344
|
for (; 0 < size; size--, value++) {
|
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
|
-
|
345
|
+
if ('1' == table[(uchar)*value]) {
|
346
|
+
*out->cur++ = *value;
|
347
|
+
} else {
|
348
|
+
switch (*value) {
|
349
|
+
case '"':
|
350
|
+
*out->cur++ = '&';
|
351
|
+
*out->cur++ = 'q';
|
352
|
+
*out->cur++ = 'u';
|
353
|
+
*out->cur++ = 'o';
|
354
|
+
*out->cur++ = 't';
|
355
|
+
*out->cur++ = ';';
|
356
|
+
break;
|
357
|
+
case '&':
|
358
|
+
*out->cur++ = '&';
|
359
|
+
*out->cur++ = 'a';
|
360
|
+
*out->cur++ = 'm';
|
361
|
+
*out->cur++ = 'p';
|
362
|
+
*out->cur++ = ';';
|
363
|
+
break;
|
364
|
+
case '\'':
|
365
|
+
*out->cur++ = '&';
|
366
|
+
*out->cur++ = 'a';
|
367
|
+
*out->cur++ = 'p';
|
368
|
+
*out->cur++ = 'o';
|
369
|
+
*out->cur++ = 's';
|
370
|
+
*out->cur++ = ';';
|
371
|
+
break;
|
372
|
+
case '<':
|
373
|
+
*out->cur++ = '&';
|
374
|
+
*out->cur++ = 'l';
|
375
|
+
*out->cur++ = 't';
|
376
|
+
*out->cur++ = ';';
|
377
|
+
break;
|
378
|
+
case '>':
|
379
|
+
*out->cur++ = '&';
|
380
|
+
*out->cur++ = 'g';
|
381
|
+
*out->cur++ = 't';
|
382
|
+
*out->cur++ = ';';
|
383
|
+
break;
|
384
|
+
default:
|
385
|
+
// Must be one of the invalid characters.
|
386
|
+
if (StrictEffort == out->opts->effort) {
|
387
|
+
rb_raise(ox_syntax_error_class, "'\\#x%02x' is not a valid XML character.", *value);
|
388
|
+
}
|
389
|
+
if (Yes == out->opts->allow_invalid) {
|
390
|
+
*out->cur++ = '&';
|
391
|
+
*out->cur++ = '#';
|
392
|
+
*out->cur++ = 'x';
|
393
|
+
*out->cur++ = '0';
|
394
|
+
*out->cur++ = '0';
|
395
|
+
dump_hex(*value, out);
|
396
|
+
*out->cur++ = ';';
|
397
|
+
} else if ('\0' != *out->opts->inv_repl) {
|
398
|
+
// If the empty string then ignore. The first character of
|
399
|
+
// the replacement is the length.
|
400
|
+
memcpy(out->cur, out->opts->inv_repl + 1, (size_t)*out->opts->inv_repl);
|
401
|
+
out->cur += *out->opts->inv_repl;
|
402
|
+
}
|
403
|
+
break;
|
404
|
+
}
|
405
|
+
}
|
417
406
|
}
|
418
407
|
*out->cur = '\0';
|
419
408
|
}
|
420
409
|
|
421
|
-
inline static void
|
422
|
-
|
423
|
-
char
|
424
|
-
|
425
|
-
|
426
|
-
int neg = 0;
|
410
|
+
inline static void dump_num(Out out, VALUE obj) {
|
411
|
+
char buf[32];
|
412
|
+
char *b = buf + sizeof(buf) - 1;
|
413
|
+
long num = NUM2LONG(obj);
|
414
|
+
int neg = 0;
|
427
415
|
|
428
416
|
if (0 > num) {
|
429
|
-
|
430
|
-
|
417
|
+
neg = 1;
|
418
|
+
num = -num;
|
431
419
|
}
|
432
420
|
*b-- = '\0';
|
433
421
|
if (0 < num) {
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
422
|
+
for (; 0 < num; num /= 10, b--) {
|
423
|
+
*b = (num % 10) + '0';
|
424
|
+
}
|
425
|
+
if (neg) {
|
426
|
+
*b = '-';
|
427
|
+
} else {
|
428
|
+
b++;
|
429
|
+
}
|
442
430
|
} else {
|
443
|
-
|
431
|
+
*b = '0';
|
444
432
|
}
|
445
433
|
if (out->end - out->cur <= (long)(sizeof(buf) - (b - buf))) {
|
446
|
-
|
434
|
+
grow(out, sizeof(buf) - (b - buf));
|
447
435
|
}
|
448
436
|
for (; '\0' != *b; b++) {
|
449
|
-
|
437
|
+
*out->cur++ = *b;
|
450
438
|
}
|
451
439
|
*out->cur = '\0';
|
452
440
|
}
|
453
441
|
|
454
|
-
static void
|
455
|
-
|
456
|
-
char
|
457
|
-
char *b = buf + sizeof(buf) - 1;
|
442
|
+
static void dump_time_thin(Out out, VALUE obj) {
|
443
|
+
char buf[64];
|
444
|
+
char *b = buf + sizeof(buf) - 1;
|
458
445
|
#if HAVE_RB_TIME_TIMESPEC
|
459
|
-
struct timespec
|
460
|
-
time_t
|
461
|
-
long
|
446
|
+
struct timespec ts = rb_time_timespec(obj);
|
447
|
+
time_t sec = ts.tv_sec;
|
448
|
+
long nsec = ts.tv_nsec;
|
462
449
|
#else
|
463
|
-
time_t
|
464
|
-
long
|
465
|
-
//long nsec = NUM2LONG(rb_funcall2(obj, ox_tv_usec_id, 0, 0)) * 1000;
|
450
|
+
time_t sec = NUM2LONG(rb_funcall2(obj, ox_tv_sec_id, 0, 0));
|
451
|
+
long nsec = NUM2LONG(rb_funcall2(obj, ox_tv_nsec_id, 0, 0));
|
452
|
+
// long nsec = NUM2LONG(rb_funcall2(obj, ox_tv_usec_id, 0, 0)) * 1000;
|
466
453
|
#endif
|
467
|
-
char
|
468
|
-
long
|
454
|
+
char *dot = b - 10;
|
455
|
+
long size;
|
469
456
|
|
470
457
|
*b-- = '\0';
|
471
458
|
for (; dot < b; b--, nsec /= 10) {
|
472
|
-
|
459
|
+
*b = '0' + (nsec % 10);
|
473
460
|
}
|
474
461
|
*b-- = '.';
|
475
462
|
for (; 0 < sec; b--, sec /= 10) {
|
476
|
-
|
463
|
+
*b = '0' + (sec % 10);
|
477
464
|
}
|
478
465
|
b++;
|
479
466
|
size = sizeof(buf) - (b - buf) - 1;
|
480
467
|
if (out->end - out->cur <= size) {
|
481
|
-
|
468
|
+
grow(out, size);
|
482
469
|
}
|
483
470
|
memcpy(out->cur, b, size);
|
484
471
|
out->cur += size;
|
485
472
|
}
|
486
473
|
|
487
|
-
static void
|
488
|
-
|
489
|
-
char
|
490
|
-
|
491
|
-
long
|
492
|
-
long size;
|
474
|
+
static void dump_date(Out out, VALUE obj) {
|
475
|
+
char buf[64];
|
476
|
+
char *b = buf + sizeof(buf) - 1;
|
477
|
+
long jd = NUM2LONG(rb_funcall2(obj, ox_jd_id, 0, 0));
|
478
|
+
long size;
|
493
479
|
|
494
480
|
*b-- = '\0';
|
495
481
|
for (; 0 < jd; b--, jd /= 10) {
|
496
|
-
|
482
|
+
*b = '0' + (jd % 10);
|
497
483
|
}
|
498
484
|
b++;
|
499
485
|
if ('\0' == *b) {
|
500
|
-
|
501
|
-
|
486
|
+
b--;
|
487
|
+
*b = '0';
|
502
488
|
}
|
503
489
|
size = sizeof(buf) - (b - buf) - 1;
|
504
490
|
if (out->end - out->cur <= size) {
|
505
|
-
|
491
|
+
grow(out, size);
|
506
492
|
}
|
507
493
|
memcpy(out->cur, b, size);
|
508
494
|
out->cur += size;
|
509
495
|
}
|
510
496
|
|
511
|
-
static void
|
512
|
-
|
513
|
-
struct tm *tm;
|
497
|
+
static void dump_time_xsd(Out out, VALUE obj) {
|
498
|
+
struct tm *tm;
|
514
499
|
#if HAVE_RB_TIME_TIMESPEC
|
515
|
-
struct timespec
|
516
|
-
time_t
|
517
|
-
long
|
500
|
+
struct timespec ts = rb_time_timespec(obj);
|
501
|
+
time_t sec = ts.tv_sec;
|
502
|
+
long nsec = ts.tv_nsec;
|
518
503
|
#else
|
519
|
-
time_t
|
520
|
-
long
|
521
|
-
//long nsec = NUM2LONG(rb_funcall2(obj, ox_tv_usec_id, 0, 0)) * 1000;
|
504
|
+
time_t sec = NUM2LONG(rb_funcall2(obj, ox_tv_sec_id, 0, 0));
|
505
|
+
long nsec = NUM2LONG(rb_funcall2(obj, ox_tv_nsec_id, 0, 0));
|
506
|
+
// long nsec = NUM2LONG(rb_funcall2(obj, ox_tv_usec_id, 0, 0)) * 1000;
|
522
507
|
#endif
|
523
|
-
int
|
524
|
-
char
|
508
|
+
int tzhour, tzmin;
|
509
|
+
char tzsign = '+';
|
525
510
|
|
526
511
|
if (out->end - out->cur <= 33) {
|
527
|
-
|
512
|
+
grow(out, 33);
|
528
513
|
}
|
529
514
|
/* 2010-07-09T10:47:45.895826+09:00 */
|
530
515
|
tm = localtime(&sec);
|
531
516
|
#if HAVE_ST_TM_GMTOFF
|
532
517
|
if (0 > tm->tm_gmtoff) {
|
533
|
-
|
534
|
-
|
535
|
-
|
518
|
+
tzsign = '-';
|
519
|
+
tzhour = (int)(tm->tm_gmtoff / -3600);
|
520
|
+
tzmin = (int)(tm->tm_gmtoff / -60) - (tzhour * 60);
|
536
521
|
} else {
|
537
|
-
|
538
|
-
|
522
|
+
tzhour = (int)(tm->tm_gmtoff / 3600);
|
523
|
+
tzmin = (int)(tm->tm_gmtoff / 60) - (tzhour * 60);
|
539
524
|
}
|
540
525
|
#else
|
541
526
|
tzhour = 0;
|
542
|
-
tzmin
|
527
|
+
tzmin = 0;
|
543
528
|
#endif
|
544
529
|
/* TBD replace with more efficient printer */
|
545
|
-
out->cur += sprintf(out->cur,
|
546
|
-
|
547
|
-
|
548
|
-
|
530
|
+
out->cur += sprintf(out->cur,
|
531
|
+
"%04d-%02d-%02dT%02d:%02d:%02d.%06ld%c%02d:%02d",
|
532
|
+
tm->tm_year + 1900,
|
533
|
+
tm->tm_mon + 1,
|
534
|
+
tm->tm_mday,
|
535
|
+
tm->tm_hour,
|
536
|
+
tm->tm_min,
|
537
|
+
tm->tm_sec,
|
538
|
+
nsec / 1000,
|
539
|
+
tzsign,
|
540
|
+
tzhour,
|
541
|
+
tzmin);
|
549
542
|
}
|
550
543
|
|
551
|
-
static void
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
int cnt;
|
544
|
+
static void dump_first_obj(VALUE obj, Out out) {
|
545
|
+
char buf[128];
|
546
|
+
Options copts = out->opts;
|
547
|
+
int cnt;
|
556
548
|
|
557
549
|
if (Yes == copts->with_xml) {
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
550
|
+
if (0 < copts->margin_len) {
|
551
|
+
dump_value(out, copts->margin, copts->margin_len);
|
552
|
+
}
|
553
|
+
if ('\0' == *copts->encoding) {
|
554
|
+
dump_value(out, "<?xml version=\"1.0\"?>", 21);
|
555
|
+
} else {
|
556
|
+
cnt = snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"%s\"?>", copts->encoding);
|
557
|
+
dump_value(out, buf, cnt);
|
558
|
+
}
|
567
559
|
}
|
568
560
|
if (Yes == copts->with_instruct) {
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
561
|
+
if (out->buf < out->cur) {
|
562
|
+
dump_value(out, "\n", 1);
|
563
|
+
}
|
564
|
+
if (0 < copts->margin_len) {
|
565
|
+
dump_value(out, copts->margin, copts->margin_len);
|
566
|
+
}
|
567
|
+
cnt = snprintf(
|
568
|
+
buf,
|
569
|
+
sizeof(buf),
|
570
|
+
"<?ox version=\"1.0\" mode=\"object\"%s%s?>",
|
571
|
+
(Yes == copts->circular) ? " circular=\"yes\"" : ((No == copts->circular) ? " circular=\"no\"" : ""),
|
572
|
+
(Yes == copts->xsd_date) ? " xsd_date=\"yes\"" : ((No == copts->xsd_date) ? " xsd_date=\"no\"" : ""));
|
573
|
+
dump_value(out, buf, cnt);
|
579
574
|
}
|
580
575
|
if (Yes == copts->with_dtd) {
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
576
|
+
if (0 < copts->margin_len) {
|
577
|
+
dump_value(out, copts->margin, copts->margin_len);
|
578
|
+
}
|
579
|
+
cnt = snprintf(buf,
|
580
|
+
sizeof(buf),
|
581
|
+
"%s<!DOCTYPE %c SYSTEM \"ox.dtd\">",
|
582
|
+
(out->buf < out->cur) ? "\n" : "",
|
583
|
+
obj_class_code(obj));
|
584
|
+
dump_value(out, buf, cnt);
|
586
585
|
}
|
587
586
|
if (0 < copts->margin_len) {
|
588
|
-
|
587
|
+
dump_value(out, copts->margin, copts->margin_len);
|
589
588
|
}
|
590
589
|
dump_obj(0, obj, 0, out);
|
591
590
|
}
|
592
591
|
|
593
|
-
static void
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
int cnt;
|
592
|
+
static void dump_obj(ID aid, VALUE obj, int depth, Out out) {
|
593
|
+
struct _element e;
|
594
|
+
VALUE prev_obj = out->obj;
|
595
|
+
char value_buf[64];
|
596
|
+
int cnt;
|
599
597
|
|
600
598
|
if (MAX_DEPTH < depth) {
|
601
|
-
|
599
|
+
rb_raise(rb_eSysStackError, "maximum depth exceeded");
|
602
600
|
}
|
603
601
|
out->obj = obj;
|
604
602
|
if (0 == aid) {
|
605
|
-
|
606
|
-
|
603
|
+
e.attr.str = 0;
|
604
|
+
e.attr.len = 0;
|
607
605
|
} else {
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
606
|
+
e.attr.str = rb_id2name(aid);
|
607
|
+
// Ruby 2.3 started to return NULL for some IDs so check for
|
608
|
+
// NULL. Ignore if NULL aid.
|
609
|
+
if (NULL == e.attr.str) {
|
610
|
+
return;
|
611
|
+
}
|
612
|
+
e.attr.len = strlen(e.attr.str);
|
615
613
|
}
|
616
614
|
e.closed = 0;
|
617
615
|
if (0 == depth) {
|
618
|
-
|
616
|
+
e.indent = (0 <= out->indent) ? 0 : -1;
|
619
617
|
} else if (0 > out->indent) {
|
620
|
-
|
618
|
+
e.indent = -1;
|
621
619
|
} else if (0 == out->indent) {
|
622
|
-
|
620
|
+
e.indent = 0;
|
623
621
|
} else {
|
624
|
-
|
622
|
+
e.indent = depth * out->indent;
|
625
623
|
}
|
626
|
-
e.id
|
624
|
+
e.id = 0;
|
627
625
|
e.clas.len = 0;
|
628
626
|
e.clas.str = 0;
|
629
627
|
switch (rb_type(obj)) {
|
630
628
|
case T_NIL:
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
629
|
+
e.type = NilClassCode;
|
630
|
+
e.closed = 1;
|
631
|
+
out->w_start(out, &e);
|
632
|
+
break;
|
635
633
|
case T_ARRAY:
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
634
|
+
if (0 != out->circ_cache && check_circular(out, obj, &e)) {
|
635
|
+
break;
|
636
|
+
}
|
637
|
+
cnt = (int)RARRAY_LEN(obj);
|
638
|
+
e.type = ArrayCode;
|
639
|
+
e.closed = (0 >= cnt);
|
640
|
+
out->w_start(out, &e);
|
641
|
+
if (!e.closed) {
|
642
|
+
const VALUE *np = RARRAY_PTR(obj);
|
643
|
+
int i;
|
644
|
+
int d2 = depth + 1;
|
645
|
+
|
646
|
+
for (i = cnt; 0 < i; i--, np++) {
|
647
|
+
dump_obj(0, *np, d2, out);
|
648
|
+
}
|
649
|
+
out->w_end(out, &e);
|
650
|
+
}
|
651
|
+
break;
|
654
652
|
case T_HASH:
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
653
|
+
if (0 != out->circ_cache && check_circular(out, obj, &e)) {
|
654
|
+
break;
|
655
|
+
}
|
656
|
+
cnt = (int)RHASH_SIZE(obj);
|
657
|
+
e.type = HashCode;
|
658
|
+
e.closed = (0 >= cnt);
|
659
|
+
out->w_start(out, &e);
|
660
|
+
if (0 < cnt) {
|
661
|
+
unsigned int od = out->depth;
|
662
|
+
|
663
|
+
out->depth = depth + 1;
|
664
|
+
rb_hash_foreach(obj, dump_hash, (VALUE)out);
|
665
|
+
out->depth = od;
|
666
|
+
out->w_end(out, &e);
|
667
|
+
}
|
668
|
+
break;
|
671
669
|
case T_TRUE:
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
670
|
+
e.type = TrueClassCode;
|
671
|
+
e.closed = 1;
|
672
|
+
out->w_start(out, &e);
|
673
|
+
break;
|
676
674
|
case T_FALSE:
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
675
|
+
e.type = FalseClassCode;
|
676
|
+
e.closed = 1;
|
677
|
+
out->w_start(out, &e);
|
678
|
+
break;
|
681
679
|
case T_FIXNUM:
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
680
|
+
e.type = FixnumCode;
|
681
|
+
out->w_start(out, &e);
|
682
|
+
dump_num(out, obj);
|
683
|
+
e.indent = -1;
|
684
|
+
out->w_end(out, &e);
|
685
|
+
break;
|
688
686
|
case T_FLOAT:
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
case T_STRING:
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
cnt = (int)RSTRING_LEN(obj);
|
687
|
+
e.type = FloatCode;
|
688
|
+
cnt = snprintf(value_buf, sizeof(value_buf), "%0.16g", rb_num2dbl(obj));
|
689
|
+
out->w_start(out, &e);
|
690
|
+
dump_value(out, value_buf, cnt);
|
691
|
+
e.indent = -1;
|
692
|
+
out->w_end(out, &e);
|
693
|
+
break;
|
694
|
+
case T_STRING: {
|
695
|
+
const char *str;
|
696
|
+
|
697
|
+
if (0 != out->circ_cache && check_circular(out, obj, &e)) {
|
698
|
+
break;
|
699
|
+
}
|
700
|
+
str = StringValuePtr(obj);
|
701
|
+
cnt = (int)RSTRING_LEN(obj);
|
705
702
|
#if USE_B64
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
703
|
+
if (is_xml_friendly((uchar *)str, cnt)) {
|
704
|
+
e.type = StringCode;
|
705
|
+
out->w_start(out, &e);
|
706
|
+
dump_str_value(out, str, cnt, '<');
|
707
|
+
e.indent = -1;
|
708
|
+
out->w_end(out, &e);
|
709
|
+
} else {
|
710
|
+
ulong size = b64_size(cnt);
|
711
|
+
char *b64 = ALLOCA_N(char, size + 1);
|
712
|
+
|
713
|
+
e.type = String64Code;
|
714
|
+
to_base64((uchar *)str, cnt, b64);
|
715
|
+
out->w_start(out, &e);
|
716
|
+
dump_value(out, b64, size);
|
717
|
+
e.indent = -1;
|
718
|
+
out->w_end(out, &e);
|
719
|
+
}
|
723
720
|
#else
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
721
|
+
e.type = StringCode;
|
722
|
+
out->w_start(out, &e);
|
723
|
+
dump_str_value(out, str, cnt, xml_element_chars);
|
724
|
+
e.indent = -1;
|
725
|
+
out->w_end(out, &e);
|
729
726
|
#endif
|
730
|
-
|
727
|
+
break;
|
731
728
|
}
|
732
|
-
case T_SYMBOL:
|
733
|
-
|
734
|
-
const char *sym = rb_id2name(SYM2ID(obj));
|
729
|
+
case T_SYMBOL: {
|
730
|
+
const char *sym = rb_id2name(SYM2ID(obj));
|
735
731
|
|
736
|
-
|
732
|
+
cnt = (int)strlen(sym);
|
737
733
|
#if USE_B64
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
734
|
+
if (is_xml_friendly((uchar *)sym, cnt)) {
|
735
|
+
e.type = SymbolCode;
|
736
|
+
out->w_start(out, &e);
|
737
|
+
dump_str_value(out, sym, cnt, '<');
|
738
|
+
e.indent = -1;
|
739
|
+
out->w_end(out, &e);
|
740
|
+
} else {
|
741
|
+
ulong size = b64_size(cnt);
|
742
|
+
char *b64 = ALLOCA_N(char, size + 1);
|
743
|
+
|
744
|
+
e.type = Symbol64Code;
|
745
|
+
to_base64((uchar *)sym, cnt, b64);
|
746
|
+
out->w_start(out, &e);
|
747
|
+
dump_value(out, b64, size);
|
748
|
+
e.indent = -1;
|
749
|
+
out->w_end(out, &e);
|
750
|
+
}
|
755
751
|
#else
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
752
|
+
e.type = SymbolCode;
|
753
|
+
out->w_start(out, &e);
|
754
|
+
dump_str_value(out, sym, cnt, xml_element_chars);
|
755
|
+
e.indent = -1;
|
756
|
+
out->w_end(out, &e);
|
761
757
|
#endif
|
762
|
-
|
763
|
-
}
|
764
|
-
case T_DATA:
|
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
|
-
case T_STRUCT:
|
805
|
-
{
|
758
|
+
break;
|
759
|
+
}
|
760
|
+
case T_DATA: {
|
761
|
+
VALUE clas;
|
762
|
+
|
763
|
+
clas = rb_obj_class(obj);
|
764
|
+
if (rb_cTime == clas) {
|
765
|
+
e.type = TimeCode;
|
766
|
+
out->w_start(out, &e);
|
767
|
+
out->w_time(out, obj);
|
768
|
+
e.indent = -1;
|
769
|
+
out->w_end(out, &e);
|
770
|
+
} else {
|
771
|
+
const char *classname = rb_class2name(clas);
|
772
|
+
|
773
|
+
if (0 == strcmp("Date", classname)) {
|
774
|
+
e.type = DateCode;
|
775
|
+
out->w_start(out, &e);
|
776
|
+
dump_date(out, obj);
|
777
|
+
e.indent = -1;
|
778
|
+
out->w_end(out, &e);
|
779
|
+
} else if (0 == strcmp("BigDecimal", classname)) {
|
780
|
+
volatile VALUE rs = rb_funcall(obj, ox_to_s_id, 0);
|
781
|
+
|
782
|
+
e.type = BigDecimalCode;
|
783
|
+
out->w_start(out, &e);
|
784
|
+
dump_value(out, StringValuePtr(rs), RSTRING_LEN(rs));
|
785
|
+
e.indent = -1;
|
786
|
+
out->w_end(out, &e);
|
787
|
+
} else {
|
788
|
+
if (StrictEffort == out->opts->effort) {
|
789
|
+
rb_raise(rb_eNotImpError, "Failed to dump T_DATA %s\n", classname);
|
790
|
+
} else {
|
791
|
+
e.type = NilClassCode;
|
792
|
+
e.closed = 1;
|
793
|
+
out->w_start(out, &e);
|
794
|
+
}
|
795
|
+
}
|
796
|
+
}
|
797
|
+
break;
|
798
|
+
}
|
799
|
+
case T_STRUCT: {
|
806
800
|
#ifdef RSTRUCT_GET
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
801
|
+
VALUE clas;
|
802
|
+
|
803
|
+
if (0 != out->circ_cache && check_circular(out, obj, &e)) {
|
804
|
+
break;
|
805
|
+
}
|
806
|
+
clas = rb_obj_class(obj);
|
807
|
+
if (rb_cRange == clas) {
|
808
|
+
VALUE beg = RSTRUCT_GET(obj, 0);
|
809
|
+
VALUE end = RSTRUCT_GET(obj, 1);
|
810
|
+
VALUE excl = RSTRUCT_GET(obj, 2);
|
811
|
+
int d2 = depth + 1;
|
812
|
+
|
813
|
+
e.type = RangeCode;
|
814
|
+
e.clas.len = 5;
|
815
|
+
e.clas.str = "Range";
|
816
|
+
out->w_start(out, &e);
|
817
|
+
dump_obj(ox_beg_id, beg, d2, out);
|
818
|
+
dump_obj(ox_end_id, end, d2, out);
|
819
|
+
dump_obj(ox_excl_id, excl, d2, out);
|
820
|
+
out->w_end(out, &e);
|
821
|
+
} else {
|
822
|
+
char num_buf[16];
|
823
|
+
int d2 = depth + 1;
|
828
824
|
#ifdef RUBY_INTEGER_UNIFICATION
|
829
|
-
|
830
|
-
|
831
|
-
#else
|
832
|
-
|
833
|
-
|
834
|
-
#endif
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
825
|
+
long i;
|
826
|
+
long cnt = NUM2LONG(rb_struct_size(obj));
|
827
|
+
#else // UNIFY_FIXNUM_AND_INTEGER
|
828
|
+
int i;
|
829
|
+
int cnt = (int)RSTRUCT_LEN(obj);
|
830
|
+
#endif // UNIFY_FIXNUM_AND_INTEGER
|
831
|
+
e.type = StructCode;
|
832
|
+
e.clas.str = rb_class2name(clas);
|
833
|
+
e.clas.len = strlen(e.clas.str);
|
834
|
+
out->w_start(out, &e);
|
835
|
+
|
836
|
+
for (i = 0; i < cnt; i++) {
|
837
|
+
VALUE v = RSTRUCT_GET(obj, (int)(i));
|
838
|
+
dump_obj(rb_intern(ulong2str(i, num_buf + sizeof(num_buf) - 1)), v, d2, out);
|
839
|
+
}
|
840
|
+
out->w_end(out, &e);
|
841
|
+
}
|
846
842
|
#else
|
847
|
-
|
848
|
-
|
849
|
-
|
843
|
+
e.type = NilClassCode;
|
844
|
+
e.closed = 1;
|
845
|
+
out->w_start(out, &e);
|
850
846
|
#endif
|
851
|
-
|
852
|
-
}
|
853
|
-
case T_OBJECT:
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
} else { /* Object */
|
847
|
+
break;
|
848
|
+
}
|
849
|
+
case T_OBJECT: {
|
850
|
+
VALUE clas;
|
851
|
+
|
852
|
+
if (0 != out->circ_cache && check_circular(out, obj, &e)) {
|
853
|
+
break;
|
854
|
+
}
|
855
|
+
clas = rb_obj_class(obj);
|
856
|
+
e.clas.str = rb_class2name(clas);
|
857
|
+
e.clas.len = strlen(e.clas.str);
|
858
|
+
if (ox_document_clas == clas) {
|
859
|
+
e.type = RawCode;
|
860
|
+
out->w_start(out, &e);
|
861
|
+
dump_gen_doc(obj, depth + 1, out);
|
862
|
+
out->w_end(out, &e);
|
863
|
+
} else if (ox_element_clas == clas) {
|
864
|
+
e.type = RawCode;
|
865
|
+
out->w_start(out, &e);
|
866
|
+
dump_gen_element(obj, depth + 1, out);
|
867
|
+
out->w_end(out, &e);
|
868
|
+
} else { /* Object */
|
874
869
|
#if HAVE_RB_IVAR_FOREACH
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
870
|
+
e.type = (Qtrue == rb_obj_is_kind_of(obj, rb_eException)) ? ExceptionCode : ObjectCode;
|
871
|
+
cnt = (int)rb_ivar_count(obj);
|
872
|
+
e.closed = (0 >= cnt);
|
873
|
+
out->w_start(out, &e);
|
874
|
+
if (0 < cnt) {
|
875
|
+
unsigned int od = out->depth;
|
876
|
+
|
877
|
+
out->depth = depth + 1;
|
878
|
+
rb_ivar_foreach(obj, dump_var, (VALUE)out);
|
879
|
+
out->depth = od;
|
880
|
+
out->w_end(out, &e);
|
881
|
+
}
|
887
882
|
#else
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
883
|
+
volatile VALUE vars = rb_obj_instance_variables(obj);
|
884
|
+
// volatile VALUE vars = rb_funcall2(obj, rb_intern("instance_variables"), 0, 0);
|
885
|
+
|
886
|
+
e.type = (Qtrue == rb_obj_is_kind_of(obj, rb_eException)) ? ExceptionCode : ObjectCode;
|
887
|
+
cnt = (int)RARRAY_LEN(vars);
|
888
|
+
e.closed = (0 >= cnt);
|
889
|
+
out->w_start(out, &e);
|
890
|
+
if (0 < cnt) {
|
891
|
+
const VALUE *np = RARRAY_PTR(vars);
|
892
|
+
ID vid;
|
893
|
+
unsigned int od = out->depth;
|
894
|
+
int i;
|
895
|
+
|
896
|
+
out->depth = depth + 1;
|
897
|
+
for (i = cnt; 0 < i; i--, np++) {
|
898
|
+
vid = rb_to_id(*np);
|
899
|
+
dump_var(vid, rb_ivar_get(obj, vid), out);
|
900
|
+
}
|
901
|
+
out->depth = od;
|
902
|
+
out->w_end(out, &e);
|
903
|
+
}
|
909
904
|
#endif
|
910
|
-
|
911
|
-
|
905
|
+
}
|
906
|
+
break;
|
912
907
|
}
|
913
|
-
case T_REGEXP:
|
914
|
-
|
915
|
-
|
916
|
-
const char *s = StringValuePtr(rs);
|
908
|
+
case T_REGEXP: {
|
909
|
+
volatile VALUE rs = rb_funcall2(obj, ox_inspect_id, 0, 0);
|
910
|
+
const char *s = StringValuePtr(rs);
|
917
911
|
|
918
|
-
|
919
|
-
|
920
|
-
|
912
|
+
cnt = (int)RSTRING_LEN(rs);
|
913
|
+
e.type = RegexpCode;
|
914
|
+
out->w_start(out, &e);
|
921
915
|
#if USE_B64
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
916
|
+
if (is_xml_friendly((uchar *)s, cnt)) {
|
917
|
+
/*dump_value(out, "/", 1); */
|
918
|
+
dump_str_value(out, s, cnt, '<');
|
919
|
+
} else {
|
920
|
+
ulong size = b64_size(cnt);
|
921
|
+
char *b64 = ALLOCA_N(char, size + 1);
|
922
|
+
|
923
|
+
to_base64((uchar *)s, cnt, b64);
|
924
|
+
dump_value(out, b64, size);
|
925
|
+
}
|
932
926
|
#else
|
933
|
-
|
927
|
+
dump_str_value(out, s, cnt, xml_element_chars);
|
934
928
|
#endif
|
935
|
-
|
936
|
-
|
937
|
-
|
929
|
+
e.indent = -1;
|
930
|
+
out->w_end(out, &e);
|
931
|
+
break;
|
938
932
|
}
|
939
|
-
case T_BIGNUM:
|
940
|
-
|
941
|
-
volatile VALUE rs = rb_big2str(obj, 10);
|
933
|
+
case T_BIGNUM: {
|
934
|
+
volatile VALUE rs = rb_big2str(obj, 10);
|
942
935
|
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
936
|
+
e.type = BignumCode;
|
937
|
+
out->w_start(out, &e);
|
938
|
+
dump_value(out, StringValuePtr(rs), RSTRING_LEN(rs));
|
939
|
+
e.indent = -1;
|
940
|
+
out->w_end(out, &e);
|
941
|
+
break;
|
949
942
|
}
|
950
943
|
#ifdef T_COMPLEX
|
951
|
-
case T_COMPLEX:
|
952
|
-
e.type = ComplexCode;
|
953
|
-
out->w_start(out, &e);
|
944
|
+
case T_COMPLEX: e.type = ComplexCode; out->w_start(out, &e);
|
954
945
|
#ifdef RCOMPLEX
|
955
|
-
|
956
|
-
|
946
|
+
dump_obj(0, RCOMPLEX(obj)->real, depth + 1, out);
|
947
|
+
dump_obj(0, RCOMPLEX(obj)->imag, depth + 1, out);
|
957
948
|
#else
|
958
|
-
|
959
|
-
|
949
|
+
dump_obj(0, rb_funcall2(obj, rb_intern("real"), 0, 0), depth + 1, out);
|
950
|
+
dump_obj(0, rb_funcall2(obj, rb_intern("imag"), 0, 0), depth + 1, out);
|
960
951
|
#endif
|
961
|
-
|
962
|
-
|
952
|
+
out->w_end(out, &e);
|
953
|
+
break;
|
963
954
|
#endif
|
964
955
|
#ifdef T_RATIONAL
|
965
|
-
case T_RATIONAL:
|
966
|
-
e.type = RationalCode;
|
967
|
-
out->w_start(out, &e);
|
956
|
+
case T_RATIONAL: e.type = RationalCode; out->w_start(out, &e);
|
968
957
|
#ifdef RRATIONAL
|
969
|
-
|
970
|
-
|
958
|
+
dump_obj(0, RRATIONAL(obj)->num, depth + 1, out);
|
959
|
+
dump_obj(0, RRATIONAL(obj)->den, depth + 1, out);
|
971
960
|
#else
|
972
|
-
|
973
|
-
|
961
|
+
dump_obj(0, rb_funcall2(obj, rb_intern("numerator"), 0, 0), depth + 1, out);
|
962
|
+
dump_obj(0, rb_funcall2(obj, rb_intern("denominator"), 0, 0), depth + 1, out);
|
974
963
|
#endif
|
975
|
-
|
976
|
-
|
964
|
+
out->w_end(out, &e);
|
965
|
+
break;
|
977
966
|
#endif
|
978
|
-
case T_CLASS:
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
break;
|
967
|
+
case T_CLASS: {
|
968
|
+
e.type = ClassCode;
|
969
|
+
e.clas.str = rb_class2name(obj);
|
970
|
+
e.clas.len = strlen(e.clas.str);
|
971
|
+
e.closed = 1;
|
972
|
+
out->w_start(out, &e);
|
973
|
+
break;
|
986
974
|
}
|
987
975
|
default:
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
break;
|
976
|
+
if (StrictEffort == out->opts->effort) {
|
977
|
+
rb_raise(rb_eNotImpError, "Failed to dump %s Object (%02x)\n", rb_obj_classname(obj), rb_type(obj));
|
978
|
+
} else {
|
979
|
+
e.type = NilClassCode;
|
980
|
+
e.closed = 1;
|
981
|
+
out->w_start(out, &e);
|
982
|
+
}
|
983
|
+
break;
|
997
984
|
}
|
998
985
|
out->obj = prev_obj;
|
999
986
|
}
|
1000
987
|
|
1001
|
-
static int
|
1002
|
-
|
1003
|
-
Out out = (Out)ov;
|
988
|
+
static int dump_var(ID key, VALUE value, VALUE ov) {
|
989
|
+
Out out = (Out)ov;
|
1004
990
|
|
1005
991
|
if (T_DATA == rb_type(value) && key == ox_mesg_id) {
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
992
|
+
/* There is a secret recipe that keeps Exception mesg attributes as a
|
993
|
+
* T_DATA until it is needed. The safe way around this hack is to call
|
994
|
+
* the message() method and use the returned string as the
|
995
|
+
* message. Not pretty but it solves the most common use of this
|
996
|
+
* hack. If there are others they will have to be handled one at a
|
997
|
+
* time.
|
998
|
+
*/
|
999
|
+
value = rb_funcall(out->obj, ox_message_id, 0);
|
1014
1000
|
}
|
1015
1001
|
dump_obj(key, value, out->depth, out);
|
1016
1002
|
|
1017
1003
|
return ST_CONTINUE;
|
1018
1004
|
}
|
1019
1005
|
|
1020
|
-
static int
|
1021
|
-
|
1022
|
-
Out out = (Out)ov;
|
1006
|
+
static int dump_hash(VALUE key, VALUE value, VALUE ov) {
|
1007
|
+
Out out = (Out)ov;
|
1023
1008
|
|
1024
1009
|
dump_obj(0, key, out->depth, out);
|
1025
1010
|
dump_obj(0, value, out->depth, out);
|
@@ -1027,199 +1012,190 @@ dump_hash(VALUE key, VALUE value, VALUE ov) {
|
|
1027
1012
|
return ST_CONTINUE;
|
1028
1013
|
}
|
1029
1014
|
|
1030
|
-
static void
|
1031
|
-
|
1032
|
-
volatile VALUE
|
1033
|
-
volatile VALUE nodes = rb_attr_get(obj, ox_nodes_id);
|
1015
|
+
static void dump_gen_doc(VALUE obj, int depth, Out out) {
|
1016
|
+
volatile VALUE attrs = rb_attr_get(obj, ox_attributes_id);
|
1017
|
+
volatile VALUE nodes = rb_attr_get(obj, ox_nodes_id);
|
1034
1018
|
|
1035
1019
|
if ('\0' == *out->opts->encoding && Qnil != attrs) {
|
1036
|
-
|
1020
|
+
volatile VALUE renc = rb_hash_lookup(attrs, ox_encoding_sym);
|
1037
1021
|
|
1038
|
-
|
1039
|
-
|
1022
|
+
if (Qnil != renc) {
|
1023
|
+
const char *enc = StringValuePtr(renc);
|
1040
1024
|
|
1041
|
-
|
1042
|
-
|
1025
|
+
strncpy(out->opts->encoding, enc, sizeof(out->opts->encoding) - 1);
|
1026
|
+
}
|
1043
1027
|
}
|
1044
1028
|
if (Yes == out->opts->with_xml) {
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1029
|
+
if (0 < out->opts->margin_len) {
|
1030
|
+
dump_value(out, out->opts->margin, out->opts->margin_len);
|
1031
|
+
}
|
1032
|
+
dump_value(out, "<?xml", 5);
|
1033
|
+
if (Qnil != attrs) {
|
1034
|
+
rb_hash_foreach(attrs, dump_gen_attr, (VALUE)out);
|
1035
|
+
}
|
1036
|
+
dump_value(out, "?>", 2);
|
1053
1037
|
}
|
1054
1038
|
if (Yes == out->opts->with_instruct) {
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1039
|
+
if (out->buf < out->cur) {
|
1040
|
+
dump_value(out, "\n", 1);
|
1041
|
+
}
|
1042
|
+
if (0 < out->opts->margin_len) {
|
1043
|
+
dump_value(out, out->opts->margin, out->opts->margin_len);
|
1044
|
+
}
|
1045
|
+
dump_value(out, "<?ox version=\"1.0\" mode=\"generic\"?>", 35);
|
1062
1046
|
}
|
1063
1047
|
if (Qnil != nodes) {
|
1064
|
-
|
1048
|
+
dump_gen_nodes(nodes, depth, out);
|
1065
1049
|
}
|
1066
1050
|
}
|
1067
1051
|
|
1068
|
-
static void
|
1069
|
-
|
1070
|
-
volatile VALUE
|
1071
|
-
volatile VALUE
|
1072
|
-
|
1073
|
-
|
1074
|
-
|
1075
|
-
|
1076
|
-
int indent;
|
1052
|
+
static void dump_gen_element(VALUE obj, int depth, Out out) {
|
1053
|
+
volatile VALUE rname = rb_attr_get(obj, ox_at_value_id);
|
1054
|
+
volatile VALUE attrs = rb_attr_get(obj, ox_attributes_id);
|
1055
|
+
volatile VALUE nodes = rb_attr_get(obj, ox_nodes_id);
|
1056
|
+
const char *name = StringValuePtr(rname);
|
1057
|
+
long nlen = RSTRING_LEN(rname);
|
1058
|
+
size_t size;
|
1059
|
+
int indent;
|
1077
1060
|
|
1078
1061
|
if (0 > out->indent) {
|
1079
|
-
|
1062
|
+
indent = -1;
|
1080
1063
|
} else if (0 == out->indent) {
|
1081
|
-
|
1064
|
+
indent = 0;
|
1082
1065
|
} else {
|
1083
|
-
|
1066
|
+
indent = depth * out->indent;
|
1084
1067
|
}
|
1085
1068
|
size = indent + 4 + nlen + out->opts->margin_len;
|
1086
1069
|
if (out->end - out->cur <= (long)size) {
|
1087
|
-
|
1070
|
+
grow(out, size);
|
1088
1071
|
}
|
1089
1072
|
if (0 == depth && 0 < out->opts->margin_len && 0 < out->indent) {
|
1090
|
-
|
1091
|
-
|
1073
|
+
memcpy(out->cur, out->opts->margin, out->opts->margin_len);
|
1074
|
+
out->cur += out->opts->margin_len;
|
1092
1075
|
}
|
1093
1076
|
fill_indent(out, indent);
|
1094
1077
|
*out->cur++ = '<';
|
1095
1078
|
fill_value(out, name, nlen);
|
1096
1079
|
if (Qnil != attrs) {
|
1097
|
-
|
1080
|
+
rb_hash_foreach(attrs, dump_gen_attr, (VALUE)out);
|
1098
1081
|
}
|
1099
1082
|
if (Qnil != nodes && 0 < RARRAY_LEN(nodes)) {
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
1103
|
-
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1083
|
+
int do_indent;
|
1084
|
+
|
1085
|
+
*out->cur++ = '>';
|
1086
|
+
do_indent = dump_gen_nodes(nodes, depth, out);
|
1087
|
+
if (out->end - out->cur <= (long)size) {
|
1088
|
+
grow(out, size);
|
1089
|
+
}
|
1090
|
+
if (do_indent) {
|
1091
|
+
fill_indent(out, indent);
|
1092
|
+
}
|
1093
|
+
*out->cur++ = '<';
|
1094
|
+
*out->cur++ = '/';
|
1095
|
+
fill_value(out, name, nlen);
|
1113
1096
|
} else if (out->opts->no_empty) {
|
1114
|
-
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1097
|
+
*out->cur++ = '>';
|
1098
|
+
*out->cur++ = '<';
|
1099
|
+
*out->cur++ = '/';
|
1100
|
+
fill_value(out, name, nlen);
|
1118
1101
|
} else {
|
1119
|
-
|
1102
|
+
*out->cur++ = '/';
|
1120
1103
|
}
|
1121
1104
|
*out->cur++ = '>';
|
1122
|
-
*out->cur
|
1105
|
+
*out->cur = '\0';
|
1123
1106
|
}
|
1124
1107
|
|
1125
|
-
static void
|
1126
|
-
|
1127
|
-
volatile VALUE
|
1128
|
-
volatile VALUE
|
1129
|
-
|
1130
|
-
const char
|
1131
|
-
|
1132
|
-
long
|
1133
|
-
|
1134
|
-
size_t size;
|
1108
|
+
static void dump_gen_instruct(VALUE obj, int depth, Out out) {
|
1109
|
+
volatile VALUE rname = rb_attr_get(obj, ox_at_value_id);
|
1110
|
+
volatile VALUE attrs = rb_attr_get(obj, ox_attributes_id);
|
1111
|
+
volatile VALUE rcontent = rb_attr_get(obj, ox_at_content_id);
|
1112
|
+
const char *name = StringValuePtr(rname);
|
1113
|
+
const char *content = 0;
|
1114
|
+
long nlen = RSTRING_LEN(rname);
|
1115
|
+
long clen = 0;
|
1116
|
+
size_t size;
|
1135
1117
|
|
1136
1118
|
if (T_STRING == rb_type(rcontent)) {
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1119
|
+
content = StringValuePtr(rcontent);
|
1120
|
+
clen = RSTRING_LEN(rcontent);
|
1121
|
+
size = 4 + nlen + clen;
|
1140
1122
|
} else {
|
1141
|
-
|
1123
|
+
size = 4 + nlen;
|
1142
1124
|
}
|
1143
1125
|
if (out->end - out->cur <= (long)size) {
|
1144
|
-
|
1126
|
+
grow(out, size);
|
1145
1127
|
}
|
1146
1128
|
*out->cur++ = '<';
|
1147
1129
|
*out->cur++ = '?';
|
1148
1130
|
fill_value(out, name, nlen);
|
1149
1131
|
if (0 != content) {
|
1150
|
-
|
1132
|
+
fill_value(out, content, clen);
|
1151
1133
|
} else if (Qnil != attrs) {
|
1152
|
-
|
1134
|
+
rb_hash_foreach(attrs, dump_gen_attr, (VALUE)out);
|
1153
1135
|
}
|
1154
1136
|
*out->cur++ = '?';
|
1155
1137
|
*out->cur++ = '>';
|
1156
|
-
*out->cur
|
1138
|
+
*out->cur = '\0';
|
1157
1139
|
}
|
1158
1140
|
|
1159
|
-
static int
|
1160
|
-
|
1161
|
-
|
1162
|
-
int indent_needed = 1;
|
1141
|
+
static int dump_gen_nodes(VALUE obj, int depth, Out out) {
|
1142
|
+
long cnt = RARRAY_LEN(obj);
|
1143
|
+
int indent_needed = 1;
|
1163
1144
|
|
1164
1145
|
if (0 < cnt) {
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1178
|
-
|
1179
|
-
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1146
|
+
const VALUE *np = RARRAY_PTR(obj);
|
1147
|
+
VALUE clas;
|
1148
|
+
int d2 = depth + 1;
|
1149
|
+
|
1150
|
+
if (MAX_DEPTH < depth) {
|
1151
|
+
rb_raise(rb_eSysStackError, "maximum depth exceeded");
|
1152
|
+
}
|
1153
|
+
for (; 0 < cnt; cnt--, np++) {
|
1154
|
+
clas = rb_obj_class(*np);
|
1155
|
+
if (ox_element_clas == clas) {
|
1156
|
+
dump_gen_element(*np, d2, out);
|
1157
|
+
} else if (ox_instruct_clas == clas) {
|
1158
|
+
dump_gen_instruct(*np, d2, out);
|
1159
|
+
indent_needed = (1 == cnt) ? 0 : 1;
|
1160
|
+
} else if (rb_cString == clas) {
|
1161
|
+
dump_str_value(out, StringValuePtr(*(VALUE *)np), RSTRING_LEN(*np), xml_element_chars);
|
1162
|
+
indent_needed = (1 == cnt) ? 0 : 1;
|
1163
|
+
} else if (ox_comment_clas == clas) {
|
1164
|
+
dump_gen_val_node(*np, d2, "<!-- ", 5, " -->", 4, out);
|
1165
|
+
} else if (ox_raw_clas == clas) {
|
1166
|
+
dump_gen_val_node(*np, d2, "", 0, "", 0, out);
|
1167
|
+
} else if (ox_cdata_clas == clas) {
|
1168
|
+
dump_gen_val_node(*np, d2, "<![CDATA[", 9, "]]>", 3, out);
|
1169
|
+
} else if (ox_doctype_clas == clas) {
|
1170
|
+
dump_gen_val_node(*np, d2, "<!DOCTYPE ", 10, ">", 1, out);
|
1171
|
+
} else {
|
1172
|
+
rb_raise(rb_eTypeError, "Unexpected class, %s, while dumping generic XML\n", rb_class2name(clas));
|
1173
|
+
}
|
1174
|
+
}
|
1194
1175
|
}
|
1195
1176
|
return indent_needed;
|
1196
1177
|
}
|
1197
1178
|
|
1198
|
-
static int
|
1199
|
-
|
1200
|
-
Out out = (Out)ov;
|
1179
|
+
static int dump_gen_attr(VALUE key, VALUE value, VALUE ov) {
|
1180
|
+
Out out = (Out)ov;
|
1201
1181
|
|
1202
|
-
const char
|
1203
|
-
size_t
|
1204
|
-
size_t
|
1182
|
+
const char *ks;
|
1183
|
+
size_t klen;
|
1184
|
+
size_t size;
|
1205
1185
|
|
1206
1186
|
switch (rb_type(key)) {
|
1207
|
-
case T_SYMBOL:
|
1208
|
-
|
1209
|
-
break;
|
1210
|
-
case T_STRING:
|
1211
|
-
ks = StringValuePtr(key);
|
1212
|
-
break;
|
1187
|
+
case T_SYMBOL: ks = rb_id2name(SYM2ID(key)); break;
|
1188
|
+
case T_STRING: ks = StringValuePtr(key); break;
|
1213
1189
|
default:
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1190
|
+
key = rb_String(key);
|
1191
|
+
ks = StringValuePtr(key);
|
1192
|
+
break;
|
1217
1193
|
}
|
1218
|
-
klen
|
1194
|
+
klen = strlen(ks);
|
1219
1195
|
value = rb_String(value);
|
1220
|
-
size
|
1196
|
+
size = 4 + klen + RSTRING_LEN(value);
|
1221
1197
|
if (out->end - out->cur <= (long)size) {
|
1222
|
-
|
1198
|
+
grow(out, size);
|
1223
1199
|
}
|
1224
1200
|
*out->cur++ = ' ';
|
1225
1201
|
fill_value(out, ks, klen);
|
@@ -1232,30 +1208,28 @@ dump_gen_attr(VALUE key, VALUE value, VALUE ov) {
|
|
1232
1208
|
}
|
1233
1209
|
|
1234
1210
|
static void
|
1235
|
-
dump_gen_val_node(VALUE obj, int depth,
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
size_t size;
|
1242
|
-
int indent;
|
1211
|
+
dump_gen_val_node(VALUE obj, int depth, const char *pre, size_t plen, const char *suf, size_t slen, Out out) {
|
1212
|
+
volatile VALUE v = rb_attr_get(obj, ox_at_value_id);
|
1213
|
+
const char *val;
|
1214
|
+
size_t vlen;
|
1215
|
+
size_t size;
|
1216
|
+
int indent;
|
1243
1217
|
|
1244
1218
|
if (T_STRING != rb_type(v)) {
|
1245
|
-
|
1219
|
+
return;
|
1246
1220
|
}
|
1247
|
-
val
|
1221
|
+
val = StringValuePtr(v);
|
1248
1222
|
vlen = RSTRING_LEN(v);
|
1249
1223
|
if (0 > out->indent) {
|
1250
|
-
|
1224
|
+
indent = -1;
|
1251
1225
|
} else if (0 == out->indent) {
|
1252
|
-
|
1226
|
+
indent = 0;
|
1253
1227
|
} else {
|
1254
|
-
|
1228
|
+
indent = depth * out->indent;
|
1255
1229
|
}
|
1256
1230
|
size = indent + plen + slen + vlen + out->opts->margin_len;
|
1257
1231
|
if (out->end - out->cur <= (long)size) {
|
1258
|
-
|
1232
|
+
grow(out, size);
|
1259
1233
|
}
|
1260
1234
|
fill_indent(out, indent);
|
1261
1235
|
fill_value(out, pre, plen);
|
@@ -1264,63 +1238,60 @@ dump_gen_val_node(VALUE obj, int depth,
|
|
1264
1238
|
*out->cur = '\0';
|
1265
1239
|
}
|
1266
1240
|
|
1267
|
-
static void
|
1268
|
-
|
1269
|
-
VALUE clas = rb_obj_class(obj);
|
1241
|
+
static void dump_obj_to_xml(VALUE obj, Options copts, Out out) {
|
1242
|
+
VALUE clas = rb_obj_class(obj);
|
1270
1243
|
|
1271
|
-
out->w_time
|
1272
|
-
out->buf
|
1273
|
-
out->end
|
1274
|
-
out->cur
|
1244
|
+
out->w_time = (Yes == copts->xsd_date) ? dump_time_xsd : dump_time_thin;
|
1245
|
+
out->buf = ALLOC_N(char, 65336);
|
1246
|
+
out->end = out->buf + 65325; /* 10 less than end plus extra for possible errors */
|
1247
|
+
out->cur = out->buf;
|
1275
1248
|
out->circ_cache = 0;
|
1276
|
-
out->circ_cnt
|
1277
|
-
out->opts
|
1278
|
-
out->obj
|
1279
|
-
*out->cur
|
1249
|
+
out->circ_cnt = 0;
|
1250
|
+
out->opts = copts;
|
1251
|
+
out->obj = obj;
|
1252
|
+
*out->cur = '\0';
|
1280
1253
|
if (Yes == copts->circular) {
|
1281
|
-
|
1254
|
+
ox_cache8_new(&out->circ_cache);
|
1282
1255
|
}
|
1283
1256
|
out->indent = copts->indent;
|
1284
1257
|
|
1285
1258
|
if (ox_document_clas == clas) {
|
1286
|
-
|
1259
|
+
dump_gen_doc(obj, -1, out);
|
1287
1260
|
} else if (ox_element_clas == clas) {
|
1288
|
-
|
1261
|
+
dump_gen_element(obj, 0, out);
|
1289
1262
|
} else {
|
1290
|
-
|
1291
|
-
|
1292
|
-
|
1263
|
+
out->w_start = dump_start;
|
1264
|
+
out->w_end = dump_end;
|
1265
|
+
dump_first_obj(obj, out);
|
1293
1266
|
}
|
1294
1267
|
if (0 <= out->indent) {
|
1295
|
-
|
1268
|
+
dump_value(out, "\n", 1);
|
1296
1269
|
}
|
1297
1270
|
if (Yes == copts->circular) {
|
1298
|
-
|
1271
|
+
ox_cache8_delete(out->circ_cache);
|
1299
1272
|
}
|
1300
1273
|
}
|
1301
1274
|
|
1302
|
-
char*
|
1303
|
-
ox_write_obj_to_str(VALUE obj, Options copts) {
|
1275
|
+
char *ox_write_obj_to_str(VALUE obj, Options copts) {
|
1304
1276
|
struct _out out;
|
1305
1277
|
|
1306
1278
|
dump_obj_to_xml(obj, copts, &out);
|
1307
1279
|
return out.buf;
|
1308
1280
|
}
|
1309
1281
|
|
1310
|
-
void
|
1311
|
-
ox_write_obj_to_file(VALUE obj, const char *path, Options copts) {
|
1282
|
+
void ox_write_obj_to_file(VALUE obj, const char *path, Options copts) {
|
1312
1283
|
struct _out out;
|
1313
|
-
size_t
|
1314
|
-
FILE
|
1284
|
+
size_t size;
|
1285
|
+
FILE *f;
|
1315
1286
|
|
1316
1287
|
dump_obj_to_xml(obj, copts, &out);
|
1317
1288
|
size = out.cur - out.buf;
|
1318
1289
|
if (0 == (f = fopen(path, "w"))) {
|
1319
|
-
|
1290
|
+
rb_raise(rb_eIOError, "%s\n", strerror(errno));
|
1320
1291
|
}
|
1321
1292
|
if (size != fwrite(out.buf, 1, size, f)) {
|
1322
|
-
|
1323
|
-
|
1293
|
+
int err = ferror(f);
|
1294
|
+
rb_raise(rb_eIOError, "Write failed. [%d:%s]\n", err, strerror(err));
|
1324
1295
|
}
|
1325
1296
|
xfree(out.buf);
|
1326
1297
|
fclose(f);
|