Ugly, not-to-be-pushed sucking in of all of Boost to get windows to work.
[dyninst.git] / external / boost / iostreams / filter / counter.hpp
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_COUNTER_HPP_INCLUDED
8 #define BOOST_IOSTREAMS_COUNTER_HPP_INCLUDED
9
10 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
11 # pragma once
12 #endif
13
14 #include <algorithm>  // count.
15 #include <boost/iostreams/categories.hpp>
16 #include <boost/iostreams/char_traits.hpp>
17 #include <boost/iostreams/operations.hpp>
18 #include <boost/iostreams/pipeline.hpp>
19
20 // Must come last.
21 #include <boost/iostreams/detail/config/disable_warnings.hpp> // VC7.1 C4244.
22
23 namespace boost { namespace iostreams {
24
25 //
26 // Template name: basic_counter.
27 // Template paramters:
28 //      Ch - The character type.
29 // Description: Filter which counts lines and characters.
30 //
31 template<typename Ch>
32 class basic_counter  {
33 public:
34     typedef Ch char_type;
35     struct category
36         : dual_use,
37           filter_tag,
38           multichar_tag,
39           optimally_buffered_tag
40         { };
41     explicit basic_counter(int first_line = 0, int first_char = 0)
42         : lines_(first_line), chars_(first_char)
43         { }
44     int lines() const { return lines_; }
45     int characters() const { return chars_; }
46     std::streamsize optimal_buffer_size() const { return 0; }
47
48     template<typename Source>
49     std::streamsize read(Source& src, char_type* s, std::streamsize n)
50     {
51         std::streamsize result = iostreams::read(src, s, n);
52         if (result == -1)
53             return -1;
54         lines_ += std::count(s, s + result, char_traits<Ch>::newline());
55         chars_ += result;
56         return result;
57     }
58
59     template<typename Sink>
60     std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
61     {
62         std::streamsize result = iostreams::write(snk, s, n);
63         lines_ += std::count(s, s + result, char_traits<Ch>::newline());
64         chars_ += result;
65         return result;
66     }
67 private:
68     int lines_;
69     int chars_;
70 };
71 BOOST_IOSTREAMS_PIPABLE(basic_counter, 1)
72
73
74 typedef basic_counter<char>     counter;
75 typedef basic_counter<wchar_t>  wcounter;
76
77 } } // End namespaces iostreams, boost.
78
79 #include <boost/iostreams/detail/config/enable_warnings.hpp>
80
81 #endif // #ifndef BOOST_IOSTREAMS_COUNTER_HPP_INCLUDED