1 | // (C) Copyright Jonathan Turkanis 2005. |
---|
2 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
---|
3 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) |
---|
4 | |
---|
5 | // Adapted from an example of James Kanze. See |
---|
6 | // http://www.gabi-soft.fr/codebase-en.html. |
---|
7 | |
---|
8 | // See http://www.boost.org/libs/iostreams for documentation. |
---|
9 | |
---|
10 | #ifndef BOOST_IOSTREAMS_LINE_WRAPPING_FILTER_HPP_INCLUDED |
---|
11 | #define BOOST_IOSTREAMS_LINE_WRAPPING_FILTER_HPP_INCLUDED |
---|
12 | |
---|
13 | #include <cstdio> // EOF. |
---|
14 | #include <boost/iostreams/concepts.hpp> // output_filter. |
---|
15 | #include <boost/iostreams/filter/stdio.hpp> |
---|
16 | #include <boost/iostreams/operations.hpp> // boost::iostreams::put. |
---|
17 | |
---|
18 | namespace boost { namespace iostreams { namespace example { |
---|
19 | |
---|
20 | class line_wrapping_stdio_filter : public stdio_filter { |
---|
21 | public: |
---|
22 | explicit line_wrapping_stdio_filter(int line_length = 80) |
---|
23 | : line_length_(line_length), col_no_(0) |
---|
24 | { } |
---|
25 | private: |
---|
26 | void do_filter() |
---|
27 | { |
---|
28 | int c; |
---|
29 | while ((c = std::cin.get()) != EOF) { |
---|
30 | if (c != '\n' && col_no_ >= line_length_) |
---|
31 | put_char('\n'); |
---|
32 | put_char(c); |
---|
33 | } |
---|
34 | } |
---|
35 | void do_close() { col_no_ = 0; } |
---|
36 | void put_char(int c) |
---|
37 | { |
---|
38 | std::cout.put(c); |
---|
39 | if (c != '\n') |
---|
40 | ++col_no_; |
---|
41 | else |
---|
42 | col_no_ = 0; |
---|
43 | } |
---|
44 | int line_length_; |
---|
45 | int col_no_; |
---|
46 | }; |
---|
47 | |
---|
48 | class line_wrapping_input_filter : public input_filter { |
---|
49 | public: |
---|
50 | explicit line_wrapping_input_filter(int line_length = 80) |
---|
51 | : line_length_(line_length), col_no_(0), has_next_(false) |
---|
52 | { } |
---|
53 | |
---|
54 | template<typename Source> |
---|
55 | int get(Source& src) |
---|
56 | { |
---|
57 | if (has_next_) { |
---|
58 | has_next_ = false; |
---|
59 | return get_char(next_); |
---|
60 | } |
---|
61 | |
---|
62 | int c; |
---|
63 | if ((c = iostreams::get(src)) == EOF || c == WOULD_BLOCK) |
---|
64 | return c; |
---|
65 | |
---|
66 | if (c != '\n' && col_no_ >= line_length_) { |
---|
67 | next_ = c; |
---|
68 | has_next_ = true; |
---|
69 | return get_char('\n'); |
---|
70 | } |
---|
71 | |
---|
72 | return get_char(c); |
---|
73 | } |
---|
74 | |
---|
75 | template<typename Sink> |
---|
76 | void close(Sink&) |
---|
77 | { |
---|
78 | col_no_ = 0; |
---|
79 | has_next_ = false; |
---|
80 | } |
---|
81 | private: |
---|
82 | int get_char(int c) |
---|
83 | { |
---|
84 | if (c != '\n') |
---|
85 | ++col_no_; |
---|
86 | else |
---|
87 | col_no_ = 0; |
---|
88 | return c; |
---|
89 | } |
---|
90 | int line_length_; |
---|
91 | int col_no_; |
---|
92 | int next_; |
---|
93 | int has_next_; |
---|
94 | }; |
---|
95 | |
---|
96 | class line_wrapping_output_filter : public output_filter { |
---|
97 | public: |
---|
98 | explicit line_wrapping_output_filter(int line_length = 80) |
---|
99 | : line_length_(line_length), col_no_(0) |
---|
100 | { } |
---|
101 | |
---|
102 | template<typename Sink> |
---|
103 | bool put(Sink& dest, int c) |
---|
104 | { |
---|
105 | if (c != '\n' && col_no_ >= line_length_ && !put_char(dest, '\n')) |
---|
106 | return false; |
---|
107 | return put_char(dest, c); |
---|
108 | } |
---|
109 | |
---|
110 | template<typename Sink> |
---|
111 | void close(Sink&) { col_no_ = 0; } |
---|
112 | private: |
---|
113 | template<typename Sink> |
---|
114 | bool put_char(Sink& dest, int c) |
---|
115 | { |
---|
116 | if (!iostreams::put(dest, c)) |
---|
117 | return false; |
---|
118 | if (c != '\n') |
---|
119 | ++col_no_; |
---|
120 | else |
---|
121 | col_no_ = 0; |
---|
122 | return true; |
---|
123 | } |
---|
124 | int line_length_; |
---|
125 | int col_no_; |
---|
126 | }; |
---|
127 | |
---|
128 | } } } // End namespaces example, iostreams, boost. |
---|
129 | |
---|
130 | #endif // #ifndef BOOST_IOSTREAMS_LINE_WRAPPING_FILTER_HPP_INCLUDED |
---|