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

qiodevice.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** $Id: qiodevice.cpp,v 1.2 2003/07/10 02:40:12 llornkcor Exp $
00003 **
00004 ** Implementation of QIODevice class
00005 **
00006 ** Created : 940913
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 "qiodevice.h"
00039 
00196 QIODevice::QIODevice()
00197 {
00198     ioMode = 0;                                 // initial mode
00199     ioSt = IO_Ok;
00200     ioIndex = 0;
00201 }
00202 
00207 QIODevice::~QIODevice()
00208 {
00209 }
00210 
00211 
00426 void QIODevice::setType( int t )
00427 {
00428 #if defined(QT_CHECK_RANGE)
00429     if ( (t & IO_TypeMask) != t )
00430         qWarning( "QIODevice::setType: Specified type out of range" );
00431 #endif
00432     ioMode &= ~IO_TypeMask;                     // reset type bits
00433     ioMode |= t;
00434 }
00435 
00441 void QIODevice::setMode( int m )
00442 {
00443 #if defined(QT_CHECK_RANGE)
00444     if ( (m & IO_ModeMask) != m )
00445         qWarning( "QIODevice::setMode: Specified mode out of range" );
00446 #endif
00447     ioMode &= ~IO_ModeMask;                     // reset mode bits
00448     ioMode |= m;
00449 }
00450 
00456 void QIODevice::setState( int s )
00457 {
00458 #if defined(QT_CHECK_RANGE)
00459     if ( ((uint)s & IO_StateMask) != (uint)s )
00460         qWarning( "QIODevice::setState: Specified state out of range" );
00461 #endif
00462     ioMode &= ~IO_StateMask;                    // reset state bits
00463     ioMode |= (uint)s;
00464 }
00465 
00471 void QIODevice::setStatus( int s )
00472 {
00473     ioSt = s;
00474 }
00475 
00476 
00542 QIODevice::Offset QIODevice::at() const
00543 {
00544     return ioIndex;
00545 }
00546 
00547 
00548 /*
00549     The following is a "bad" overload, since it does "not behave essentially
00550     the same" like the above. So don't use  \overload in the documentation of
00551     this function and we have to live with the qdoc warning which is generated
00552     for this.
00553 */
00562 bool QIODevice::at( Offset pos )
00563 {
00564 #if defined(QT_CHECK_RANGE)
00565     if ( pos > size() ) {
00566 #if defined(QT_LARGEFILE_SUPPORT) && defined(QT_ABI_64BITOFFSET)
00567         qWarning( "QIODevice::at: Index %llu out of range", pos );
00568 #else
00569         qWarning( "QIODevice::at: Index %lu out of range", pos );
00570 #endif
00571         return FALSE;
00572     }
00573 #endif
00574     ioIndex = pos;
00575     return TRUE;
00576 }
00577 
00583 bool QIODevice::atEnd() const
00584 {
00585     if ( isSequentialAccess() || isTranslated() ) {
00586         QIODevice* that = (QIODevice*)this;
00587         int c = that->getch();
00588         bool result = c < 0;
00589         that->ungetch(c);
00590         return result;
00591     } else {
00592         return at() == size();
00593     }
00594 }
00595 
00625 QByteArray QIODevice::readAll()
00626 {
00627     if ( isDirectAccess() ) {
00628         // we know the size
00629         int n = size()-at(); // ### fix for 64-bit or large files?
00630         int totalRead = 0;
00631         QByteArray ba( n );
00632         char* c = ba.data();
00633         while ( n ) {
00634             int r = readBlock( c, n );
00635             if ( r < 0 )
00636                 return QByteArray();
00637             n -= r;
00638             c += r;
00639             totalRead += r;
00640             // If we have a translated file, then it is possible that
00641             // we read less bytes than size() reports
00642             if ( atEnd() ) {
00643                 ba.resize( totalRead );
00644                 break;
00645             }
00646         }
00647         return ba;
00648     } else {
00649         // read until we reach the end
00650         const int blocksize = 512;
00651         int nread = 0;
00652         QByteArray ba;
00653         while ( !atEnd() ) {
00654             ba.resize( nread + blocksize );
00655             int r = readBlock( ba.data()+nread, blocksize );
00656             if ( r < 0 )
00657                 return QByteArray();
00658             nread += r;
00659         }
00660         ba.resize( nread );
00661         return ba;
00662     }
00663 }
00664 
00684 Q_LONG QIODevice::writeBlock( const QByteArray& data )
00685 {
00686     return writeBlock( data.data(), data.size() );
00687 }
00688 
00703 Q_LONG QIODevice::readLine( char *data, Q_ULONG maxlen )
00704 {
00705     if ( maxlen == 0 )                          // application bug?
00706         return 0;
00707     char *p = data;
00708     while ( --maxlen && (readBlock(p,1)>0) ) {  // read one byte at a time
00709         if ( *p++ == '\n' )                     // end of line
00710             break;
00711     }
00712     *p++ = '\0';
00713     return p - data;
00714 }
00715 
00716 

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