File indexing completed on 2025-01-30 09:44:30
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_IOSTREAMS_COUNTER_HPP_INCLUDED
0009 #define BOOST_IOSTREAMS_COUNTER_HPP_INCLUDED
0010
0011 #if defined(_MSC_VER)
0012 # pragma once
0013 #endif
0014
0015 #include <algorithm> // count.
0016 #include <boost/iostreams/categories.hpp>
0017 #include <boost/iostreams/char_traits.hpp>
0018 #include <boost/iostreams/operations.hpp>
0019 #include <boost/iostreams/pipeline.hpp>
0020
0021
0022 #include <boost/iostreams/detail/config/disable_warnings.hpp> // VC7.1 C4244.
0023
0024 namespace boost { namespace iostreams {
0025
0026
0027
0028
0029
0030
0031
0032 template<typename Ch>
0033 class basic_counter {
0034 public:
0035 typedef Ch char_type;
0036 struct category
0037 : dual_use,
0038 filter_tag,
0039 multichar_tag,
0040 optimally_buffered_tag
0041 { };
0042 explicit basic_counter(int first_line = 0, int first_char = 0)
0043 : lines_(first_line), chars_(first_char)
0044 { }
0045 int lines() const { return lines_; }
0046 int characters() const { return chars_; }
0047 std::streamsize optimal_buffer_size() const { return 0; }
0048
0049 template<typename Source>
0050 std::streamsize read(Source& src, char_type* s, std::streamsize n)
0051 {
0052 std::streamsize result = iostreams::read(src, s, n);
0053 if (result == -1)
0054 return -1;
0055 lines_ += std::count(s, s + result, char_traits<Ch>::newline());
0056 chars_ += result;
0057 return result;
0058 }
0059
0060 template<typename Sink>
0061 std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
0062 {
0063 std::streamsize result = iostreams::write(snk, s, n);
0064 lines_ += std::count(s, s + result, char_traits<Ch>::newline());
0065 chars_ += result;
0066 return result;
0067 }
0068 private:
0069 int lines_;
0070 int chars_;
0071 };
0072 BOOST_IOSTREAMS_PIPABLE(basic_counter, 1)
0073
0074
0075 typedef basic_counter<char> counter;
0076 typedef basic_counter<wchar_t> wcounter;
0077
0078 } }
0079
0080 #include <boost/iostreams/detail/config/enable_warnings.hpp>
0081
0082 #endif