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