File indexing completed on 2025-01-30 09:44:31
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED
0011 #define BOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED
0012
0013 #if defined(_MSC_VER)
0014 # pragma once
0015 #endif
0016
0017 #include <iostream>
0018 #include <memory> // allocator.
0019 #include <vector>
0020 #include <boost/iostreams/detail/config/wide_streams.hpp>
0021 #include <boost/iostreams/detail/char_traits.hpp>
0022 #include <boost/iostreams/detail/ios.hpp>
0023 #include <boost/iostreams/device/array.hpp>
0024 #include <boost/iostreams/device/back_inserter.hpp>
0025 #include <boost/iostreams/filter/aggregate.hpp>
0026 #include <boost/iostreams/pipeline.hpp>
0027 #include <boost/iostreams/stream_buffer.hpp>
0028
0029 namespace boost { namespace iostreams {
0030
0031 namespace detail {
0032
0033 }
0034
0035 template<typename Ch, typename Alloc = std::allocator<Ch> >
0036 class basic_stdio_filter : public aggregate_filter<Ch, Alloc> {
0037 private:
0038 typedef aggregate_filter<Ch, Alloc> base_type;
0039 public:
0040 typedef typename base_type::char_type char_type;
0041 typedef typename base_type::category category;
0042 typedef typename base_type::vector_type vector_type;
0043 private:
0044 static std::istream& standard_input(char*) { return std::cin; }
0045 static std::ostream& standard_output(char*) { return std::cout; }
0046 #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
0047 static std::wistream& standard_input(wchar_t*) { return std::wcin; }
0048 static std::wostream& standard_output(wchar_t*) { return std::wcout; }
0049 #endif
0050
0051 struct scoped_redirector {
0052 typedef BOOST_IOSTREAMS_CHAR_TRAITS(Ch) traits_type;
0053 typedef BOOST_IOSTREAMS_BASIC_IOS(Ch, traits_type) ios_type;
0054 typedef BOOST_IOSTREAMS_BASIC_STREAMBUF(Ch, traits_type) streambuf_type;
0055 scoped_redirector( ios_type& ios,
0056 streambuf_type* newbuf )
0057 : ios_(ios), old_(ios.rdbuf(newbuf))
0058 { }
0059 ~scoped_redirector() { ios_.rdbuf(old_); }
0060 scoped_redirector& operator=(const scoped_redirector&);
0061 ios_type& ios_;
0062 streambuf_type* old_;
0063 };
0064
0065 virtual void do_filter() = 0;
0066 virtual void do_filter(const vector_type& src, vector_type& dest)
0067 {
0068 stream_buffer< basic_array_source<Ch> >
0069 srcbuf(&src[0], &src[0] + src.size());
0070 stream_buffer< back_insert_device<vector_type> >
0071 destbuf(iostreams::back_inserter(dest));
0072 scoped_redirector redirect_input(standard_input((Ch*)0), &srcbuf);
0073 scoped_redirector redirect_output(standard_output((Ch*)0), &destbuf);
0074 do_filter();
0075 }
0076 };
0077 BOOST_IOSTREAMS_PIPABLE(basic_stdio_filter, 2)
0078
0079 typedef basic_stdio_filter<char> stdio_filter;
0080 typedef basic_stdio_filter<wchar_t> wstdio_wfilter;
0081
0082 } }
0083
0084 #endif