Chris@16: // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com) Chris@16: // (C) Copyright 2005-2007 Jonathan Turkanis Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) Chris@16: Chris@16: // See http://www.boost.org/libs/iostreams for documentation. Chris@16: Chris@16: #ifndef BOOST_IOSTREAMS_DETAIL_COUNTED_ARRAY_HPP_INCLUDED Chris@16: #define BOOST_IOSTREAMS_DETAIL_COUNTED_ARRAY_HPP_INCLUDED Chris@16: Chris@16: #include // min. Chris@16: #include Chris@16: #include Chris@16: #include // streamsize. Chris@16: Chris@16: namespace boost { namespace iostreams { namespace detail { Chris@16: Chris@16: template Chris@16: class counted_array_source { Chris@16: public: Chris@16: typedef Ch char_type; Chris@16: typedef source_tag category; Chris@16: counted_array_source(const Ch* buf, std::streamsize size) Chris@16: : buf_(buf), ptr_(0), end_(size) Chris@16: { } Chris@16: std::streamsize read(Ch* s, std::streamsize n) Chris@16: { Chris@16: std::streamsize result = (std::min)(n, end_ - ptr_); Chris@16: BOOST_IOSTREAMS_CHAR_TRAITS(char_type)::copy Chris@16: (s, buf_ + ptr_, result); Chris@16: ptr_ += result; Chris@16: return result; Chris@16: } Chris@16: std::streamsize count() const { return ptr_; } Chris@16: private: Chris@16: const Ch* buf_; Chris@16: std::streamsize ptr_, end_; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct counted_array_sink { Chris@16: public: Chris@16: typedef Ch char_type; Chris@16: typedef sink_tag category; Chris@16: counted_array_sink(Ch* buf, std::streamsize size) Chris@16: : buf_(buf), ptr_(0), end_(size) Chris@16: { } Chris@16: std::streamsize write(const Ch* s, std::streamsize n) Chris@16: { Chris@16: std::streamsize result = (std::min)(n, end_ - ptr_); Chris@16: BOOST_IOSTREAMS_CHAR_TRAITS(char_type)::copy Chris@16: (buf_ + ptr_, s, result); Chris@16: ptr_ += result; Chris@16: return result; Chris@16: } Chris@16: std::streamsize count() const { return ptr_; } Chris@16: private: Chris@16: Ch* buf_; Chris@16: std::streamsize ptr_, end_; Chris@16: }; Chris@16: Chris@16: } } } // End namespaces iostreams, boost. Chris@16: Chris@16: #endif // #ifndef BOOST_IOSTREAMS_DETAIL_COUNTED_ARRAY_HPP_INCLUDED