| 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 | #ifndef BOOST_IOSTREAMS_DETAIL_COUNTED_ARRAY_HPP_INCLUDED |
|---|
| 8 | #define BOOST_IOSTREAMS_DETAIL_COUNTED_ARRAY_HPP_INCLUDED |
|---|
| 9 | |
|---|
| 10 | #include <algorithm> // min. |
|---|
| 11 | #include <boost/iostreams/categories.hpp> |
|---|
| 12 | #include <boost/iostreams/detail/char_traits.hpp> |
|---|
| 13 | #include <boost/iostreams/detail/ios.hpp> // streamsize. |
|---|
| 14 | |
|---|
| 15 | namespace boost { namespace iostreams { namespace detail { |
|---|
| 16 | |
|---|
| 17 | template<typename Ch> |
|---|
| 18 | class counted_array_source { |
|---|
| 19 | public: |
|---|
| 20 | typedef Ch char_type; |
|---|
| 21 | typedef source_tag category; |
|---|
| 22 | counted_array_source(const Ch* buf, std::streamsize size) |
|---|
| 23 | : buf_(buf), ptr_(0), end_(size) |
|---|
| 24 | { } |
|---|
| 25 | std::streamsize read(Ch* s, std::streamsize n) |
|---|
| 26 | { |
|---|
| 27 | std::streamsize result = (std::min)(n, end_ - ptr_); |
|---|
| 28 | BOOST_IOSTREAMS_CHAR_TRAITS(char_type)::copy |
|---|
| 29 | (s, buf_ + ptr_, result); |
|---|
| 30 | ptr_ += result; |
|---|
| 31 | return result; |
|---|
| 32 | } |
|---|
| 33 | std::streamsize count() const { return ptr_; } |
|---|
| 34 | private: |
|---|
| 35 | const Ch* buf_; |
|---|
| 36 | std::streamsize ptr_, end_; |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | template<typename Ch> |
|---|
| 40 | struct counted_array_sink { |
|---|
| 41 | public: |
|---|
| 42 | typedef Ch char_type; |
|---|
| 43 | typedef sink_tag category; |
|---|
| 44 | counted_array_sink(Ch* buf, std::streamsize size) |
|---|
| 45 | : buf_(buf), ptr_(0), end_(size) |
|---|
| 46 | { } |
|---|
| 47 | std::streamsize write(const Ch* s, std::streamsize n) |
|---|
| 48 | { |
|---|
| 49 | std::streamsize result = (std::min)(n, end_ - ptr_); |
|---|
| 50 | BOOST_IOSTREAMS_CHAR_TRAITS(char_type)::copy |
|---|
| 51 | (buf_ + ptr_, s, result); |
|---|
| 52 | ptr_ += result; |
|---|
| 53 | return result; |
|---|
| 54 | } |
|---|
| 55 | std::streamsize count() const { return ptr_; } |
|---|
| 56 | private: |
|---|
| 57 | Ch* buf_; |
|---|
| 58 | std::streamsize ptr_, end_; |
|---|
| 59 | }; |
|---|
| 60 | |
|---|
| 61 | } } } // End namespaces iostreams, boost. |
|---|
| 62 | |
|---|
| 63 | #endif // #ifndef BOOST_IOSTREAMS_DETAIL_COUNTED_ARRAY_HPP_INCLUDED |
|---|