summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvisvgm/dvisvgm-0.8.7/src/StreamCounter.h
blob: 00df0703b8a9f45219b93f1de329d4e00856a951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*************************************************************************
** StreamCounter.h                                                      **
**                                                                      **
** This file is part of dvisvgm -- the DVI to SVG converter             **
** Copyright (C) 2005-2009 Martin Gieseking <martin.gieseking@uos.de>   **
**                                                                      **
** This program is free software; you can redistribute it and/or        **
** modify it under the terms of the GNU General Public License as       **
** published by the Free Software Foundation; either version 3 of       **
** the License, or (at your option) any later version.                  **
**                                                                      **
** This program is distributed in the hope that it will be useful, but  **
** WITHOUT ANY WARRANTY; without even the implied warranty of           **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         **
** GNU General Public License for more details.                         **
**                                                                      **
** You should have received a copy of the GNU General Public License    **
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/

#ifndef STREAMCOUNTER_H
#define STREAMCOUNTER_H

#include <ostream>
#include "types.h"


/** This class template is used to count the number of characters
 *  written to a stream buffer. It simply counts the characters written
 *  and forwards them to the target buffer. */
template <typename C, typename T=std::char_traits<C> >
class CountingStreamBuf : public std::basic_streambuf<C,T>
{
   public:
      explicit CountingStreamBuf (std::basic_streambuf<C,T> *b)
			: targetBuf(b), counter(0) {}

		UInt64 count () const {return counter;}
		void reset ()         {counter = 0;}

	protected:
		CountingStreamBuf (const CountingStreamBuf &csb);
		CountingStreamBuf& operator = (const CountingStreamBuf &csb);

		typename T::int_type overflow (typename T::int_type c=T::eof()) {
			typename T::int_type n = targetBuf->sputc(c);
			if (!T::eq_int_type(c, T::eof()) && !T::eq_int_type(n, T::eof()))
				counter++;
			return n;
		}

		std::streamsize xsputn (const C *buf, std::streamsize bufsize) {
			std::streamsize n = targetBuf->sputn(buf, bufsize);
         counter += n;
         return n;
		}

   private:
		std::basic_streambuf<C,T> *targetBuf;
		UInt64 counter;
};


/** This class template assigns a counting buffer to a stream. It makes
 *  the use of CountingStreamBuf more comfortable. */
template <typename C, typename T=std::char_traits<C> >
class StreamCounter
{
	public:
		explicit StreamCounter (std::basic_ios<C,T> &stream)
			: targetStream(stream),
		     targetBuf(stream.rdbuf()),
			  countingBuf(new CountingStreamBuf<C,T>(stream.rdbuf()))
		{
			stream.rdbuf(countingBuf);
			valid = true;
		}

		~StreamCounter () {
			if (valid)
				targetStream.rdbuf(targetBuf);
			delete countingBuf;
		}

		UInt64 count () const {return countingBuf->count();}
		void reset ()         {countingBuf->reset();}
		void invalidate ()    {valid = false;}

	protected:
		StreamCounter (const StreamCounter &csf);
		StreamCounter operator = (const StreamCounter &csf);

	private:
		bool                      valid; ///< true if target stream is valid
		std::basic_ios<C,T>       &targetStream;
		std::basic_streambuf<C,T> *targetBuf;
		CountingStreamBuf<C,T>    *countingBuf;
};

#endif