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 // See http://www.boost.org/libs/iostreams for documentation.
8 #ifndef BOOST_IOSTREAMS_LINE_FILTER_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_LINE_FILTER_HPP_INCLUDED
15 #include <algorithm> // min.
16 #include <boost/assert.hpp>
17 #include <memory> // allocator.
19 #include <boost/config.hpp> // BOOST_STATIC_CONSTANT.
20 #include <boost/iostreams/categories.hpp>
21 #include <boost/iostreams/checked_operations.hpp>
22 #include <boost/iostreams/detail/ios.hpp> // openmode, streamsize.
23 #include <boost/iostreams/read.hpp> // check_eof
24 #include <boost/iostreams/pipeline.hpp>
25 #include <boost/iostreams/write.hpp>
28 #include <boost/iostreams/detail/config/disable_warnings.hpp> // VC7.1 C4244.
30 namespace boost { namespace iostreams {
33 // Template name: line_filter.
34 // Template parameters:
35 // Ch - The character type.
36 // Alloc - The allocator type.
37 // Description: Filter which processes data one line at a time.
39 template< typename Ch,
40 typename Alloc = std::allocator<Ch> >
41 class basic_line_filter {
43 typedef typename std::basic_string<Ch>::traits_type string_traits;
46 typedef char_traits<char_type> traits_type;
47 typedef std::basic_string<
59 basic_line_filter(bool suppress_newlines = false)
60 : pos_(string_type::npos),
61 flags_(suppress_newlines ? f_suppress : 0)
64 virtual ~basic_line_filter() { }
66 template<typename Source>
67 std::streamsize read(Source& src, char_type* s, std::streamsize n)
70 BOOST_ASSERT(!(flags_ & f_write));
73 // Handle unfinished business.
74 std::streamsize result = 0;
75 if (!cur_line_.empty() && (result = read_line(s, n)) == n)
78 typename traits_type::int_type status = traits_type::good();
79 while (result < n && !traits_type::is_eof(status)) {
81 // Call next_line() to retrieve a line of filtered text, and
82 // read_line() to copy it into buffer s.
83 if (traits_type::would_block(status = next_line(src)))
85 result += read_line(s + result, n - result);
88 return detail::check_eof(result);
91 template<typename Sink>
92 std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
95 BOOST_ASSERT(!(flags_ & f_read));
98 // Handle unfinished business.
99 if (pos_ != string_type::npos && !write_line(snk))
102 const char_type *cur = s, *next;
105 // Search for the next full line in [cur, s + n), filter it
106 // and write it to snk.
107 typename string_type::size_type rest = n - (cur - s);
108 if ((next = traits_type::find(cur, rest, traits_type::newline()))) {
109 cur_line_.append(cur, next - cur);
111 if (!write_line(snk))
112 return static_cast<std::streamsize>(cur - s);
114 cur_line_.append(cur, rest);
120 template<typename Sink>
121 void close(Sink& snk, BOOST_IOS::openmode which)
123 if ((flags_ & f_read) && which == BOOST_IOS::in)
126 if ((flags_ & f_write) && which == BOOST_IOS::out) {
128 if (!cur_line_.empty())
140 virtual string_type do_filter(const string_type& line) = 0;
142 // Copies filtered characters fron the current line into
144 std::streamsize read_line(char_type* s, std::streamsize n)
147 std::streamsize result =
148 (std::min) (n, static_cast<std::streamsize>(cur_line_.size()));
149 traits_type::copy(s, cur_line_.data(), result);
150 cur_line_.erase(0, result);
154 // Attempts to retrieve a line of text from the given source; returns
155 // an int_type as a good/eof/would_block status code.
156 template<typename Source>
157 typename traits_type::int_type next_line(Source& src)
160 typename traits_type::int_type c;
161 while ( traits_type::is_good(c = iostreams::get(src)) &&
162 c != traits_type::newline() )
164 cur_line_ += traits_type::to_int_type(c);
166 if (!traits_type::would_block(c)) {
167 if (!cur_line_.empty() || c == traits_type::newline())
168 cur_line_ = do_filter(cur_line_);
169 if (c == traits_type::newline() && (flags_ & f_suppress) == 0)
172 return c; // status indicator.
175 // Filters the current line and attemps to write it to the given sink.
176 // Returns true for success.
177 template<typename Sink>
178 bool write_line(Sink& snk)
180 string_type line = do_filter(cur_line_);
181 if ((flags_ & f_suppress) == 0)
182 line += traits_type::newline();
183 std::streamsize amt = static_cast<std::streamsize>(line.size());
184 bool result = iostreams::write_if(snk, line.data(), amt) == amt;
193 flags_ &= f_suppress;
199 pos_ = string_type::npos;
204 f_write = f_read << 1,
205 f_suppress = f_write << 1
208 string_type cur_line_;
209 typename string_type::size_type pos_;
212 BOOST_IOSTREAMS_PIPABLE(basic_line_filter, 2)
214 typedef basic_line_filter<char> line_filter;
215 typedef basic_line_filter<wchar_t> wline_filter;
217 } } // End namespaces iostreams, boost.
219 #include <boost/iostreams/detail/config/enable_warnings.hpp>
221 #endif // #ifndef BOOST_IOSTREAMS_LINE_FILTER_HPP_INCLUDED