1 | // |
2 | // CountingStream.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Streams |
6 | // Module: CountingStream |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/CountingStream.h" |
16 | |
17 | |
18 | namespace Poco { |
19 | |
20 | |
21 | CountingStreamBuf::CountingStreamBuf(): |
22 | _pIstr(0), |
23 | _pOstr(0), |
24 | _chars(0), |
25 | _lines(0), |
26 | _pos(0) |
27 | { |
28 | } |
29 | |
30 | |
31 | CountingStreamBuf::CountingStreamBuf(std::istream& istr): |
32 | _pIstr(&istr), |
33 | _pOstr(0), |
34 | _chars(0), |
35 | _lines(0), |
36 | _pos(0) |
37 | { |
38 | } |
39 | |
40 | |
41 | CountingStreamBuf::CountingStreamBuf(std::ostream& ostr): |
42 | _pIstr(0), |
43 | _pOstr(&ostr), |
44 | _chars(0), |
45 | _lines(0), |
46 | _pos(0) |
47 | { |
48 | } |
49 | |
50 | |
51 | CountingStreamBuf::~CountingStreamBuf() |
52 | { |
53 | } |
54 | |
55 | |
56 | int CountingStreamBuf::readFromDevice() |
57 | { |
58 | if (_pIstr) |
59 | { |
60 | int c = _pIstr->get(); |
61 | if (c != -1) |
62 | { |
63 | ++_chars; |
64 | if (_pos++ == 0) ++_lines; |
65 | if (c == '\n') _pos = 0; |
66 | } |
67 | return c; |
68 | } |
69 | return -1; |
70 | } |
71 | |
72 | |
73 | int CountingStreamBuf::writeToDevice(char c) |
74 | { |
75 | ++_chars; |
76 | if (_pos++ == 0) ++_lines; |
77 | if (c == '\n') _pos = 0; |
78 | if (_pOstr) _pOstr->put(c); |
79 | return charToInt(c); |
80 | } |
81 | |
82 | |
83 | void CountingStreamBuf::reset() |
84 | { |
85 | _chars = 0; |
86 | _lines = 0; |
87 | _pos = 0; |
88 | } |
89 | |
90 | |
91 | void CountingStreamBuf::setCurrentLineNumber(int line) |
92 | { |
93 | _lines = line; |
94 | } |
95 | |
96 | |
97 | void CountingStreamBuf::addChars(std::streamsize charsToAdd) |
98 | { |
99 | _chars += charsToAdd; |
100 | } |
101 | |
102 | |
103 | void CountingStreamBuf::addLines(std::streamsize linesToAdd) |
104 | { |
105 | _lines += linesToAdd; |
106 | } |
107 | |
108 | |
109 | void CountingStreamBuf::addPos(std::streamsize posToAdd) |
110 | { |
111 | _pos += posToAdd; |
112 | } |
113 | |
114 | |
115 | CountingIOS::CountingIOS() |
116 | { |
117 | poco_ios_init(&_buf); |
118 | } |
119 | |
120 | |
121 | CountingIOS::CountingIOS(std::istream& istr): _buf(istr) |
122 | { |
123 | poco_ios_init(&_buf); |
124 | } |
125 | |
126 | |
127 | CountingIOS::CountingIOS(std::ostream& ostr): _buf(ostr) |
128 | { |
129 | poco_ios_init(&_buf); |
130 | } |
131 | |
132 | |
133 | CountingIOS::~CountingIOS() |
134 | { |
135 | } |
136 | |
137 | |
138 | void CountingIOS::reset() |
139 | { |
140 | _buf.reset(); |
141 | } |
142 | |
143 | |
144 | void CountingIOS::setCurrentLineNumber(std::streamsize line) |
145 | { |
146 | _buf.setCurrentLineNumber(line); |
147 | } |
148 | |
149 | |
150 | void CountingIOS::addChars(std::streamsize charsToAdd) |
151 | { |
152 | _buf.addChars(charsToAdd); |
153 | } |
154 | |
155 | |
156 | void CountingIOS::addLines(std::streamsize linesToAdd) |
157 | { |
158 | _buf.addLines(linesToAdd); |
159 | } |
160 | |
161 | |
162 | void CountingIOS::addPos(std::streamsize posToAdd) |
163 | { |
164 | _buf.addPos(posToAdd); |
165 | } |
166 | |
167 | |
168 | CountingStreamBuf* CountingIOS::rdbuf() |
169 | { |
170 | return &_buf; |
171 | } |
172 | |
173 | |
174 | CountingInputStream::CountingInputStream(std::istream& istr): CountingIOS(istr), std::istream(&_buf) |
175 | { |
176 | } |
177 | |
178 | |
179 | CountingInputStream::~CountingInputStream() |
180 | { |
181 | } |
182 | |
183 | |
184 | CountingOutputStream::CountingOutputStream(): std::ostream(&_buf) |
185 | { |
186 | } |
187 | |
188 | |
189 | CountingOutputStream::CountingOutputStream(std::ostream& ostr): CountingIOS(ostr), std::ostream(&_buf) |
190 | { |
191 | } |
192 | |
193 | |
194 | CountingOutputStream::~CountingOutputStream() |
195 | { |
196 | } |
197 | |
198 | |
199 | } // namespace Poco |
200 | |