kbufferedio.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022
00023 #include <string.h>
00024
00025 #include <qptrlist.h>
00026 #include <qcstring.h>
00027 #include "kbufferedio.h"
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 KBufferedIO::KBufferedIO() :
00088 inBufIndex(0), outBufIndex(0)
00089 {
00090 inBuf.setAutoDelete(true);
00091 outBuf.setAutoDelete(true);
00092 }
00093
00094
00095 KBufferedIO::~KBufferedIO()
00096 {
00097 }
00098
00099
00100
00101
00102 bool KBufferedIO::setBufferSize(int rsize, int wsize )
00103 {
00104 if (wsize != -2 && wsize != -1)
00105 return false;
00106 if (rsize != -2 && rsize != -1)
00107 return false;
00108
00109 return true;
00110 }
00111
00112 int KBufferedIO::bytesAvailable() const
00113 {
00114 return readBufferSize();
00115 }
00116
00117 int KBufferedIO::bytesToWrite() const
00118 {
00119 return writeBufferSize();
00120 }
00121
00122
00123 bool KBufferedIO::canReadLine() const
00124 {
00125 if (bytesAvailable() == 0)
00126 return false;
00127
00128 QByteArray* buf;
00129
00130
00131 QPtrList<QByteArray> &buflist = ((KBufferedIO*)this)->inBuf;
00132 buf = buflist.first();
00133 char *p = buf->data() + inBufIndex;
00134 int n = buf->size() - inBufIndex;
00135 while (buf != NULL)
00136 {
00137 while (n--)
00138 if (*p++ == '\n')
00139 return true;
00140 buf = buflist.next();
00141 if (buf != NULL)
00142 {
00143 p = buf->data();
00144 n = buf->size();
00145 }
00146 }
00147
00148 return false;
00149 }
00150
00151
00152
00153 int KBufferedIO::unreadBlock(const char *data, uint len)
00154 {
00155 return feedReadBuffer(len, data, true);
00156 }
00157
00158
00159
00160
00161
00162 unsigned KBufferedIO::consumeReadBuffer(unsigned nbytes, char *destbuffer, bool discard)
00163 {
00164 {
00165 register unsigned u = readBufferSize();
00166 if (nbytes > u)
00167 nbytes = u;
00168 }
00169
00170 QByteArray *buf;
00171 unsigned copied = 0;
00172 unsigned index = inBufIndex;
00173
00174 buf = inBuf.first();
00175 while (nbytes && buf)
00176 {
00177
00178 unsigned to_copy = buf->size() - index;
00179 if (to_copy > nbytes)
00180 to_copy = nbytes;
00181
00182 if (destbuffer)
00183 memcpy(destbuffer + copied, buf->data() + index, to_copy);
00184 nbytes -= to_copy;
00185 copied += to_copy;
00186
00187 if (buf->size() - index > to_copy)
00188 {
00189 index += to_copy;
00190 break;
00191
00192 }
00193 else
00194 {
00195 index = 0;
00196 if (discard)
00197 {
00198 inBuf.remove();
00199 buf = inBuf.first();
00200 }
00201 else
00202 buf = inBuf.next();
00203 }
00204 }
00205
00206 if (discard)
00207 inBufIndex = index;
00208
00209 return copied;
00210 }
00211
00212 void KBufferedIO::consumeWriteBuffer(unsigned nbytes)
00213 {
00214 QByteArray *buf = outBuf.first();
00215 if (buf == NULL)
00216 return;
00217
00218 if (nbytes < buf->size() - outBufIndex)
00219
00220 outBufIndex += nbytes;
00221 else
00222 {
00223 nbytes -= buf->size() - outBufIndex;
00224 outBufIndex = 0;
00225 outBuf.remove();
00226
00227 while ((buf = outBuf.current()) != NULL)
00228 if (buf->size() <= nbytes)
00229 {
00230 nbytes -= buf->size();
00231 outBuf.remove();
00232 }
00233 else
00234 {
00235 outBufIndex = nbytes;
00236 break;
00237 }
00238 }
00239 }
00240
00241 unsigned KBufferedIO::feedReadBuffer(unsigned nbytes, const char *buffer, bool atBeginning)
00242 {
00243 if (nbytes == 0)
00244 return 0;
00245
00246 QByteArray *a = new QByteArray(nbytes);
00247 a->duplicate(buffer, nbytes);
00248
00249 if (atBeginning)
00250 inBuf.prepend(a);
00251 else
00252 inBuf.append(a);
00253
00254 return nbytes;
00255 }
00256
00257 unsigned KBufferedIO::feedWriteBuffer(unsigned nbytes, const char *buffer)
00258 {
00259 if (nbytes == 0)
00260 return 0;
00261
00262 QByteArray *a = new QByteArray(nbytes);
00263 a->duplicate(buffer, nbytes);
00264 outBuf.append(a);
00265 return nbytes;
00266 }
00267
00268 unsigned KBufferedIO::readBufferSize() const
00269 {
00270 unsigned count = 0;
00271 QByteArray *buf = ((KBufferedIO*)this)->inBuf.first();
00272 while (buf != NULL)
00273 {
00274 count += buf->size();
00275 buf = ((KBufferedIO*)this)->inBuf.next();
00276 }
00277
00278 return count - inBufIndex;
00279 }
00280
00281 unsigned KBufferedIO::writeBufferSize() const
00282 {
00283 unsigned count = 0;
00284 QByteArray *buf = ((KBufferedIO*)this)->outBuf.first();
00285 while (buf != NULL)
00286 {
00287 count += buf->size();
00288 buf = (const_cast<KBufferedIO*>(this))->outBuf.next();
00289 }
00290
00291 return count - outBufIndex;
00292 }
00293
00294 void KBufferedIO::virtual_hook( int id, void* data )
00295 { KAsyncIO::virtual_hook( id, data ); }
00296
00297 #include "kbufferedio.moc"
This file is part of the documentation for kdecore Library Version 3.4.3.