TextDebugView.cpp
Go to the documentation of this file.
1 #include "TextDebugView.hpp"
2 
3 namespace iv
4 {
5 
6 //------------------ TextDebugView ------------------------------------------
8  DebugView( context ),
9  prefixes(),
10  _prefix(),
11  postfixes(),
12  _postfix(),
13  outstream( this )
14 {
15 }
16 
17 std::ostream & TextDebugView::out()
18 {
19  return this->outstream;
20 }
21 
22 void TextDebugView::write_line( const char * line )
23 {
24  std::string prefix = this->prefix();
25  std::string postfix = this->postfix();
26  this->print_line( prefix.c_str(), line, postfix.c_str() );
27 }
28 
29 void TextDebugView::init_line()
30 {
31  this->_prefix = "";
32  for( auto & pref : this->prefixes )
33  this->_prefix = this->_prefix + pref;
34 
35  this->_postfix = "";
36  for( auto & postf : this->postfixes )
37  this->_postfix = postf + this->_postfix;
38 }
39 
40 void TextDebugView::prefix_push( const char * pref )
41 {
42  this->prefixes.push_back( pref );
43 }
44 
46 {
47  size_t size = this->outstream.get_line_size();
48  size_t active = this->_prefix.size();
49  size_t full = 0;
50  for( auto & pref : this->prefixes )
51  full += pref.size();
52 
53  size_t inactive = 0;
54  if( full > active )
55  inactive = full - active;
56 
57  size_t align = 0;
58  if( size > inactive )
59  align = size - inactive;
60 
61  std::string prefix;
62  for( size_t i = 0; i < align; i++ )
63  prefix += " ";
64 
65  this->prefix_push( prefix.c_str() );
66 }
67 
69 {
70  this->prefixes.pop_back();
71 }
72 
73 std::string TextDebugView::prefix()
74 {
75  return this->_prefix;
76 }
77 
78 void TextDebugView::postfix_push( const char * postf )
79 {
80  this->postfixes.push_back( postf );
81 }
82 
84 {
85  this->postfixes.pop_back();
86 }
87 
88 std::string TextDebugView::postfix()
89 {
90  return this->_postfix;
91 }
92 
94 {
95  this->outstream.endline();
96 }
97 
98 //---TextDebugView::tdc_streambuf --
99 TextDebugView::tdc_streambuf::tdc_streambuf( TextDebugView * context ) :
100  context( context )
101 {
102 }
103 
104 std::streambuf::int_type TextDebugView::tdc_streambuf::overflow( std::streambuf::int_type value )
105 {
106  if( value == traits_type::eof() )
107  {
108  this->flush_as_line();
109  }
110  else
111  {
112  auto c = traits_type::to_char_type( value );
113  if( c == '\n' )
114  {
115  this->flush_as_line();
116  }
117  else
118  {
119  if( this->buffer.empty() )
120  this->context->init_line();
121  this->buffer.push_back( c );
122  }
123  }
124 
125  return traits_type::not_eof( value );
126 }
127 
128 void TextDebugView::tdc_streambuf::flush_as_line()
129 {
130  this->context->write_line( this->buffer.c_str() );
131  this->buffer.clear();
132 }
133 
134 void TextDebugView::tdc_streambuf::endline()
135 {
136  if( this->buffer.size() )
137  this->flush_as_line();
138 }
139 
140 size_t TextDebugView::tdc_streambuf::get_line_size()
141 {
142  return this->buffer.size();
143 }
144 
145 //---TextDebugView::tdc_ostream --
146 TextDebugView::tdc_ostream::tdc_ostream( TextDebugView * context ) :
147  std::ostream( new tdc_streambuf( context ) )
148 {
149 }
150 
151 TextDebugView::tdc_ostream::~tdc_ostream()
152 {
153  delete this->rdbuf();
154 }
155 
156 void TextDebugView::tdc_ostream::endline()
157 {
158  static_cast< TextDebugView::tdc_streambuf * >( this->rdbuf() )->endline();
159 }
160 
161 size_t TextDebugView::tdc_ostream::get_line_size()
162 {
163  return static_cast< TextDebugView::tdc_streambuf * >( this->rdbuf() )->get_line_size();
164 }
165 
166 }