ndnboost: Include boost::iostreams for internal use.
diff --git a/include/ndnboost/iostreams/detail/streambuf/chainbuf.hpp b/include/ndnboost/iostreams/detail/streambuf/chainbuf.hpp
new file mode 100644
index 0000000..157b855
--- /dev/null
+++ b/include/ndnboost/iostreams/detail/streambuf/chainbuf.hpp
@@ -0,0 +1,116 @@
+// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
+// (C) Copyright 2003-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.
+
+#ifndef NDNBOOST_IOSTREAMS_DETAIL_CHAINBUF_HPP_INCLUDED
+#define NDNBOOST_IOSTREAMS_DETAIL_CHAINBUF_HPP_INCLUDED
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include <ndnboost/config.hpp> // NDNBOOST_MSVC, template friends.
+#include <ndnboost/detail/workaround.hpp>
+#include <ndnboost/iostreams/chain.hpp>
+#include <ndnboost/iostreams/detail/access_control.hpp>
+#include <ndnboost/iostreams/detail/config/wide_streams.hpp>
+#include <ndnboost/iostreams/detail/streambuf.hpp>
+#include <ndnboost/iostreams/detail/streambuf/linked_streambuf.hpp>
+#include <ndnboost/iostreams/detail/translate_int_type.hpp>
+#include <ndnboost/iostreams/traits.hpp>
+#include <ndnboost/noncopyable.hpp>
+
+namespace ndnboost { namespace iostreams { namespace detail {
+
+//--------------Definition of chainbuf----------------------------------------//
+
+//
+// Template name: chainbuf.
+// Description: Stream buffer which operates by delegating to the first
+// linked_streambuf in a chain.
+// Template parameters:
+// Chain - The chain type.
+//
+template<typename Chain, typename Mode, typename Access>
+class chainbuf
+ : public NDNBOOST_IOSTREAMS_BASIC_STREAMBUF(
+ typename Chain::char_type,
+ typename Chain::traits_type
+ ),
+ public access_control<typename Chain::client_type, Access>,
+ private noncopyable
+{
+private:
+ typedef access_control<chain_client<Chain>, Access> client_type;
+public:
+ typedef typename Chain::char_type char_type;
+ NDNBOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(typename Chain::traits_type)
+protected:
+ typedef linked_streambuf<char_type, traits_type> delegate_type;
+ chainbuf() { client_type::set_chain(&chain_); }
+ int_type underflow()
+ { sentry t(this); return translate(delegate().underflow()); }
+ int_type pbackfail(int_type c)
+ { sentry t(this); return translate(delegate().pbackfail(c)); }
+ std::streamsize xsgetn(char_type* s, std::streamsize n)
+ { sentry t(this); return delegate().xsgetn(s, n); }
+ int_type overflow(int_type c)
+ { sentry t(this); return translate(delegate().overflow(c)); }
+ std::streamsize xsputn(const char_type* s, std::streamsize n)
+ { sentry t(this); return delegate().xsputn(s, n); }
+ int sync() { sentry t(this); return delegate().sync(); }
+ pos_type seekoff( off_type off, NDNBOOST_IOS::seekdir way,
+ NDNBOOST_IOS::openmode which =
+ NDNBOOST_IOS::in | NDNBOOST_IOS::out )
+ { sentry t(this); return delegate().seekoff(off, way, which); }
+ pos_type seekpos( pos_type sp,
+ NDNBOOST_IOS::openmode which =
+ NDNBOOST_IOS::in | NDNBOOST_IOS::out )
+ { sentry t(this); return delegate().seekpos(sp, which); }
+protected:
+ typedef NDNBOOST_IOSTREAMS_BASIC_STREAMBUF(
+ typename Chain::char_type,
+ typename Chain::traits_type
+ ) base_type;
+//#if !NDNBOOST_WORKAROUND(__GNUC__, == 2)
+// NDNBOOST_IOSTREAMS_USING_PROTECTED_STREAMBUF_MEMBERS(base_type)
+//#endif
+private:
+
+ // Translate from std int_type to chain's int_type.
+ typedef NDNBOOST_IOSTREAMS_CHAR_TRAITS(char_type) std_traits;
+ typedef typename Chain::traits_type chain_traits;
+ static typename chain_traits::int_type
+ translate(typename std_traits::int_type c)
+ { return translate_int_type<std_traits, chain_traits>(c); }
+
+ delegate_type& delegate()
+ { return static_cast<delegate_type&>(chain_.front()); }
+ void get_pointers()
+ {
+ this->setg(delegate().eback(), delegate().gptr(), delegate().egptr());
+ this->setp(delegate().pbase(), delegate().epptr());
+ this->pbump((int) (delegate().pptr() - delegate().pbase()));
+ }
+ void set_pointers()
+ {
+ delegate().setg(this->eback(), this->gptr(), this->egptr());
+ delegate().setp(this->pbase(), this->epptr());
+ delegate().pbump((int) (this->pptr() - this->pbase()));
+ }
+ struct sentry {
+ sentry(chainbuf<Chain, Mode, Access>* buf) : buf_(buf)
+ { buf_->set_pointers(); }
+ ~sentry() { buf_->get_pointers(); }
+ chainbuf<Chain, Mode, Access>* buf_;
+ };
+ friend struct sentry;
+ Chain chain_;
+};
+
+} } } // End namespaces detail, iostreams, boost.
+
+#endif // #ifndef NDNBOOST_IOSTREAMS_DETAIL_CHAINBUF_HPP_INCLUDED
diff --git a/include/ndnboost/iostreams/detail/streambuf/direct_streambuf.hpp b/include/ndnboost/iostreams/detail/streambuf/direct_streambuf.hpp
new file mode 100644
index 0000000..ec79604
--- /dev/null
+++ b/include/ndnboost/iostreams/detail/streambuf/direct_streambuf.hpp
@@ -0,0 +1,313 @@
+// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
+// (C) Copyright 2003-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.
+
+#ifndef NDNBOOST_IOSTREAMS_DETAIL_DIRECT_STREAMBUF_HPP_INCLUDED
+#define NDNBOOST_IOSTREAMS_DETAIL_DIRECT_STREAMBUF_HPP_INCLUDED
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include <ndnboost/assert.hpp>
+#include <cstddef>
+#include <typeinfo>
+#include <utility> // pair.
+#include <ndnboost/config.hpp> // NDNBOOST_DEDUCED_TYPENAME,
+#include <ndnboost/iostreams/detail/char_traits.hpp> // member template friends.
+#include <ndnboost/iostreams/detail/config/wide_streams.hpp>
+#include <ndnboost/iostreams/detail/error.hpp>
+#include <ndnboost/iostreams/detail/execute.hpp>
+#include <ndnboost/iostreams/detail/functional.hpp>
+#include <ndnboost/iostreams/detail/ios.hpp>
+#include <ndnboost/iostreams/detail/optional.hpp>
+#include <ndnboost/iostreams/detail/streambuf.hpp>
+#include <ndnboost/iostreams/detail/streambuf/linked_streambuf.hpp>
+#include <ndnboost/iostreams/operations.hpp>
+#include <ndnboost/iostreams/positioning.hpp>
+#include <ndnboost/iostreams/traits.hpp>
+#include <ndnboost/throw_exception.hpp>
+
+// Must come last.
+#include <ndnboost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
+
+namespace ndnboost { namespace iostreams {
+
+namespace detail {
+
+template< typename T,
+ typename Tr =
+ NDNBOOST_IOSTREAMS_CHAR_TRAITS(
+ NDNBOOST_DEDUCED_TYPENAME char_type_of<T>::type
+ ) >
+class direct_streambuf
+ : public linked_streambuf<NDNBOOST_DEDUCED_TYPENAME char_type_of<T>::type, Tr>
+{
+public:
+ typedef typename char_type_of<T>::type char_type;
+ NDNBOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr)
+private:
+ typedef linked_streambuf<char_type, traits_type> base_type;
+ typedef typename category_of<T>::type category;
+ typedef NDNBOOST_IOSTREAMS_BASIC_STREAMBUF(
+ char_type, traits_type
+ ) streambuf_type;
+public: // stream needs access.
+ void open(const T& t, std::streamsize buffer_size,
+ std::streamsize pback_size);
+ bool is_open() const;
+ void close();
+ bool auto_close() const { return auto_close_; }
+ void set_auto_close(bool close) { auto_close_ = close; }
+ bool strict_sync() { return true; }
+
+ // Declared in linked_streambuf.
+ T* component() { return storage_.get(); }
+protected:
+#if !NDNBOOST_WORKAROUND(__GNUC__, == 2)
+ NDNBOOST_IOSTREAMS_USING_PROTECTED_STREAMBUF_MEMBERS(base_type)
+#endif
+ direct_streambuf();
+
+ //--------------Virtual functions-----------------------------------------//
+
+ // Declared in linked_streambuf.
+ void close_impl(NDNBOOST_IOS::openmode m);
+ const std::type_info& component_type() const { return typeid(T); }
+ void* component_impl() { return component(); }
+#ifdef NDNBOOST_IOSTREAMS_NO_STREAM_TEMPLATES
+ public:
+#endif
+
+ // Declared in basic_streambuf.
+ int_type underflow();
+ int_type pbackfail(int_type c);
+ int_type overflow(int_type c);
+ pos_type seekoff( off_type off, NDNBOOST_IOS::seekdir way,
+ NDNBOOST_IOS::openmode which );
+ pos_type seekpos(pos_type sp, NDNBOOST_IOS::openmode which);
+private:
+ pos_type seek_impl( stream_offset off, NDNBOOST_IOS::seekdir way,
+ NDNBOOST_IOS::openmode which );
+ void init_input(any_tag) { }
+ void init_input(input);
+ void init_output(any_tag) { }
+ void init_output(output);
+ void init_get_area();
+ void init_put_area();
+ bool one_head() const;
+ bool two_head() const;
+ optional<T> storage_;
+ char_type *ibeg_, *iend_, *obeg_, *oend_;
+ bool auto_close_;
+};
+
+//------------------Implementation of direct_streambuf------------------------//
+
+template<typename T, typename Tr>
+direct_streambuf<T, Tr>::direct_streambuf()
+ : ibeg_(0), iend_(0), obeg_(0), oend_(0), auto_close_(true)
+{ this->set_true_eof(true); }
+
+template<typename T, typename Tr>
+void direct_streambuf<T, Tr>::open
+ (const T& t, std::streamsize, std::streamsize)
+{
+ storage_.reset(t);
+ init_input(category());
+ init_output(category());
+ setg(0, 0, 0);
+ setp(0, 0);
+ this->set_needs_close();
+}
+
+template<typename T, typename Tr>
+bool direct_streambuf<T, Tr>::is_open() const
+{ return ibeg_ != 0 || obeg_ != 0; }
+
+template<typename T, typename Tr>
+void direct_streambuf<T, Tr>::close()
+{
+ base_type* self = this;
+ detail::execute_all( detail::call_member_close(*self, NDNBOOST_IOS::in),
+ detail::call_member_close(*self, NDNBOOST_IOS::out),
+ detail::call_reset(storage_) );
+}
+
+template<typename T, typename Tr>
+typename direct_streambuf<T, Tr>::int_type
+direct_streambuf<T, Tr>::underflow()
+{
+ if (!ibeg_)
+ ndnboost::throw_exception(cant_read());
+ if (!gptr())
+ init_get_area();
+ return gptr() != iend_ ?
+ traits_type::to_int_type(*gptr()) :
+ traits_type::eof();
+}
+
+template<typename T, typename Tr>
+typename direct_streambuf<T, Tr>::int_type
+direct_streambuf<T, Tr>::pbackfail(int_type c)
+{
+ using namespace std;
+ if (!ibeg_)
+ ndnboost::throw_exception(cant_read());
+ if (gptr() != 0 && gptr() != ibeg_) {
+ gbump(-1);
+ if (!traits_type::eq_int_type(c, traits_type::eof()))
+ *gptr() = traits_type::to_char_type(c);
+ return traits_type::not_eof(c);
+ }
+ ndnboost::throw_exception(bad_putback());
+}
+
+template<typename T, typename Tr>
+typename direct_streambuf<T, Tr>::int_type
+direct_streambuf<T, Tr>::overflow(int_type c)
+{
+ using namespace std;
+ if (!obeg_)
+ ndnboost::throw_exception(NDNBOOST_IOSTREAMS_FAILURE("no write access"));
+ if (!pptr()) init_put_area();
+ if (!traits_type::eq_int_type(c, traits_type::eof())) {
+ if (pptr() == oend_)
+ ndnboost::throw_exception(
+ NDNBOOST_IOSTREAMS_FAILURE("write area exhausted")
+ );
+ *pptr() = traits_type::to_char_type(c);
+ pbump(1);
+ return c;
+ }
+ return traits_type::not_eof(c);
+}
+
+template<typename T, typename Tr>
+inline typename direct_streambuf<T, Tr>::pos_type
+direct_streambuf<T, Tr>::seekoff
+ (off_type off, NDNBOOST_IOS::seekdir way, NDNBOOST_IOS::openmode which)
+{ return seek_impl(off, way, which); }
+
+template<typename T, typename Tr>
+inline typename direct_streambuf<T, Tr>::pos_type
+direct_streambuf<T, Tr>::seekpos
+ (pos_type sp, NDNBOOST_IOS::openmode which)
+{
+ return seek_impl(position_to_offset(sp), NDNBOOST_IOS::beg, which);
+}
+
+template<typename T, typename Tr>
+void direct_streambuf<T, Tr>::close_impl(NDNBOOST_IOS::openmode which)
+{
+ if (which == NDNBOOST_IOS::in && ibeg_ != 0) {
+ setg(0, 0, 0);
+ ibeg_ = iend_ = 0;
+ }
+ if (which == NDNBOOST_IOS::out && obeg_ != 0) {
+ sync();
+ setp(0, 0);
+ obeg_ = oend_ = 0;
+ }
+ ndnboost::iostreams::close(*storage_, which);
+}
+
+template<typename T, typename Tr>
+typename direct_streambuf<T, Tr>::pos_type direct_streambuf<T, Tr>::seek_impl
+ (stream_offset off, NDNBOOST_IOS::seekdir way, NDNBOOST_IOS::openmode which)
+{
+ using namespace std;
+ NDNBOOST_IOS::openmode both = NDNBOOST_IOS::in | NDNBOOST_IOS::out;
+ if (two_head() && (which & both) == both)
+ ndnboost::throw_exception(bad_seek());
+ stream_offset result = -1;
+ bool one = one_head();
+ if (one && (pptr() != 0 || gptr()== 0))
+ init_get_area(); // Switch to input mode, for code reuse.
+ if (one || ((which & NDNBOOST_IOS::in) != 0 && ibeg_ != 0)) {
+ if (!gptr()) setg(ibeg_, ibeg_, iend_);
+ ptrdiff_t next = 0;
+ switch (way) {
+ case NDNBOOST_IOS::beg: next = off; break;
+ case NDNBOOST_IOS::cur: next = (gptr() - ibeg_) + off; break;
+ case NDNBOOST_IOS::end: next = (iend_ - ibeg_) + off; break;
+ default: NDNBOOST_ASSERT(0);
+ }
+ if (next < 0 || next > (iend_ - ibeg_))
+ ndnboost::throw_exception(bad_seek());
+ setg(ibeg_, ibeg_ + next, iend_);
+ result = next;
+ }
+ if (!one && (which & NDNBOOST_IOS::out) != 0 && obeg_ != 0) {
+ if (!pptr()) setp(obeg_, oend_);
+ ptrdiff_t next = 0;
+ switch (way) {
+ case NDNBOOST_IOS::beg: next = off; break;
+ case NDNBOOST_IOS::cur: next = (pptr() - obeg_) + off; break;
+ case NDNBOOST_IOS::end: next = (oend_ - obeg_) + off; break;
+ default: NDNBOOST_ASSERT(0);
+ }
+ if (next < 0 || next > (oend_ - obeg_))
+ ndnboost::throw_exception(bad_seek());
+ pbump(static_cast<int>(next - (pptr() - obeg_)));
+ result = next;
+ }
+ return offset_to_position(result);
+}
+
+template<typename T, typename Tr>
+void direct_streambuf<T, Tr>::init_input(input)
+{
+ std::pair<char_type*, char_type*> p = input_sequence(*storage_);
+ ibeg_ = p.first;
+ iend_ = p.second;
+}
+
+template<typename T, typename Tr>
+void direct_streambuf<T, Tr>::init_output(output)
+{
+ std::pair<char_type*, char_type*> p = output_sequence(*storage_);
+ obeg_ = p.first;
+ oend_ = p.second;
+}
+
+template<typename T, typename Tr>
+void direct_streambuf<T, Tr>::init_get_area()
+{
+ setg(ibeg_, ibeg_, iend_);
+ if (one_head() && pptr()) {
+ gbump(static_cast<int>(pptr() - obeg_));
+ setp(0, 0);
+ }
+}
+
+template<typename T, typename Tr>
+void direct_streambuf<T, Tr>::init_put_area()
+{
+ setp(obeg_, oend_);
+ if (one_head() && gptr()) {
+ pbump(static_cast<int>(gptr() - ibeg_));
+ setg(0, 0, 0);
+ }
+}
+
+template<typename T, typename Tr>
+inline bool direct_streambuf<T, Tr>::one_head() const
+{ return ibeg_ && obeg_ && ibeg_ == obeg_; }
+
+template<typename T, typename Tr>
+inline bool direct_streambuf<T, Tr>::two_head() const
+{ return ibeg_ && obeg_ && ibeg_ != obeg_; }
+
+//----------------------------------------------------------------------------//
+
+} // End namespace detail.
+
+} } // End namespaces iostreams, boost.
+
+#include <ndnboost/iostreams/detail/config/enable_warnings.hpp> // MSVC
+
+#endif // #ifndef NDNBOOST_IOSTREAMS_DETAIL_DIRECT_STREAMBUF_HPP_INCLUDED
diff --git a/include/ndnboost/iostreams/detail/streambuf/indirect_streambuf.hpp b/include/ndnboost/iostreams/detail/streambuf/indirect_streambuf.hpp
new file mode 100644
index 0000000..d657ddb
--- /dev/null
+++ b/include/ndnboost/iostreams/detail/streambuf/indirect_streambuf.hpp
@@ -0,0 +1,432 @@
+// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
+// (C) Copyright 2003-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.
+
+// This material is heavily indebted to the discussion and code samples in
+// A. Langer and K. Kreft, "Standard C++ IOStreams and Locales",
+// Addison-Wesley, 2000, pp. 228-43.
+
+// User "GMSB" provided an optimization for small seeks.
+
+#ifndef NDNBOOST_IOSTREAMS_DETAIL_INDIRECT_STREAMBUF_HPP_INCLUDED
+#define NDNBOOST_IOSTREAMS_DETAIL_INDIRECT_STREAMBUF_HPP_INCLUDED
+
+#include <algorithm> // min, max.
+#include <cassert>
+#include <exception>
+#include <typeinfo>
+#include <ndnboost/config.hpp> // Member template friends.
+#include <ndnboost/detail/workaround.hpp>
+#include <ndnboost/iostreams/constants.hpp>
+#include <ndnboost/iostreams/detail/adapter/concept_adapter.hpp>
+#include <ndnboost/iostreams/detail/buffer.hpp>
+#include <ndnboost/iostreams/detail/config/wide_streams.hpp>
+#include <ndnboost/iostreams/detail/double_object.hpp>
+#include <ndnboost/iostreams/detail/execute.hpp>
+#include <ndnboost/iostreams/detail/functional.hpp>
+#include <ndnboost/iostreams/detail/ios.hpp>
+#include <ndnboost/iostreams/detail/optional.hpp>
+#include <ndnboost/iostreams/detail/push.hpp>
+#include <ndnboost/iostreams/detail/streambuf/linked_streambuf.hpp>
+#include <ndnboost/iostreams/operations.hpp>
+#include <ndnboost/iostreams/positioning.hpp>
+#include <ndnboost/iostreams/traits.hpp>
+#include <ndnboost/iostreams/operations.hpp>
+#include <ndnboost/mpl/if.hpp>
+#include <ndnboost/throw_exception.hpp>
+#include <ndnboost/type_traits/is_convertible.hpp>
+
+// Must come last.
+#include <ndnboost/iostreams/detail/config/disable_warnings.hpp> // MSVC, BCC 5.x
+
+namespace ndnboost { namespace iostreams { namespace detail {
+
+//
+// Description: The implementation of basic_streambuf used by chains.
+//
+template<typename T, typename Tr, typename Alloc, typename Mode>
+class indirect_streambuf
+ : public linked_streambuf<NDNBOOST_DEDUCED_TYPENAME char_type_of<T>::type, Tr>
+{
+public:
+ typedef typename char_type_of<T>::type char_type;
+ NDNBOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr)
+private:
+ typedef typename category_of<T>::type category;
+ typedef concept_adapter<T> wrapper;
+ typedef detail::basic_buffer<char_type, Alloc> buffer_type;
+ typedef indirect_streambuf<T, Tr, Alloc, Mode> my_type;
+ typedef detail::linked_streambuf<char_type, traits_type> base_type;
+ typedef linked_streambuf<char_type, Tr> streambuf_type;
+public:
+ indirect_streambuf();
+
+ void open(const T& t NDNBOOST_IOSTREAMS_PUSH_PARAMS());
+ bool is_open() const;
+ void close();
+ bool auto_close() const;
+ void set_auto_close(bool close);
+ bool strict_sync();
+
+ // Declared in linked_streambuf.
+ T* component() { return &*obj(); }
+protected:
+#if !NDNBOOST_WORKAROUND(__GNUC__, == 2)
+ NDNBOOST_IOSTREAMS_USING_PROTECTED_STREAMBUF_MEMBERS(base_type)
+#endif
+
+ //----------virtual functions---------------------------------------------//
+
+#ifndef NDNBOOST_IOSTREAMS_NO_LOCALE
+ void imbue(const std::locale& loc);
+#endif
+#ifdef NDNBOOST_IOSTREAMS_NO_STREAM_TEMPLATES
+ public:
+#endif
+ int_type underflow();
+ int_type pbackfail(int_type c);
+ int_type overflow(int_type c);
+ int sync();
+ pos_type seekoff( off_type off, NDNBOOST_IOS::seekdir way,
+ NDNBOOST_IOS::openmode which );
+ pos_type seekpos(pos_type sp, NDNBOOST_IOS::openmode which);
+
+ // Declared in linked_streambuf.
+ void set_next(streambuf_type* next);
+ void close_impl(NDNBOOST_IOS::openmode m);
+ const std::type_info& component_type() const { return typeid(T); }
+ void* component_impl() { return component(); }
+private:
+
+ //----------Accessor functions--------------------------------------------//
+
+ wrapper& obj() { return *storage_; }
+ streambuf_type* next() const { return next_; }
+ buffer_type& in() { return buffer_.first(); }
+ buffer_type& out() { return buffer_.second(); }
+ bool can_read() const { return is_convertible<Mode, input>::value; }
+ bool can_write() const { return is_convertible<Mode, output>::value; }
+ bool output_buffered() const { return (flags_ & f_output_buffered) != 0; }
+ bool shared_buffer() const { return is_convertible<Mode, seekable>::value; }
+ void set_flags(int f) { flags_ = f; }
+
+ //----------State changing functions--------------------------------------//
+
+ virtual void init_get_area();
+ virtual void init_put_area();
+
+ //----------Utility function----------------------------------------------//
+
+ pos_type seek_impl( stream_offset off, NDNBOOST_IOS::seekdir way,
+ NDNBOOST_IOS::openmode which );
+ void sync_impl();
+
+ enum flag_type {
+ f_open = 1,
+ f_output_buffered = f_open << 1,
+ f_auto_close = f_output_buffered << 1
+ };
+
+ optional<wrapper> storage_;
+ streambuf_type* next_;
+ double_object<
+ buffer_type,
+ is_convertible<
+ Mode,
+ two_sequence
+ >
+ > buffer_;
+ std::streamsize pback_size_;
+ int flags_;
+};
+
+//--------------Implementation of indirect_streambuf--------------------------//
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+indirect_streambuf<T, Tr, Alloc, Mode>::indirect_streambuf()
+ : next_(0), pback_size_(0), flags_(f_auto_close) { }
+
+//--------------Implementation of open, is_open and close---------------------//
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+void indirect_streambuf<T, Tr, Alloc, Mode>::open
+ (const T& t, std::streamsize buffer_size, std::streamsize pback_size)
+{
+ using namespace std;
+
+ // Normalize buffer sizes.
+ buffer_size =
+ (buffer_size != -1) ?
+ buffer_size :
+ iostreams::optimal_buffer_size(t);
+ pback_size =
+ (pback_size != -1) ?
+ pback_size :
+ default_pback_buffer_size;
+
+ // Construct input buffer.
+ if (can_read()) {
+ pback_size_ = (std::max)(std::streamsize(2), pback_size); // STLPort needs 2.
+ std::streamsize size =
+ pback_size_ +
+ ( buffer_size ? buffer_size: 1 );
+ in().resize(size);
+ if (!shared_buffer())
+ init_get_area();
+ }
+
+ // Construct output buffer.
+ if (can_write() && !shared_buffer()) {
+ if (buffer_size != 0)
+ out().resize(buffer_size);
+ init_put_area();
+ }
+
+ storage_.reset(wrapper(t));
+ flags_ |= f_open;
+ if (can_write() && buffer_size > 1)
+ flags_ |= f_output_buffered;
+ this->set_true_eof(false);
+ this->set_needs_close();
+}
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+inline bool indirect_streambuf<T, Tr, Alloc, Mode>::is_open() const
+{ return (flags_ & f_open) != 0; }
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+void indirect_streambuf<T, Tr, Alloc, Mode>::close()
+{
+ using namespace std;
+ base_type* self = this;
+ detail::execute_all(
+ detail::call_member_close(*self, NDNBOOST_IOS::in),
+ detail::call_member_close(*self, NDNBOOST_IOS::out),
+ detail::call_reset(storage_),
+ detail::clear_flags(flags_)
+ );
+}
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+bool indirect_streambuf<T, Tr, Alloc, Mode>::auto_close() const
+{ return (flags_ & f_auto_close) != 0; }
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+void indirect_streambuf<T, Tr, Alloc, Mode>::set_auto_close(bool close)
+{ flags_ = (flags_ & ~f_auto_close) | (close ? f_auto_close : 0); }
+
+//--------------Implementation virtual functions------------------------------//
+
+#ifndef NDNBOOST_IOSTREAMS_NO_LOCALE
+template<typename T, typename Tr, typename Alloc, typename Mode>
+void indirect_streambuf<T, Tr, Alloc, Mode>::imbue(const std::locale& loc)
+{
+ if (is_open()) {
+ obj().imbue(loc);
+ if (next_)
+ next_->pubimbue(loc);
+ }
+}
+#endif
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+typename indirect_streambuf<T, Tr, Alloc, Mode>::int_type
+indirect_streambuf<T, Tr, Alloc, Mode>::underflow()
+{
+ using namespace std;
+ if (!gptr()) init_get_area();
+ buffer_type& buf = in();
+ if (gptr() < egptr()) return traits_type::to_int_type(*gptr());
+
+ // Fill putback buffer.
+ std::streamsize keep =
+ (std::min)( static_cast<std::streamsize>(gptr() - eback()),
+ pback_size_ );
+ if (keep)
+ traits_type::move( buf.data() + (pback_size_ - keep),
+ gptr() - keep, keep );
+
+ // Set pointers to reasonable values in case read throws.
+ setg( buf.data() + pback_size_ - keep,
+ buf.data() + pback_size_,
+ buf.data() + pback_size_ );
+
+ // Read from source.
+ std::streamsize chars =
+ obj().read(buf.data() + pback_size_, buf.size() - pback_size_, next_);
+ if (chars == -1) {
+ this->set_true_eof(true);
+ chars = 0;
+ }
+ setg(eback(), gptr(), buf.data() + pback_size_ + chars);
+ return chars != 0 ?
+ traits_type::to_int_type(*gptr()) :
+ traits_type::eof();
+}
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+typename indirect_streambuf<T, Tr, Alloc, Mode>::int_type
+indirect_streambuf<T, Tr, Alloc, Mode>::pbackfail(int_type c)
+{
+ if (gptr() != eback()) {
+ gbump(-1);
+ if (!traits_type::eq_int_type(c, traits_type::eof()))
+ *gptr() = traits_type::to_char_type(c);
+ return traits_type::not_eof(c);
+ } else {
+ ndnboost::throw_exception(bad_putback());
+ }
+}
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+typename indirect_streambuf<T, Tr, Alloc, Mode>::int_type
+indirect_streambuf<T, Tr, Alloc, Mode>::overflow(int_type c)
+{
+ if ( (output_buffered() && pptr() == 0) ||
+ (shared_buffer() && gptr() != 0) )
+ {
+ init_put_area();
+ }
+ if (!traits_type::eq_int_type(c, traits_type::eof())) {
+ if (output_buffered()) {
+ if (pptr() == epptr()) {
+ sync_impl();
+ if (pptr() == epptr())
+ return traits_type::eof();
+ }
+ *pptr() = traits_type::to_char_type(c);
+ pbump(1);
+ } else {
+ char_type d = traits_type::to_char_type(c);
+ if (obj().write(&d, 1, next_) != 1)
+ return traits_type::eof();
+ }
+ }
+ return traits_type::not_eof(c);
+}
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+int indirect_streambuf<T, Tr, Alloc, Mode>::sync()
+{
+ try { // sync() is no-throw.
+ sync_impl();
+ obj().flush(next_);
+ return 0;
+ } catch (...) { return -1; }
+}
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+bool indirect_streambuf<T, Tr, Alloc, Mode>::strict_sync()
+{
+ try { // sync() is no-throw.
+ sync_impl();
+ return obj().flush(next_);
+ } catch (...) { return false; }
+}
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+inline typename indirect_streambuf<T, Tr, Alloc, Mode>::pos_type
+indirect_streambuf<T, Tr, Alloc, Mode>::seekoff
+ (off_type off, NDNBOOST_IOS::seekdir way, NDNBOOST_IOS::openmode which)
+{ return seek_impl(off, way, which); }
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+inline typename indirect_streambuf<T, Tr, Alloc, Mode>::pos_type
+indirect_streambuf<T, Tr, Alloc, Mode>::seekpos
+ (pos_type sp, NDNBOOST_IOS::openmode which)
+{
+ return seek_impl(position_to_offset(sp), NDNBOOST_IOS::beg, which);
+}
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+typename indirect_streambuf<T, Tr, Alloc, Mode>::pos_type
+indirect_streambuf<T, Tr, Alloc, Mode>::seek_impl
+ (stream_offset off, NDNBOOST_IOS::seekdir way, NDNBOOST_IOS::openmode which)
+{
+ if ( gptr() != 0 && way == NDNBOOST_IOS::cur && which == NDNBOOST_IOS::in &&
+ eback() - gptr() <= off && off <= egptr() - gptr() )
+ { // Small seek optimization
+ gbump(off);
+ return obj().seek(0, NDNBOOST_IOS::cur, NDNBOOST_IOS::in, next_) -
+ static_cast<off_type>(egptr() - gptr());
+ }
+ if (pptr() != 0)
+ this->NDNBOOST_IOSTREAMS_PUBSYNC(); // sync() confuses VisualAge 6.
+ if (way == NDNBOOST_IOS::cur && gptr())
+ off -= static_cast<off_type>(egptr() - gptr());
+ setg(0, 0, 0);
+ setp(0, 0);
+ return obj().seek(off, way, which, next_);
+}
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+inline void indirect_streambuf<T, Tr, Alloc, Mode>::set_next
+ (streambuf_type* next)
+{ next_ = next; }
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+inline void indirect_streambuf<T, Tr, Alloc, Mode>::close_impl
+ (NDNBOOST_IOS::openmode which)
+{
+ if (which == NDNBOOST_IOS::in && is_convertible<Mode, input>::value) {
+ setg(0, 0, 0);
+ }
+ if (which == NDNBOOST_IOS::out && is_convertible<Mode, output>::value) {
+ sync();
+ setp(0, 0);
+ }
+ if ( !is_convertible<category, dual_use>::value ||
+ is_convertible<Mode, input>::value == (which == NDNBOOST_IOS::in) )
+ {
+ obj().close(which, next_);
+ }
+}
+
+//----------State changing functions------------------------------------------//
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+void indirect_streambuf<T, Tr, Alloc, Mode>::sync_impl()
+{
+ std::streamsize avail, amt;
+ if ((avail = static_cast<std::streamsize>(pptr() - pbase())) > 0) {
+ if ((amt = obj().write(pbase(), avail, next())) == avail)
+ setp(out().begin(), out().end());
+ else {
+ const char_type* ptr = pptr();
+ setp(out().begin() + amt, out().end());
+ pbump(ptr - pptr());
+ }
+ }
+}
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+void indirect_streambuf<T, Tr, Alloc, Mode>::init_get_area()
+{
+ if (shared_buffer() && pptr() != 0) {
+ sync_impl();
+ setp(0, 0);
+ }
+ setg(in().begin(), in().begin(), in().begin());
+}
+
+template<typename T, typename Tr, typename Alloc, typename Mode>
+void indirect_streambuf<T, Tr, Alloc, Mode>::init_put_area()
+{
+ using namespace std;
+ if (shared_buffer() && gptr() != 0)
+ setg(0, 0, 0);
+ if (output_buffered())
+ setp(out().begin(), out().end());
+ else
+ setp(0, 0);
+}
+
+//----------------------------------------------------------------------------//
+
+} } } // End namespaces detail, iostreams, boost.
+
+#include <ndnboost/iostreams/detail/config/enable_warnings.hpp> // MSVC, BCC 5.x
+
+#endif // #ifndef NDNBOOST_IOSTREAMS_DETAIL_INDIRECT_STREAMBUF_HPP_INCLUDED
diff --git a/include/ndnboost/iostreams/detail/streambuf/linked_streambuf.hpp b/include/ndnboost/iostreams/detail/streambuf/linked_streambuf.hpp
new file mode 100644
index 0000000..d6ec23d
--- /dev/null
+++ b/include/ndnboost/iostreams/detail/streambuf/linked_streambuf.hpp
@@ -0,0 +1,114 @@
+// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
+// (C) Copyright 2003-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.
+
+#ifndef NDNBOOST_IOSTREAMS_DETAIL_LINKED_STREAMBUF_HPP_INCLUDED
+#define NDNBOOST_IOSTREAMS_DETAIL_LINKED_STREAMBUF_HPP_INCLUDED
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include <typeinfo>
+#include <ndnboost/config.hpp> // member template friends.
+#include <ndnboost/iostreams/detail/char_traits.hpp>
+#include <ndnboost/iostreams/detail/ios.hpp> // openmode.
+#include <ndnboost/iostreams/detail/streambuf.hpp>
+
+// Must come last.
+#include <ndnboost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
+
+namespace ndnboost { namespace iostreams { namespace detail {
+
+template<typename Self, typename Ch, typename Tr, typename Alloc, typename Mode>
+class chain_base;
+
+template<typename Chain, typename Access, typename Mode> class chainbuf;
+
+#define NDNBOOST_IOSTREAMS_USING_PROTECTED_STREAMBUF_MEMBERS(base) \
+ using base::eback; using base::gptr; using base::egptr; \
+ using base::setg; using base::gbump; using base::pbase; \
+ using base::pptr; using base::epptr; using base::setp; \
+ using base::pbump; using base::underflow; using base::pbackfail; \
+ using base::xsgetn; using base::overflow; using base::xsputn; \
+ using base::sync; using base::seekoff; using base::seekpos; \
+ /**/
+
+template<typename Ch, typename Tr = NDNBOOST_IOSTREAMS_CHAR_TRAITS(Ch) >
+class linked_streambuf : public NDNBOOST_IOSTREAMS_BASIC_STREAMBUF(Ch, Tr) {
+protected:
+ linked_streambuf() : flags_(0) { }
+ void set_true_eof(bool eof)
+ {
+ flags_ = (flags_ & ~f_true_eof) | (eof ? f_true_eof : 0);
+ }
+public:
+
+ // Should be called only after receiving an ordinary EOF indication,
+ // to confirm that it represents EOF rather than WOULD_BLOCK.
+ bool true_eof() const { return (flags_ & f_true_eof) != 0; }
+protected:
+
+ //----------grant friendship to chain_base and chainbuf-------------------//
+
+#ifndef NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS
+ template< typename Self, typename ChT, typename TrT,
+ typename Alloc, typename Mode >
+ friend class chain_base;
+ template<typename Chain, typename Mode, typename Access>
+ friend class chainbuf;
+ template<typename U>
+ friend class member_close_operation;
+#else
+ public:
+ typedef NDNBOOST_IOSTREAMS_BASIC_STREAMBUF(Ch, Tr) base;
+ NDNBOOST_IOSTREAMS_USING_PROTECTED_STREAMBUF_MEMBERS(base)
+#endif
+ void close(NDNBOOST_IOS::openmode which)
+ {
+ if ( which == NDNBOOST_IOS::in &&
+ (flags_ & f_input_closed) == 0 )
+ {
+ flags_ |= f_input_closed;
+ close_impl(which);
+ }
+ if ( which == NDNBOOST_IOS::out &&
+ (flags_ & f_output_closed) == 0 )
+ {
+ flags_ |= f_output_closed;
+ close_impl(which);
+ }
+ }
+ void set_needs_close()
+ {
+ flags_ &= ~(f_input_closed | f_output_closed);
+ }
+ virtual void set_next(linked_streambuf<Ch, Tr>* /* next */) { }
+ virtual void close_impl(NDNBOOST_IOS::openmode) = 0;
+ virtual bool auto_close() const = 0;
+ virtual void set_auto_close(bool) = 0;
+ virtual bool strict_sync() = 0;
+ virtual const std::type_info& component_type() const = 0;
+ virtual void* component_impl() = 0;
+#ifndef NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS
+ private:
+#else
+ public:
+#endif
+private:
+ enum flag_type {
+ f_true_eof = 1,
+ f_input_closed = f_true_eof << 1,
+ f_output_closed = f_input_closed << 1
+ };
+ int flags_;
+};
+
+} } } // End namespaces detail, iostreams, boost.
+
+#include <ndnboost/iostreams/detail/config/enable_warnings.hpp> // MSVC.
+
+#endif // #ifndef NDNBOOST_IOSTREAMS_DETAIL_LINKED_STREAMBUF_HPP_INCLUDED