📄 qtextstream.cpp
字号:
/*! Constructs a text stream that operates on an existing file handle \e fh through an internal QFile device. Example: \code QTextStream cout( stdout, IO_WriteOnly ); QTextStream cin ( stdin, IO_ReadOnly ); QTextStream cerr( stderr, IO_WriteOnly ); \endcode*/QTextStream::QTextStream( FILE *fh, int mode ){ init(); setEncoding( Locale ); //### dev = new QFile; ((QFile *)dev)->open( mode, fh ); fstrm = owndev = TRUE; reset(); d->sourceType = QTextStreamPrivate::File;}/*! Destructs the text stream. The destructor does not affect the current IO device.*/QTextStream::~QTextStream(){ if ( owndev ) delete dev; delete d;}/*! Positions the read pointer at the first non-whitespace character.*/void QTextStream::skipWhiteSpace(){ ts_ungetc( eat_ws() );}/*! \fn Encoding QTextStream::encoding() const Returns the encoding mode of the stream. \sa setEncoding()*//*! Tries to read len characters from the stream and stores them in \a buf. Returns the number of characters really read. Attention: There will no QEOF appended if the read reaches the end of the file. EOF is reached when the return value does not equal \a len.*/uint QTextStream::ts_getbuf( QChar* buf, uint len ){ if( len<1 ) return 0; uint rnum=0; // the number of QChars really read if ( d && d->ungetcBuf.length() ) { while( rnum < len && rnum < d->ungetcBuf.length() ) { buf[rnum] = d->ungetcBuf.constref(rnum); rnum++; } d->ungetcBuf = d->ungetcBuf.mid( rnum ); if ( rnum >= len ) return rnum; } // we use dev->ungetch() for one of the bytes of the unicode // byte-order mark, but a local unget hack for the other byte: int ungetHack = EOF; if ( doUnicodeHeader ) { doUnicodeHeader = FALSE; //only at the top int c1 = dev->getch(); if ( c1 == EOF ) return rnum; int c2 = dev->getch(); if ( c1 == 0xfe && c2 == 0xff ) { mapper = 0; latin1 = FALSE; internalOrder = QChar::networkOrdered(); //network order } else if ( c1 == 0xff && c2 == 0xfe ) { mapper = 0; latin1 = FALSE; internalOrder = !QChar::networkOrdered(); //reverse network order } else { if ( c2 != EOF ) { dev->ungetch( c2 ); ungetHack = c1; } else { dev->ungetch( c1 ); // note that a small possible bug might hide here // here, if only the first byte of a file has made it // so far, and that first byte is half of the // byte-order mark, then the utfness will not be // detected. whether or not this is a bug depends on // taste. I can't really decide. } } }#ifndef QT_NO_TEXTCODEC if ( mapper ) { bool shortRead = FALSE; if ( !d->decoder ) d->decoder = mapper->makeDecoder(); while( rnum < len ) { QString s; bool readBlock = !( len == 1+rnum ); while ( TRUE ) { // for efficiency: normally read a whole block if ( readBlock ) { // guess buffersize; this may be wrong (too small or too // big). But we can handle this (either iterate reading // or use ungetcBuf). // Note that this might cause problems for codecs where // one byte can result in >1 Unicode Characters if bytes // are written to the stream in the meantime (loss of // synchronicity). uint rlen = len - rnum; char *cbuf = new char[ rlen ]; if ( ungetHack != EOF ) { rlen = 1+dev->readBlock( cbuf+1, rlen-1 ); cbuf[0] = (char)ungetHack; ungetHack = EOF; } else { rlen = dev->readBlock( cbuf, rlen ); } s += d->decoder->toUnicode( cbuf, rlen ); delete[] cbuf; // use buffered reading only for the first time, because we // have to get the stream synchronous again (this is easier // with single character reading) readBlock = FALSE; } // get stream (and codec) in sync int c; if ( ungetHack == EOF ) { c = dev->getch(); } else { c = ungetHack; ungetHack = EOF; } if ( c == EOF ) { shortRead = TRUE; break; } char b = c; uint lengthBefore = s.length(); s += d->decoder->toUnicode( &b, 1 ); if ( s.length() > lengthBefore ) break; // it seems we are in sync now } uint i = 0; while( rnum < len && i < s.length() ) buf[rnum++] = s.constref(i++); if ( s.length() > i ) // could be = but append is clearer d->ungetcBuf.append( s.mid( i ) ); if ( shortRead ) return rnum; } } else#endif if ( latin1 ) { if ( len == 1+rnum ) { // use this method for one character because it is more efficient // (arnt doubts whether it makes a difference, but lets it stand) int c = (ungetHack == EOF) ? dev->getch() : ungetHack; if ( c != EOF ) buf[rnum++] = (char)c; } else { if ( ungetHack != EOF ) { buf[rnum++] = (char)ungetHack; ungetHack = EOF; } char *cbuf = new char[len - rnum]; while ( !dev->atEnd() && rnum < len ) { uint rlen = len - rnum; rlen = dev->readBlock( cbuf, rlen ); uint i = 0; while( i < rlen ) buf[rnum++] = cbuf[i++]; } delete[] cbuf; } } else { // UCS-2 or UTF-16 if ( len == 1+rnum ) { int c1 = (ungetHack == EOF) ? dev->getch() : ungetHack; if ( c1 == EOF ) return rnum; int c2 = dev->getch(); if ( c2 == EOF ) return rnum; if ( isNetworkOrder() ) buf[rnum++] = QChar( c2, c1 ); else buf[rnum++] = QChar( c1, c2 ); } else { char *cbuf = new char[ 2*( len - rnum ) ]; // for paranoids: overflow possible while ( !dev->atEnd() && rnum < len ) { uint rlen = 2 * ( len-rnum ); if ( ungetHack != EOF ) { rlen = 1+dev->readBlock( cbuf+1, rlen-1 ); cbuf[0] = (char)ungetHack; ungetHack = EOF; } else { rlen = dev->readBlock( cbuf, rlen ); } // We can't use an odd number of bytes, so put it back. But // do it only if we are capable of reading more -- normally // there should not be an odd number, but the file might be // truncated or not in UTF-16... if ( (rlen & 1) == 1 ) if ( !dev->atEnd() ) dev->ungetch( cbuf[--rlen] ); uint i = 0; if ( isNetworkOrder() ) { while( i < rlen ) { buf[rnum++] = QChar( cbuf[i+1], cbuf[i] ); i+=2; } } else { while( i < rlen ) { buf[rnum++] = QChar( cbuf[i], cbuf[i+1] ); i+=2; } } } delete[] cbuf; } } return rnum;}/*! Puts one character to the stream.*/void QTextStream::ts_putc( QChar c ){#ifndef QT_NO_TEXTCODEC if ( mapper ) { int len = 1; QString s = c; QCString block = mapper->fromUnicode( s, len ); dev->writeBlock( block, len ); } else#endif if ( latin1 ) { if( c.row() ) dev->putch( '?' ); //######unknown character??? else dev->putch( c.cell() ); } else { if ( doUnicodeHeader ) { doUnicodeHeader = FALSE; ts_putc( QChar::byteOrderMark ); } if ( internalOrder ) { dev->writeBlock( (char*)&c, sizeof(QChar) ); } else if ( isNetworkOrder() ) { dev->putch(c.row()); dev->putch(c.cell()); } else { dev->putch(c.cell()); dev->putch(c.row()); } }}/*! Puts one character to the stream.*/void QTextStream::ts_putc(int ch){ ts_putc(QChar((ushort)ch));}bool QTextStream::ts_isdigit(QChar c){ return c.isDigit();}bool QTextStream::ts_isspace( QChar c ){ return c.isSpace();}void QTextStream::ts_ungetc( QChar c ){ if ( c.unicode() == 0xffff ) return; d->ungetcBuf.prepend( c );}/*! Reads \e len bytes from the stream into \e e s and returns a reference to the stream. The buffer \e s must be preallocated. Note that no encoding is done by this function. \warning The behaviour of this function is undefined unless the stream's encoding is set to Unicode or Latin1. \sa QIODevice::readBlock()*/QTextStream &QTextStream::readRawBytes( char *s, uint len ){ dev->readBlock( s, len ); return *this;}/*! Writes the \e len bytes from \e s to the stream and returns a reference to the stream. Note that no encoding is done by this function. \sa QIODevice::writeBlock()*/QTextStream &QTextStream::writeRawBytes( const char* s, uint len ){ dev->writeBlock( s, len ); return *this;}QTextStream &QTextStream::writeBlock( const char* p, uint len ){ if ( doUnicodeHeader ) { doUnicodeHeader = FALSE; if ( !mapper && !latin1 ) ts_putc( QChar::byteOrderMark ); } //All QCStrings and const char* are defined to be in Latin1 if ( !mapper && latin1 ) { dev->writeBlock( p, len ); } else if ( !mapper && internalOrder ) { QChar *u = new QChar[len]; for (uint i=0; i<len; i++) u[i] = p[i]; dev->writeBlock( (char*)u, len*sizeof(QChar) ); delete [] u; } else { for (uint i=0; i<len; i++) ts_putc( (uchar)p[i] ); } return *this;}QTextStream &QTextStream::writeBlock( const QChar* p, uint len ){ if ( !mapper && !latin1 && internalOrder ) { if ( doUnicodeHeader ) { doUnicodeHeader = FALSE; ts_putc( QChar::byteOrderMark ); } dev->writeBlock( (char*)p, sizeof(QChar)*len ); } else { for (uint i=0; i<len; i++) ts_putc( p[i] ); } return *this;}/*! Resets the text stream. <ul> <li> All flags are set to 0. <li> The field width is set to 0. <li> The fill character is set to ' ' (space). <li> The precision is set to 6. </ul> \sa setf(), width(), fill(), precision()*/void QTextStream::reset(){ fflags = 0; fwidth = 0; fillchar = ' '; fprec = 6;}/*! \fn QIODevice *QTextStream::device() const Returns the IO device currently set. \sa setDevice(), unsetDevice()*//*! Sets the IO device to \a iod. \sa device(), unsetDevice()*/void QTextStream::setDevice( QIODevice *iod ){ if ( owndev ) { delete dev; owndev = FALSE; } dev = iod; d->sourceType = QTextStreamPrivate::IODevice;}/*! Unsets the IO device. Equivalent to setDevice( 0 ). \sa device(), setDevice()*/void QTextStream::unsetDevice(){ setDevice( 0 ); d->sourceType = QTextStreamPrivate::NotSet;}/*! \fn bool QTextStream::atEnd() const Returns TRUE if the IO device has reached the end position (end of stream or file) or if there is no IO device set. Returns FALSE if the current position of the read/write head of the IO device is somewhere before the end position. \sa QIODevice::atEnd()*//*!\fn bool QTextStream::eof() const \obsolete This function has been renamed to atEnd(). \sa QIODevice::atEnd()*//***************************************************************************** QTextStream read functions *****************************************************************************//*! Reads a \c char from the stream and returns a reference to the stream. Note that whitespace is skipped.*/QTextStream &QTextStream::operator>>( char &c ){ CHECK_STREAM_PRECOND c = eat_ws(); return *this;}/*! Reads a \c char from the stream and returns a reference to the stream. Note that whitespace is \e not skipped.*/QTextStream &QTextStream::operator>>( QChar &c ){ CHECK_STREAM_PRECOND c = ts_getc(); return *this;}ulong QTextStream::input_bin(){ ulong val = 0; QChar ch = eat_ws(); int dv = ch.digitValue(); while ( dv == 0 || dv == 1 ) { val = ( val << 1 ) + dv; ch = ts_getc(); dv = ch.digitValue(); } if ( ch != QEOF ) ts_ungetc( ch ); return val;}ulong QTextStream::input_oct(){ ulong val = 0; QChar ch = eat_ws(); int dv = ch.digitValue(); while ( dv >= 0 && dv <= 7 ) { val = ( val << 3 ) + dv; ch = ts_getc(); dv = ch.digitValue(); } if ( dv == 8 || dv == 9 ) { while ( ts_isdigit(ch) ) ch = ts_getc(); } if ( ch != QEOF ) ts_ungetc( ch ); return val;}ulong QTextStream::input_dec(){ ulong val = 0; QChar ch = eat_ws(); int dv = ch.digitValue(); while ( ts_isdigit(ch) ) { val = val * 10 + dv; ch = ts_getc(); dv = ch.digitValue(); } if ( ch != QEOF ) ts_ungetc( ch ); return val;}ulong QTextStream::input_hex(){ ulong val = 0; QChar ch = eat_ws(); char c = ch; while ( isxdigit(c) ) { val <<= 4; if ( ts_isdigit(c) ) val += c - '0'; else val += 10 + tolower(c) - 'a'; c = ch = ts_getc(); } if ( ch != QEOF ) ts_ungetc( ch ); return val;}long QTextStream::input_int(){ long val; QChar ch; char c;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -