print.hpp (3335B)
1 #ifndef C4_YML_DETAIL_PRINT_HPP_ 2 #define C4_YML_DETAIL_PRINT_HPP_ 3 4 #include "c4/yml/tree.hpp" 5 #include "c4/yml/node.hpp" 6 7 8 namespace c4 { 9 namespace yml { 10 11 C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH("-Wold-style-cast") 12 13 inline size_t print_node(Tree const& p, size_t node, int level, size_t count, bool print_children) 14 { 15 printf("[%zd]%*s[%zd] %p", count, (2*level), "", node, (void const*)p.get(node)); 16 if(p.is_root(node)) 17 { 18 printf(" [ROOT]"); 19 } 20 printf(" %s:", p.type_str(node)); 21 if(p.has_key(node)) 22 { 23 if(p.has_key_anchor(node)) 24 { 25 csubstr ka = p.key_anchor(node); 26 printf(" &%.*s", (int)ka.len, ka.str); 27 } 28 if(p.has_key_tag(node)) 29 { 30 csubstr kt = p.key_tag(node); 31 csubstr k = p.key(node); 32 printf(" %.*s '%.*s'", (int)kt.len, kt.str, (int)k.len, k.str); 33 } 34 else 35 { 36 csubstr k = p.key(node); 37 printf(" '%.*s'", (int)k.len, k.str); 38 } 39 } 40 else 41 { 42 RYML_ASSERT( ! p.has_key_tag(node)); 43 } 44 if(p.has_val(node)) 45 { 46 if(p.has_val_tag(node)) 47 { 48 csubstr vt = p.val_tag(node); 49 csubstr v = p.val(node); 50 printf(" %.*s '%.*s'", (int)vt.len, vt.str, (int)v.len, v.str); 51 } 52 else 53 { 54 csubstr v = p.val(node); 55 printf(" '%.*s'", (int)v.len, v.str); 56 } 57 } 58 else 59 { 60 if(p.has_val_tag(node)) 61 { 62 csubstr vt = p.val_tag(node); 63 printf(" %.*s", (int)vt.len, vt.str); 64 } 65 } 66 if(p.has_val_anchor(node)) 67 { 68 auto &a = p.val_anchor(node); 69 printf(" valanchor='&%.*s'", (int)a.len, a.str); 70 } 71 printf(" (%zd sibs)", p.num_siblings(node)); 72 73 ++count; 74 75 if(p.is_container(node)) 76 { 77 printf(" %zd children:\n", p.num_children(node)); 78 if(print_children) 79 { 80 for(size_t i = p.first_child(node); i != NONE; i = p.next_sibling(i)) 81 { 82 count = print_node(p, i, level+1, count, print_children); 83 } 84 } 85 } 86 else 87 { 88 printf("\n"); 89 } 90 91 return count; 92 } 93 94 95 //----------------------------------------------------------------------------- 96 //----------------------------------------------------------------------------- 97 //----------------------------------------------------------------------------- 98 99 inline void print_node(ConstNodeRef const& p, int level=0) 100 { 101 print_node(*p.tree(), p.id(), level, 0, true); 102 } 103 104 105 //----------------------------------------------------------------------------- 106 //----------------------------------------------------------------------------- 107 //----------------------------------------------------------------------------- 108 109 inline size_t print_tree(Tree const& p, size_t node=NONE) 110 { 111 printf("--------------------------------------\n"); 112 size_t ret = 0; 113 if(!p.empty()) 114 { 115 if(node == NONE) 116 node = p.root_id(); 117 ret = print_node(p, node, 0, 0, true); 118 } 119 printf("#nodes=%zd vs #printed=%zd\n", p.size(), ret); 120 printf("--------------------------------------\n"); 121 return ret; 122 } 123 124 C4_SUPPRESS_WARNING_GCC_CLANG_POP 125 126 } /* namespace yml */ 127 } /* namespace c4 */ 128 129 130 #endif /* C4_YML_DETAIL_PRINT_HPP_ */