00001 /**************************************************************************** 00002 ** 00003 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. 00004 ** 00005 ** This file is part of the Qt3Support module of the Qt Toolkit. 00006 ** 00007 ** This file may be used under the terms of the GNU General Public 00008 ** License versions 2.0 or 3.0 as published by the Free Software 00009 ** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 00010 ** included in the packaging of this file. Alternatively you may (at 00011 ** your option) use any later version of the GNU General Public 00012 ** License if such license has been publicly approved by Trolltech ASA 00013 ** (or its successors, if any) and the KDE Free Qt Foundation. In 00014 ** addition, as a special exception, Trolltech gives you certain 00015 ** additional rights. These rights are described in the Trolltech GPL 00016 ** Exception version 1.2, which can be found at 00017 ** http://www.trolltech.com/products/qt/gplexception/ and in the file 00018 ** GPL_EXCEPTION.txt in this package. 00019 ** 00020 ** Please review the following information to ensure GNU General 00021 ** Public Licensing requirements will be met: 00022 ** http://trolltech.com/products/qt/licenses/licensing/opensource/. If 00023 ** you are unsure which license is appropriate for your use, please 00024 ** review the following information: 00025 ** http://trolltech.com/products/qt/licenses/licensing/licensingoverview 00026 ** or contact the sales department at sales@trolltech.com. 00027 ** 00028 ** In addition, as a special exception, Trolltech, as the sole 00029 ** copyright holder for Qt Designer, grants users of the Qt/Eclipse 00030 ** Integration plug-in the right for the Qt/Eclipse Integration to 00031 ** link to functionality provided by Qt Designer and its related 00032 ** libraries. 00033 ** 00034 ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, 00035 ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR 00036 ** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly 00037 ** granted herein. 00038 ** 00039 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00040 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00041 ** 00042 ****************************************************************************/ 00043 00044 #include "mmembuf.h" 00045 00046 // ******************************************************************* 00047 // QMembuf declaration and implementation 00048 // ******************************************************************* 00049 00050 /* \internal 00051 This class implements an efficient buffering of data that is often used by 00052 asynchronous IO classes like QSocket, QHttp and QProcess. 00053 */ 00054 00055 MMembuf::MMembuf() : _size(0), _index(0) 00056 { 00057 } 00058 00059 MMembuf::~MMembuf() 00060 { 00061 while (!buf.isEmpty()) 00062 delete buf.takeFirst(); 00063 } 00064 00070 bool MMembuf::consumeBytes(quint64 nbytes, char *sink) 00071 { 00072 if (nbytes <= 0 || (qint64)nbytes > _size) 00073 return false; 00074 _size -= nbytes; 00075 while (!buf.isEmpty()) { 00076 QByteArray *a = buf.first(); 00077 if ((int)(_index + nbytes) >= a->size()) { 00078 // Here we skip the whole byte array and get the next later 00079 int len = a->size() - _index; 00080 if (sink) { 00081 memcpy(sink, a->constData()+_index, len); 00082 sink += len; 00083 } 00084 nbytes -= len; 00085 buf.removeFirst(); 00086 delete a; 00087 _index = 0; 00088 if (nbytes == 0) 00089 break; 00090 } else { 00091 // Here we skip only a part of the first byte array 00092 if (sink) 00093 memcpy(sink, a->constData()+_index, nbytes); 00094 _index += nbytes; 00095 break; 00096 } 00097 } 00098 return true; 00099 } 00100 00108 bool MMembuf::scanNewline(QByteArray *store) 00109 { 00110 if (_size == 0) 00111 return false; 00112 int i = 0; // index into 'store' 00113 QByteArray *a = 0; 00114 char *p; 00115 int n; 00116 bool retval = false; 00117 for (int j = 0; j < buf.size(); ++j) { 00118 a = buf.at(j); 00119 p = a->data(); 00120 n = a->size(); 00121 if (!j) { 00122 // first buffer 00123 p += _index; 00124 n -= _index; 00125 } 00126 if (store) { 00127 while (n-- > 0) { 00128 *(store->data()+i) = *p; 00129 if (++i == (int)store->size()) 00130 store->resize(store->size() < 256 00131 ? 1024 : store->size()*4); 00132 if (*p == '\n') { 00133 retval = true; 00134 goto end; 00135 } 00136 p++; 00137 } 00138 } else { 00139 while (n-- > 0) { 00140 if(*p == '\n') 00141 return true; 00142 p++; 00143 } 00144 } 00145 } 00146 end: 00147 if (store) 00148 store->resize(i); 00149 return retval; 00150 } 00151 00152 int MMembuf::ungetch(int ch) 00153 { 00154 if (buf.isEmpty() || _index==0) { 00155 // we need a new QByteArray 00156 QByteArray *ba = new QByteArray; 00157 ba->resize(1); 00158 buf.prepend(ba); 00159 _size++; 00160 (*ba)[0] = ch; 00161 } else { 00162 // we can reuse a place in the buffer 00163 QByteArray *ba = buf.first(); 00164 _index--; 00165 _size++; 00166 (*ba)[(int)_index] = ch; 00167 } 00168 return ch; 00169 }
1.6.3