Blame qtools/qbuffer.cpp

Packit Service 50c9f2
/****************************************************************************
Packit Service 50c9f2
** 
Packit Service 50c9f2
**
Packit Service 50c9f2
** Implementation of QBuffer class
Packit Service 50c9f2
**
Packit Service 50c9f2
** Created : 930812
Packit Service 50c9f2
**
Packit Service 50c9f2
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file is part of the tools module of the Qt GUI Toolkit.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file may be distributed under the terms of the Q Public License
Packit Service 50c9f2
** as defined by Trolltech AS of Norway and appearing in the file
Packit Service 50c9f2
** LICENSE.QPL included in the packaging of this file.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file may be distributed and/or modified under the terms of the
Packit Service 50c9f2
** GNU General Public License version 2 as published by the Free Software
Packit Service 50c9f2
** Foundation and appearing in the file LICENSE.GPL included in the
Packit Service 50c9f2
** packaging of this file.
Packit Service 50c9f2
**
Packit Service 50c9f2
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
Packit Service 50c9f2
** licenses may use this file in accordance with the Qt Commercial License
Packit Service 50c9f2
** Agreement provided with the Software.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
Packit Service 50c9f2
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Packit Service 50c9f2
**
Packit Service 50c9f2
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
Packit Service 50c9f2
**   information about Qt Commercial License Agreements.
Packit Service 50c9f2
** See http://www.trolltech.com/qpl/ for QPL licensing information.
Packit Service 50c9f2
** See http://www.trolltech.com/gpl/ for GPL licensing information.
Packit Service 50c9f2
**
Packit Service 50c9f2
** Contact info@trolltech.com if any conditions of this licensing are
Packit Service 50c9f2
** not clear to you.
Packit Service 50c9f2
**
Packit Service 50c9f2
**********************************************************************/
Packit Service 50c9f2
Packit Service 50c9f2
#include "qbuffer.h"
Packit Service 50c9f2
#include <stdlib.h>
Packit Service 50c9f2
Packit Service 50c9f2
// REVISED: paul
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  \class QBuffer qbuffer.h
Packit Service 50c9f2
  \brief The QBuffer class is an I/O device that operates on a QByteArray
Packit Service 50c9f2
Packit Service 50c9f2
  \ingroup io
Packit Service 50c9f2
Packit Service 50c9f2
  QBuffer allows reading and writing a memory buffer. It is normally
Packit Service 50c9f2
  used together with a QTextStream or a QDataStream.  QBuffer has an
Packit Service 50c9f2
  associated QByteArray which holds the buffer data. The size() of the
Packit Service 50c9f2
  buffer is automatically adjusted as data is written.
Packit Service 50c9f2
Packit Service 50c9f2
  The constructor \c QBuffer(QByteArray) creates a QBuffer with an
Packit Service 50c9f2
  existing byte array.  The byte array can also be set with setBuffer().
Packit Service 50c9f2
  Writing to the QBuffer will modify the original byte array, since
Packit Service 50c9f2
  QByteArray is \link shclass.html explicitly shared.\endlink
Packit Service 50c9f2
Packit Service 50c9f2
  Use open() to open the buffer before use, and to set the mode
Packit Service 50c9f2
  (read-only,write-only, etc.).  close() closes the buffer. The buffer
Packit Service 50c9f2
  must be closed before reopening or calling setBuffer().
Packit Service 50c9f2
Packit Service 50c9f2
  The common way to use QBuffer is through \l QDataStream or \l QTextStream
Packit Service 50c9f2
  which have constructors that take a QBuffer parameter. For
Packit Service 50c9f2
  convenience, there are also QDataStream and QTextStream constructors
Packit Service 50c9f2
  that take a QByteArray parameter.  These constructors create and open
Packit Service 50c9f2
  an internal QBuffer.
Packit Service 50c9f2
Packit Service 50c9f2
  Note that QTextStream can also operate on a QString (a Unicode
Packit Service 50c9f2
  string); a QBuffer cannot.
Packit Service 50c9f2
Packit Service 50c9f2
  You can also use QBuffer directly through the standard QIODevice
Packit Service 50c9f2
  functions readBlock(), writeBlock() readLine(), at(), getch(), putch() and
Packit Service 50c9f2
  ungetch().
Packit Service 50c9f2
Packit Service 50c9f2
  \sa QFile, QDataStream, QTextStream, QByteArray, \link shclass.html Shared Classes\endlink
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  Constructs an empty buffer.
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
QBuffer::QBuffer()
Packit Service 50c9f2
{
Packit Service 50c9f2
    setFlags( IO_Direct );
Packit Service 50c9f2
    a_inc = 16;					// initial increment
Packit Service 50c9f2
    a_len = 0;
Packit Service 50c9f2
    ioIndex = 0;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  Constructs a buffer that operates on \a buf.
Packit Service 50c9f2
  If you open the buffer in write mode (\c IO_WriteOnly or
Packit Service 50c9f2
  \c IO_ReadWrite) and write something into the buffer, \a buf
Packit Service 50c9f2
  will be modified.
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
  Example:
Packit Service 50c9f2
  \code
Packit Service 50c9f2
    QCString str = "abc";
Packit Service 50c9f2
    QBuffer b( str );
Packit Service 50c9f2
    b.open( IO_WriteOnly );
Packit Service 50c9f2
    b.at( 3 );					// position at \0
Packit Service 50c9f2
    b.writeBlock( "def", 4 );			// write including \0
Packit Service 50c9f2
    b.close();
Packit Service 50c9f2
      // Now, str == "abcdef"
Packit Service 50c9f2
  \endcode
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
  \sa setBuffer()
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
QBuffer::QBuffer( QByteArray buf ) : a(buf)
Packit Service 50c9f2
{
Packit Service 50c9f2
    setFlags( IO_Direct );
Packit Service 50c9f2
    a_len = a.size();
Packit Service 50c9f2
    a_inc = (a_len > 512) ? 512 : a_len;	// initial increment
Packit Service 50c9f2
    if ( a_inc < 16 )
Packit Service 50c9f2
	a_inc = 16;
Packit Service 50c9f2
    ioIndex = 0;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  Destructs the buffer.
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
QBuffer::~QBuffer()
Packit Service 50c9f2
{
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  Replaces the buffer's contents with \a buf.
Packit Service 50c9f2
Packit Service 50c9f2
  This may not be done when isOpen() is TRUE.
Packit Service 50c9f2
Packit Service 50c9f2
  Note that if you open the buffer in write mode (\c IO_WriteOnly or
Packit Service 50c9f2
  IO_ReadWrite) and write something into the buffer, \a buf is also
Packit Service 50c9f2
  modified because QByteArray is an explicitly shared class.
Packit Service 50c9f2
Packit Service 50c9f2
  \sa buffer(), open(), close()
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
bool QBuffer::setBuffer( QByteArray buf )
Packit Service 50c9f2
{
Packit Service 50c9f2
    if ( isOpen() ) {
Packit Service 50c9f2
#if defined(CHECK_STATE)
Packit Service 50c9f2
	qWarning( "QBuffer::setBuffer: Buffer is open");
Packit Service 50c9f2
#endif
Packit Service 50c9f2
	return FALSE;
Packit Service 50c9f2
    }
Packit Service 50c9f2
    a = buf;
Packit Service 50c9f2
    a_len = a.size();
Packit Service 50c9f2
    a_inc = (a_len > 512) ? 512 : a_len;	// initial increment
Packit Service 50c9f2
    if ( a_inc < 16 )
Packit Service 50c9f2
	a_inc = 16;
Packit Service 50c9f2
    ioIndex = 0;
Packit Service 50c9f2
    return TRUE;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  \fn QByteArray QBuffer::buffer() const
Packit Service 50c9f2
Packit Service 50c9f2
  Returns this buffer's byte array.
Packit Service 50c9f2
Packit Service 50c9f2
  \sa setBuffer()
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  \reimp
Packit Service 50c9f2
  Opens the buffer in the mode \a m.  Returns TRUE if successful,
Packit Service 50c9f2
  otherwise FALSE. The buffer must be opened before use.
Packit Service 50c9f2
Packit Service 50c9f2
  The mode parameter \a m must be a combination of the following flags.
Packit Service 50c9f2
  
    Packit Service 50c9f2
      
  • \c IO_ReadOnly opens a buffer in read-only mode.
  • Packit Service 50c9f2
      
  • \c IO_WriteOnly opens a buffer in write-only mode.
  • Packit Service 50c9f2
      
  • \c IO_ReadWrite opens a buffer in read/write mode.
  • Packit Service 50c9f2
      
  • \c IO_Append sets the buffer index to the end of the buffer.
  • Packit Service 50c9f2
      
  • \c IO_Truncate truncates the buffer.
  • Packit Service 50c9f2
      
    Packit Service 50c9f2
    Packit Service 50c9f2
      \sa close(), isOpen()
    Packit Service 50c9f2
    */
    Packit Service 50c9f2
    Packit Service 50c9f2
    bool QBuffer::open( int m  )
    Packit Service 50c9f2
    {
    Packit Service 50c9f2
        if ( isOpen() ) {				// buffer already open
    Packit Service 50c9f2
    #if defined(CHECK_STATE)
    Packit Service 50c9f2
    	qWarning( "QBuffer::open: Buffer already open" );
    Packit Service 50c9f2
    #endif
    Packit Service 50c9f2
    	return FALSE;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        setMode( m );
    Packit Service 50c9f2
        if ( m & IO_Truncate ) {			// truncate buffer
    Packit Service 50c9f2
    	a.resize( 0 );
    Packit Service 50c9f2
    	a_len = 0;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        if ( m & IO_Append ) {			// append to end of buffer
    Packit Service 50c9f2
    	ioIndex = a.size();
    Packit Service 50c9f2
        } else {
    Packit Service 50c9f2
    	ioIndex = 0;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        a_inc = 16;
    Packit Service 50c9f2
        setState( IO_Open );
    Packit Service 50c9f2
        setStatus( 0 );
    Packit Service 50c9f2
        return TRUE;
    Packit Service 50c9f2
    }
    Packit Service 50c9f2
    Packit Service 50c9f2
    /*!
    Packit Service 50c9f2
      \reimp
    Packit Service 50c9f2
      Closes an open buffer.
    Packit Service 50c9f2
      \sa open()
    Packit Service 50c9f2
    */
    Packit Service 50c9f2
    Packit Service 50c9f2
    void QBuffer::close()
    Packit Service 50c9f2
    {
    Packit Service 50c9f2
        if ( isOpen() ) {
    Packit Service 50c9f2
    	setFlags( IO_Direct );
    Packit Service 50c9f2
    	ioIndex = 0;
    Packit Service 50c9f2
    	a_inc = 16;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
    }
    Packit Service 50c9f2
    Packit Service 50c9f2
    /*!
    Packit Service 50c9f2
      \reimp
    Packit Service 50c9f2
      The flush function does nothing for a QBuffer.
    Packit Service 50c9f2
    */
    Packit Service 50c9f2
    Packit Service 50c9f2
    void QBuffer::flush()
    Packit Service 50c9f2
    {
    Packit Service 50c9f2
        return;
    Packit Service 50c9f2
    }
    Packit Service 50c9f2
    Packit Service 50c9f2
    Packit Service 50c9f2
    /*!
    Packit Service 50c9f2
      \fn int QBuffer::at() const
    Packit Service 50c9f2
      \reimp
    Packit Service 50c9f2
    */
    Packit Service 50c9f2
    Packit Service 50c9f2
    /*!
    Packit Service 50c9f2
      \fn uint QBuffer::size() const
    Packit Service 50c9f2
      \reimp
    Packit Service 50c9f2
    */
    Packit Service 50c9f2
    Packit Service 50c9f2
    /*!
    Packit Service 50c9f2
      \reimp
    Packit Service 50c9f2
    */
    Packit Service 50c9f2
    Packit Service 50c9f2
    bool QBuffer::at( int pos )
    Packit Service 50c9f2
    {
    Packit Service 50c9f2
    #if defined(CHECK_STATE)
    Packit Service 50c9f2
        if ( !isOpen() ) {
    Packit Service 50c9f2
    	qWarning( "QBuffer::at: Buffer is not open" );
    Packit Service 50c9f2
    	return FALSE;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
    #endif
    Packit Service 50c9f2
        if ( (uint)pos > a_len ) {
    Packit Service 50c9f2
    #if defined(CHECK_RANGE)
    Packit Service 50c9f2
    	qWarning( "QBuffer::at: Index %d out of range", pos );
    Packit Service 50c9f2
    #endif
    Packit Service 50c9f2
    	return FALSE;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        ioIndex = pos;
    Packit Service 50c9f2
        return TRUE;
    Packit Service 50c9f2
    }
    Packit Service 50c9f2
    Packit Service 50c9f2
    Packit Service 50c9f2
    /*!
    Packit Service 50c9f2
      \reimp
    Packit Service 50c9f2
    */
    Packit Service 50c9f2
    Packit Service 50c9f2
    int QBuffer::readBlock( char *p, uint len )
    Packit Service 50c9f2
    {
    Packit Service 50c9f2
    #if defined(CHECK_STATE)
    Packit Service 50c9f2
        CHECK_PTR( p );
    Packit Service 50c9f2
        if ( !isOpen() ) {				// buffer not open
    Packit Service 50c9f2
    	qWarning( "QBuffer::readBlock: Buffer not open" );
    Packit Service 50c9f2
    	return -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        if ( !isReadable() ) {			// reading not permitted
    Packit Service 50c9f2
    	qWarning( "QBuffer::readBlock: Read operation not permitted" );
    Packit Service 50c9f2
    	return -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
    #endif
    Packit Service 50c9f2
        if ( (uint)ioIndex + len > a.size() ) {	// overflow
    Packit Service 50c9f2
    	if ( (uint)ioIndex >= a.size() ) {
    Packit Service 50c9f2
    	    setStatus( IO_ReadError );
    Packit Service 50c9f2
    	    return -1;
    Packit Service 50c9f2
    	} else {
    Packit Service 50c9f2
    	    len = a.size() - (uint)ioIndex;
    Packit Service 50c9f2
    	}
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        memcpy( p, a.data()+ioIndex, len );
    Packit Service 50c9f2
        ioIndex += len;
    Packit Service 50c9f2
        return len;
    Packit Service 50c9f2
    }
    Packit Service 50c9f2
    Packit Service 50c9f2
    /*!
    Packit Service 50c9f2
      \reimp
    Packit Service 50c9f2
    Packit Service 50c9f2
      Writes \a len bytes from \a p into the buffer at the current index,
    Packit Service 50c9f2
      overwriting any characters there and extending the buffer if necessary.
    Packit Service 50c9f2
      Returns the number of bytes actually written.
    Packit Service 50c9f2
    Packit Service 50c9f2
      Returns -1 if a serious error occurred.
    Packit Service 50c9f2
    Packit Service 50c9f2
      \sa readBlock()
    Packit Service 50c9f2
    */
    Packit Service 50c9f2
    Packit Service 50c9f2
    int QBuffer::writeBlock( const char *p, uint len )
    Packit Service 50c9f2
    {
    Packit Service 50c9f2
    #if defined(CHECK_NULL)
    Packit Service 50c9f2
        if ( p == 0 && len != 0 )
    Packit Service 50c9f2
    	qWarning( "QBuffer::writeBlock: Null pointer error" );
    Packit Service 50c9f2
    #endif
    Packit Service 50c9f2
    #if defined(CHECK_STATE)
    Packit Service 50c9f2
        if ( !isOpen() ) {				// buffer not open
    Packit Service 50c9f2
    	qWarning( "QBuffer::writeBlock: Buffer not open" );
    Packit Service 50c9f2
    	return -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        if ( !isWritable() ) {			// writing not permitted
    Packit Service 50c9f2
    	qWarning( "QBuffer::writeBlock: Write operation not permitted" );
    Packit Service 50c9f2
    	return -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
    #endif
    Packit Service 50c9f2
        if ( (uint)ioIndex + len >= a_len ) {		// overflow
    Packit Service 50c9f2
    	uint new_len = a_len + a_inc*(((uint)ioIndex+len-a_len)/a_inc+1);
    Packit Service 50c9f2
    	if ( !a.resize( new_len ) ) {		// could not resize
    Packit Service 50c9f2
    #if defined(CHECK_NULL)
    Packit Service 50c9f2
    	    qWarning( "QBuffer::writeBlock: Memory allocation error" );
    Packit Service 50c9f2
    #endif
    Packit Service 50c9f2
    	    setStatus( IO_ResourceError );
    Packit Service 50c9f2
    	    return -1;
    Packit Service 50c9f2
    	}
    Packit Service 50c9f2
    	a_inc *= 2;				// double increment
    Packit Service 50c9f2
    	a_len = new_len;
    Packit Service 50c9f2
    	a.shd->len = (uint)ioIndex + len;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        memcpy( a.data()+ioIndex, p, len );
    Packit Service 50c9f2
        ioIndex += len;
    Packit Service 50c9f2
        if ( a.shd->len < (uint)ioIndex )
    Packit Service 50c9f2
    	a.shd->len = (uint)ioIndex;		// fake (not alloc'd) length
    Packit Service 50c9f2
        return len;
    Packit Service 50c9f2
    }
    Packit Service 50c9f2
    Packit Service 50c9f2
    Packit Service 50c9f2
    /*!
    Packit Service 50c9f2
      \reimp
    Packit Service 50c9f2
    */
    Packit Service 50c9f2
    Packit Service 50c9f2
    int QBuffer::readLine( char *p, uint maxlen )
    Packit Service 50c9f2
    {
    Packit Service 50c9f2
    #if defined(CHECK_STATE)
    Packit Service 50c9f2
        CHECK_PTR( p );
    Packit Service 50c9f2
        if ( !isOpen() ) {				// buffer not open
    Packit Service 50c9f2
    	qWarning( "QBuffer::readLine: Buffer not open" );
    Packit Service 50c9f2
    	return -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        if ( !isReadable() ) {			// reading not permitted
    Packit Service 50c9f2
    	qWarning( "QBuffer::readLine: Read operation not permitted" );
    Packit Service 50c9f2
    	return -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
    #endif
    Packit Service 50c9f2
        if ( maxlen == 0 )
    Packit Service 50c9f2
    	return 0;
    Packit Service 50c9f2
        uint start = (uint)ioIndex;
    Packit Service 50c9f2
        char *d = a.data() + ioIndex;
    Packit Service 50c9f2
        maxlen--;					// make room for 0-terminator
    Packit Service 50c9f2
        if ( a.size() - (uint)ioIndex < maxlen )
    Packit Service 50c9f2
    	maxlen = a.size() - (uint)ioIndex;
    Packit Service 50c9f2
        while ( maxlen-- ) {
    Packit Service 50c9f2
    	if ( (*p++ = *d++) == '\n' )
    Packit Service 50c9f2
    	    break;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        *p = '\0';
    Packit Service 50c9f2
        ioIndex = (int)(d - a.data());
    Packit Service 50c9f2
        return (uint)ioIndex - start;
    Packit Service 50c9f2
    }
    Packit Service 50c9f2
    Packit Service 50c9f2
    Packit Service 50c9f2
    /*!
    Packit Service 50c9f2
      \reimp
    Packit Service 50c9f2
    */
    Packit Service 50c9f2
    Packit Service 50c9f2
    int QBuffer::getch()
    Packit Service 50c9f2
    {
    Packit Service 50c9f2
    #if defined(CHECK_STATE)
    Packit Service 50c9f2
        if ( !isOpen() ) {				// buffer not open
    Packit Service 50c9f2
    	qWarning( "QBuffer::getch: Buffer not open" );
    Packit Service 50c9f2
    	return -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        if ( !isReadable() ) {			// reading not permitted
    Packit Service 50c9f2
    	qWarning( "QBuffer::getch: Read operation not permitted" );
    Packit Service 50c9f2
    	return -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
    #endif
    Packit Service 50c9f2
        if ( (uint)ioIndex+1 > a.size() ) {		// overflow
    Packit Service 50c9f2
    	setStatus( IO_ReadError );
    Packit Service 50c9f2
    	return -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        return uchar(*(a.data()+ioIndex++));
    Packit Service 50c9f2
    }
    Packit Service 50c9f2
    Packit Service 50c9f2
    /*!
    Packit Service 50c9f2
      \reimp
    Packit Service 50c9f2
      Writes the character \a ch into the buffer, overwriting
    Packit Service 50c9f2
      the character at the current index, extending the buffer
    Packit Service 50c9f2
      if necessary.
    Packit Service 50c9f2
    Packit Service 50c9f2
      Returns \a ch, or -1 if some error occurred.
    Packit Service 50c9f2
    Packit Service 50c9f2
      \sa getch(), ungetch()
    Packit Service 50c9f2
    */
    Packit Service 50c9f2
    Packit Service 50c9f2
    int QBuffer::putch( int ch )
    Packit Service 50c9f2
    {
    Packit Service 50c9f2
    #if defined(CHECK_STATE)
    Packit Service 50c9f2
        if ( !isOpen() ) {				// buffer not open
    Packit Service 50c9f2
    	qWarning( "QBuffer::putch: Buffer not open" );
    Packit Service 50c9f2
    	return -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        if ( !isWritable() ) {			// writing not permitted
    Packit Service 50c9f2
    	qWarning( "QBuffer::putch: Write operation not permitted" );
    Packit Service 50c9f2
    	return -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
    #endif
    Packit Service 50c9f2
        if ( (uint)ioIndex + 1 >= a_len ) {		// overflow
    Packit Service 50c9f2
    	char buf[1];
    Packit Service 50c9f2
    	buf[0] = (char)ch;
    Packit Service 50c9f2
    	if ( writeBlock(buf,1) != 1 )
    Packit Service 50c9f2
    	    return -1;				// write error
    Packit Service 50c9f2
        } else {
    Packit Service 50c9f2
    	*(a.data() + ioIndex++) = (char)ch;
    Packit Service 50c9f2
    	if ( a.shd->len < (uint)ioIndex )
    Packit Service 50c9f2
    	    a.shd->len = (uint)ioIndex;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        return ch;
    Packit Service 50c9f2
    }
    Packit Service 50c9f2
    Packit Service 50c9f2
    /*!
    Packit Service 50c9f2
      \reimp
    Packit Service 50c9f2
    */
    Packit Service 50c9f2
    Packit Service 50c9f2
    int QBuffer::ungetch( int ch )
    Packit Service 50c9f2
    {
    Packit Service 50c9f2
    #if defined(CHECK_STATE)
    Packit Service 50c9f2
        if ( !isOpen() ) {				// buffer not open
    Packit Service 50c9f2
    	qWarning( "QBuffer::ungetch: Buffer not open" );
    Packit Service 50c9f2
    	return -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        if ( !isReadable() ) {			// reading not permitted
    Packit Service 50c9f2
    	qWarning( "QBuffer::ungetch: Read operation not permitted" );
    Packit Service 50c9f2
    	return -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
    #endif
    Packit Service 50c9f2
        if ( ch != -1 ) {
    Packit Service 50c9f2
    	if ( ioIndex )
    Packit Service 50c9f2
    	    ioIndex--;
    Packit Service 50c9f2
    	else
    Packit Service 50c9f2
    	    ch = -1;
    Packit Service 50c9f2
        }
    Packit Service 50c9f2
        return ch;
    Packit Service 50c9f2
    }