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

kdecore

  • kdecore
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 KBufferedIO::KBufferedIO() :
90  inBufIndex(0), outBufIndex(0)
91 {
92  inBuf.setAutoDelete(true);
93  outBuf.setAutoDelete(true);
94 }
95 
96 // destructor
97 KBufferedIO::~KBufferedIO()
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 KBufferedIO::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 KBufferedIO::bytesAvailable() const
116 #endif // USE_QT3
117 #ifdef USE_QT4
118 qint64 KBufferedIO::bytesAvailable() const
119 #endif // USE_QT4
120 {
121  return readBufferSize();
122 }
123 
124 #ifdef USE_QT3
125 int KBufferedIO::bytesToWrite() const
126 #endif // USE_QT3
127 #ifdef USE_QT4
128 qint64 KBufferedIO::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 KBufferedIO::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 = ((KBufferedIO*)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 KBufferedIO::unreadBlock(const char *data, uint len)
166 {
167  return feedReadBuffer(len, data, true);
168 }
169 
170 //
171 // protected member functions
172 //
173 
174 unsigned KBufferedIO::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 KBufferedIO::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 KBufferedIO::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 KBufferedIO::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 KBufferedIO::readBufferSize() const
281 {
282  unsigned count = 0;
283  TQByteArray *buf = ((KBufferedIO*)this)->inBuf.first();
284  while (buf != NULL)
285  {
286  count += buf->size();
287  buf = ((KBufferedIO*)this)->inBuf.next();
288  }
289 
290  return count - inBufIndex;
291 }
292 
293 unsigned KBufferedIO::writeBufferSize() const
294 {
295  unsigned count = 0;
296  TQByteArray *buf = ((KBufferedIO*)this)->outBuf.first();
297  while (buf != NULL)
298  {
299  count += buf->size();
300  buf = (const_cast<KBufferedIO*>(this))->outBuf.next();
301  }
302 
303  return count - outBufIndex;
304 }
305 
306 void KBufferedIO::virtual_hook( int id, void* data )
307 { KAsyncIO::virtual_hook( id, data ); }
308 
309 #include "kbufferedio.moc"

kdecore

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

kdecore

Skip menu "kdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdecore by doxygen 1.8.1.2
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |