// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com) // (C) Copyright 2005-2007 Jonathan Turkanis // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) // See http://www.boost.org/libs/iostreams for documentation. // Based on the work of Christopher Diggins. #ifndef BOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED #define BOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED #if defined(_MSC_VER) && (_MSC_VER >= 1020) # pragma once #endif #include #include // allocator. #include #include #include #include #include #include #include #include #include namespace boost { namespace iostreams { namespace detail { } // End namespace detail. template > class basic_stdio_filter : public aggregate_filter { private: typedef aggregate_filter base_type; public: typedef typename base_type::char_type char_type; typedef typename base_type::category category; typedef typename base_type::vector_type vector_type; private: static std::istream& standard_input(char*) { return std::cin; } static std::ostream& standard_output(char*) { return std::cout; } #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS static std::wistream& standard_input(wchar_t*) { return std::wcin; } static std::wostream& standard_output(wchar_t*) { return std::wcout; } #endif // BOOST_IOSTREAMS_NO_WIDE_STREAMS struct scoped_redirector { // Thanks to Maxim Egorushkin. typedef BOOST_IOSTREAMS_CHAR_TRAITS(Ch) traits_type; typedef BOOST_IOSTREAMS_BASIC_IOS(Ch, traits_type) ios_type; typedef BOOST_IOSTREAMS_BASIC_STREAMBUF(Ch, traits_type) streambuf_type; scoped_redirector( ios_type& ios, streambuf_type* newbuf ) : ios_(ios), old_(ios.rdbuf(newbuf)) { } ~scoped_redirector() { ios_.rdbuf(old_); } scoped_redirector& operator=(const scoped_redirector&); ios_type& ios_; streambuf_type* old_; }; virtual void do_filter() = 0; virtual void do_filter(const vector_type& src, vector_type& dest) { stream_buffer< basic_array_source > srcbuf(&src[0], &src[0] + src.size()); stream_buffer< back_insert_device > destbuf(iostreams::back_inserter(dest)); scoped_redirector redirect_input(standard_input((Ch*)0), &srcbuf); scoped_redirector redirect_output(standard_output((Ch*)0), &destbuf); do_filter(); } }; BOOST_IOSTREAMS_PIPABLE(basic_stdio_filter, 2) typedef basic_stdio_filter stdio_filter; typedef basic_stdio_filter wstdio_wfilter; } } // End namespaces iostreams, boost. #endif // #ifndef BOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED