Chris@16
|
1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
|
Chris@16
|
2 // (C) Copyright 2005-2007 Jonathan Turkanis
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
|
Chris@16
|
5
|
Chris@16
|
6 // See http://www.boost.org/libs/iostreams for documentation.
|
Chris@16
|
7
|
Chris@16
|
8 #ifndef BOOST_IOSTREAMS_DETAIL_COUNTED_ARRAY_HPP_INCLUDED
|
Chris@16
|
9 #define BOOST_IOSTREAMS_DETAIL_COUNTED_ARRAY_HPP_INCLUDED
|
Chris@16
|
10
|
Chris@16
|
11 #include <algorithm> // min.
|
Chris@16
|
12 #include <boost/iostreams/categories.hpp>
|
Chris@16
|
13 #include <boost/iostreams/detail/char_traits.hpp>
|
Chris@16
|
14 #include <boost/iostreams/detail/ios.hpp> // streamsize.
|
Chris@16
|
15
|
Chris@16
|
16 namespace boost { namespace iostreams { namespace detail {
|
Chris@16
|
17
|
Chris@16
|
18 template<typename Ch>
|
Chris@16
|
19 class counted_array_source {
|
Chris@16
|
20 public:
|
Chris@16
|
21 typedef Ch char_type;
|
Chris@16
|
22 typedef source_tag category;
|
Chris@16
|
23 counted_array_source(const Ch* buf, std::streamsize size)
|
Chris@16
|
24 : buf_(buf), ptr_(0), end_(size)
|
Chris@16
|
25 { }
|
Chris@16
|
26 std::streamsize read(Ch* s, std::streamsize n)
|
Chris@16
|
27 {
|
Chris@16
|
28 std::streamsize result = (std::min)(n, end_ - ptr_);
|
Chris@16
|
29 BOOST_IOSTREAMS_CHAR_TRAITS(char_type)::copy
|
Chris@16
|
30 (s, buf_ + ptr_, result);
|
Chris@16
|
31 ptr_ += result;
|
Chris@16
|
32 return result;
|
Chris@16
|
33 }
|
Chris@16
|
34 std::streamsize count() const { return ptr_; }
|
Chris@16
|
35 private:
|
Chris@16
|
36 const Ch* buf_;
|
Chris@16
|
37 std::streamsize ptr_, end_;
|
Chris@16
|
38 };
|
Chris@16
|
39
|
Chris@16
|
40 template<typename Ch>
|
Chris@16
|
41 struct counted_array_sink {
|
Chris@16
|
42 public:
|
Chris@16
|
43 typedef Ch char_type;
|
Chris@16
|
44 typedef sink_tag category;
|
Chris@16
|
45 counted_array_sink(Ch* buf, std::streamsize size)
|
Chris@16
|
46 : buf_(buf), ptr_(0), end_(size)
|
Chris@16
|
47 { }
|
Chris@16
|
48 std::streamsize write(const Ch* s, std::streamsize n)
|
Chris@16
|
49 {
|
Chris@16
|
50 std::streamsize result = (std::min)(n, end_ - ptr_);
|
Chris@16
|
51 BOOST_IOSTREAMS_CHAR_TRAITS(char_type)::copy
|
Chris@16
|
52 (buf_ + ptr_, s, result);
|
Chris@16
|
53 ptr_ += result;
|
Chris@16
|
54 return result;
|
Chris@16
|
55 }
|
Chris@16
|
56 std::streamsize count() const { return ptr_; }
|
Chris@16
|
57 private:
|
Chris@16
|
58 Ch* buf_;
|
Chris@16
|
59 std::streamsize ptr_, end_;
|
Chris@16
|
60 };
|
Chris@16
|
61
|
Chris@16
|
62 } } } // End namespaces iostreams, boost.
|
Chris@16
|
63
|
Chris@16
|
64 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_COUNTED_ARRAY_HPP_INCLUDED
|