1 | // |
---|
2 | // CountingStream.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/CountingStream.h#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Streams |
---|
8 | // Module: CountingStream |
---|
9 | // |
---|
10 | // Definition of the CountingStreamBuf, CountingInputStream and CountingOutputStream classes. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
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: |
---|
21 | // |
---|
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. |
---|
28 | // |
---|
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. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_CountingStream_INCLUDED |
---|
40 | #define Foundation_CountingStream_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/UnbufferedStreamBuf.h" |
---|
45 | #include <istream> |
---|
46 | #include <ostream> |
---|
47 | |
---|
48 | |
---|
49 | namespace Poco { |
---|
50 | |
---|
51 | |
---|
52 | class Foundation_API CountingStreamBuf: public UnbufferedStreamBuf |
---|
53 | /// This stream buffer counts all characters and lines |
---|
54 | /// going through it. |
---|
55 | { |
---|
56 | public: |
---|
57 | CountingStreamBuf(); |
---|
58 | /// Creates an unconnected CountingStreamBuf. |
---|
59 | |
---|
60 | CountingStreamBuf(std::istream& istr); |
---|
61 | /// Creates the CountingStreamBuf and connects it |
---|
62 | /// to the given input stream. |
---|
63 | |
---|
64 | CountingStreamBuf(std::ostream& ostr); |
---|
65 | /// Creates the CountingStreamBuf and connects it |
---|
66 | /// to the given output stream. |
---|
67 | |
---|
68 | ~CountingStreamBuf(); |
---|
69 | /// Destroys the CountingStream. |
---|
70 | |
---|
71 | int chars() const; |
---|
72 | /// Returns the total number of characters. |
---|
73 | |
---|
74 | int lines() const; |
---|
75 | /// Returns the total number of lines. |
---|
76 | |
---|
77 | int pos() const; |
---|
78 | /// Returns the number of characters on the current line. |
---|
79 | |
---|
80 | void reset(); |
---|
81 | /// Resets all counters. |
---|
82 | |
---|
83 | void setCurrentLineNumber(int line); |
---|
84 | /// Sets the current line number. |
---|
85 | /// |
---|
86 | /// This is mainly useful when parsing C/C++ |
---|
87 | /// preprocessed source code containing #line directives. |
---|
88 | |
---|
89 | int getCurrentLineNumber() const; |
---|
90 | /// Returns the current line number (same as lines()). |
---|
91 | |
---|
92 | protected: |
---|
93 | int readFromDevice(); |
---|
94 | int writeToDevice(char c); |
---|
95 | |
---|
96 | private: |
---|
97 | std::istream* _pIstr; |
---|
98 | std::ostream* _pOstr; |
---|
99 | int _chars; |
---|
100 | int _lines; |
---|
101 | int _pos; |
---|
102 | }; |
---|
103 | |
---|
104 | |
---|
105 | class Foundation_API CountingIOS: public virtual std::ios |
---|
106 | /// The base class for CountingInputStream and CountingOutputStream. |
---|
107 | /// |
---|
108 | /// This class is needed to ensure the correct initialization |
---|
109 | /// order of the stream buffer and base classes. |
---|
110 | { |
---|
111 | public: |
---|
112 | CountingIOS(); |
---|
113 | /// Creates the basic stream and leaves it unconnected. |
---|
114 | |
---|
115 | CountingIOS(std::istream& istr); |
---|
116 | /// Creates the basic stream and connects it |
---|
117 | /// to the given input stream. |
---|
118 | |
---|
119 | CountingIOS(std::ostream& ostr); |
---|
120 | /// Creates the basic stream and connects it |
---|
121 | /// to the given output stream. |
---|
122 | |
---|
123 | ~CountingIOS(); |
---|
124 | /// Destroys the stream. |
---|
125 | |
---|
126 | int chars() const; |
---|
127 | /// Returns the total number of characters. |
---|
128 | |
---|
129 | int lines() const; |
---|
130 | /// Returns the total number of lines. |
---|
131 | |
---|
132 | int pos() const; |
---|
133 | /// Returns the number of characters on the current line. |
---|
134 | |
---|
135 | void reset(); |
---|
136 | /// Resets all counters. |
---|
137 | |
---|
138 | void setCurrentLineNumber(int line); |
---|
139 | /// Sets the current line number. |
---|
140 | /// |
---|
141 | /// This is mainly useful when parsing C/C++ |
---|
142 | /// preprocessed source code containing #line directives. |
---|
143 | |
---|
144 | int getCurrentLineNumber() const; |
---|
145 | /// Returns the current line number (same as lines()). |
---|
146 | |
---|
147 | CountingStreamBuf* rdbuf(); |
---|
148 | /// Returns a pointer to the underlying streambuf. |
---|
149 | |
---|
150 | protected: |
---|
151 | CountingStreamBuf _buf; |
---|
152 | }; |
---|
153 | |
---|
154 | |
---|
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. |
---|
159 | { |
---|
160 | public: |
---|
161 | CountingInputStream(std::istream& istr); |
---|
162 | /// Creates the CountingInputStream and connects it |
---|
163 | /// to the given input stream. |
---|
164 | |
---|
165 | ~CountingInputStream(); |
---|
166 | /// Destroys the stream. |
---|
167 | }; |
---|
168 | |
---|
169 | |
---|
170 | class Foundation_API CountingOutputStream: public CountingIOS, public std::ostream |
---|
171 | /// This stream counts all characters and lines |
---|
172 | /// going through it. |
---|
173 | { |
---|
174 | public: |
---|
175 | CountingOutputStream(); |
---|
176 | /// Creates an unconnected CountingOutputStream. |
---|
177 | |
---|
178 | CountingOutputStream(std::ostream& ostr); |
---|
179 | /// Creates the CountingOutputStream and connects it |
---|
180 | /// to the given input stream. |
---|
181 | |
---|
182 | ~CountingOutputStream(); |
---|
183 | /// Destroys the CountingOutputStream. |
---|
184 | }; |
---|
185 | |
---|
186 | |
---|
187 | // |
---|
188 | // inlines |
---|
189 | // |
---|
190 | inline int CountingStreamBuf::chars() const |
---|
191 | { |
---|
192 | return _chars; |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | inline int CountingStreamBuf::lines() const |
---|
197 | { |
---|
198 | return _lines; |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | inline int CountingStreamBuf::pos() const |
---|
203 | { |
---|
204 | return _pos; |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | inline int CountingStreamBuf::getCurrentLineNumber() const |
---|
209 | { |
---|
210 | return _lines; |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | inline int CountingIOS::chars() const |
---|
215 | { |
---|
216 | return _buf.chars(); |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | inline int CountingIOS::lines() const |
---|
221 | { |
---|
222 | return _buf.lines(); |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | inline int CountingIOS::pos() const |
---|
227 | { |
---|
228 | return _buf.pos(); |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | inline int CountingIOS::getCurrentLineNumber() const |
---|
233 | { |
---|
234 | return _buf.getCurrentLineNumber(); |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | } // namespace Poco |
---|
239 | |
---|
240 | |
---|
241 | #endif // Foundation_CountingStream_INCLUDED |
---|