1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2005-2007 Jonathan Turkanis
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
6 // Adapted from an example of James Kanze. See
7 // https://web.archive.org/web/20041222094942/http://www.gabi-soft.fr/codebase-en.html.
9 // See http://www.boost.org/libs/iostreams for documentation.
11 #ifndef BOOST_IOSTREAMS_LINE_WRAPPING_FILTER_HPP_INCLUDED
12 #define BOOST_IOSTREAMS_LINE_WRAPPING_FILTER_HPP_INCLUDED
14 #include <cstdio> // EOF.
15 #include <boost/iostreams/concepts.hpp> // output_filter.
16 #include <boost/iostreams/filter/stdio.hpp>
17 #include <boost/iostreams/operations.hpp> // boost::iostreams::put.
19 namespace boost { namespace iostreams { namespace example {
21 class line_wrapping_stdio_filter : public stdio_filter {
23 explicit line_wrapping_stdio_filter(int line_length = 80)
24 : line_length_(line_length), col_no_(0)
30 while ((c = std::cin.get()) != EOF) {
31 if (c != '\n' && col_no_ >= line_length_)
36 void do_close() { col_no_ = 0; }
49 class line_wrapping_input_filter : public input_filter {
51 explicit line_wrapping_input_filter(int line_length = 80)
52 : line_length_(line_length), col_no_(0), next_(0), has_next_(false)
55 template<typename Source>
60 return get_char(next_);
64 if ((c = iostreams::get(src)) == EOF || c == WOULD_BLOCK)
67 if (c != '\n' && col_no_ >= line_length_) {
70 return get_char('\n');
76 template<typename Sink>
97 class line_wrapping_output_filter : public output_filter {
99 explicit line_wrapping_output_filter(int line_length = 80)
100 : line_length_(line_length), col_no_(0)
103 template<typename Sink>
104 bool put(Sink& dest, int c)
106 if (c != '\n' && col_no_ >= line_length_ && !put_char(dest, '\n'))
108 return put_char(dest, c);
111 template<typename Sink>
112 void close(Sink&) { col_no_ = 0; }
114 template<typename Sink>
115 bool put_char(Sink& dest, int c)
117 if (!iostreams::put(dest, c))
129 } } } // End namespaces example, iostreams, boost.
131 #endif // #ifndef BOOST_IOSTREAMS_LINE_WRAPPING_FILTER_HPP_INCLUDED