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