multiline_ostream.cpp
Go to the documentation of this file.
1 #include "multiline_ostream.hpp"
2 #include "utils.hpp"
3 
4 namespace iv
5 {
6 
7 //-------------- multiline_ostream_streambuf ---------------------
9  out( out ),
10  pos( 0 )
11 {
12 }
13 
14 std::streambuf::int_type multiline_ostream_streambuf::overflow( std::streambuf::int_type value )
15 {
16  if( value == traits_type::eof() )
17  {
18  }
19  else
20  {
21  auto c = traits_type::to_char_type( value );
22 
23  if( c == '\n' )
24  {
25  size_t length = 0;
26  if( this->lengths.size() )
27  length = this->lengths[ this->lengths.size() - 1 ];
28 
29  this->out->put( '\n' );
30 
31  for( size_t i = 0; i < length; i++ )
32  this->out->put( ' ' );
33 
34  this->pos = length;
35  }
36  else
37  {
38  if( utf8_is_first_byte( c ) )
39  this->pos++;
40  this->out->put( c );
41  }
42  }
43 
44  return traits_type::not_eof( value );
45 }
46 
48 {
49  this->lengths.push_back( this->pos );
50 }
51 
53 {
54  if( this->lengths.size() )
55  this->lengths.pop_back();
56 }
57 
59 {
60  this->pos = 0;
61  this->lengths.clear();
62 }
63 
64 //-------------- multiline_ostream ---------------------
65 multiline_ostream::multiline_ostream( std::ostream * out ) :
66  std::ostream( new multiline_ostream_streambuf( out ) )
67 {
68 }
69 
71 {
72  delete this->rdbuf();
73 }
74 
76 {
77  static_cast< multiline_ostream_streambuf * >( this->rdbuf() )->multiline_begin();
78 }
79 
81 {
82  static_cast< multiline_ostream_streambuf * >( this->rdbuf() )->multiline_end();
83 }
84 
86 {
87  static_cast< multiline_ostream_streambuf * >( this->rdbuf() )->clear();
88 }
89 
90 
91 }