Ticket #3839: indent.hpp

File indent.hpp, 2.0 KB (added by cppljevans@…, 13 years ago)

file #included by previous attachment

Line 
1#ifndef BOOST_IOSTREAMS_FILTER_INDENT_HPP
2#define BOOST_IOSTREAMS_FILTER_INDENT_HPP
3
4#include <boost/iostreams/concepts.hpp>
5#include <boost/iostreams/operations.hpp>
6
7// Must come last.
8#include <boost/iostreams/detail/config/disable_warnings.hpp> // VC7.1 C4244.
9
10namespace boost { namespace iostreams {
11
12//
13// Class name: indent_filter.
14//
15// Template paramters:
16// Ch - The character type.
17// Description: Output Filter which indents each line by user modifiable amount.
18//
19 template
20 < typename Ch = BOOST_IOSTREAMS_DEFAULT_ARG(char)
21 >
22class indent_filter
23 : public filter
24 < output
25 , Ch
26 >
27{
28 public:
29
30 typedef filter< output, Ch> super_type;
31 typedef Ch char_type;
32 static int const width_default=4;
33
34 template<typename Sink>
35 bool put(Sink& dest, char_type c)
36 {
37 if (c == '\n')
38 linestart_ = true;
39 else
40 if (linestart_) {
41 for(int n=0; n<indent_; ++n)
42 boost::iostreams::put(dest, ' ');
43 linestart_ = false;
44 }
45 return boost::iostreams::put(dest, c);
46 }
47
48 template<typename Sink>
49 void close(Sink&)
50 {
51 indent_ = 0;
52 linestart_ = true;
53 }
54
55 void indent_in()
56 {
57 indent_by(+width_);
58 }
59
60 void indent_out()
61 {
62 indent_by(-width_);
63 }
64
65 int indentation()const
66 {
67 return indent_;
68 }
69
70 int width()const
71 {
72 return width_;
73 }
74
75 void indent_by(int amount)
76 {
77 int my_indent=indent_+amount;
78 indent_=my_indent>0?my_indent:0;
79 }
80
81 explicit indent_filter
82 ( int width=width_default
83 )
84 : indent_(0)
85 , linestart_(true)
86 , width_((width>0)?width:0)
87 {
88 }
89 private:
90 int indent_;//current indentation
91 bool linestart_;//at the start of line?
92 int width_;//amount to change indentation.
93};
94
95} } // End namespaces iostreams, boost.
96
97#include <boost/iostreams/detail/config/enable_warnings.hpp>
98
99#endif // INDENT