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 // Adapted from an example of James Kanze, with suggestions from Peter Dimov.
9 // See http://www.gabi-soft.fr/codebase-en.html.
11 #ifndef BOOST_IOSTREAMS_SHELL_COMMENTS_FILTER_HPP_INCLUDED
12 #define BOOST_IOSTREAMS_SHELL_COMMENTS_FILTER_HPP_INCLUDED
15 #include <cstdio> // EOF.
16 #include <iostream> // cin, cout.
17 #include <boost/iostreams/concepts.hpp>
18 #include <boost/iostreams/detail/ios.hpp> // BOOST_IOS.
19 #include <boost/iostreams/filter/stdio.hpp>
20 #include <boost/iostreams/operations.hpp>
22 namespace boost { namespace iostreams { namespace example {
24 class shell_comments_stdio_filter : public stdio_filter {
26 explicit shell_comments_stdio_filter(char comment_char = '#')
27 : comment_char_(comment_char)
34 while ((c = std::cin.get()) != EOF) {
35 skip = c == comment_char_ ?
47 class shell_comments_input_filter : public input_filter {
49 explicit shell_comments_input_filter(char comment_char = '#')
50 : comment_char_(comment_char), skip_(false)
53 template<typename Source>
58 if ((c = boost::iostreams::get(src)) == EOF || c == WOULD_BLOCK)
60 skip_ = c == comment_char_ ?
71 template<typename Source>
72 void close(Source&) { skip_ = false; }
78 class shell_comments_output_filter : public output_filter {
80 explicit shell_comments_output_filter(char comment_char = '#')
81 : comment_char_(comment_char), skip_(false)
84 template<typename Sink>
85 bool put(Sink& dest, int c)
87 skip_ = c == comment_char_ ?
96 return iostreams::put(dest, c);
99 template<typename Source>
100 void close(Source&) { skip_ = false; }
106 class shell_comments_dual_use_filter : public dual_use_filter {
108 explicit shell_comments_dual_use_filter(char comment_char = '#')
109 : comment_char_(comment_char), skip_(false)
112 template<typename Source>
117 if ((c = boost::iostreams::get(src)) == EOF || c == WOULD_BLOCK)
119 skip_ = c == comment_char_ ?
130 template<typename Sink>
131 bool put(Sink& dest, int c)
133 skip_ = c == comment_char_ ?
142 return iostreams::put(dest, c);
145 template<typename Device>
146 void close(Device&, BOOST_IOS::openmode) { skip_ = false; }
152 class shell_comments_multichar_input_filter : public multichar_input_filter {
154 explicit shell_comments_multichar_input_filter(char comment_char = '#')
155 : comment_char_(comment_char), skip_(false)
158 template<typename Source>
159 std::streamsize read(Source& src, char* s, std::streamsize n)
161 for (std::streamsize z = 0; z < n; ++z) {
164 if ((c = boost::iostreams::get(src)) == EOF)
165 return z != 0 ? z : -1;
166 else if (c == WOULD_BLOCK)
168 skip_ = c == comment_char_ ?
181 template<typename Source>
182 void close(Source&) { skip_ = false; }
188 class shell_comments_multichar_output_filter : public multichar_output_filter {
190 explicit shell_comments_multichar_output_filter(char comment_char = '#')
191 : comment_char_(comment_char), skip_(false)
194 template<typename Sink>
195 std::streamsize write(Sink& dest, const char* s, std::streamsize n)
198 for (z = 0; z < n; ++z) {
200 skip_ = c == comment_char_ ?
207 if (!iostreams::put(dest, c))
213 template<typename Source>
214 void close(Source&) { skip_ = false; }
220 } } } // End namespaces example, iostreams, boost.
222 #endif // #ifndef BOOST_IOSTREAMS_SHELL_COMMENTS_FILTER_HPP_INCLUDED