1    | /***************************************
2    |   $Revision: 1.7 $
3    | 
4    |   Radix tree (rx).  rx_print.c - functions to print a forest/tree/node
5    |                                  (mainly for debugging purposes)
6    | 
7    |   Status: NOT REVUED, TESTED, INCOMPLETE
8    | 
9    |   Design and implementation by: Marek Bukowy
10   | 
11   |   ******************/ /******************
12   |   Copyright (c) 1999                              RIPE NCC
13   |  
14   |   All Rights Reserved
15   |   
16   |   Permission to use, copy, modify, and distribute this software and its
17   |   documentation for any purpose and without fee is hereby granted,
18   |   provided that the above copyright notice appear in all copies and that
19   |   both that copyright notice and this permission notice appear in
20   |   supporting documentation, and that the name of the author not be
21   |   used in advertising or publicity pertaining to distribution of the
22   |   software without specific, written prior permission.
23   |   
24   |   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25   |   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
26   |   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
27   |   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
28   |   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
29   |   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30   |   ***************************************/
31   | 
32   | #define RX_IMPL
33   | #include <rxroutines.h>
34   | 
35   | er_ret_t
36   | rx_walk_hook_printnode(rx_node_t *node, int level, int nodecounter, void *trash)
37   | {
38   |   char line[200]="", buf[1024];
39   | 
40   |   int i;
41   | 
42   |   // indent
43   |   for(i=0;i<level;i++) strcat(line,"  ");
44   |   
45   |   rx_nod_print(node, buf, 1024);
46   | 
47   |   fprintf(stderr,
48   | 	  "%s** level %d ** node: %s\n",
49   |     // @ %p; parent %p, child[0]=%p, child[1]=%p\n", 
50   | 	  line, level, buf
51   | 	  );
52   |     //  node, node->parent_ptr, node->child_ptr[0], node->child_ptr[1] );
53   | 
54   |   return RX_OK;
55   | }
56   | 
57   | /***************************************************************************/
58   | 
59   | er_ret_t
60   | rx_tree_print( rx_tree_t     *tree ) 
61   | {
62   | int cnt;
63   | er_ret_t err;
64   | 
65   |  if( tree->top_ptr != NULL ) {
66   |    cnt = rx_walk_tree(tree->top_ptr, rx_walk_hook_printnode, 
67   | 		      RX_WALK_CNTGLU,  // print also glue nodes
68   | 		      255, 0, 0, NULL, &err);
69   |    fprintf(stderr,"Traversed %d nodes\n", cnt);
70   |  }
71   |  else {
72   |    fprintf(stderr,"The tree is empty!\n");
73   |  }
74   | 
75   |  return err;
76   | }
77   | 
78   | 
79   | /***************************************************************************/
80   | static
81   | void
82   | rx_space_printone(void *voptr, void *junkdata)
83   | {
84   | rx_tree_t *ptr = voptr;
85   | char prstr[IP_PREFSTR_MAX];
86   | 
87   |   printf("%50s:%d\n", "reg_id",   ptr->reg_id);
88   |   printf("%50s:%d\n", "space",    ptr->space );
89   |   printf("%50s:%d\n", "family",   ptr->family );
90   |   printf("%50s:%d\n", "subtrees", ptr->subtrees);
91   |   printf("%50s:%d\n", "mem_mode", ptr->mem_mode);
92   |   printf("%50s:%d\n", "num_nodes",ptr->num_nodes);
93   |   printf("%50s:%08x\n", "top_ptr", (int) ptr->top_ptr);
94   |   printf("%50s:%d\n", "maxbits",  ptr->maxbits);
95   | 
96   |   if( IP_pref_b2a(  &(ptr->prefix), prstr, IP_PREFSTR_MAX) != IP_OK )
97   |     die; // program error.
98   | 
99   |   printf("%50s:%s\n", "prefix", prstr);
100  | }
101  | 
102  | 
103  | /***************************************************************************/
104  | /*+ print the whole forest +*/
105  | 
106  | void
107  | rx_space_list(void)
108  | {
109  | g_list_foreach( rx_forest, rx_space_printone, NULL);
110  | }
111  | /***************************************************************************/
112  | 
113  | void
114  | rx_nod_print( rx_node_t *node, char *buf, int maxchar )
115  | {
116  |   char pref[IP_PREFSTR_MAX];
117  |   
118  |   if( IP_pref_b2a(  &(node->prefix), pref, IP_PREFSTR_MAX) != IP_OK ) {
119  |     die;
120  |   }
121  |   
122  |   snprintf(buf, maxchar, "%s%s", 
123  | 	   ( node->glue ) ? "++glue++" : "", pref);
124  | }
125  | /***************************************************************************/
126  | 
127  | void 
128  | rx_stk_print( rx_nodcpy_t   stack[],         // stack==array of node_copies
129  | 	      int           stackdepth )
130  | {
131  |   int i;
132  |   rx_node_t *node;
133  |   char buf[1024];
134  | 
135  |   ER_dbg_va(FAC_RX, ASP_RX_STKBLD_DET, 
136  | 	    "stack dump: %d elements", stackdepth);
137  | 
138  |   for(i = 0; i < stackdepth; i++) {
139  |     node = & stack[i].cpy;
140  | 
141  |     rx_nod_print(node, buf, 1024);
142  | 
143  |     ER_dbg_va(FAC_RX, ASP_RX_STKBLD_DET, "position %d: %s", i, buf);
144  |   }
145  | }