| 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.) |
| 5 | |
| 6 | // See http://www.boost.org/libs/iostreams for documentation. |
| 7 | |
| 8 | // Adapted from an example of James Kanze, with suggestions from Peter Dimov. |
| 9 | // See https://web.archive.org/web/20041222094942/http://www.gabi-soft.fr/codebase-en.html. |
| 10 | |
| 11 | #ifndef BOOST_IOSTREAMS_SHELL_COMMENTS_FILTER_HPP_INCLUDED |
| 12 | #define |
| 13 | |
| 14 | #include <cassert> |
| 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> |
| 21 | |
| 22 | namespace boost { namespace iostreams { namespace example { |
| 23 | |
| 24 | class : public stdio_filter { |
| 25 | public: |
| 26 | explicit (char = '#') |
| 27 | : comment_char_(comment_char) |
| 28 | { } |
| 29 | private: |
| 30 | void () |
| 31 | { |
| 32 | bool skip = false; |
| 33 | int c; |
| 34 | while ((c = std::cin.get()) != EOF) { |
| 35 | skip = c == comment_char_ ? |
| 36 | true : |
| 37 | c == '\n' ? |
| 38 | false : |
| 39 | skip; |
| 40 | if (!skip) |
| 41 | std::cout.put(c: c); |
| 42 | } |
| 43 | } |
| 44 | char ; |
| 45 | }; |
| 46 | |
| 47 | class : public input_filter { |
| 48 | public: |
| 49 | explicit (char = '#') |
| 50 | : comment_char_(comment_char), skip_(false) |
| 51 | { } |
| 52 | |
| 53 | template<typename Source> |
| 54 | int (Source& src) |
| 55 | { |
| 56 | int c; |
| 57 | while (true) { |
| 58 | if ((c = boost::iostreams::get(src)) == EOF || c == WOULD_BLOCK) |
| 59 | break; |
| 60 | skip_ = c == comment_char_ ? |
| 61 | true : |
| 62 | c == '\n' ? |
| 63 | false : |
| 64 | skip_; |
| 65 | if (!skip_) |
| 66 | break; |
| 67 | } |
| 68 | return c; |
| 69 | } |
| 70 | |
| 71 | template<typename Source> |
| 72 | void (Source&) { skip_ = false; } |
| 73 | private: |
| 74 | char ; |
| 75 | bool ; |
| 76 | }; |
| 77 | |
| 78 | class : public output_filter { |
| 79 | public: |
| 80 | explicit (char = '#') |
| 81 | : comment_char_(comment_char), skip_(false) |
| 82 | { } |
| 83 | |
| 84 | template<typename Sink> |
| 85 | bool (Sink& dest, int c) |
| 86 | { |
| 87 | skip_ = c == comment_char_ ? |
| 88 | true : |
| 89 | c == '\n' ? |
| 90 | false : |
| 91 | skip_; |
| 92 | |
| 93 | if (skip_) |
| 94 | return true; |
| 95 | |
| 96 | return iostreams::put(dest, c); |
| 97 | } |
| 98 | |
| 99 | template<typename Source> |
| 100 | void (Source&) { skip_ = false; } |
| 101 | private: |
| 102 | char ; |
| 103 | bool ; |
| 104 | }; |
| 105 | |
| 106 | class : public dual_use_filter { |
| 107 | public: |
| 108 | explicit (char = '#') |
| 109 | : comment_char_(comment_char), skip_(false) |
| 110 | { } |
| 111 | |
| 112 | template<typename Source> |
| 113 | int (Source& src) |
| 114 | { |
| 115 | int c; |
| 116 | while (true) { |
| 117 | if ((c = boost::iostreams::get(src)) == EOF || c == WOULD_BLOCK) |
| 118 | break; |
| 119 | skip_ = c == comment_char_ ? |
| 120 | true : |
| 121 | c == '\n' ? |
| 122 | false : |
| 123 | skip_; |
| 124 | if (!skip_) |
| 125 | break; |
| 126 | } |
| 127 | return c; |
| 128 | } |
| 129 | |
| 130 | template<typename Sink> |
| 131 | bool (Sink& dest, int c) |
| 132 | { |
| 133 | skip_ = c == comment_char_ ? |
| 134 | true : |
| 135 | c == '\n' ? |
| 136 | false : |
| 137 | skip_; |
| 138 | |
| 139 | if (skip_) |
| 140 | return true; |
| 141 | |
| 142 | return iostreams::put(dest, c); |
| 143 | } |
| 144 | |
| 145 | template<typename Device> |
| 146 | void (Device&, BOOST_IOS::openmode) { skip_ = false; } |
| 147 | private: |
| 148 | char ; |
| 149 | bool ; |
| 150 | }; |
| 151 | |
| 152 | class : public multichar_input_filter { |
| 153 | public: |
| 154 | explicit (char = '#') |
| 155 | : comment_char_(comment_char), skip_(false) |
| 156 | { } |
| 157 | |
| 158 | template<typename Source> |
| 159 | std::streamsize (Source& src, char* s, std::streamsize n) |
| 160 | { |
| 161 | for (std::streamsize z = 0; z < n; ++z) { |
| 162 | int c; |
| 163 | while (true) { |
| 164 | if ((c = boost::iostreams::get(src)) == EOF) |
| 165 | return z != 0 ? z : -1; |
| 166 | else if (c == WOULD_BLOCK) |
| 167 | return z; |
| 168 | skip_ = c == comment_char_ ? |
| 169 | true : |
| 170 | c == '\n' ? |
| 171 | false : |
| 172 | skip_; |
| 173 | if (!skip_) |
| 174 | break; |
| 175 | } |
| 176 | s[z] = c; |
| 177 | } |
| 178 | return n; |
| 179 | } |
| 180 | |
| 181 | template<typename Source> |
| 182 | void (Source&) { skip_ = false; } |
| 183 | private: |
| 184 | char ; |
| 185 | bool ; |
| 186 | }; |
| 187 | |
| 188 | class : public multichar_output_filter { |
| 189 | public: |
| 190 | explicit (char = '#') |
| 191 | : comment_char_(comment_char), skip_(false) |
| 192 | { } |
| 193 | |
| 194 | template<typename Sink> |
| 195 | std::streamsize (Sink& dest, const char* s, std::streamsize n) |
| 196 | { |
| 197 | std::streamsize z; |
| 198 | for (z = 0; z < n; ++z) { |
| 199 | int c = s[z]; |
| 200 | skip_ = c == comment_char_ ? |
| 201 | true : |
| 202 | c == '\n' ? |
| 203 | false : |
| 204 | skip_; |
| 205 | if (skip_) |
| 206 | continue; |
| 207 | if (!iostreams::put(dest, c)) |
| 208 | break; |
| 209 | } |
| 210 | return z; |
| 211 | } |
| 212 | |
| 213 | template<typename Source> |
| 214 | void (Source&) { skip_ = false; } |
| 215 | private: |
| 216 | char ; |
| 217 | bool ; |
| 218 | }; |
| 219 | |
| 220 | } } } // End namespaces example, iostreams, boost. |
| 221 | |
| 222 | #endif // #ifndef BOOST_IOSTREAMS_SHELL_COMMENTS_FILTER_HPP_INCLUDED |
| 223 | |