⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 q3textstream.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		    c = dev->getch();		} else {		    c = ungetHack;		    ungetHack = EOF;		}		if ( c == EOF ) {		    shortRead = TRUE;		    break;		}		char b = c;		uint lengthBefore = s.length();		s += mapper->toUnicode( &b, 1, &mapperWriteState );		if ( uint(s.length()) > lengthBefore )		    break; // it seems we are in sync now	    }	    uint i = 0;	    uint end = QMIN( len-rnum, uint(s.length()) );	    while( i < end ) {		*buf = s.constref(i++);		buf++;	    }	    rnum += end;	    if ( uint(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 = QLatin1Char((char)c);		buf++;		rnum++;	    }	} else {	    if ( ungetHack != EOF ) {		*buf = QLatin1Char((char)ungetHack);		buf++;		rnum++;		ungetHack = EOF;	    }	    char *cbuf = new char[len - rnum];	    while ( !dev->atEnd() && rnum < len ) {		uint rlen = len - rnum;		rlen = dev->readBlock( cbuf, rlen );		char *it = cbuf;		char *end = cbuf + rlen;		while ( it < end ) {		    *buf = QLatin1Char(*it);		    buf++;		    it++;		}		rnum += rlen;	    }	    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 ( networkOrder ) {		*buf = QChar( c2, c1 );	    } else {		*buf = QChar( c1, c2 );	    }	    buf++;	    rnum++;	} 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 ( networkOrder ) {		    while( i < rlen ) {			*buf = QChar( cbuf[i+1], cbuf[i] );			buf++;			i+=2;		    }		} else {		    while( i < rlen ) {			*buf = QChar( cbuf[i], cbuf[i+1] );			buf++;			i+=2;		    }		}		rnum += i/2;	    }	    delete[] cbuf;	}    }    return rnum;}/*!    Tries to read one line, but at most len characters from the stream    and stores them in \a buf.    Returns the number of characters really read. Newlines are not    stripped.    There will be a QEOF appended if the read reaches the end of file;    this is different to ts_getbuf().    This function works only if a newline (as byte) is also a newline    (as resulting character) since it uses QIODevice::readLine(). So    use it only for such codecs where this is true!    This function is (almost) a no-op for UTF 16. Don't use it if    doUnicodeHeader is TRUE!*/uint Q3TextStream::ts_getline( QChar* buf ){    uint rnum=0;   // the number of QChars really read    char cbuf[ getline_buf_size+1 ];    if ( d && d->ungetcBuf.length() ) {	while( rnum < getline_buf_size && rnum < uint(d->ungetcBuf.length()) ) {	    buf[rnum] = d->ungetcBuf.constref(rnum);	    rnum++;	}	d->ungetcBuf = d->ungetcBuf.mid( rnum );	if ( rnum >= getline_buf_size )	    return rnum;    }#ifndef QT_NO_TEXTCODEC    if ( mapper ) {	QString s;	bool readBlock = TRUE;	for (;;) {	    // for efficiency: try to read a line	    if ( readBlock ) {		int rlen = getline_buf_size - rnum;		rlen = dev->readLine( cbuf, rlen+1 );		if ( rlen == -1 )		    rlen = 0;		s  += mapper->toUnicode( cbuf, rlen, &mapperWriteState );		readBlock = FALSE;	    }	    if ( dev->atEnd()		    || s.at( s.length()-1 ) == QLatin1Char('\n')		    || s.at( s.length()-1 ) == QLatin1Char('\r')	       ) {		break;	    } else {		// get stream (and codec) in sync		int c;		c = dev->getch();		if ( c == EOF ) {		    break;		}		char b = c;		uint lengthBefore = s.length();		s  += mapper->toUnicode( &b, 1, &mapperWriteState );		if ( uint(s.length()) > lengthBefore )		    break; // it seems we are in sync now	    }	}	uint i = 0;	while( rnum < getline_buf_size && i < uint(s.length()) )	    buf[rnum++] = s.constref(i++);	if ( uint(s.length()) > i )	    // could be = but append is clearer	    d->ungetcBuf.append( s.mid( i ) );	if ( rnum < getline_buf_size && dev->atEnd() )	    buf[rnum++] = QEOF;    } else#endif    if ( latin1 ) {	int rlen = getline_buf_size - rnum;	rlen = dev->readLine( cbuf, rlen+1 );	if ( rlen == -1 )	    rlen = 0;	char *end = cbuf+rlen;	char *it = cbuf;	buf +=rnum;	while ( it != end ) {	    buf->setCell( *(it++) );	    buf->setRow( 0 );	    buf++;	}	rnum += rlen;	if ( rnum < getline_buf_size && dev->atEnd() )	    buf[1] = QEOF;    }    return rnum;}/*!    Puts one character into the stream.*/void Q3TextStream::ts_putc( QChar c ){#ifndef QT_NO_TEXTCODEC    if ( mapper ) {	int len = 1;	QString s = c;	Q3CString block = mapper->fromUnicode( s.data(), len );//, &mapperReadState );	dev->writeBlock( block );    } 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 ) {	    // this case is needed by QStringBuffer	    dev->writeBlock( (char*)&c, sizeof(QChar) );	} else if ( networkOrder ) {	    dev->putch( c.row() );	    dev->putch( c.cell() );	} else {	    dev->putch( c.cell() );	    dev->putch( c.row() );	}    }}/*!    Puts one character into the stream.*/void Q3TextStream::ts_putc( int ch ){    ts_putc( QChar((ushort)ch) );}bool Q3TextStream::ts_isdigit( QChar c ){    return c.isDigit();}bool Q3TextStream::ts_isspace( QChar c ){    return c.isSpace();}void Q3TextStream::ts_ungetc( QChar c ){    if ( c.unicode() == 0xffff )	return;    d->ungetcBuf.prepend( c );}/*!    \since 4.2    Reads \a len bytes from the stream into \a s and returns a    reference to the stream.    The buffer \a s must be preallocated.    Note that no encoding is done by this function.    \warning The behavior of this function is undefined unless the    stream's encoding is set to Unicode or Latin1.    \sa QIODevice::readBlock()*/Q3TextStream &Q3TextStream::readRawBytes( char *s, uint len ){    dev->readBlock( s, len );    return *this;}/*!    \since 4.2    Writes the \a len bytes from \a s to the stream and returns a    reference to the stream.    Note that no encoding is done by this function.    \sa QIODevice::writeBlock()*/Q3TextStream &Q3TextStream::writeRawBytes( const char* s, uint len ){    dev->writeBlock( s, len );    return *this;}Q3TextStream &Q3TextStream::writeBlock( const char* p, uint len ){    if ( doUnicodeHeader ) {	doUnicodeHeader = FALSE;	if ( !mapper && !latin1 ) {	    ts_putc( QChar::ByteOrderMark );        }    }    // QCString and const char * are treated as Latin-1    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] = QLatin1Char(p[i]);	dev->writeBlock( (char*)u, len * sizeof(QChar) );	delete [] u;    }#ifndef QT_NO_TEXTCODEC    else if (mapper) {        QString s = QString::fromLatin1(p, len);        int l = len;        Q3CString block = mapper->fromUnicode(s.data(), l );//, &mapperReadState );        dev->writeBlock( block );    }#endif    else {	for ( uint i = 0; i < len; i++ )	    ts_putc( (uchar)p[i] );    }    return *this;}Q3TextStream &Q3TextStream::writeBlock( const QChar* p, uint len ){#ifndef QT_NO_TEXTCODEC    if ( mapper ) {	QConstString s( p, len );	int l = len;	Q3CString block = mapper->fromUnicode( s.string().data(), l );//, &mapperReadState );	dev->writeBlock( block );    } else#endif    if ( latin1 ) {	dev->write(QString( p, len ).toLatin1());    } else if ( 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;}/*!    \since 4.2    Resets the text stream.    \list    \i All flags are set to 0.    \i The field width is set to 0.    \i The fill character is set to ' ' (Space).    \i The precision is set to 6.    \endlist    \sa setf(), width(), fill(), precision()*/void Q3TextStream::reset(){    fflags = 0;    fwidth = 0;    fillchar = ' ';    fprec = 6;}/*!    \fn QIODevice *Q3TextStream::device() const    \since 4.2    Returns the IO device currently set.    \sa setDevice(), unsetDevice()*//*!    \since 4.2    Sets the IO device to \a iod.    \sa device(), unsetDevice()*/void Q3TextStream::setDevice( QIODevice *iod ){    if ( owndev ) {	delete dev;	owndev = FALSE;    }    dev = iod;    d->sourceType = Q3TextStreamPrivate::IODevice;}/*!    \since 4.2    Unsets the IO device. Equivalent to setDevice( 0 ).    \sa device(), setDevice()*/void Q3TextStream::unsetDevice(){    setDevice( 0 );    d->sourceType = Q3TextStreamPrivate::NotSet;}/*!    \fn bool Q3TextStream::atEnd() const    \since 4.2    Returns TRUE if the IO device has reached the end position (end of    the stream or file) or if there is no IO device set; otherwise    returns FALSE.    \sa QIODevice::atEnd()*//*!\fn bool Q3TextStream::eof() const  \obsolete  This function has been renamed to atEnd().  \sa QIODevice::atEnd()*//*****************************************************************************  Q3TextStream read functions *****************************************************************************//*!    \overload    Reads a char \a c from the stream and returns a reference to the    stream. Note that whitespace is skipped.*/Q3TextStream &Q3TextStream::operator>>( char &c ){    CHECK_STREAM_PRECOND    c = eat_ws().toLatin1();    return *this;}/*!    Reads a char \a c from the stream and returns a reference to the    stream. Note that whitespace is \e not skipped.*/Q3TextStream &Q3TextStream::operator>>( QChar &c ){    CHECK_STREAM_PRECOND    c = ts_getc();    return *this;}ulong Q3TextStream::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 Q3TextStream::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 Q3TextStream::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 Q3TextStream::input_hex(){    ulong val = 0;    QChar ch = eat_ws();    char c = ch.toLatin1();    while ( isxdigit((uchar) c) ) {	val <<= 4;	if ( ts_isdigit(QLatin1Char(c)) )	    val += c - '0';	else	    val += 10 + tolower( (uchar) c ) - 'a';        ch = ts_getc();	c = ch.toLatin1();    }    if ( ch != QEOF )	ts_ungetc( ch );    return val;}long Q3TextStream::input_int(){    long val;    QChar ch;    char c;    switch ( flags() & basefield ) {    case bin:	val = (long)input_bin();	break;    case oct:	val = (long)input_oct();	break;    case dec:        ch = eat_ws();	c = ch.toLatin1();	if ( ch == QEOF ) {	    val = 0;	} else {	    if ( !(c == '-' || c == '+') )		ts_ungetc( ch );	    if ( c == '-' ) {		ulong v = input_dec();		if ( v ) {		// ensure that LONG_MIN can be read		    v--;		    val = -((long)v) - 1;		} else {		    val = 0;		}	    } else {		val = (long)input_dec();	    }	}	break;    case hex:	val = (long)input_hex();	break;    default:	val = 0;        ch = eat_ws();	c = ch.toLatin1();	if ( c == '0' ) {		// bin, oct or hex            ch = ts_getc();	    c = ch.toLatin1();	    if ( tolower((uchar) c) == 'x' )		val = (long)input_hex();	    else if ( tolower((uchar) c) == 'b' )		val = (long)input_bin();	    else {			// octal

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -