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
18namespace Poco {
19
20
21CountingStreamBuf::CountingStreamBuf():
22 _pIstr(0),
23 _pOstr(0),
24 _chars(0),
25 _lines(0),
26 _pos(0)
27{
28}
29
30
31CountingStreamBuf::CountingStreamBuf(std::istream& istr):
32 _pIstr(&istr),
33 _pOstr(0),
34 _chars(0),
35 _lines(0),
36 _pos(0)
37{
38}
39
40
41CountingStreamBuf::CountingStreamBuf(std::ostream& ostr):
42 _pIstr(0),
43 _pOstr(&ostr),
44 _chars(0),
45 _lines(0),
46 _pos(0)
47{
48}
49
50
51CountingStreamBuf::~CountingStreamBuf()
52{
53}
54
55
56int 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
73int 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
83void CountingStreamBuf::reset()
84{
85 _chars = 0;
86 _lines = 0;
87 _pos = 0;
88}
89
90
91void CountingStreamBuf::setCurrentLineNumber(int line)
92{
93 _lines = line;
94}
95
96
97void CountingStreamBuf::addChars(int charsToAdd)
98{
99 _chars += charsToAdd;
100}
101
102
103void CountingStreamBuf::addLines(int linesToAdd)
104{
105 _lines += linesToAdd;
106}
107
108
109void CountingStreamBuf::addPos(int posToAdd)
110{
111 _pos += posToAdd;
112}
113
114
115CountingIOS::CountingIOS()
116{
117 poco_ios_init(&_buf);
118}
119
120
121CountingIOS::CountingIOS(std::istream& istr): _buf(istr)
122{
123 poco_ios_init(&_buf);
124}
125
126
127CountingIOS::CountingIOS(std::ostream& ostr): _buf(ostr)
128{
129 poco_ios_init(&_buf);
130}
131
132
133CountingIOS::~CountingIOS()
134{
135}
136
137
138void CountingIOS::reset()
139{
140 _buf.reset();
141}
142
143
144void CountingIOS::setCurrentLineNumber(int line)
145{
146 _buf.setCurrentLineNumber(line);
147}
148
149
150void CountingIOS::addChars(int charsToAdd)
151{
152 _buf.addChars(charsToAdd);
153}
154
155
156void CountingIOS::addLines(int linesToAdd)
157{
158 _buf.addLines(linesToAdd);
159}
160
161
162void CountingIOS::addPos(int posToAdd)
163{
164 _buf.addPos(posToAdd);
165}
166
167
168CountingStreamBuf* CountingIOS::rdbuf()
169{
170 return &_buf;
171}
172
173
174CountingInputStream::CountingInputStream(std::istream& istr): CountingIOS(istr), std::istream(&_buf)
175{
176}
177
178
179CountingInputStream::~CountingInputStream()
180{
181}
182
183
184CountingOutputStream::CountingOutputStream(): std::ostream(&_buf)
185{
186}
187
188
189CountingOutputStream::CountingOutputStream(std::ostream& ostr): CountingIOS(ostr), std::ostream(&_buf)
190{
191}
192
193
194CountingOutputStream::~CountingOutputStream()
195{
196}
197
198
199} // namespace Poco
200