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