Debian Codegraph @

terps/scare/scprops.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o ...
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 /* Assorted definitions and constants. */
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 /* Properties trace flag. */
44 static sc_bool prop_trace = FALSE;
45
46
47 /*
48 * Property tree node definition, uses a child list representation for
49 * fast lookup on indexed nodes. Name is a variable type, as is property,
50 * which is also overloaded to contain the child count for internal nodes.
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 * Properties set structure. This is a set of properties, on which the
63 * properties functions operate (a properties "object"). Node string
64 * names are held in a dictionary to help save space. To avoid excessive
65 * malloc'ing of nodes, new nodes are preallocated in pools.
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 * prop_is_valid()
85 *
86 * Return TRUE if pointer is a valid properties set, FALSE otherwise.
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 * prop_round_up()
97 *
98 * Round up a count of elements to the next block of grow increments.
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 * prop_ensure_capacity()
112 *
113 * Ensure that capacity exists in a growable array for a given number of
114 * elements, growing if necessary.
115 *
116 * Some libc's allocate generously on realloc(), and some not. Those that
117 * don't will thrash badly if we realloc() on each grow, so here we try to
118 * realloc() in blocks of elements, and thus need to realloc() much less
119 * frequently.
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 * See if there's any resize necessary, that is, does the new size round up
129 * to a larger number of elements than the old size.
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 /* Grow array to the required size, and zero new elements. */
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 /* No resize necessary. */
146 return array;
147 }
148
149
150 /*
151 * prop_trim_capacity()
152 *
153 * Trim an array allocation back to the bare minimum required. This will
154 * "unblock" the allocations of prop_ensure_capacity(). Once trimmed,
155 * the array cannot ever be grown safely again.
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 * prop_compare()
169 *
170 * String comparison routine for sorting and searching dictionary strings.
171 * The function has return type "int" to match the libc implementations of
172 * bsearch() and qsort().
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 * prop_dictionary_lookup()
183 *
184 * Find a string in the dictionary. If the string is not present, the
185 * function will add it. The function returns the string's address, if
186 * either added or already present. Any new dictionary entry will
187 * contain a malloced copy of the string passed in.
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 * Search the existing dictionary for the string. Although not GNU libc,
196 * some libc's loop or crash when given a list of zero length, so we need to
197 * trap that here.
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 /* Not found, so copy the string for dictionary insertion. */
211 dict_string = sc_malloc (strlen (string) + 1);
212 strcpy (dict_string, string);
213
214 /* Extend the dictionary if necessary. */
215 bundle->dictionary = prop_ensure_capacity (bundle->dictionary,
216 bundle->dictionary_length,
217 bundle->dictionary_length + 1,
218 sizeof (bundle->dictionary[0]));
219
220 /* Add the new entry to the end of the dictionary array, and sort. */
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 /* Return the address of the new string. */
227 return dict_string;
228 }
229
230
231 /*
232 * prop_new_node()
233 *
234 * Return the address of the next free properties node from the node pool.
235 * Using a pool gives a performance boost; the number of properties nodes
236 * for even a small game is large, and preallocating pools avoids excessive
237 * malloc's of small individual nodes.
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 /* See if we need to create a new node pool. */
246 node_index = bundle->node_count % NODE_POOL_CAPACITY;
247 if (node_index == 0)
248 {
249 sc_int required;
250
251 /* Extend the node pools array if necessary. */
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 /* Create a new node pool, and increment the length. */
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 /* Find the next node address, and increment the node counter. */
265 node = bundle->node_pools[bundle->node_pools_length - 1] + node_index;
266 bundle->node_count++;
267
268 /* Return the new node. */
269 return node;
270 }
271
272
273 /*
274 * prop_find_child()
275 *
276 * Find a child node of the given parent whose name matches that passed in.
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 /* See if this node has any children. */
282 if (parent->child_list)
283 {
284 sc_int index_;
285 sc_prop_noderef_t child;
286
287 /* Do the lookup based on name type. */
288 switch (type)
289 {
290 case PROP_KEY_INTEGER:
291 /*
292 * As with adding a child below, here we'll range-check an integer
293 * key just to make sure nobody has any unreal expectations of us.
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 * For integer lookups, return the child at the indexed offset
302 * directly, provided it exists.
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 /* Scan children for a string name match. */
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 /* Return child if we found a match. */
321 if (index_ < parent->property.integer)
322 {
323 /*
324 * Before returning the child, try to improve future scans by
325 * moving the matched entry to index_ 0 -- this gives a key set
326 * sorted by recent usage, helpful as the same string key is
327 * used repeatedly in loops.
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 /* No matching child found. */
345 return NULL;
346 }
347
348
349 /*
350 * prop_add_child()
351 *
352 * Add a new child node to the given parent. Return its reference. Set
353 * needs to be passed so that string names can be added to the dictionary.
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 /* Not possible if growable allocations have been trimmed. */
362 if (bundle->is_readonly)
363 sc_fatal ("prop_add_child: can't add to readonly properties\n");
364
365 /* Create the new node. */
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 /* Initialize property and child list to visible nulls. */
381 child->property.voidp = NULL;
382 child->child_list = NULL;
383
384 /* Make a brief check for obvious overwrites. */
385 if (!parent->child_list && parent->property.voidp)
386 sc_error ("prop_add_child: node overwritten, probable data loss\n");
387
388 /* Add the child to the parent, position dependent on key type. */
389 switch (type)
390 {
391 case PROP_KEY_INTEGER:
392 /*
393 * Range check on integer keys, must be >= 0 for direct indexing to work,
394 * and we'll also apply a reasonableness constraint, to try to catch
395 * errors where string pointers are passed in as integers, which would
396 * otherwise lead to some extreme malloc() attempts.
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 /* Resize the parent's child list if necessary. */
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 /* Update the child count if the new node increases it. */
410 if (parent->property.integer <= name.integer)
411 parent->property.integer = name.integer + 1;
412
413 /* Store the child in its indexed list location. */
414 parent->child_list[name.integer] = child;
415 break;
416
417 case PROP_KEY_STRING:
418 /* Add a single entry to the child list, and resize. */
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 /* Store the child at the end of the list. */
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 * prop_put()
438 *
439 * Add a property to a properties set. Duplicate entries will replace
440 * prior ones.
441 *
442 * Stores a value of variable type as a property. The value type is one of
443 * 'I', 'B', or 'S', for integer, boolean, and string values, held in the
444 * first character of format. The next two characters of format are "->",
445 * and are syntactic sugar. The remainder of format shows the key makeup,
446 * with 'i' indicating integer, and 's' string key elements. Example format:
447 * "I->sssis", stores an integer, with a key composed of three strings, an
448 * integer, and another string.
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 /* Format check. */
459 if (!format || format[0] == NUL
460 || format[1] != '-' || format[2] != '>' || format[3] == NUL)
461 sc_fatal ("prop_put: format error\n");
462
463 /* Trace property put. */
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 * Iterate keys, finding matching child nodes at each level. If no matching
506 * child is found, insert one and continue.
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 * Search this level for a name matching the key. If found, advance
516 * to that child node. Otherwise, add the node to the tree, including
517 * the set so that the dictionary can be extended.
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 * Ensure that we're not about to overwrite an internal node child count.
529 */
530 if (node->child_list)
531 sc_fatal ("prop_put: overwrite of internal node\n");
532
533 /* Set our properties in the final node. */
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 * prop_get()
554 *
555 * Retrieve a property from a properties set. Format stuff as above, except
556 * with "->" replaced with "<-". Returns FALSE if no such property exists.
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 /* Format check. */
567 if (!format || format[0] == NUL
568 || format[1] != '<' || format[2] != '-' || format[3] == NUL)
569 sc_fatal ("prop_get: format error\n");
570
571 /* Trace property get. */
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 * Iterate keys, finding matching child nodes at each level. Stop if no
597 * matching child is found.
598 */
599 node = bundle->root_node;
600 for (index_ = 0; format[index_ + 3] != NUL; index_++)
601 {
602 sc_int type;
603
604 /* Move node down to the matching child, NULL if no match. */
605 type = format[index_ + 3 ];
606 node = prop_find_child (node, type, vt_key[index_]);
607 if (!node)
608 break;
609 }
610
611 /* If key iteration halted because no child was found, return FALSE. */
612 if (!node)
613 {
614 if (prop_trace)
615 sc_trace ("Property: ...get FAILED\n");
616
617 return FALSE;
618 }
619
620 /*
621 * Enforce integer-only queries on internal nodes, since this is the only
622 * type of query that makes sense -- any other type is probably a mistake.
623 */
624 if (node->child_list && format[0] != PROP_INTEGER)
625 sc_fatal ("prop_get: only integer gets on internal nodes\n");
626
627 /* Return the properties of the final node. */
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 /* Complete tracing property get. */
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 * prop_trim_node()
672 * prop_solidify()
673 *
674 * Trim excess allocation from growable arrays, and fix the properties set
675 * so that no further property insertions are allowed.
676 */
677 static void
678 prop_trim_node (sc_prop_noderef_t node)
679 {
680 /* End recursion on null or childless node. */
681 if (node && node->child_list)
682 {
683 sc_int index_;
684
685 /* Recursively trim allocation on children. */
686 for (index_ = 0; index_ < node->property.integer; index_++)
687 prop_trim_node (node->child_list[index_]);
688
689 /* Trim allocation on this node. */
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 * Trim back the dictionary, orphans, pools array, and every internal tree
703 * node. The one thing _not_ to trim is the final node pool -- there are
704 * references to nodes within it strewn all over the properties tree, and
705 * it's a large job to try to find and update them; instead, we just live
706 * with a little wasted heap memory.
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 /* Set the bundle so that no more properties can be added. */
720 bundle->is_readonly = TRUE;
721 }
722
723
724 /*
725 * prop_get_integer()
726 * prop_get_boolean()
727 * prop_get_string()
728 *
729 * Convenience functions to retrieve a property of a known type directly.
730 * It is an error for the property not to exist on retrieval.
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 * prop_get_child_count()
774 *
775 * Convenience function to retrieve a count of child properties available
776 * for a given property. Returns zero if the property does not exist.
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 /* Return overloaded integer property value, the child count. */
789 return vt_rvalue.integer;
790 }
791
792
793 /*
794 * prop_create_empty()
795 *
796 * Create a new, empty properties set, and return it.
797 */
798 static sc_prop_setref_t
799 prop_create_empty (void)
800 {
801 sc_prop_setref_t bundle;
802
803 /* Create a new, empty set. */
804 bundle = sc_malloc (sizeof (*bundle));
805 bundle->magic = PROP_MAGIC;
806
807 /* Begin with an empty strings dictionary. */
808 bundle->dictionary_length = 0;
809 bundle->dictionary = NULL;
810
811 /* Begin with no allocated node pools. */
812 bundle->node_pools_length = 0;
813 bundle->node_pools = NULL;
814 bundle->node_count = 0;
815
816 /* Begin with no adopted addresses. */
817 bundle->orphans_length = 0;
818 bundle->orphans = NULL;
819
820 /* Leave open for insertions. */
821 bundle->is_readonly = FALSE;
822
823 /*
824 * Start the set off with a root node. This will also kick off node pools,
825 * ensuring that every set has at least one node and one allocated pool.
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 /* No taf is yet connected with this set. */
833 bundle->taf = NULL;
834
835 return bundle;
836 }
837
838
839 /*
840 * prop_destroy_child_list()
841 * prop_destroy()
842 *
843 * Free set memory, and destroy a properties set structure.
844 */
845 static void
846 prop_destroy_child_list (sc_prop_noderef_t node)
847 {
848 /* End recursion on null or childless node. */
849 if (node && node->child_list)
850 {
851 sc_int index_;
852
853 /* Recursively destroy the children's child lists. */
854 for (index_ = 0; index_ < node->property.integer; index_++)
855 prop_destroy_child_list (node->child_list[index_]);
856
857 /* Free our own child list. */
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 /* Destroy the dictionary, and free it. */
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 /* Free adopted addresses. */
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 /* Walk the tree, destroying the child list for each node found. */
883 prop_destroy_child_list (bundle->root_node);
884 bundle->root_node = NULL;
885
886 /* Destroy each node pool. */
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 /* Destroy any taf associated with the bundle. */
894 if (bundle->taf)
895 taf_destroy (bundle->taf);
896
897 /* Poison and free the bundle. */
898 memset (bundle, 0xaa, sizeof (*bundle));
899 sc_free (bundle);
900 }
901
902
903 /*
904 * prop_create()
905 *
906 * Create a new properties set based on a taf, and return it.
907 */
908 sc_prop_setref_t
909 prop_create (const sc_tafref_t taf)
910 {
911 sc_prop_setref_t bundle;
912
913 /* Create a new, empty set. */
914 bundle = prop_create_empty ();
915
916 /* Populate it with data parsed from the taf file. */
917 if (!parse_game (taf, bundle))
918 {
919 prop_destroy (bundle);
920 return NULL;
921 }
922
923 /* Note the taf for destruction later, and return the new set. */
924 bundle->taf = taf;
925 return bundle;
926 }
927
928
929 /*
930 * prop_adopt()
931 *
932 * Adopt a memory address for free'ing on destroy.
933 */
934 void
935 prop_adopt (sc_prop_setref_t bundle, void *addr)
936 {
937 assert (prop_is_valid (bundle));
938
939 /* Extend the orphans array if necessary. */
940 bundle->orphans = prop_ensure_capacity (bundle->orphans,
941 bundle->orphans_length,
942 bundle->orphans_length + 1,
943 sizeof (bundle->orphans[0]));
944
945 /* Add the new address to the end of the array. */
946 bundle->orphans[bundle->orphans_length++] = addr;
947 }
948
949
950 /*
951 * prop_debug_is_dictionary_string()
952 * prop_debug_dump_node()
953 * prop_debug_dump()
954 *
955 * Print out a complete properties set.
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 /* Compare by pointer directly, not by string value comparisons. */
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 /* Write node preamble, indented two spaces for each depth count. */
980 for (index_ = 0; index_ < depth; index_++)
981 sc_trace (" ");
982 sc_trace ("%ld : %p", child_index, (void *) node);
983
984 /* Write node, or just a newline if none. */
985 if (node)
986 {
987 /* Print out the node's key, as hex and either string or decimal. */
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 /* Recursively dump children. */
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 /* Print out the node's property, again hex and string or decimal. */
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 /* Dump complete structure. */
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 * prop_debug_trace()
1055 *
1056 * Set property tracing on/off.
1057 */
1058 void
1059 prop_debug_trace (sc_bool flag)
1060 {
1061 prop_trace = flag;
1062 }