stdio.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // See http://www.boost.org/libs/iostreams for documentation.
  6. // Based on the work of Christopher Diggins.
  7. #ifndef BOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED
  8. #define BOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED
  9. #if defined(_MSC_VER)
  10. # pragma once
  11. #endif
  12. #include <iostream>
  13. #include <memory> // allocator.
  14. #include <vector>
  15. #include <boost/iostreams/detail/config/wide_streams.hpp>
  16. #include <boost/iostreams/detail/char_traits.hpp>
  17. #include <boost/iostreams/detail/ios.hpp>
  18. #include <boost/iostreams/device/array.hpp>
  19. #include <boost/iostreams/device/back_inserter.hpp>
  20. #include <boost/iostreams/filter/aggregate.hpp>
  21. #include <boost/iostreams/pipeline.hpp>
  22. #include <boost/iostreams/stream_buffer.hpp>
  23. namespace boost { namespace iostreams {
  24. namespace detail {
  25. } // End namespace detail.
  26. template<typename Ch, typename Alloc = std::allocator<Ch> >
  27. class basic_stdio_filter : public aggregate_filter<Ch, Alloc> {
  28. private:
  29. typedef aggregate_filter<Ch, Alloc> base_type;
  30. public:
  31. typedef typename base_type::char_type char_type;
  32. typedef typename base_type::category category;
  33. typedef typename base_type::vector_type vector_type;
  34. private:
  35. static std::istream& standard_input(char*) { return std::cin; }
  36. static std::ostream& standard_output(char*) { return std::cout; }
  37. #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
  38. static std::wistream& standard_input(wchar_t*) { return std::wcin; }
  39. static std::wostream& standard_output(wchar_t*) { return std::wcout; }
  40. #endif // BOOST_IOSTREAMS_NO_WIDE_STREAMS
  41. struct scoped_redirector { // Thanks to Maxim Egorushkin.
  42. typedef BOOST_IOSTREAMS_CHAR_TRAITS(Ch) traits_type;
  43. typedef BOOST_IOSTREAMS_BASIC_IOS(Ch, traits_type) ios_type;
  44. typedef BOOST_IOSTREAMS_BASIC_STREAMBUF(Ch, traits_type) streambuf_type;
  45. scoped_redirector( ios_type& ios,
  46. streambuf_type* newbuf )
  47. : ios_(ios), old_(ios.rdbuf(newbuf))
  48. { }
  49. ~scoped_redirector() { ios_.rdbuf(old_); }
  50. scoped_redirector& operator=(const scoped_redirector&);
  51. ios_type& ios_;
  52. streambuf_type* old_;
  53. };
  54. virtual void do_filter() = 0;
  55. virtual void do_filter(const vector_type& src, vector_type& dest)
  56. {
  57. stream_buffer< basic_array_source<Ch> >
  58. srcbuf(&src[0], &src[0] + src.size());
  59. stream_buffer< back_insert_device<vector_type> >
  60. destbuf(iostreams::back_inserter(dest));
  61. scoped_redirector redirect_input(standard_input((Ch*)0), &srcbuf);
  62. scoped_redirector redirect_output(standard_output((Ch*)0), &destbuf);
  63. do_filter();
  64. }
  65. };
  66. BOOST_IOSTREAMS_PIPABLE(basic_stdio_filter, 2)
  67. typedef basic_stdio_filter<char> stdio_filter;
  68. typedef basic_stdio_filter<wchar_t> wstdio_wfilter;
  69. } } // End namespaces iostreams, boost.
  70. #endif // #ifndef BOOST_IOSTREAMS_STDIO_FILTER_HPP_INCLUDED