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
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
#include <Inventor/C/XML/element.h>
#include "elementp.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <Inventor/C/base/string.h>
#include <Inventor/lists/SbList.h>
#include <Inventor/C/XML/document.h>
#include <Inventor/C/XML/attribute.h>
#include <Inventor/C/XML/path.h>
#include "attributep.h"
#include "utils.h"
// *************************************************************************
struct cc_xml_elt {
char * type;
char * data;
char * cdata;
cc_xml_elt * parent;
SbList<cc_xml_attr *> attributes;
SbList<cc_xml_elt *> children;
};
// *************************************************************************
/*!
Creates a new element with no type, no attributes, and no children elements.
*/
cc_xml_elt *
cc_xml_elt_new(void)
{
cc_xml_elt * elt = new cc_xml_elt;
assert(elt);
elt->type = NULL;
elt->data = NULL;
elt->cdata = NULL;
elt->parent = NULL;
return elt;
}
/*!
Creates a new element with the given type and attributes.
The \a attrs argument can be NULL.
*/
cc_xml_elt *
cc_xml_elt_new_from_data(const char * type, cc_xml_attr ** attrs)
{
cc_xml_elt * elt = cc_xml_elt_new();
cc_xml_elt_set_type_x(elt, type);
if (attrs) { cc_xml_elt_set_attributes_x(elt, attrs); }
return elt;
}
/*!
Returns a clone of the element, including child elements and attributes.
Only the parent connection is dropped.
*/
cc_xml_elt *
cc_xml_elt_clone(const cc_xml_elt * elt)
{
cc_xml_elt * clone = cc_xml_elt_new();
if (elt->type) {
cc_xml_elt_set_type_x(clone, elt->type);
}
if (elt->data) {
clone->data = cc_xml_strdup(elt->data);
}
if (elt->cdata) {
clone->cdata = cc_xml_strdup(elt->cdata);
}
int i = 0;
for (i = 0; i < elt->attributes.getLength(); ++i) {
cc_xml_elt_set_attribute_x(clone, cc_xml_attr_clone(elt->attributes[i]));
}
for (i = 0; i < elt->children.getLength(); ++i) {
cc_xml_elt_add_child_x(clone, cc_xml_elt_clone(elt->children[i]));
}
return clone;
}
/*!
Frees the given \a elt element, including its attributes and children.
*/
void
cc_xml_elt_delete_x(cc_xml_elt * elt)
{
assert(elt);
if (elt->type) delete [] elt->type;
if (elt->data) delete [] elt->data;
if (elt->cdata) delete [] elt->cdata;
if (elt->attributes.getLength() > 0) {
const int num = elt->attributes.getLength();
for (int i = 0; i < num; ++i) {
cc_xml_attr_delete_x(elt->attributes[i]);
}
}
if (elt->children.getLength() > 0) {
const int num = elt->children.getLength();
for (int i = 0; i < num; ++i) {
cc_xml_elt_delete_x(elt->children[i]);
}
}
delete elt;
}
// *************************************************************************
/*!
Sets the element type identifier. The old value will be freed, if any.
The \a type argument can be NULL to just clear the old value.
*/
void
cc_xml_elt_set_type_x(cc_xml_elt * elt, const char * type)
{
assert(elt);
if (elt->type) {
delete [] elt->type;
elt->type = NULL;
}
if (type) elt->type = cc_xml_strdup(type);
}
/*!
Returns the element type identifier if one is set, and NULL otherwise.
*/
const char *
cc_xml_elt_get_type(const cc_xml_elt * elt)
{
return elt->type;
}
// FIXME: document behaviour
void
cc_xml_elt_set_cdata_x(cc_xml_elt * elt, const char * cdata)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( cc_xml_elt_get_num_children(elt) == 0 ) {
// cdata child not existing yet
cc_xml_elt * child = cc_xml_elt_new();
cc_xml_elt_set_type_x(child, COIN_XML_CDATA_TYPE);
cc_xml_elt_add_child_x(elt, child);
elt = child;
} else if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
// we really want to manipulate the cdata child, not this element
elt = elt->children[0];
} else {
// FIXME: warn that this doesn't make sense 20021119 larsa
// but we can run through anyways
}
}
if ( elt->cdata ) {
delete [] elt->cdata;
elt->cdata = NULL;
}
if ( cdata ) elt->cdata = cc_xml_strdup(cdata);
if ( elt->data ) {
delete [] elt->data;
elt->data = NULL;
}
// Update data to whitespace-stripped cdata
if( cdata) {
const char * startptr = elt->cdata;
const char * endptr = startptr + (strlen(startptr) - 1);
while ( *startptr == '\t' || *startptr == '\r' || *startptr == '\n' || *startptr == ' ' ) {
startptr++;
}
// FIXME: avoid whitespace-only cdata from parser, then uncomment assert's.
// assert(startptr <= endptr && "just whitespace from the front");
while ( endptr > startptr && (*endptr == '\t' || *endptr == '\r' || *endptr == '\n' || *endptr == ' ') ) {
endptr--;
}
// assert(endptr > startptr && "makes no sense at all");
endptr++;
if( endptr > startptr) {
elt->data = cc_xml_strndup(startptr, static_cast<int>(endptr - startptr));
}
}
}
const char *
cc_xml_elt_get_cdata(const cc_xml_elt * elt)
{
assert(elt);
if (strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0) {
if ((cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0)) {
return elt->children[0]->cdata;
}
}
return elt->cdata;
}
const char *
cc_xml_elt_get_data(const cc_xml_elt * elt)
{
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
return elt->children[0]->data;
}
}
return elt->data;
}
// *************************************************************************
void
cc_xml_elt_remove_all_attributes_x(cc_xml_elt * elt)
{
assert(elt);
if (elt->attributes.getLength()) {
}
}
void
cc_xml_elt_set_attribute_x(cc_xml_elt * elt, cc_xml_attr * attr)
{
cc_xml_attr * shadowed = cc_xml_elt_get_attribute(elt, cc_xml_attr_get_name(attr));
if (shadowed) {
cc_xml_attr_set_value_x(shadowed, cc_xml_attr_get_value(attr));
// FIXME: free attr, or replace shadowed by attr and free shadowed?
} else {
elt->attributes.append(attr);
}
}
void
cc_xml_elt_set_attributes_x(cc_xml_elt * elt, cc_xml_attr ** attrs)
{
for (int c = 0; attrs[c] != NULL; ++c) {
cc_xml_elt_set_attribute_x(elt, attrs[c]);
}
}
cc_xml_attr *
cc_xml_elt_get_attribute(const cc_xml_elt * elt, const char * attrname)
{
assert(elt);
const int num = elt->attributes.getLength();
for (int c = 0; c < num; ++c) {
if (strcmp(attrname, cc_xml_attr_get_name(elt->attributes[c])) == 0)
return elt->attributes[c];
}
return NULL;
}
int
cc_xml_elt_get_num_attributes(const cc_xml_elt * elt)
{
return elt->attributes.getLength();
}
const cc_xml_attr **
cc_xml_elt_get_attributes(const cc_xml_elt * elt)
{
assert(elt);
return const_cast<const cc_xml_attr **>(elt->attributes.getArrayPtr());
}
// *************************************************************************
cc_xml_elt *
cc_xml_elt_get_parent(const cc_xml_elt * elt)
{
assert(elt);
return elt->parent;
}
int
cc_xml_elt_get_num_children(const cc_xml_elt * elt)
{
assert(elt);
return elt->children.getLength();
}
/*!
Returns the number of child elements of the given type.
*/
int
cc_xml_elt_get_num_children_of_type(const cc_xml_elt * elt, const char * type)
{
assert(elt);
int count = 0;
const int numchildren = elt->children.getLength();
for (int i = 0; i < numchildren; ++i) {
if (strcmp(type, cc_xml_elt_get_type(elt->children[i])) == 0) ++count;
}
return count;
}
/*!
Returns the child element at index \a childidx.
The index has to be an existing index, invalid values are considered programming errors.
*/
cc_xml_elt *
cc_xml_elt_get_child(const cc_xml_elt * elt, int childidx)
{
assert(elt);
assert(childidx >= 0);
assert(childidx < cc_xml_elt_get_num_children(elt));
return elt->children[childidx];
}
/*!
Returns the child index of the child element.
Giving a child element that is not a child is considered a programming error.
*/
int
cc_xml_elt_get_child_index(const cc_xml_elt * elt, const cc_xml_elt * child)
{
assert(elt);
const int numchildren = elt->children.getLength();
for (int idx = 0; idx < numchildren; ++idx) {
if (elt->children[idx] == child) return idx;
}
assert(!"no such child");
return -1;
}
/*
Returns the child element's index when only counting elements of that given type.
Giving a child element that is not a child is considered a programming error.
*/
int
cc_xml_elt_get_child_type_index(const cc_xml_elt * elt, const cc_xml_elt * child)
{
assert(elt);
const int numchildren = elt->children.getLength();
int idx = -1;
const char * type = cc_xml_elt_get_type(child);
for (int i = 0; i < numchildren; ++i) {
if (strcmp(cc_xml_elt_get_type(elt->children[i]), type) == 0) ++idx;
if (elt->children[i] == child) return idx;
}
assert(!"no such child");
return -1;
}
cc_xml_path *
cc_xml_elt_get_path(const cc_xml_elt * elt)
{
assert(elt);
cc_xml_path * path = cc_xml_path_new();
while ( elt != NULL ) {
const cc_xml_elt * parent = cc_xml_elt_get_parent(elt);
if ( parent != NULL ) {
int idx = cc_xml_elt_get_child_type_index(parent, elt);
cc_xml_path_prepend_x(path, elt->type, idx);
}
elt = parent;
}
return path;
}
cc_xml_elt *
cc_xml_elt_get_child_of_type(const cc_xml_elt * elt, const char * type, int idx)
{
assert(elt);
if ( !elt->children.getLength() ) return NULL;
int i;
for ( i = 0; i < elt->children.getLength(); i++ ) {
if ( strcmp(elt->children[i]->type, type) == 0 ) {
if ( idx == 0 ) return elt->children[i];
idx -= 1;
}
}
return NULL;
}
cc_xml_elt *
cc_xml_elt_get_child_of_type_x(cc_xml_elt * elt, const char * type, int idx)
{
assert(elt);
if ( elt->children.getLength() ) {
int i;
for ( i = 0; i < elt->children.getLength(); i++ ) {
if ( strcmp(elt->children[i]->type, type) == 0 ) {
if ( idx == 0 ) return elt->children[i];
idx -= 1;
}
}
}
cc_xml_elt * child = NULL;
while ( idx >= 0 ) {
child = cc_xml_elt_new();
cc_xml_elt_set_type_x(child, type);
cc_xml_elt_add_child_x(elt, child);
idx -= 1;
}
return child;
}
// *************************************************************************
void
cc_xml_elt_set_parent_x(cc_xml_elt * elt, cc_xml_elt * parent)
{
assert(elt);
elt->parent = parent;
}
void
cc_xml_elt_add_child_x(cc_xml_elt * elt, cc_xml_elt * child)
{
assert(elt);
assert(child);
if (child->parent != NULL) {
// FIXME: ERROR - element already a child of another element
return;
}
int numchildren = cc_xml_elt_get_num_children(elt);
elt->children.append(child);
child->parent = elt;
}
/*! This function will not free the child being removed. Giving a
nonexistent child to this function is a programming error and will
result in an assert.
*/
void
cc_xml_elt_remove_child_x(cc_xml_elt * elt, cc_xml_elt * child)
{
assert(elt);
assert(child);
const int numchildren = elt->children.getLength();
for (int i = 0; i < numchildren; ++i) {
if (elt->children[i] == child) {
elt->children.remove(i);
child->parent = NULL;
return;
}
}
assert(!"no such child");
}
void
cc_xml_elt_insert_child_x(cc_xml_elt * elt, cc_xml_elt * child, int idx)
{
assert(elt);
assert(child);
if (child->parent != NULL) {
// FIXME: error, child already a child of another element
return;
}
const int numchildren = elt->children.getLength();
assert(idx >= 0 && idx <= numchildren);
elt->children.insert(child, idx);
child->parent = elt;
}
int
cc_xml_elt_replace_child_x(cc_xml_elt * elt, cc_xml_elt * oldchild, cc_xml_elt * newchild)
{
assert(elt);
int idx = cc_xml_elt_get_child_index(elt, oldchild);
if ( idx == -1 ) return FALSE;
cc_xml_elt_remove_child_x(elt, oldchild);
cc_xml_elt_insert_child_x(elt, newchild, idx);
return TRUE;
}
// *************************************************************************
int
cc_xml_elt_get_boolean(const cc_xml_elt * elt, int * value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
const char * data = cc_xml_elt_get_data(elt);
assert(value != NULL);
if ( data == NULL ) return FALSE;
if ( cc_xml_strieq(data, "true") ||
cc_xml_strieq(data, "on") ||
cc_xml_strieq(data, "t") ) {
*value = TRUE;
return TRUE;
}
if ( cc_xml_strieq(data, "false") ||
cc_xml_strieq(data, "off") ||
cc_xml_strieq(data, "f") ) {
*value = FALSE;
return TRUE;
}
return FALSE;
}
int
cc_xml_elt_get_integer(const cc_xml_elt * elt, int * value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
const char * data = cc_xml_elt_get_data(elt);
assert(value != NULL);
if ( data == NULL ) return FALSE;
if ( sscanf(data, "%d", value) == 1 ) return TRUE;
return false;
}
int
cc_xml_elt_get_uint64(const cc_xml_elt * elt, uint64_t * value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
const char * data = cc_xml_elt_get_data(elt);
assert(value != NULL);
if ( data == NULL ) return FALSE;
if ( sscanf(data, "%lld", value) == 1 ) return TRUE; // FIXME: unsigned
return FALSE;
}
int
cc_xml_elt_get_int64(const cc_xml_elt * elt, int64_t * value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
const char * data = cc_xml_elt_get_data(elt);
assert(value != NULL);
if ( data == NULL ) return FALSE;
if ( sscanf(data, "%lld", value) == 1 ) return TRUE;
return FALSE;
}
int
cc_xml_elt_get_uint32(const cc_xml_elt * elt, uint32_t * value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
const char * data = cc_xml_elt_get_data(elt);
assert(value != NULL);
if ( data == NULL ) return FALSE;
if ( sscanf(data, "%ld", value) == 1 ) return TRUE; // FIXME: unsigned
return FALSE;
}
int
cc_xml_elt_get_int32(const cc_xml_elt * elt, int32_t * value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
const char * data = cc_xml_elt_get_data(elt);
assert(value != NULL);
if ( data == NULL ) return FALSE;
if ( sscanf(data, "%ld", value) == 1 ) return TRUE;
return FALSE;
}
int
cc_xml_elt_get_float(const cc_xml_elt * elt, float * value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
const char * data = cc_xml_elt_get_data(elt);
assert(value != NULL);
if ( data == NULL ) return FALSE;
if ( sscanf(data, "%g", value) == 1 ) return TRUE;
return FALSE;
}
int
cc_xml_elt_get_double(const cc_xml_elt * elt, double * value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
const char * data = cc_xml_elt_get_data(elt);
assert(value != NULL);
if ( data == NULL ) return FALSE;
if ( sscanf(data, "%lg", value) == 1 ) return TRUE;
return FALSE;
}
void
cc_xml_elt_set_boolean_x(cc_xml_elt * elt, int value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
cc_xml_elt_set_cdata_x(elt, value ? "true" : "false");
}
void
cc_xml_elt_set_integer_x(cc_xml_elt * elt, int value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
cc_string str;
cc_string_construct(&str);
cc_string_sprintf(&str, "%d", value);
cc_xml_elt_set_cdata_x(elt, cc_string_get_text(&str));
cc_string_clean(&str);
}
void
cc_xml_elt_set_uint64_x(cc_xml_elt * elt, uint64_t value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
cc_string str;
cc_string_construct(&str);
cc_string_sprintf(&str, "%lld", value); // FIXME: unsigned
cc_xml_elt_set_cdata_x(elt, cc_string_get_text(&str));
cc_string_clean(&str);
}
void
cc_xml_elt_set_int64_x(cc_xml_elt * elt, int64_t value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
cc_string str;
cc_string_construct(&str);
cc_string_sprintf(&str, "%lld", value);
cc_xml_elt_set_cdata_x(elt, cc_string_get_text(&str));
cc_string_clean(&str);
}
void
cc_xml_elt_set_uint32_x(cc_xml_elt * elt, uint32_t value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
cc_string str;
cc_string_construct(&str);
cc_string_sprintf(&str, "%ld", value); // FIXME: unsigned
cc_xml_elt_set_cdata_x(elt, cc_string_get_text(&str));
cc_string_clean(&str);
}
void
cc_xml_elt_set_int32_x(cc_xml_elt * elt, int32_t value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
cc_string str;
cc_string_construct(&str);
cc_string_sprintf(&str, "%ld", value);
cc_xml_elt_set_cdata_x(elt, cc_string_get_text(&str));
cc_string_clean(&str);
}
void
cc_xml_elt_set_float_x(cc_xml_elt * elt, float value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
cc_string str;
cc_string_construct(&str);
cc_string_sprintf(&str, "%g", value);
cc_xml_elt_set_cdata_x(elt, cc_string_get_text(&str));
cc_string_clean(&str);
}
void
cc_xml_elt_set_double_x(cc_xml_elt * elt, double value)
{
assert(elt);
if ( strcmp(elt->type, COIN_XML_CDATA_TYPE) != 0 ) {
if ( (cc_xml_elt_get_num_children(elt) == 1) &&
(strcmp(elt->children[0]->type, COIN_XML_CDATA_TYPE) == 0) ) {
elt = elt->children[0];
} else {
// FIXME: warn about structure senselessness
}
}
cc_string str;
cc_string_construct(&str);
cc_string_sprintf(&str, "%lf", value);
cc_xml_elt_set_cdata_x(elt, cc_string_get_text(&str));
cc_string_clean(&str);
}
// *************************************************************************
void
cc_xml_elt_strip_whitespace_x(cc_xml_elt * elt)
{
// FIXME: update to reflect cdata change - remove function probably
assert(elt->type && strcmp(elt->type, COIN_XML_CDATA_TYPE) == 0);
assert(0);
const char * startptr = elt->data;
const char * endptr = startptr + (strlen(startptr) - 1);
while ( *startptr ) {
switch ( *startptr ) {
case '\t':
case '\r':
case '\n':
case ' ':
break;
default:
goto cont1;
}
startptr++;
}
assert(0 && "just whitespace from the front");
cont1:
while ( endptr > startptr ) {
switch ( *endptr ) {
case '\t':
case '\r':
case '\n':
case ' ':
break;
default:
goto cont2;
}
endptr--;
}
assert(0 && "makes no sense at all");
cont2:
endptr++;
if ( (startptr == elt->data) &&
(endptr == (startptr + strlen(startptr))) ) return;
char * substr = cc_xml_strndup(startptr, static_cast<int>(endptr - startptr));
cc_xml_elt_set_cdata_x(elt, substr);
delete [] substr;
}
// *************************************************************************
void
cc_xml_elt_dump_to_file(const cc_xml_elt * elt, int indent, FILE * fp)
{
// todo
// - support notation for empty elements and shortcuts
// - document-wide settings? how to propagate...
assert( elt && elt->type != NULL );
int i;
if ( cc_xml_elt_get_num_children(elt) == 1 &&
strcmp(COIN_XML_CDATA_TYPE, elt->children[0]->type) == 0 ) {
for ( i = 0; i < indent; i++ ) fprintf(fp, " ");
if ( elt->children[0]->data != NULL )
fprintf(fp, "<%s>%s</%s>\n", elt->type, elt->children[0]->data, elt->type);
else
fprintf(fp, "<%s/>\n", elt->type);
} else if ( strcmp(COIN_XML_CDATA_TYPE, elt->type) != 0 ) {
for ( i = 0; i < indent; i++ ) fprintf(fp, " ");
fprintf(fp, "<%s>\n", elt->type);
const int children = cc_xml_elt_get_num_children(elt);
for ( i = 0; i < children; i++ ) {
cc_xml_elt * child = cc_xml_elt_get_child(elt, i);
cc_xml_elt_dump_to_file(child, indent + 2, fp);
}
for ( i = 0; i < indent; i++ ) fprintf(fp, " ");
fprintf(fp, "</%s>\n", elt->type);
} else if ( elt->data != NULL ) {
for ( i = 0; i < indent; i++ ) fprintf(fp, " ");
fprintf(fp, "%s\n", elt->data);
}
}
// *************************************************************************
/*!
\fn cc_xml_elt * cc_xml_elt_get_traversal_next(cc_xml_elt * root, cc_xml_elt * here)
Utility function for flattening recursive traversals to do-while loops.
\ingroup cc_xml_elt
*/
cc_xml_elt *
cc_xml_elt_get_traversal_next(const cc_xml_elt * root, cc_xml_elt * here)
{
assert(root && here);
// first traverse into children - but ignore any "cdata" element...
if (cc_xml_elt_get_num_children(here) > 0) {
// Check if we can find a child that is not cdata..
for (int i = 0; i < cc_xml_elt_get_num_children(here); ++i) {
if (strcmp(COIN_XML_CDATA_TYPE, here->children[i]->type) != 0) {
return cc_xml_elt_get_child(here, i);
}
}
}
// if we're here then 'here' has no children (except possibly cdata).
do {
cc_xml_elt * parent = cc_xml_elt_get_parent(here);
if (parent == NULL) return NULL; // here is the root
int idx = cc_xml_elt_get_child_index(parent, here);
// if 'here' was the last child then set here as parent..
if (idx == (cc_xml_elt_get_num_children(parent) - 1)) {
here = parent;
// if we're back to root, simply quit..
if (here == root) return NULL;
} else {
// there is more children than 'here' left, find someone thats not "cdata".
do {
++idx;
here = cc_xml_elt_get_child(parent, idx);
// return this element if it's not a "cdata" element.
if (strcmp(COIN_XML_CDATA_TYPE, here->type) != 0) return here;
} while (idx != (cc_xml_elt_get_num_children(parent) - 1));
}
} while (TRUE);
return NULL;
} // cc_xml_elt_get_traversal_next()
// *************************************************************************
const cc_xml_elt *
cc_xml_elt_find(const cc_xml_elt * root, const cc_xml_path * path)
{
assert(root && path);
cc_xml_elt * elt = const_cast<cc_xml_elt *>(root);
while ( (elt != NULL) && (!cc_xml_path_match_p(path, elt)) ) {
elt = cc_xml_elt_get_traversal_next(root, elt);
}
return elt;
} // cc_xml_elt_find()
const cc_xml_elt *
cc_xml_elt_find_next(const cc_xml_elt * root, cc_xml_elt * from, cc_xml_path * path)
{
assert(root && from && path);
cc_xml_elt * elt = from;
do {
elt = cc_xml_elt_get_traversal_next(root, elt);
} while ( (elt != NULL) && (!cc_xml_path_match_p(path, elt)) );
return elt;
} // cc_xml_elt_find_next()
// *************************************************************************
cc_xml_elt *
cc_xml_elt_create_x(cc_xml_elt * from, cc_xml_path * path)
{
assert(from && path);
cc_xml_elt * current = from;
const char * type;
int idx;
int length = cc_xml_path_get_length(path);
int i;
for ( i = 0; i < length; i++ ) {
int lastpos = -1, lastidx = -1;
type = cc_xml_path_get_type(path, i);
idx = cc_xml_path_get_index(path, i);
if ( current->children.getLength() ) {
int child;
for ( child = 0; i < current->children.getLength(); child++ ) {
if ( strcmp(type, cc_xml_elt_get_type(current->children[child])) == 0 ) {
lastpos = child;
lastidx += 1;
if ( idx == -1 || idx == lastidx ) {
// match - enter this child
goto cont;
}
}
}
// no match - need to create for idx idx
if ( idx == -1 ) idx = lastidx + 1;
while ( lastidx < idx ) {
// insert empty children after lastpos - enter last one
lastidx += 1;
lastpos += 1;
cc_xml_elt * elt = cc_xml_elt_new();
cc_xml_elt_set_type_x(elt, type);
if ( lastpos == 0 ) lastpos = cc_xml_elt_get_num_children(current);
cc_xml_elt_insert_child_x(current, elt, lastpos);
}
} else {
// no children - need to create them
lastpos = -1;
lastidx = -1;
if ( idx == -1 ) idx = 0;
while ( lastidx < idx ) {
cc_xml_elt * newelt = cc_xml_elt_new();
cc_xml_elt_set_type_x(newelt, type);
cc_xml_elt_add_child_x(current, newelt);
lastidx += 1;
lastpos += 1;
}
}
cont:
current = current->children[lastpos];
}
return current;
} // cc_xml_elt_create_x()
// *************************************************************************
size_t
cc_xml_elt_calculate_size(const cc_xml_elt * elt, int indent, int indentincrement)
{
assert(elt);
assert(elt->type);
assert(indent >= 0);
size_t bytes = 0;
// macro to advance a number of bytes
#define ADVANCE_NUM_BYTES(num) \
do { bytes += num; } while (0)
// macro to advance a number of blanks (indentation)
#define ADVANCE_NUM_SPACES(num) \
do { bytes += num; } while (0)
// macro to increment bytecount for string literal
#define ADVANCE_STRING_LITERAL(str) \
do { static const char strobj[] = str; bytes += (sizeof(strobj) - 1); } while (0)
// macro to increment bytecount for run-time string
#define ADVANCE_STRING(str) \
do { bytes += strlen(str); } while (0)
// duplicate block - see cc_xml_elt_write_to_buffer()
if (elt->type && strcmp(elt->type, COIN_XML_CDATA_TYPE) == 0) {
// this is a leaf element character data container
ADVANCE_STRING(elt->cdata);
} else {
ADVANCE_NUM_SPACES(indent);
ADVANCE_STRING_LITERAL("<");
ADVANCE_STRING(elt->type);
int c;
const int numattributes = elt->attributes.getLength();
for (c = 0; c < numattributes; ++c) {
ADVANCE_STRING_LITERAL(" ");
ADVANCE_NUM_BYTES(cc_xml_attr_calculate_size(elt->attributes[c]));
}
const int numchildren = elt->children.getLength();
if (numchildren == 0) { // close element directly
ADVANCE_STRING_LITERAL("/>\n");
} else if ((numchildren == 1) &&
(strcmp(cc_xml_elt_get_type(elt->children[0]), COIN_XML_CDATA_TYPE) == 0)) {
ADVANCE_STRING_LITERAL(">");
ADVANCE_STRING(cc_xml_elt_get_cdata(elt->children[0]));
ADVANCE_STRING_LITERAL("</");
ADVANCE_STRING(elt->type);
ADVANCE_STRING_LITERAL(">\n");
} else {
ADVANCE_STRING_LITERAL(">\n");
for (c = 0; c < numchildren; ++c) {
ADVANCE_NUM_BYTES(cc_xml_elt_calculate_size(elt->children[c], indent + indentincrement, indentincrement));
}
ADVANCE_NUM_SPACES(indent);
ADVANCE_STRING_LITERAL("</");
ADVANCE_STRING(elt->type);
ADVANCE_STRING_LITERAL(">\n");
}
}
#undef ADVANCE_NUM_BYTES
#undef ADVANCE_NUM_SPACES
#undef ADVANCE_STRING
#undef ADVANCE_STRING_LITERAL
return bytes;
}
size_t
cc_xml_elt_write_to_buffer(const cc_xml_elt * elt, char * buffer, size_t bufsize, int indent, int indentincrement)
{
assert(elt);
assert(elt->type);
assert(indent >= 0);
const size_t assumed = cc_xml_elt_calculate_size(elt, indent, indentincrement);
size_t bytes = 0;
char * hereptr = buffer;
size_t bytesleft = bufsize;
// macro to advance buffer pointer and decrement bytesleft count
#define ADVANCE_NUM_BYTES(len) \
do { const int length = (len); \
bytes += length; \
hereptr += length; \
bytesleft -= length; } while (0)
// macro to copy in a string literal and advance pointers
#define ADVANCE_STRING_LITERAL(str) \
do { static const char strobj[] = str; \
const int strlength = (sizeof(strobj) - 1); \
strncpy(hereptr, strobj, strlength); \
ADVANCE_NUM_BYTES(strlength); } while (0)
// macro to copy in a run-time string and advance pointers
#define ADVANCE_STRING(str) \
do { const int strlength = static_cast<int>(strlen(str)); \
strncpy(hereptr, str, strlength); \
ADVANCE_NUM_BYTES(strlength); } while (0)
// macro to advance a number of blanks (indentation)
#define ADVANCE_NUM_SPACES(num) \
do { const int strlength = (num); \
memset(hereptr, ' ', strlength); \
ADVANCE_NUM_BYTES(strlength); } while (0)
// ***********************************************************************
// almost duplicate block - see cc_xml_elt_calculate_size()
if (elt->type && strcmp(elt->type, COIN_XML_CDATA_TYPE) == 0) {
// this is a leaf element character data container
ADVANCE_STRING(elt->cdata);
} else {
ADVANCE_NUM_SPACES(indent);
ADVANCE_STRING_LITERAL("<");
ADVANCE_STRING(elt->type);
int c;
const int numattributes = elt->attributes.getLength();
for (c = 0; c < numattributes; ++c) {
ADVANCE_STRING_LITERAL(" ");
ADVANCE_NUM_BYTES(cc_xml_attr_write_to_buffer(elt->attributes[c], hereptr, bytesleft));
}
const int numchildren = elt->children.getLength();
if (numchildren == 0) { // close element directly
ADVANCE_STRING_LITERAL("/>\n");
} else if ((numchildren == 1) &&
(strcmp(cc_xml_elt_get_type(elt->children[0]), COIN_XML_CDATA_TYPE) == 0)) {
ADVANCE_STRING_LITERAL(">");
ADVANCE_STRING(cc_xml_elt_get_cdata(elt->children[0]));
ADVANCE_STRING_LITERAL("</");
ADVANCE_STRING(elt->type);
ADVANCE_STRING_LITERAL(">\n");
} else {
ADVANCE_STRING_LITERAL(">\n");
for (c = 0; c < numchildren; ++c) {
ADVANCE_NUM_BYTES(cc_xml_elt_write_to_buffer(elt->children[c], hereptr, bytesleft, indent + indentincrement, indentincrement));
}
ADVANCE_NUM_SPACES(indent);
ADVANCE_STRING_LITERAL("</");
ADVANCE_STRING(elt->type);
ADVANCE_STRING_LITERAL(">\n");
}
}
#undef ADVANCE_NUM_BYTES
#undef ADVANCE_NUM_SPACES
#undef ADVANCE_STRING
#undef ADVANCE_STRING_LITERAL
assert(bytes == assumed);
return bytes;
}
// *************************************************************************
|