• 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
89 TDEBufferedIO::TDEBufferedIO() :
90  inBufIndex(0), outBufIndex(0)
91 {
92  inBuf.setAutoDelete(true);
93  outBuf.setAutoDelete(true);
94 }
95 
96 // destructor
97 TDEBufferedIO::~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
104 bool 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 
114 #ifdef USE_QT3
115 int TDEBufferedIO::bytesAvailable() const
116 #endif // USE_QT3
117 #ifdef USE_QT4
118 qint64 TDEBufferedIO::bytesAvailable() const
119 #endif // USE_QT4
120 {
121  return readBufferSize();
122 }
123 
124 #ifdef USE_QT3
125 int TDEBufferedIO::bytesToWrite() const
126 #endif // USE_QT3
127 #ifdef USE_QT4
128 qint64 TDEBufferedIO::bytesToWrite() const
129 #endif // USE_QT4
130 {
131  return writeBufferSize();
132 }
133 
134 // This function will scan the read buffer for a '\n'
135 bool TDEBufferedIO::canReadLine() const
136 {
137  if (bytesAvailable() == 0)
138  return false; // no new line in here
139 
140  TQByteArray* buf;
141 
142  // scan each TQByteArray for the occurrence of '\n'
143  TQPtrList<TQByteArray> &buflist = ((TDEBufferedIO*)this)->inBuf;
144  buf = buflist.first();
145  char *p = buf->data() + inBufIndex;
146  int n = buf->size() - inBufIndex;
147  while (buf != NULL)
148  {
149  while (n--)
150  if (*p++ == '\n')
151  return true;
152  buf = buflist.next();
153  if (buf != NULL)
154  {
155  p = buf->data();
156  n = buf->size();
157  }
158  }
159 
160  return false; // no new line found
161 }
162 
163 // unreads the current data
164 // that is, writes into the read buffer, at the beginning
165 int TDEBufferedIO::unreadBlock(const char *data, uint len)
166 {
167  return feedReadBuffer(len, data, true);
168 }
169 
170 //
171 // protected member functions
172 //
173 
174 unsigned TDEBufferedIO::consumeReadBuffer(unsigned nbytes, char *destbuffer, bool discard)
175 {
176  {
177  unsigned u = readBufferSize();
178  if (nbytes > u)
179  nbytes = u; // we can't consume more than there is
180  }
181 
182  TQByteArray *buf;
183  unsigned copied = 0;
184  unsigned index = inBufIndex;
185 
186  buf = inBuf.first();
187  while (nbytes && buf)
188  {
189  // should we copy it all?
190  unsigned to_copy = buf->size() - index;
191  if (to_copy > nbytes)
192  to_copy = nbytes;
193 
194  if (destbuffer)
195  memcpy(destbuffer + copied, buf->data() + index, to_copy);
196  nbytes -= to_copy;
197  copied += to_copy;
198 
199  if (buf->size() - index > to_copy)
200  {
201  index += to_copy;
202  break; // we aren't copying everything, that means that's
203  // all the user wants
204  }
205  else
206  {
207  index = 0;
208  if (discard)
209  {
210  inBuf.remove();
211  buf = inBuf.first();
212  }
213  else
214  buf = inBuf.next();
215  }
216  }
217 
218  if (discard)
219  inBufIndex = index;
220 
221  return copied;
222 }
223 
224 void TDEBufferedIO::consumeWriteBuffer(unsigned nbytes)
225 {
226  TQByteArray *buf = outBuf.first();
227  if (buf == NULL)
228  return; // nothing to consume
229 
230  if (nbytes < buf->size() - outBufIndex)
231  // we want to consume less than there is in the first buffer
232  outBufIndex += nbytes;
233  else
234  {
235  nbytes -= buf->size() - outBufIndex;
236  outBufIndex = 0;
237  outBuf.remove();
238 
239  while ((buf = outBuf.current()) != NULL)
240  if (buf->size() <= nbytes)
241  {
242  nbytes -= buf->size();
243  outBuf.remove();
244  }
245  else
246  {
247  outBufIndex = nbytes;
248  break;
249  }
250  }
251 }
252 
253 unsigned TDEBufferedIO::feedReadBuffer(unsigned nbytes, const char *buffer, bool atBeginning)
254 {
255  if (nbytes == 0)
256  return 0;
257 
258  TQByteArray *a = new TQByteArray(nbytes);
259  a->duplicate(buffer, nbytes);
260 
261  if (atBeginning)
262  inBuf.prepend(a);
263  else
264  inBuf.append(a);
265 
266  return nbytes;
267 }
268 
269 unsigned TDEBufferedIO::feedWriteBuffer(unsigned nbytes, const char *buffer)
270 {
271  if (nbytes == 0)
272  return 0;
273 
274  TQByteArray *a = new TQByteArray(nbytes);
275  a->duplicate(buffer, nbytes);
276  outBuf.append(a);
277  return nbytes;
278 }
279 
280 unsigned TDEBufferedIO::readBufferSize() const
281 {
282  unsigned count = 0;
283  TQByteArray *buf = ((TDEBufferedIO*)this)->inBuf.first();
284  while (buf != NULL)
285  {
286  count += buf->size();
287  buf = ((TDEBufferedIO*)this)->inBuf.next();
288  }
289 
290  return count - inBufIndex;
291 }
292 
293 unsigned TDEBufferedIO::writeBufferSize() const
294 {
295  unsigned count = 0;
296  TQByteArray *buf = ((TDEBufferedIO*)this)->outBuf.first();
297  while (buf != NULL)
298  {
299  count += buf->size();
300  buf = (const_cast<TDEBufferedIO*>(this))->outBuf.next();
301  }
302 
303  return count - outBufIndex;
304 }
305 
306 void TDEBufferedIO::virtual_hook( int id, void* data )
307 { KAsyncIO::virtual_hook( id, data ); }
308 
309 #include "kbufferedio.moc"
TDEBufferedIO::canReadLine
virtual bool canReadLine() const
Returns the number of bytes yet to write, still in the write buffer.
Definition: kbufferedio.cpp:135
TDEBufferedIO::TDEBufferedIO
TDEBufferedIO()
Definition: kbufferedio.cpp:89
TDEBufferedIO::readBufferSize
virtual unsigned readBufferSize() const
Returns the number of bytes in the read buffer.
Definition: kbufferedio.cpp:280
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:223
TDEBufferedIO::unreadBlock
virtual int unreadBlock(const char *data, uint len)
Unreads some data.
Definition: kbufferedio.cpp:165
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:229
TDEBufferedIO::feedReadBuffer
virtual unsigned feedReadBuffer(unsigned nbytes, const char *buffer, bool atBeginning=false)
Feeds data into the input buffer.
Definition: kbufferedio.cpp:253
TDEBufferedIO::setBufferSize
virtual bool setBufferSize(int rsize, int wsize=-2)
Sets the internal buffer size to value.
Definition: kbufferedio.cpp:104
TDEBufferedIO::writeBufferSize
virtual unsigned writeBufferSize() const
Returns the number of bytes in the write buffer.
Definition: kbufferedio.cpp:293
TDEBufferedIO::outBufIndex
unsigned outBufIndex
Offset into first output buffer.
Definition: kbufferedio.h:231
TDEBufferedIO::inBufIndex
unsigned inBufIndex
Offset into first input buffer.
Definition: kbufferedio.h:231
TDEBufferedIO::~TDEBufferedIO
virtual ~TDEBufferedIO()
Destroys this class.
Definition: kbufferedio.cpp:97
TDEBufferedIO::consumeWriteBuffer
virtual void consumeWriteBuffer(unsigned nbytes)
Consumes data from the output buffer.
Definition: kbufferedio.cpp:224
TDEBufferedIO::consumeReadBuffer
virtual unsigned consumeReadBuffer(unsigned nbytes, char *destbuffer, bool discard=true)
Consumes data from the input buffer.
Definition: kbufferedio.cpp:174
TDEBufferedIO
This abstract class implements basic functionality for buffered input/output.
Definition: kbufferedio.h:56
TDEBufferedIO::feedWriteBuffer
virtual unsigned feedWriteBuffer(unsigned nbytes, const char *buffer)
Feeds data into the output buffer.
Definition: kbufferedio.cpp:269

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
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.8.8
This website is maintained by Timothy Pearson.