This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
/
pub3debug.C
153 lines (122 loc) · 3.15 KB
/
pub3debug.C
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
#include "pub3debug.h"
namespace pub3 {
//----------------------------------------------------------------------
void
dumper_t::begin_obj (str s, const void *p, lineno_t l)
{
strbuf b;
b.fmt ("-{ %s (%p", s.cstr (), p);
if (l) b << "," << l;
b << ")";
dump (b, true);
inc_level ();
}
//----------------------------------------------------------------------
void
dumper_t::inc_level ()
{
_level ++ ;
}
//----------------------------------------------------------------------
str
dumper_t::get_space (size_t l)
{
str ret;
str *sp = _spaces[l];
if (!sp) {
size_t ns = l * INDENT;
mstr m (ns);
memset (m.cstr (), ' ', ns);
m.setlen (ns);
ret = m;
_spaces.insert (l, ret);
} else {
ret = *sp;
}
return ret;
}
//----------------------------------------------------------------------
dumper_t::dumper_t () : _level (0) {}
//----------------------------------------------------------------------
void
dumper_t::dec_level ()
{
--_level;
}
//----------------------------------------------------------------------
void
dumper_t::end_obj ()
{
dec_level ();
dump ("}", true);
}
//----------------------------------------------------------------------
ptr<dumper_t::line_t>
dumper_t::get_curr ()
{
if (!_curr) { _curr = New refcounted<line_t> (_level); }
return _curr;
}
//----------------------------------------------------------------------
void
dumper_t::push_curr ()
{
if (_curr) { _lines.push_back (_curr); }
_curr = NULL;
}
//----------------------------------------------------------------------
void
dumper_t::line_t::append (str s)
{
if (s) {
_v.push_back (s);
_b << s;
}
}
//----------------------------------------------------------------------
void
dumper_t::dump (str s, bool nl)
{
get_curr ()->append (s);
if (nl) { push_curr (); }
}
//----------------------------------------------------------------------
void
dumper_t::dump_to (strbuf &b)
{
push_curr ();
for (size_t i = 0; i < _lines.size (); i++) {
ptr<line_t> l = _lines[i];
str space = get_space (l->level ());
str content = l->to_str ();
b << space << content << "\n";
}
}
//----------------------------------------------------------------------
void
dumper_t::dump_to_stderr (const dumpable_t *e)
{
dumper_t d;
e->dump (&d);
warnobj w ( (int) ::warnobj::xflag);
d.dump_to (w);
}
//=======================================================================
void
dumpable_t::s_dump (dumper_t *d, str prfx, const dumpable_t *obj)
{
if (prfx) { d->dump (strbuf ("%s ", prfx.cstr ()), false); }
if (obj) { obj->dump (d); }
else { d->dump ("(null)", true); }
}
//----------------------------------------------------------------------
void
dumpable_t::dump (dumper_t *d) const
{
d->begin_obj (get_obj_name (), static_cast<const void *> (this),
dump_get_lineno());
v_dump (d);
d->end_obj ();
}
//=======================================================================
};