• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
kbufferedio.cpp
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2001 Thiago Macieira <thiago.macieira@kdemail.net>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22
23#include <string.h>
24
25#include <tqptrlist.h>
26#include <tqcstring.h>
27#include "kbufferedio.h"
28
88// constructor
89TDEBufferedIO::TDEBufferedIO() :
90 inBufIndex(0), outBufIndex(0)
91{
92 inBuf.setAutoDelete(true);
93 outBuf.setAutoDelete(true);
94}
95
96// destructor
97TDEBufferedIO::~TDEBufferedIO()
98{
99}
100
101// sets the buffer sizes
102// this implementation doesn't support setting the buffer sizes
103// if any parameter is different than -1 or -2, fail
104bool TDEBufferedIO::setBufferSize(int rsize, int wsize /* = -2 */)
105{
106 if (wsize != -2 && wsize != -1)
107 return false;
108 if (rsize != -2 && rsize != -1)
109 return false;
110
111 return true;
112}
113
114int TDEBufferedIO::bytesAvailable() const
115{
116 return readBufferSize();
117}
118
119int TDEBufferedIO::bytesToWrite() const
120{
121 return writeBufferSize();
122}
123
124// This function will scan the read buffer for a '\n'
125bool TDEBufferedIO::canReadLine() const
126{
127 if (bytesAvailable() == 0)
128 return false; // no new line in here
129
130 TQByteArray* buf;
131
132 // scan each TQByteArray for the occurrence of '\n'
133 TQPtrList<TQByteArray> &buflist = ((TDEBufferedIO*)this)->inBuf;
134 buf = buflist.first();
135 char *p = buf->data() + inBufIndex;
136 int n = buf->size() - inBufIndex;
137 while (buf != NULL)
138 {
139 while (n--)
140 if (*p++ == '\n')
141 return true;
142 buf = buflist.next();
143 if (buf != NULL)
144 {
145 p = buf->data();
146 n = buf->size();
147 }
148 }
149
150 return false; // no new line found
151}
152
153// unreads the current data
154// that is, writes into the read buffer, at the beginning
155int TDEBufferedIO::unreadBlock(const char *data, uint len)
156{
157 return feedReadBuffer(len, data, true);
158}
159
160//
161// protected member functions
162//
163
164unsigned TDEBufferedIO::consumeReadBuffer(unsigned nbytes, char *destbuffer, bool discard)
165{
166 {
167 unsigned u = readBufferSize();
168 if (nbytes > u)
169 nbytes = u; // we can't consume more than there is
170 }
171
172 TQByteArray *buf;
173 unsigned copied = 0;
174 unsigned index = inBufIndex;
175
176 buf = inBuf.first();
177 while (nbytes && buf)
178 {
179 // should we copy it all?
180 unsigned to_copy = buf->size() - index;
181 if (to_copy > nbytes)
182 to_copy = nbytes;
183
184 if (destbuffer)
185 memcpy(destbuffer + copied, buf->data() + index, to_copy);
186 nbytes -= to_copy;
187 copied += to_copy;
188
189 if (buf->size() - index > to_copy)
190 {
191 index += to_copy;
192 break; // we aren't copying everything, that means that's
193 // all the user wants
194 }
195 else
196 {
197 index = 0;
198 if (discard)
199 {
200 inBuf.remove();
201 buf = inBuf.first();
202 }
203 else
204 buf = inBuf.next();
205 }
206 }
207
208 if (discard)
209 inBufIndex = index;
210
211 return copied;
212}
213
214void TDEBufferedIO::consumeWriteBuffer(unsigned nbytes)
215{
216 TQByteArray *buf = outBuf.first();
217 if (buf == NULL)
218 return; // nothing to consume
219
220 if (nbytes < buf->size() - outBufIndex)
221 // we want to consume less than there is in the first buffer
222 outBufIndex += nbytes;
223 else
224 {
225 nbytes -= buf->size() - outBufIndex;
226 outBufIndex = 0;
227 outBuf.remove();
228
229 while ((buf = outBuf.current()) != NULL)
230 if (buf->size() <= nbytes)
231 {
232 nbytes -= buf->size();
233 outBuf.remove();
234 }
235 else
236 {
237 outBufIndex = nbytes;
238 break;
239 }
240 }
241}
242
243unsigned TDEBufferedIO::feedReadBuffer(unsigned nbytes, const char *buffer, bool atBeginning)
244{
245 if (nbytes == 0)
246 return 0;
247
248 TQByteArray *a = new TQByteArray(nbytes);
249 a->duplicate(buffer, nbytes);
250
251 if (atBeginning)
252 inBuf.prepend(a);
253 else
254 inBuf.append(a);
255
256 return nbytes;
257}
258
259unsigned TDEBufferedIO::feedWriteBuffer(unsigned nbytes, const char *buffer)
260{
261 if (nbytes == 0)
262 return 0;
263
264 TQByteArray *a = new TQByteArray(nbytes);
265 a->duplicate(buffer, nbytes);
266 outBuf.append(a);
267 return nbytes;
268}
269
270unsigned TDEBufferedIO::readBufferSize() const
271{
272 unsigned count = 0;
273 TQByteArray *buf = ((TDEBufferedIO*)this)->inBuf.first();
274 while (buf != NULL)
275 {
276 count += buf->size();
277 buf = ((TDEBufferedIO*)this)->inBuf.next();
278 }
279
280 return count - inBufIndex;
281}
282
283unsigned TDEBufferedIO::writeBufferSize() const
284{
285 unsigned count = 0;
286 TQByteArray *buf = ((TDEBufferedIO*)this)->outBuf.first();
287 while (buf != NULL)
288 {
289 count += buf->size();
290 buf = (const_cast<TDEBufferedIO*>(this))->outBuf.next();
291 }
292
293 return count - outBufIndex;
294}
295
296void TDEBufferedIO::virtual_hook( int id, void* data )
297{ KAsyncIO::virtual_hook( id, data ); }
298
299#include "kbufferedio.moc"
TDEBufferedIO
This abstract class implements basic functionality for buffered input/output.
Definition: kbufferedio.h:57
TDEBufferedIO::bytesToWrite
virtual int bytesToWrite() const
Returns the number of bytes yet to write, still in the write buffer.
Definition: kbufferedio.cpp:119
TDEBufferedIO::feedWriteBuffer
virtual unsigned feedWriteBuffer(unsigned nbytes, const char *buffer)
Feeds data into the output buffer.
Definition: kbufferedio.cpp:259
TDEBufferedIO::canReadLine
virtual bool canReadLine() const
Checks whether there is enough data in the buffer to read a line.
Definition: kbufferedio.cpp:125
TDEBufferedIO::~TDEBufferedIO
virtual ~TDEBufferedIO()
Destroys this class.
Definition: kbufferedio.cpp:97
TDEBufferedIO::consumeReadBuffer
virtual unsigned consumeReadBuffer(unsigned nbytes, char *destbuffer, bool discard=true)
Consumes data from the input buffer.
Definition: kbufferedio.cpp:164
TDEBufferedIO::TDEBufferedIO
TDEBufferedIO()
Definition: kbufferedio.cpp:89
TDEBufferedIO::unreadBlock
virtual int unreadBlock(const char *data, uint len)
Unreads some data.
Definition: kbufferedio.cpp:155
TDEBufferedIO::readBufferSize
virtual unsigned readBufferSize() const
Returns the number of bytes in the read buffer.
Definition: kbufferedio.cpp:270
TDEBufferedIO::setBufferSize
virtual bool setBufferSize(int rsize, int wsize=-2)
Sets the internal buffer size to value.
Definition: kbufferedio.cpp:104
TDEBufferedIO::feedReadBuffer
virtual unsigned feedReadBuffer(unsigned nbytes, const char *buffer, bool atBeginning=false)
Feeds data into the input buffer.
Definition: kbufferedio.cpp:243
TDEBufferedIO::inBuf
TQPtrList< TQByteArray > inBuf
For an explanation on how this buffer work, please refer to the comments at the top of kbufferedio....
Definition: kbufferedio.h:213
TDEBufferedIO::outBufIndex
unsigned outBufIndex
Offset into first output buffer.
Definition: kbufferedio.h:222
TDEBufferedIO::outBuf
TQPtrList< TQByteArray > outBuf
For an explanation on how this buffer work, please refer to the comments at the top of kbufferedio....
Definition: kbufferedio.h:219
TDEBufferedIO::bytesAvailable
virtual int bytesAvailable() const
Returns the number of bytes available for reading in the read buffer.
Definition: kbufferedio.cpp:114
TDEBufferedIO::inBufIndex
unsigned inBufIndex
Offset into first input buffer.
Definition: kbufferedio.h:221
TDEBufferedIO::consumeWriteBuffer
virtual void consumeWriteBuffer(unsigned nbytes)
Consumes data from the output buffer.
Definition: kbufferedio.cpp:214
TDEBufferedIO::writeBufferSize
virtual unsigned writeBufferSize() const
Returns the number of bytes in the write buffer.
Definition: kbufferedio.cpp:283

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.