4
// $Id: //poco/1.2/Foundation/include/Poco/CountingStream.h#1 $
8
// Module: CountingStream
10
// Definition of the CountingStreamBuf, CountingInputStream and CountingOutputStream classes.
12
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
15
// Permission is hereby granted, free of charge, to any person or organization
16
// obtaining a copy of the software and accompanying documentation covered by
17
// this license (the "Software") to use, reproduce, display, distribute,
18
// execute, and transmit the Software, and to prepare derivative works of the
19
// Software, and to permit third-parties to whom the Software is furnished to
20
// do so, all subject to the following:
22
// The copyright notices in the Software and this entire statement, including
23
// the above license grant, this restriction and the following disclaimer,
24
// must be included in all copies of the Software, in whole or in part, and
25
// all derivative works of the Software, unless such copies or derivative
26
// works are solely in the form of machine-executable object code generated by
27
// a source language processor.
29
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35
// DEALINGS IN THE SOFTWARE.
39
#ifndef Foundation_CountingStream_INCLUDED
40
#define Foundation_CountingStream_INCLUDED
43
#include "Poco/Foundation.h"
44
#include "Poco/UnbufferedStreamBuf.h"
52
class Foundation_API CountingStreamBuf: public UnbufferedStreamBuf
53
/// This stream buffer counts all characters and lines
58
/// Creates an unconnected CountingStreamBuf.
60
CountingStreamBuf(std::istream& istr);
61
/// Creates the CountingStreamBuf and connects it
62
/// to the given input stream.
64
CountingStreamBuf(std::ostream& ostr);
65
/// Creates the CountingStreamBuf and connects it
66
/// to the given output stream.
69
/// Destroys the CountingStream.
72
/// Returns the total number of characters.
75
/// Returns the total number of lines.
78
/// Returns the number of characters on the current line.
81
/// Resets all counters.
83
void setCurrentLineNumber(int line);
84
/// Sets the current line number.
86
/// This is mainly useful when parsing C/C++
87
/// preprocessed source code containing #line directives.
89
int getCurrentLineNumber() const;
90
/// Returns the current line number (same as lines()).
94
int writeToDevice(char c);
105
class Foundation_API CountingIOS: public virtual std::ios
106
/// The base class for CountingInputStream and CountingOutputStream.
108
/// This class is needed to ensure the correct initialization
109
/// order of the stream buffer and base classes.
113
/// Creates the basic stream and leaves it unconnected.
115
CountingIOS(std::istream& istr);
116
/// Creates the basic stream and connects it
117
/// to the given input stream.
119
CountingIOS(std::ostream& ostr);
120
/// Creates the basic stream and connects it
121
/// to the given output stream.
124
/// Destroys the stream.
127
/// Returns the total number of characters.
130
/// Returns the total number of lines.
133
/// Returns the number of characters on the current line.
136
/// Resets all counters.
138
void setCurrentLineNumber(int line);
139
/// Sets the current line number.
141
/// This is mainly useful when parsing C/C++
142
/// preprocessed source code containing #line directives.
144
int getCurrentLineNumber() const;
145
/// Returns the current line number (same as lines()).
147
CountingStreamBuf* rdbuf();
148
/// Returns a pointer to the underlying streambuf.
151
CountingStreamBuf _buf;
155
class Foundation_API CountingInputStream: public CountingIOS, public std::istream
156
/// This stream counts all characters and lines
157
/// going through it. This is useful for lexers and parsers
158
/// that need to determine the current position in the stream.
161
CountingInputStream(std::istream& istr);
162
/// Creates the CountingInputStream and connects it
163
/// to the given input stream.
165
~CountingInputStream();
166
/// Destroys the stream.
170
class Foundation_API CountingOutputStream: public CountingIOS, public std::ostream
171
/// This stream counts all characters and lines
172
/// going through it.
175
CountingOutputStream();
176
/// Creates an unconnected CountingOutputStream.
178
CountingOutputStream(std::ostream& ostr);
179
/// Creates the CountingOutputStream and connects it
180
/// to the given input stream.
182
~CountingOutputStream();
183
/// Destroys the CountingOutputStream.
190
inline int CountingStreamBuf::chars() const
196
inline int CountingStreamBuf::lines() const
202
inline int CountingStreamBuf::pos() const
208
inline int CountingStreamBuf::getCurrentLineNumber() const
214
inline int CountingIOS::chars() const
220
inline int CountingIOS::lines() const
226
inline int CountingIOS::pos() const
232
inline int CountingIOS::getCurrentLineNumber() const
234
return _buf.getCurrentLineNumber();
241
#endif // Foundation_CountingStream_INCLUDED