| 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 |
#include <assert.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
#include "scare.h" |
| 31 |
#include "scprotos.h" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
static const sc_uint PROP_MAGIC = 0x7927b2e0; |
| 36 |
enum |
| 37 |
{ PROP_GROW_INCREMENT = 32, |
| 38 |
MAX_INTEGER_KEY = 65535, |
| 39 |
NODE_POOL_CAPACITY = 512 |
| 40 |
}; |
| 41 |
static const sc_char NUL = '\0'; |
| 42 |
|
| 43 |
|
| 44 |
static sc_bool prop_trace = FALSE; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
typedef struct sc_prop_node_s |
| 53 |
{ |
| 54 |
sc_vartype_t name; |
| 55 |
sc_vartype_t property; |
| 56 |
|
| 57 |
struct sc_prop_node_s **child_list; |
| 58 |
} sc_prop_node_t; |
| 59 |
typedef sc_prop_node_t *sc_prop_noderef_t; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
typedef struct sc_prop_set_s |
| 68 |
{ |
| 69 |
sc_uint magic; |
| 70 |
sc_int dictionary_length; |
| 71 |
sc_char **dictionary; |
| 72 |
sc_int node_pools_length; |
| 73 |
sc_prop_noderef_t *node_pools; |
| 74 |
sc_int node_count; |
| 75 |
sc_int orphans_length; |
| 76 |
void **orphans; |
| 77 |
sc_bool is_readonly; |
| 78 |
sc_prop_noderef_t root_node; |
| 79 |
sc_tafref_t taf; |
| 80 |
} sc_prop_set_t; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
static sc_bool |
| 89 |
prop_is_valid (sc_prop_setref_t bundle) |
| 90 |
{ |
| 91 |
return bundle && bundle->magic == PROP_MAGIC; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
static sc_int |
| 101 |
prop_round_up (sc_int elements) |
| 102 |
{ |
| 103 |
sc_int extended; |
| 104 |
|
| 105 |
extended = elements + PROP_GROW_INCREMENT - 1; |
| 106 |
return (extended / PROP_GROW_INCREMENT) * PROP_GROW_INCREMENT; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
static void * |
| 122 |
prop_ensure_capacity (void *array, |
| 123 |
sc_int old_size, sc_int new_size, sc_int element_size) |
| 124 |
{ |
| 125 |
sc_int current, required; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
current = prop_round_up (old_size); |
| 132 |
required = prop_round_up (new_size); |
| 133 |
if (required > current) |
| 134 |
{ |
| 135 |
sc_byte *new_array, *start_clearing; |
| 136 |
|
| 137 |
|
| 138 |
new_array = sc_realloc (array, required * element_size); |
| 139 |
start_clearing = new_array + current * element_size; |
| 140 |
memset (start_clearing, 0, (required - current) * element_size); |
| 141 |
|
| 142 |
return new_array; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
return array; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
static void * |
| 158 |
prop_trim_capacity (void *array, sc_int size, sc_int element_size) |
| 159 |
{ |
| 160 |
if (prop_round_up (size) > size) |
| 161 |
return sc_realloc (array, size * element_size); |
| 162 |
else |
| 163 |
return array; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static int |
| 175 |
prop_compare (const void *string1, const void *string2) |
| 176 |
{ |
| 177 |
return strcmp (*(sc_char *const *) string1, *(sc_char *const *) string2); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
static const sc_char * |
| 190 |
prop_dictionary_lookup (sc_prop_setref_t bundle, const sc_char *string) |
| 191 |
{ |
| 192 |
sc_char *dict_string; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
if (bundle->dictionary_length > 0) |
| 200 |
{ |
| 201 |
const sc_char *const *dict_search; |
| 202 |
|
| 203 |
dict_search = bsearch (&string, bundle->dictionary, |
| 204 |
bundle->dictionary_length, |
| 205 |
sizeof (bundle->dictionary[0]), prop_compare); |
| 206 |
if (dict_search) |
| 207 |
return *dict_search; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
dict_string = sc_malloc (strlen (string) + 1); |
| 212 |
strcpy (dict_string, string); |
| 213 |
|
| 214 |
|
| 215 |
bundle->dictionary = prop_ensure_capacity (bundle->dictionary, |
| 216 |
bundle->dictionary_length, |
| 217 |
bundle->dictionary_length + 1, |
| 218 |
sizeof (bundle->dictionary[0])); |
| 219 |
|
| 220 |
|
| 221 |
bundle->dictionary[bundle->dictionary_length++] = dict_string; |
| 222 |
qsort (bundle->dictionary, |
| 223 |
bundle->dictionary_length, |
| 224 |
sizeof (bundle->dictionary[0]), prop_compare); |
| 225 |
|
| 226 |
|
| 227 |
return dict_string; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
static sc_prop_noderef_t |
| 240 |
prop_new_node (sc_prop_setref_t bundle) |
| 241 |
{ |
| 242 |
sc_int node_index; |
| 243 |
sc_prop_noderef_t node; |
| 244 |
|
| 245 |
|
| 246 |
node_index = bundle->node_count % NODE_POOL_CAPACITY; |
| 247 |
if (node_index == 0) |
| 248 |
{ |
| 249 |
sc_int required; |
| 250 |
|
| 251 |
|
| 252 |
bundle->node_pools = prop_ensure_capacity (bundle->node_pools, |
| 253 |
bundle->node_pools_length, |
| 254 |
bundle->node_pools_length + 1, |
| 255 |
sizeof (bundle-> |
| 256 |
node_pools[0])); |
| 257 |
|
| 258 |
|
| 259 |
required = NODE_POOL_CAPACITY * sizeof (*bundle->node_pools[0]); |
| 260 |
bundle->node_pools[bundle->node_pools_length] = sc_malloc (required); |
| 261 |
bundle->node_pools_length++; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
node = bundle->node_pools[bundle->node_pools_length - 1] + node_index; |
| 266 |
bundle->node_count++; |
| 267 |
|
| 268 |
|
| 269 |
return node; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
static sc_prop_noderef_t |
| 279 |
prop_find_child (sc_prop_noderef_t parent, sc_int type, sc_vartype_t name) |
| 280 |
{ |
| 281 |
|
| 282 |
if (parent->child_list) |
| 283 |
{ |
| 284 |
sc_int index_; |
| 285 |
sc_prop_noderef_t child; |
| 286 |
|
| 287 |
|
| 288 |
switch (type) |
| 289 |
{ |
| 290 |
case PROP_KEY_INTEGER: |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
if (name.integer < 0) |
| 296 |
sc_fatal ("prop_find_child: integer key cannot be negative\n"); |
| 297 |
else if (name.integer > MAX_INTEGER_KEY) |
| 298 |
sc_fatal ("prop_find_child: integer key is too large\n"); |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
if (name.integer >= 0 && name.integer < parent->property.integer) |
| 305 |
{ |
| 306 |
child = parent->child_list[name.integer]; |
| 307 |
return child; |
| 308 |
} |
| 309 |
break; |
| 310 |
|
| 311 |
case PROP_KEY_STRING: |
| 312 |
|
| 313 |
for (index_ = 0; index_ < parent->property.integer; index_++) |
| 314 |
{ |
| 315 |
child = parent->child_list[index_]; |
| 316 |
if (strcmp (child->name.string, name.string) == 0) |
| 317 |
break; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
if (index_ < parent->property.integer) |
| 322 |
{ |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
if (index_ > 0) |
| 330 |
{ |
| 331 |
memmove (parent->child_list + 1, |
| 332 |
parent->child_list, index_ * sizeof (child)); |
| 333 |
parent->child_list[0] = child; |
| 334 |
} |
| 335 |
return child; |
| 336 |
} |
| 337 |
break; |
| 338 |
|
| 339 |
default: |
| 340 |
sc_fatal ("prop_find_child: invalid key type\n"); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
return NULL; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
static sc_prop_noderef_t |
| 356 |
prop_add_child (sc_prop_noderef_t parent, |
| 357 |
sc_int type, sc_vartype_t name, sc_prop_setref_t bundle) |
| 358 |
{ |
| 359 |
sc_prop_noderef_t child; |
| 360 |
|
| 361 |
|
| 362 |
if (bundle->is_readonly) |
| 363 |
sc_fatal ("prop_add_child: can't add to readonly properties\n"); |
| 364 |
|
| 365 |
|
| 366 |
child = prop_new_node (bundle); |
| 367 |
switch (type) |
| 368 |
{ |
| 369 |
case PROP_KEY_INTEGER: |
| 370 |
child->name.integer = name.integer; |
| 371 |
break; |
| 372 |
case PROP_KEY_STRING: |
| 373 |
child->name.string = prop_dictionary_lookup (bundle, name.string); |
| 374 |
break; |
| 375 |
|
| 376 |
default: |
| 377 |
sc_fatal ("prop_add_child: invalid key type\n"); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
child->property.voidp = NULL; |
| 382 |
child->child_list = NULL; |
| 383 |
|
| 384 |
|
| 385 |
if (!parent->child_list && parent->property.voidp) |
| 386 |
sc_error ("prop_add_child: node overwritten, probable data loss\n"); |
| 387 |
|
| 388 |
|
| 389 |
switch (type) |
| 390 |
{ |
| 391 |
case PROP_KEY_INTEGER: |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
if (name.integer < 0) |
| 399 |
sc_fatal ("prop_add_child: integer key cannot be negative\n"); |
| 400 |
else if (name.integer > MAX_INTEGER_KEY) |
| 401 |
sc_fatal ("prop_add_child: integer key is too large\n"); |
| 402 |
|
| 403 |
|
| 404 |
parent->child_list = prop_ensure_capacity (parent->child_list, |
| 405 |
parent->property.integer, |
| 406 |
name.integer + 1, |
| 407 |
sizeof (*parent->child_list)); |
| 408 |
|
| 409 |
|
| 410 |
if (parent->property.integer <= name.integer) |
| 411 |
parent->property.integer = name.integer + 1; |
| 412 |
|
| 413 |
|
| 414 |
parent->child_list[name.integer] = child; |
| 415 |
break; |
| 416 |
|
| 417 |
case PROP_KEY_STRING: |
| 418 |
|
| 419 |
parent->child_list = prop_ensure_capacity (parent->child_list, |
| 420 |
parent->property.integer, |
| 421 |
parent->property.integer + 1, |
| 422 |
sizeof (*parent->child_list)); |
| 423 |
|
| 424 |
|
| 425 |
parent->child_list[parent->property.integer++] = child; |
| 426 |
break; |
| 427 |
|
| 428 |
default: |
| 429 |
sc_fatal ("prop_add_child: invalid key type\n"); |
| 430 |
} |
| 431 |
|
| 432 |
return child; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
void |
| 451 |
prop_put (sc_prop_setref_t bundle, const sc_char *format, |
| 452 |
sc_vartype_t vt_value, const sc_vartype_t vt_key[]) |
| 453 |
{ |
| 454 |
sc_prop_noderef_t node; |
| 455 |
sc_int index_; |
| 456 |
assert (prop_is_valid (bundle)); |
| 457 |
|
| 458 |
|
| 459 |
if (!format || format[0] == NUL |
| 460 |
|| format[1] != '-' || format[2] != '>' || format[3] == NUL) |
| 461 |
sc_fatal ("prop_put: format error\n"); |
| 462 |
|
| 463 |
|
| 464 |
if (prop_trace) |
| 465 |
{ |
| 466 |
sc_trace ("Property: put "); |
| 467 |
switch (format[0]) |
| 468 |
{ |
| 469 |
case PROP_STRING: |
| 470 |
sc_trace ("\"%s\"", vt_value.string); |
| 471 |
break; |
| 472 |
case PROP_INTEGER: |
| 473 |
sc_trace ("%ld", vt_value.integer); |
| 474 |
break; |
| 475 |
case PROP_BOOLEAN: |
| 476 |
sc_trace ("%s", vt_value.boolean ? "true" : "false"); |
| 477 |
break; |
| 478 |
|
| 479 |
default: |
| 480 |
sc_trace ("%p [invalid type]", vt_value.voidp); |
| 481 |
break; |
| 482 |
} |
| 483 |
sc_trace (", key \"%s\" : ", format); |
| 484 |
for (index_ = 0; format[index_ + 3] != NUL; index_++) |
| 485 |
{ |
| 486 |
sc_trace ("%s", index_ > 0 ? "," : ""); |
| 487 |
switch (format[index_ + 3]) |
| 488 |
{ |
| 489 |
case PROP_KEY_STRING: |
| 490 |
sc_trace ("\"%s\"", vt_key[index_].string); |
| 491 |
break; |
| 492 |
case PROP_KEY_INTEGER: |
| 493 |
sc_trace ("%ld", vt_key[index_].integer); |
| 494 |
break; |
| 495 |
|
| 496 |
default: |
| 497 |
sc_trace ("%p [invalid type]", vt_key[index_].voidp); |
| 498 |
break; |
| 499 |
} |
| 500 |
} |
| 501 |
sc_trace ("\n"); |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
node = bundle->root_node; |
| 509 |
for (index_ = 0; format[index_ + 3] != NUL; index_++) |
| 510 |
{ |
| 511 |
sc_prop_noderef_t child; |
| 512 |
sc_int type; |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
type = format[index_ + 3]; |
| 520 |
child = prop_find_child (node, type, vt_key[index_]); |
| 521 |
if (child) |
| 522 |
node = child; |
| 523 |
else |
| 524 |
node = prop_add_child (node, type, vt_key[index_], bundle); |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
if (node->child_list) |
| 531 |
sc_fatal ("prop_put: overwrite of internal node\n"); |
| 532 |
|
| 533 |
|
| 534 |
switch (format[0]) |
| 535 |
{ |
| 536 |
case PROP_INTEGER: |
| 537 |
node->property.integer = vt_value.integer; |
| 538 |
break; |
| 539 |
case PROP_BOOLEAN: |
| 540 |
node->property.boolean = vt_value.boolean; |
| 541 |
break; |
| 542 |
case PROP_STRING: |
| 543 |
node->property.string = vt_value.string; |
| 544 |
break; |
| 545 |
|
| 546 |
default: |
| 547 |
sc_fatal ("prop_put: invalid property type\n"); |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
sc_bool |
| 559 |
prop_get (sc_prop_setref_t bundle, const sc_char *format, |
| 560 |
sc_vartype_t *vt_rvalue, const sc_vartype_t vt_key[]) |
| 561 |
{ |
| 562 |
sc_prop_noderef_t node; |
| 563 |
sc_int index_; |
| 564 |
assert (prop_is_valid (bundle)); |
| 565 |
|
| 566 |
|
| 567 |
if (!format || format[0] == NUL |
| 568 |
|| format[1] != '<' || format[2] != '-' || format[3] == NUL) |
| 569 |
sc_fatal ("prop_get: format error\n"); |
| 570 |
|
| 571 |
|
| 572 |
if (prop_trace) |
| 573 |
{ |
| 574 |
sc_trace ("Property: get, key \"%s\" : ", format); |
| 575 |
for (index_ = 0; format[index_ + 3] != NUL; index_++) |
| 576 |
{ |
| 577 |
sc_trace ("%s", index_ > 0 ? "," : ""); |
| 578 |
switch (format[index_ + 3]) |
| 579 |
{ |
| 580 |
case PROP_KEY_STRING: |
| 581 |
sc_trace ("\"%s\"", vt_key[index_].string); |
| 582 |
break; |
| 583 |
case PROP_KEY_INTEGER: |
| 584 |
sc_trace ("%ld", vt_key[index_].integer); |
| 585 |
break; |
| 586 |
|
| 587 |
default: |
| 588 |
sc_trace ("%p [invalid type]", vt_key[index_].voidp); |
| 589 |
break; |
| 590 |
} |
| 591 |
} |
| 592 |
sc_trace ("\n"); |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
node = bundle->root_node; |
| 600 |
for (index_ = 0; format[index_ + 3] != NUL; index_++) |
| 601 |
{ |
| 602 |
sc_int type; |
| 603 |
|
| 604 |
|
| 605 |
type = format[index_ + 3 ]; |
| 606 |
node = prop_find_child (node, type, vt_key[index_]); |
| 607 |
if (!node) |
| 608 |
break; |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
if (!node) |
| 613 |
{ |
| 614 |
if (prop_trace) |
| 615 |
sc_trace ("Property: ...get FAILED\n"); |
| 616 |
|
| 617 |
return FALSE; |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
if (node->child_list && format[0] != PROP_INTEGER) |
| 625 |
sc_fatal ("prop_get: only integer gets on internal nodes\n"); |
| 626 |
|
| 627 |
|
| 628 |
switch (format[0]) |
| 629 |
{ |
| 630 |
case PROP_INTEGER: |
| 631 |
vt_rvalue->integer = node->property.integer; |
| 632 |
break; |
| 633 |
case PROP_BOOLEAN: |
| 634 |
vt_rvalue->boolean = node->property.boolean; |
| 635 |
break; |
| 636 |
case PROP_STRING: |
| 637 |
vt_rvalue->string = node->property.string; |
| 638 |
break; |
| 639 |
|
| 640 |
default: |
| 641 |
sc_fatal ("prop_get: invalid property type\n"); |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
if (prop_trace) |
| 646 |
{ |
| 647 |
sc_trace ("Property: ...get returned : "); |
| 648 |
switch (format[0]) |
| 649 |
{ |
| 650 |
case PROP_STRING: |
| 651 |
sc_trace ("\"%s\"", vt_rvalue->string); |
| 652 |
break; |
| 653 |
case PROP_INTEGER: |
| 654 |
sc_trace ("%ld", vt_rvalue->integer); |
| 655 |
break; |
| 656 |
case PROP_BOOLEAN: |
| 657 |
sc_trace ("%s", vt_rvalue->boolean ? "true" : "false"); |
| 658 |
break; |
| 659 |
|
| 660 |
default: |
| 661 |
sc_trace ("%p [invalid type]", vt_rvalue->voidp); |
| 662 |
break; |
| 663 |
} |
| 664 |
sc_trace ("\n"); |
| 665 |
} |
| 666 |
return TRUE; |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
static void |
| 678 |
prop_trim_node (sc_prop_noderef_t node) |
| 679 |
{ |
| 680 |
|
| 681 |
if (node && node->child_list) |
| 682 |
{ |
| 683 |
sc_int index_; |
| 684 |
|
| 685 |
|
| 686 |
for (index_ = 0; index_ < node->property.integer; index_++) |
| 687 |
prop_trim_node (node->child_list[index_]); |
| 688 |
|
| 689 |
|
| 690 |
node->child_list = prop_trim_capacity (node->child_list, |
| 691 |
node->property.integer, |
| 692 |
sizeof (*node->child_list)); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
void |
| 697 |
prop_solidify (sc_prop_setref_t bundle) |
| 698 |
{ |
| 699 |
assert (prop_is_valid (bundle)); |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
bundle->dictionary = prop_trim_capacity (bundle->dictionary, |
| 709 |
bundle->dictionary_length, |
| 710 |
sizeof (bundle->dictionary[0])); |
| 711 |
bundle->node_pools = prop_trim_capacity (bundle->node_pools, |
| 712 |
bundle->node_pools_length, |
| 713 |
sizeof (bundle->node_pools[0])); |
| 714 |
bundle->orphans = prop_trim_capacity (bundle->orphans, |
| 715 |
bundle->orphans_length, |
| 716 |
sizeof (bundle->orphans[0])); |
| 717 |
prop_trim_node (bundle->root_node); |
| 718 |
|
| 719 |
|
| 720 |
bundle->is_readonly = TRUE; |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
sc_int |
| 733 |
prop_get_integer (sc_prop_setref_t bundle, |
| 734 |
const sc_char *format, const sc_vartype_t vt_key[]) |
| 735 |
{ |
| 736 |
sc_vartype_t vt_rvalue; |
| 737 |
assert (format[0] == PROP_INTEGER); |
| 738 |
|
| 739 |
if (!prop_get (bundle, format, &vt_rvalue, vt_key)) |
| 740 |
sc_fatal ("prop_get_integer: can't retrieve property\n"); |
| 741 |
|
| 742 |
return vt_rvalue.integer; |
| 743 |
} |
| 744 |
|
| 745 |
sc_bool |
| 746 |
prop_get_boolean (sc_prop_setref_t bundle, |
| 747 |
const sc_char *format, const sc_vartype_t vt_key[]) |
| 748 |
{ |
| 749 |
sc_vartype_t vt_rvalue; |
| 750 |
assert (format[0] == PROP_BOOLEAN); |
| 751 |
|
| 752 |
if (!prop_get (bundle, format, &vt_rvalue, vt_key)) |
| 753 |
sc_fatal ("prop_get_boolean: can't retrieve property\n"); |
| 754 |
|
| 755 |
return vt_rvalue.boolean; |
| 756 |
} |
| 757 |
|
| 758 |
const sc_char * |
| 759 |
prop_get_string (sc_prop_setref_t bundle, |
| 760 |
const sc_char *format, const sc_vartype_t vt_key[]) |
| 761 |
{ |
| 762 |
sc_vartype_t vt_rvalue; |
| 763 |
assert (format[0] == PROP_STRING); |
| 764 |
|
| 765 |
if (!prop_get (bundle, format, &vt_rvalue, vt_key)) |
| 766 |
sc_fatal ("prop_get_string: can't retrieve property\n"); |
| 767 |
|
| 768 |
return vt_rvalue.string; |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
sc_int |
| 779 |
prop_get_child_count (sc_prop_setref_t bundle, |
| 780 |
const sc_char *format, const sc_vartype_t vt_key[]) |
| 781 |
{ |
| 782 |
sc_vartype_t vt_rvalue; |
| 783 |
assert (format[0] == PROP_INTEGER); |
| 784 |
|
| 785 |
if (!prop_get (bundle, format, &vt_rvalue, vt_key)) |
| 786 |
return 0; |
| 787 |
|
| 788 |
|
| 789 |
return vt_rvalue.integer; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
static sc_prop_setref_t |
| 799 |
prop_create_empty (void) |
| 800 |
{ |
| 801 |
sc_prop_setref_t bundle; |
| 802 |
|
| 803 |
|
| 804 |
bundle = sc_malloc (sizeof (*bundle)); |
| 805 |
bundle->magic = PROP_MAGIC; |
| 806 |
|
| 807 |
|
| 808 |
bundle->dictionary_length = 0; |
| 809 |
bundle->dictionary = NULL; |
| 810 |
|
| 811 |
|
| 812 |
bundle->node_pools_length = 0; |
| 813 |
bundle->node_pools = NULL; |
| 814 |
bundle->node_count = 0; |
| 815 |
|
| 816 |
|
| 817 |
bundle->orphans_length = 0; |
| 818 |
bundle->orphans = NULL; |
| 819 |
|
| 820 |
|
| 821 |
bundle->is_readonly = FALSE; |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
bundle->root_node = prop_new_node (bundle); |
| 828 |
bundle->root_node->child_list = NULL; |
| 829 |
bundle->root_node->name.string = "ROOT"; |
| 830 |
bundle->root_node->property.voidp = NULL; |
| 831 |
|
| 832 |
|
| 833 |
bundle->taf = NULL; |
| 834 |
|
| 835 |
return bundle; |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
static void |
| 846 |
prop_destroy_child_list (sc_prop_noderef_t node) |
| 847 |
{ |
| 848 |
|
| 849 |
if (node && node->child_list) |
| 850 |
{ |
| 851 |
sc_int index_; |
| 852 |
|
| 853 |
|
| 854 |
for (index_ = 0; index_ < node->property.integer; index_++) |
| 855 |
prop_destroy_child_list (node->child_list[index_]); |
| 856 |
|
| 857 |
|
| 858 |
sc_free (node->child_list); |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
void |
| 863 |
prop_destroy (sc_prop_setref_t bundle) |
| 864 |
{ |
| 865 |
sc_int index_; |
| 866 |
assert (prop_is_valid (bundle)); |
| 867 |
|
| 868 |
|
| 869 |
for (index_ = 0; index_ < bundle->dictionary_length; index_++) |
| 870 |
sc_free (bundle->dictionary[index_]); |
| 871 |
bundle->dictionary_length = 0; |
| 872 |
sc_free (bundle->dictionary); |
| 873 |
bundle->dictionary = NULL; |
| 874 |
|
| 875 |
|
| 876 |
for (index_ = 0; index_ < bundle->orphans_length; index_++) |
| 877 |
sc_free (bundle->orphans[index_]); |
| 878 |
bundle->orphans_length = 0; |
| 879 |
sc_free (bundle->orphans); |
| 880 |
bundle->orphans = NULL; |
| 881 |
|
| 882 |
|
| 883 |
prop_destroy_child_list (bundle->root_node); |
| 884 |
bundle->root_node = NULL; |
| 885 |
|
| 886 |
|
| 887 |
for (index_ = 0; index_ < bundle->node_pools_length; index_++) |
| 888 |
sc_free (bundle->node_pools[index_]); |
| 889 |
bundle->node_pools_length = 0; |
| 890 |
sc_free (bundle->node_pools); |
| 891 |
bundle->node_pools = NULL; |
| 892 |
|
| 893 |
|
| 894 |
if (bundle->taf) |
| 895 |
taf_destroy (bundle->taf); |
| 896 |
|
| 897 |
|
| 898 |
memset (bundle, 0xaa, sizeof (*bundle)); |
| 899 |
sc_free (bundle); |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
sc_prop_setref_t |
| 909 |
prop_create (const sc_tafref_t taf) |
| 910 |
{ |
| 911 |
sc_prop_setref_t bundle; |
| 912 |
|
| 913 |
|
| 914 |
bundle = prop_create_empty (); |
| 915 |
|
| 916 |
|
| 917 |
if (!parse_game (taf, bundle)) |
| 918 |
{ |
| 919 |
prop_destroy (bundle); |
| 920 |
return NULL; |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
bundle->taf = taf; |
| 925 |
return bundle; |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
void |
| 935 |
prop_adopt (sc_prop_setref_t bundle, void *addr) |
| 936 |
{ |
| 937 |
assert (prop_is_valid (bundle)); |
| 938 |
|
| 939 |
|
| 940 |
bundle->orphans = prop_ensure_capacity (bundle->orphans, |
| 941 |
bundle->orphans_length, |
| 942 |
bundle->orphans_length + 1, |
| 943 |
sizeof (bundle->orphans[0])); |
| 944 |
|
| 945 |
|
| 946 |
bundle->orphans[bundle->orphans_length++] = addr; |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
static sc_bool |
| 958 |
prop_debug_is_dictionary_string (sc_prop_setref_t bundle, const void *pointer) |
| 959 |
{ |
| 960 |
const sc_char *const pointer_ = pointer; |
| 961 |
sc_int index_; |
| 962 |
|
| 963 |
|
| 964 |
for (index_ = 0; index_ < bundle->dictionary_length; index_++) |
| 965 |
{ |
| 966 |
if (bundle->dictionary[index_] == pointer_) |
| 967 |
return TRUE; |
| 968 |
} |
| 969 |
|
| 970 |
return FALSE; |
| 971 |
} |
| 972 |
|
| 973 |
static void |
| 974 |
prop_debug_dump_node (sc_prop_setref_t bundle, |
| 975 |
sc_int depth, sc_int child_index, sc_prop_noderef_t node) |
| 976 |
{ |
| 977 |
sc_int index_; |
| 978 |
|
| 979 |
|
| 980 |
for (index_ = 0; index_ < depth; index_++) |
| 981 |
sc_trace (" "); |
| 982 |
sc_trace ("%ld : %p", child_index, (void *) node); |
| 983 |
|
| 984 |
|
| 985 |
if (node) |
| 986 |
{ |
| 987 |
|
| 988 |
sc_trace (", name %p", node->name.voidp); |
| 989 |
if (node != bundle->root_node) |
| 990 |
{ |
| 991 |
if (prop_debug_is_dictionary_string (bundle, node->name.string)) |
| 992 |
sc_trace (" \"%s\"", node->name.string); |
| 993 |
else |
| 994 |
sc_trace (" %ld", node->name.integer); |
| 995 |
} |
| 996 |
|
| 997 |
if (node->child_list) |
| 998 |
{ |
| 999 |
|
| 1000 |
sc_trace (", child count %ld\n", node->property.integer); |
| 1001 |
for (index_ = 0; index_ < node->property.integer; index_++) |
| 1002 |
{ |
| 1003 |
prop_debug_dump_node (bundle, depth + 1, |
| 1004 |
index_, node->child_list[index_]); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
else |
| 1008 |
{ |
| 1009 |
|
| 1010 |
sc_trace (", property %p", node->property.voidp); |
| 1011 |
if (taf_debug_is_taf_string (bundle->taf, node->property.string)) |
| 1012 |
sc_trace (" \"%s\"\n", node->property.string); |
| 1013 |
else |
| 1014 |
sc_trace (" %ld\n", node->property.integer); |
| 1015 |
} |
| 1016 |
} |
| 1017 |
else |
| 1018 |
sc_trace ("\n"); |
| 1019 |
} |
| 1020 |
|
| 1021 |
void |
| 1022 |
prop_debug_dump (sc_prop_setref_t bundle) |
| 1023 |
{ |
| 1024 |
sc_int index_; |
| 1025 |
assert (prop_is_valid (bundle)); |
| 1026 |
|
| 1027 |
|
| 1028 |
sc_trace ("Property: debug dump follows...\n"); |
| 1029 |
sc_trace ("bundle->is_readonly = %s\n", |
| 1030 |
bundle->is_readonly ? "true" : "false"); |
| 1031 |
sc_trace ("bundle->dictionary_length = %ld\n", bundle->dictionary_length); |
| 1032 |
|
| 1033 |
sc_trace ("bundle->dictionary =\n"); |
| 1034 |
for (index_ = 0; index_ < bundle->dictionary_length; index_++) |
| 1035 |
{ |
| 1036 |
sc_trace ("%3ld : %p \"%s\"\n", index_, |
| 1037 |
bundle->dictionary[index_], bundle->dictionary[index_]); |
| 1038 |
} |
| 1039 |
|
| 1040 |
sc_trace ("bundle->node_pools_length = %ld\n", bundle->node_pools_length); |
| 1041 |
|
| 1042 |
sc_trace ("bundle->node_pools =\n"); |
| 1043 |
for (index_ = 0; index_ < bundle->node_pools_length; index_++) |
| 1044 |
sc_trace ("%3ld : %p\n", index_, (void *) bundle->node_pools[index_]); |
| 1045 |
|
| 1046 |
sc_trace ("bundle->node_count = %ld\n", bundle->node_count); |
| 1047 |
sc_trace ("bundle->root_node = {\n"); |
| 1048 |
prop_debug_dump_node (bundle, 0, 0, bundle->root_node); |
| 1049 |
sc_trace ("}\nbundle->taf = %p\n", (void *) bundle->taf); |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
void |
| 1059 |
prop_debug_trace (sc_bool flag) |
| 1060 |
{ |
| 1061 |
prop_trace = flag; |
| 1062 |
} |