Jeff Thompson | 86b6d64 | 2013-10-17 15:01:56 -0700 | [diff] [blame] | 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 | // Based on the work of Christopher Diggins. |
| 9 | |
| 10 | #ifndef NDNBOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED |
| 11 | #define NDNBOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED |
| 12 | |
| 13 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
| 14 | # pragma once |
| 15 | #endif |
| 16 | |
| 17 | #include <iostream> |
| 18 | #include <memory> // allocator. |
| 19 | #include <vector> |
| 20 | #include <ndnboost/iostreams/detail/config/wide_streams.hpp> |
| 21 | #include <ndnboost/iostreams/detail/char_traits.hpp> |
| 22 | #include <ndnboost/iostreams/detail/ios.hpp> |
| 23 | #include <ndnboost/iostreams/device/array.hpp> |
| 24 | #include <ndnboost/iostreams/device/back_inserter.hpp> |
| 25 | #include <ndnboost/iostreams/filter/aggregate.hpp> |
| 26 | #include <ndnboost/iostreams/pipeline.hpp> |
| 27 | #include <ndnboost/iostreams/stream_buffer.hpp> |
| 28 | |
| 29 | namespace ndnboost { namespace iostreams { |
| 30 | |
| 31 | namespace detail { |
| 32 | |
| 33 | } // End namespace detail. |
| 34 | |
| 35 | template<typename Ch, typename Alloc = std::allocator<Ch> > |
| 36 | class basic_stdio_filter : public aggregate_filter<Ch, Alloc> { |
| 37 | private: |
| 38 | typedef aggregate_filter<Ch, Alloc> base_type; |
| 39 | public: |
| 40 | typedef typename base_type::char_type char_type; |
| 41 | typedef typename base_type::category category; |
| 42 | typedef typename base_type::vector_type vector_type; |
| 43 | private: |
| 44 | static std::istream& standard_input(char*) { return std::cin; } |
| 45 | static std::ostream& standard_output(char*) { return std::cout; } |
| 46 | #ifndef NDNBOOST_IOSTREAMS_NO_WIDE_STREAMS |
| 47 | static std::wistream& standard_input(wchar_t*) { return std::wcin; } |
| 48 | static std::wostream& standard_output(wchar_t*) { return std::wcout; } |
| 49 | #endif // NDNBOOST_IOSTREAMS_NO_WIDE_STREAMS |
| 50 | |
| 51 | struct scoped_redirector { // Thanks to Maxim Egorushkin. |
| 52 | typedef NDNBOOST_IOSTREAMS_CHAR_TRAITS(Ch) traits_type; |
| 53 | typedef NDNBOOST_IOSTREAMS_BASIC_IOS(Ch, traits_type) ios_type; |
| 54 | typedef NDNBOOST_IOSTREAMS_BASIC_STREAMBUF(Ch, traits_type) streambuf_type; |
| 55 | scoped_redirector( ios_type& ios, |
| 56 | streambuf_type* newbuf ) |
| 57 | : ios_(ios), old_(ios.rdbuf(newbuf)) |
| 58 | { } |
| 59 | ~scoped_redirector() { ios_.rdbuf(old_); } |
| 60 | scoped_redirector& operator=(const scoped_redirector&); |
| 61 | ios_type& ios_; |
| 62 | streambuf_type* old_; |
| 63 | }; |
| 64 | |
| 65 | virtual void do_filter() = 0; |
| 66 | virtual void do_filter(const vector_type& src, vector_type& dest) |
| 67 | { |
| 68 | stream_buffer< basic_array_source<Ch> > |
| 69 | srcbuf(&src[0], &src[0] + src.size()); |
| 70 | stream_buffer< back_insert_device<vector_type> > |
| 71 | destbuf(iostreams::back_inserter(dest)); |
| 72 | scoped_redirector redirect_input(standard_input((Ch*)0), &srcbuf); |
| 73 | scoped_redirector redirect_output(standard_output((Ch*)0), &destbuf); |
| 74 | do_filter(); |
| 75 | } |
| 76 | }; |
| 77 | NDNBOOST_IOSTREAMS_PIPABLE(basic_stdio_filter, 2) |
| 78 | |
| 79 | typedef basic_stdio_filter<char> stdio_filter; |
| 80 | typedef basic_stdio_filter<wchar_t> wstdio_wfilter; |
| 81 | |
| 82 | } } // End namespaces iostreams, boost. |
| 83 | |
| 84 | #endif // #ifndef NDNBOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED |