source: NonGTP/Boost/boost/iostreams/filter/stdio.hpp @ 857

Revision 857, 3.1 KB checked in by igarcia, 18 years ago (diff)
Line 
1// (C) Copyright Jonathan Turkanis 2005.
2// Distributed under the Boost Software License, Version 1.0. (See accompanying
3// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
4
5// See http://www.boost.org/libs/iostreams for documentation.
6
7// Based on the work of Christopher Diggins.
8
9#ifndef BOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED
10#define BOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED
11
12#if defined(_MSC_VER) && (_MSC_VER >= 1020)
13# pragma once
14#endif
15
16#include <iostream>
17#include <memory>    // allocator.
18#include <vector>
19#include <boost/iostreams/detail/config/wide_streams.hpp>
20#include <boost/iostreams/detail/char_traits.hpp>
21#include <boost/iostreams/detail/ios.hpp>
22#include <boost/iostreams/device/array.hpp>
23#include <boost/iostreams/device/back_inserter.hpp>
24#include <boost/iostreams/filter/aggregate.hpp>
25#include <boost/iostreams/pipeline.hpp>
26#include <boost/iostreams/stream_buffer.hpp>
27
28namespace boost { namespace iostreams {
29
30namespace detail {
31
32} // End namespace detail.
33
34template<typename Ch, typename Alloc = std::allocator<Ch> >
35class basic_stdio_filter : public aggregate_filter<Ch, Alloc> {
36private:
37    typedef aggregate_filter<Ch, Alloc>       base_type;
38public:
39    typedef typename base_type::char_type    char_type;
40    typedef typename base_type::category     category;
41    typedef typename base_type::vector_type  vector_type;
42private:
43    static std::istream& standard_input(char*) { return std::cin; }
44    static std::ostream& standard_output(char*) { return std::cout; }
45#ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
46    static std::wistream& standard_input(wchar_t*) { return std::wcin; }
47    static std::wostream& standard_output(wchar_t*) { return std::wcout; }
48#endif // BOOST_IOSTREAMS_NO_WIDE_STREAMS
49
50    struct scoped_redirector { // Thanks to Maxim Egorushkin.
51        typedef BOOST_IOSTREAMS_CHAR_TRAITS(Ch)                  traits_type;
52        typedef BOOST_IOSTREAMS_BASIC_IOS(Ch, traits_type)       ios_type;
53        typedef BOOST_IOSTREAMS_BASIC_STREAMBUF(Ch, traits_type) streambuf_type;
54        scoped_redirector( ios_type& ios,
55                           streambuf_type* newbuf )
56            : ios_(ios), old_(ios.rdbuf(newbuf))
57            { }
58        ~scoped_redirector() { ios_.rdbuf(old_); }
59        ios_type&  ios_;
60        streambuf_type*                 old_;
61    };
62
63    virtual void do_filter() = 0;
64    virtual void do_filter(const vector_type& src, vector_type& dest)
65    {
66        stream_buffer< basic_array_source<Ch> >
67                          srcbuf(&src[0], &src[0] + src.size());
68        stream_buffer< back_insert_device<vector_type> >
69                          destbuf(iostreams::back_inserter(dest));
70        scoped_redirector redirect_input(standard_input((Ch*)0), &srcbuf);
71        scoped_redirector redirect_output(standard_output((Ch*)0), &destbuf);
72        do_filter();
73    }
74};
75BOOST_IOSTREAMS_PIPABLE(basic_stdio_filter, 2)
76
77typedef basic_stdio_filter<char>     stdio_filter;
78typedef basic_stdio_filter<wchar_t>  wstdio_wfilter;
79
80} } // End namespaces iostreams, boost.
81
82#endif // #ifndef BOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED
Note: See TracBrowser for help on using the repository browser.