Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

qbuffer.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** $Id: qbuffer.cpp,v 1.2 2003/07/10 02:40:11 llornkcor Exp $
00003 **
00004 ** Implementation of QBuffer class
00005 **
00006 ** Created : 930812
00007 **
00008 ** Copyright (C) 1992-2002 Trolltech AS.  All rights reserved.
00009 **
00010 ** This file is part of the tools module of the Qt GUI Toolkit.
00011 **
00012 ** This file may be distributed under the terms of the Q Public License
00013 ** as defined by Trolltech AS of Norway and appearing in the file
00014 ** LICENSE.QPL included in the packaging of this file.
00015 **
00016 ** This file may be distributed and/or modified under the terms of the
00017 ** GNU General Public License version 2 as published by the Free Software
00018 ** Foundation and appearing in the file LICENSE.GPL included in the
00019 ** packaging of this file.
00020 **
00021 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00022 ** licenses may use this file in accordance with the Qt Commercial License
00023 ** Agreement provided with the Software.
00024 **
00025 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00026 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00027 **
00028 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00029 **   information about Qt Commercial License Agreements.
00030 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00031 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00032 **
00033 ** Contact info@trolltech.com if any conditions of this licensing are
00034 ** not clear to you.
00035 **
00036 **********************************************************************/
00037 
00038 #include "qbuffer.h"
00039 #include <stdlib.h>
00040 
00085 QBuffer::QBuffer()
00086 {
00087     setFlags( IO_Direct );
00088     a_inc = 16;                                 // initial increment
00089     a_len = 0;
00090     ioIndex = 0;
00091 }
00092 
00093 
00115 QBuffer::QBuffer( QByteArray buf ) : a(buf)
00116 {
00117     setFlags( IO_Direct );
00118     a_len = a.size();
00119     a_inc = (a_len > 512) ? 512 : a_len;        // initial increment
00120     if ( a_inc < 16 )
00121         a_inc = 16;
00122     ioIndex = 0;
00123 }
00124 
00129 QBuffer::~QBuffer()
00130 {
00131 }
00132 
00133 
00146 bool QBuffer::setBuffer( QByteArray buf )
00147 {
00148     if ( isOpen() ) {
00149 #if defined(QT_CHECK_STATE)
00150         qWarning( "QBuffer::setBuffer: Buffer is open" );
00151 #endif
00152         return FALSE;
00153     }
00154     a = buf;
00155     a_len = a.size();
00156     a_inc = (a_len > 512) ? 512 : a_len;        // initial increment
00157     if ( a_inc < 16 )
00158         a_inc = 16;
00159     ioIndex = 0;
00160     return TRUE;
00161 }
00162 
00189 bool QBuffer::open( int m  )
00190 {
00191     if ( isOpen() ) {                           // buffer already open
00192 #if defined(QT_CHECK_STATE)
00193         qWarning( "QBuffer::open: Buffer already open" );
00194 #endif
00195         return FALSE;
00196     }
00197     setMode( m );
00198     if ( m & IO_Truncate ) {                    // truncate buffer
00199         a.resize( 1 );
00200         a_len = 1;
00201     }
00202     if ( m & IO_Append ) {                      // append to end of buffer
00203         ioIndex = a.size();
00204     } else {
00205         ioIndex = 0;
00206     }
00207     a_inc = 16;
00208     setState( IO_Open );
00209     setStatus( 0 );
00210     return TRUE;
00211 }
00212 
00221 void QBuffer::close()
00222 {
00223     if ( isOpen() ) {
00224         setFlags( IO_Direct );
00225         ioIndex = 0;
00226         a_inc = 16;
00227     }
00228 }
00229 
00236 void QBuffer::flush()
00237 {
00238     return;
00239 }
00240 
00241 
00258 bool QBuffer::at( Offset pos )
00259 {
00260 #if defined(QT_CHECK_STATE)
00261     if ( !isOpen() ) {
00262         qWarning( "QBuffer::at: Buffer is not open" );
00263         return FALSE;
00264     }
00265 #endif
00266     if ( pos > a_len ) {
00267 #if defined(QT_CHECK_RANGE)
00268 #if defined(QT_LARGEFILE_SUPPORT) && defined(QT_ABI_64BITOFFSET)
00269         qWarning( "QBuffer::at: Index %llu out of range", pos );
00270 #else
00271         qWarning( "QBuffer::at: Index %lu out of range", pos );
00272 #endif
00273 #endif
00274         return FALSE;
00275     }
00276     ioIndex = pos;
00277     return TRUE;
00278 }
00279 
00280 
00285 Q_LONG QBuffer::readBlock( char *p, Q_ULONG len )
00286 {
00287 #if defined(QT_CHECK_STATE)
00288     if ( !p ) {
00289         qWarning( "QBuffer::readBlock: Null pointer error" );
00290         return -1;
00291     }
00292     if ( !isOpen() ) {                          // buffer not open
00293         qWarning( "QBuffer::readBlock: Buffer not open" );
00294         return -1;
00295     }
00296     if ( !isReadable() ) {                      // reading not permitted
00297         qWarning( "QBuffer::readBlock: Read operation not permitted" );
00298         return -1;
00299     }
00300 #endif
00301     if ( ioIndex + len > a.size() ) {   // overflow
00302         if ( ioIndex >= a.size() ) {
00303             return 0;
00304         } else {
00305             len = a.size() - ioIndex;
00306         }
00307     }
00308     memcpy( p, a.data()+ioIndex, len );
00309     ioIndex += len;
00310     return len;
00311 }
00312 
00330 Q_LONG QBuffer::writeBlock( const char *p, Q_ULONG len )
00331 {
00332     if ( len == 0 )
00333         return 0;
00334 
00335 #if defined(QT_CHECK_NULL)
00336     if ( p == 0 ) {
00337         qWarning( "QBuffer::writeBlock: Null pointer error" );
00338         return -1;
00339     }
00340 #endif
00341 #if defined(QT_CHECK_STATE)
00342     if ( !isOpen() ) {                          // buffer not open
00343         qWarning( "QBuffer::writeBlock: Buffer not open" );
00344         return -1;
00345     }
00346     if ( !isWritable() ) {                      // writing not permitted
00347         qWarning( "QBuffer::writeBlock: Write operation not permitted" );
00348         return -1;
00349     }
00350 #endif
00351     if ( ioIndex + len >= a_len ) {             // overflow
00352         Q_ULONG new_len = a_len + a_inc*((ioIndex+len-a_len)/a_inc+1);
00353         if ( !a.resize( new_len ) ) {           // could not resize
00354 #if defined(QT_CHECK_NULL)
00355             qWarning( "QBuffer::writeBlock: Memory allocation error" );
00356 #endif
00357             setStatus( IO_ResourceError );
00358             return -1;
00359         }
00360         a_inc *= 2;                             // double increment
00361         a_len = new_len;
00362         a.shd->len = ioIndex + len;
00363     }
00364     memcpy( a.data()+ioIndex, p, len );
00365     ioIndex += len;
00366     if ( a.shd->len < ioIndex )
00367         a.shd->len = ioIndex;                   // fake (not alloc'd) length
00368     return len;
00369 }
00370 
00371 
00376 Q_LONG QBuffer::readLine( char *p, Q_ULONG maxlen )
00377 {
00378 #if defined(QT_CHECK_NULL)
00379     if ( p == 0 ) {
00380         qWarning( "QBuffer::readLine: Null pointer error" );
00381         return -1;
00382     }
00383 #endif
00384 #if defined(QT_CHECK_STATE)
00385     if ( !isOpen() ) {                          // buffer not open
00386         qWarning( "QBuffer::readLine: Buffer not open" );
00387         return -1;
00388     }
00389     if ( !isReadable() ) {                      // reading not permitted
00390         qWarning( "QBuffer::readLine: Read operation not permitted" );
00391         return -1;
00392     }
00393 #endif
00394     if ( maxlen == 0 )
00395         return 0;
00396     Q_ULONG start = ioIndex;
00397     char *d = a.data() + ioIndex;
00398     maxlen--;                                   // make room for 0-terminator
00399     if ( a.size() - ioIndex < maxlen )
00400         maxlen = a.size() - ioIndex;
00401     while ( maxlen-- ) {
00402         if ( (*p++ = *d++) == '\n' )
00403             break;
00404     }
00405     *p = '\0';
00406     ioIndex = d - a.data();
00407     return ioIndex - start;
00408 }
00409 
00410 
00415 int QBuffer::getch()
00416 {
00417 #if defined(QT_CHECK_STATE)
00418     if ( !isOpen() ) {                          // buffer not open
00419         qWarning( "QBuffer::getch: Buffer not open" );
00420         return -1;
00421     }
00422     if ( !isReadable() ) {                      // reading not permitted
00423         qWarning( "QBuffer::getch: Read operation not permitted" );
00424         return -1;
00425     }
00426 #endif
00427     if ( ioIndex+1 > a.size() ) {               // overflow
00428         setStatus( IO_ReadError );
00429         return -1;
00430     }
00431     return uchar(*(a.data()+ioIndex++));
00432 }
00433 
00446 int QBuffer::putch( int ch )
00447 {
00448 #if defined(QT_CHECK_STATE)
00449     if ( !isOpen() ) {                          // buffer not open
00450         qWarning( "QBuffer::putch: Buffer not open" );
00451         return -1;
00452     }
00453     if ( !isWritable() ) {                      // writing not permitted
00454         qWarning( "QBuffer::putch: Write operation not permitted" );
00455         return -1;
00456     }
00457 #endif
00458     if ( ioIndex + 1 >= a_len ) {               // overflow
00459         char buf[1];
00460         buf[0] = (char)ch;
00461         if ( writeBlock(buf,1) != 1 )
00462             return -1;                          // write error
00463     } else {
00464         *(a.data() + ioIndex++) = (char)ch;
00465         if ( a.shd->len < ioIndex )
00466             a.shd->len = ioIndex;
00467     }
00468     return ch;
00469 }
00470 
00475 int QBuffer::ungetch( int ch )
00476 {
00477 #if defined(QT_CHECK_STATE)
00478     if ( !isOpen() ) {                          // buffer not open
00479         qWarning( "QBuffer::ungetch: Buffer not open" );
00480         return -1;
00481     }
00482     if ( !isReadable() ) {                      // reading not permitted
00483         qWarning( "QBuffer::ungetch: Read operation not permitted" );
00484         return -1;
00485     }
00486 #endif
00487     if ( ch != -1 ) {
00488         if ( ioIndex )
00489             ioIndex--;
00490         else
00491             ch = -1;
00492     }
00493     return ch;
00494 }
00495 

Generated on Sat Nov 5 16:18:28 2005 for OPIE by  doxygen 1.4.2