00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "qiodevice.h"
00039
00196 QIODevice::QIODevice()
00197 {
00198 ioMode = 0;
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;
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;
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;
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
00550
00551
00552
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
00629 int n = size()-at();
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
00641
00642 if ( atEnd() ) {
00643 ba.resize( totalRead );
00644 break;
00645 }
00646 }
00647 return ba;
00648 } else {
00649
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 )
00706 return 0;
00707 char *p = data;
00708 while ( --maxlen && (readBlock(p,1)>0) ) {
00709 if ( *p++ == '\n' )
00710 break;
00711 }
00712 *p++ = '\0';
00713 return p - data;
00714 }
00715
00716