📄 qtextstream.cpp
字号:
\overload Writes a \c short integer \a i to the stream and returns a reference to the stream.*/QTextStream &QTextStream::operator<<( signed short i ){ return output_int( I_SHORT | I_SIGNED, i, i < 0 );}/*! \overload Writes an \c unsigned \c short integer \a i to the stream and returns a reference to the stream.*/QTextStream &QTextStream::operator<<( unsigned short i ){ return output_int( I_SHORT | I_UNSIGNED, i, FALSE );}/*! \overload Writes an \c int \a i to the stream and returns a reference to the stream.*/QTextStream &QTextStream::operator<<( signed int i ){ return output_int( I_INT | I_SIGNED, i, i < 0 );}/*! \overload Writes an \c unsigned \c int \a i to the stream and returns a reference to the stream.*/QTextStream &QTextStream::operator<<( unsigned int i ){ return output_int( I_INT | I_UNSIGNED, i, FALSE );}/*! \overload Writes a \c long \c int \a i to the stream and returns a reference to the stream.*/QTextStream &QTextStream::operator<<( signed long i ){ return output_int( I_LONG | I_SIGNED, i, i < 0 );}/*! \overload Writes an \c unsigned \c long \c int \a i to the stream and returns a reference to the stream.*/QTextStream &QTextStream::operator<<( unsigned long i ){ return output_int( I_LONG | I_UNSIGNED, i, FALSE );}/*! \overload Writes a \c float \a f to the stream and returns a reference to the stream.*/QTextStream &QTextStream::operator<<( float f ){ return *this << (double)f;}extern void qt_fix_double(char *);/*! \overload Writes a \c double \a f to the stream and returns a reference to the stream.*/QTextStream &QTextStream::operator<<( double f ){ CHECK_STREAM_PRECOND char buf[64]; char f_char; char format[16]; if ( (flags()&floatfield) == fixed ) f_char = 'f'; else if ( (flags()&floatfield) == scientific ) f_char = (flags() & uppercase) ? 'E' : 'e'; else f_char = (flags() & uppercase) ? 'G' : 'g'; register char *fs = format; // generate format string *fs++ = '%'; // "%.<prec>l<f_char>" *fs++ = '.'; int prec = precision(); if ( prec > 99 ) prec = 99; if ( prec >= 10 ) { *fs++ = prec / 10 + '0'; *fs++ = prec % 10 + '0'; } else { *fs++ = prec + '0'; } *fs++ = 'l'; *fs++ = f_char; *fs = '\0'; sprintf( buf, format, f ); // convert to text qt_fix_double(buf); if ( fwidth ) // padding *this << (const char*)buf; else // just write it writeBlock( buf, qstrlen(buf) ); return *this;}/*! \overload Writes a string to the stream and returns a reference to the stream. The string \a s is assumed to be Latin1 encoded independent of the Encoding set for the QTextStream.*/QTextStream &QTextStream::operator<<( const char* s ){ CHECK_STREAM_PRECOND char padbuf[48]; uint len = qstrlen( s ); // don't write null terminator if ( fwidth ) { // field width set int padlen = fwidth - len; fwidth = 0; // reset width if ( padlen > 0 ) { char *ppad; if ( padlen > 46 ) { // create extra big fill buffer ppad = new char[padlen]; Q_CHECK_PTR( ppad ); } else { ppad = padbuf; } memset( ppad, (char)fillchar, padlen ); // fill with fillchar if ( !(flags() & left) ) { writeBlock( ppad, padlen ); padlen = 0; } writeBlock( s, len ); if ( padlen ) writeBlock( ppad, padlen ); if ( ppad != padbuf ) // delete extra big fill buf delete[] ppad; return *this; } } writeBlock( s, len ); return *this;}/*! \overload Writes \a s to the stream and returns a reference to the stream. The string \a s is assumed to be Latin1 encoded independent of the Encoding set for the QTextStream.*/QTextStream &QTextStream::operator<<( const QCString & s ){ return operator<<(s.data());}/*! \overload Writes \a s to the stream and returns a reference to the stream.*/QTextStream &QTextStream::operator<<( const QString& s ){ if ( !mapper && latin1 ) return operator<<(s.latin1()); CHECK_STREAM_PRECOND QString s1 = s; if ( fwidth ) { // field width set if ( !(flags() & left) ) { s1 = s.rightJustify(fwidth, (char)fillchar); } else { s1 = s.leftJustify(fwidth, (char)fillchar); } fwidth = 0; // reset width } writeBlock( s1.unicode(), s1.length() ); return *this;}/*! \overload Writes a pointer to the stream and returns a reference to the stream. The \a ptr is output as an unsigned long hexadecimal integer.*/QTextStream &QTextStream::operator<<( void *ptr ){ int f = flags(); setf( hex, basefield ); setf( showbase ); unsetf( uppercase ); output_int( I_LONG | I_UNSIGNED, (ulong)ptr, FALSE ); flags( f ); return *this;}/*! \fn int QTextStream::flags() const Returns the current stream flags. The default value is 0. \table \header \i Flag \i Meaning \row \i \c skipws \i Not currently used; whitespace always skipped \row \i \c left \i Numeric fields are left-aligned \row \i \c right \i Not currently used (by default, numerics are right-aligned) \row \i \c internal \i Puts any padding spaces between +/- and value \row \i \c bin \i Output \e and input only in binary \row \i \c oct \i Output \e and input only in octal \row \i \c dec \i Output \e and input only in decimal \row \i \c hex \i Output \e and input only in hexadecimal \row \i \c showbase \i Annotates numeric outputs with 0b, 0, or 0x if in \c bin, \c oct, or \c hex format \row \i \c showpoint \i Not currently used \row \i \c uppercase \i Uses 0B and 0X rather than 0b and 0x \row \i \c showpos \i Shows + for positive numeric values \row \i \c scientific \i Uses scientific notation for floating point values \row \i \c fixed \i Uses fixed-point notation for floating point values \endtable Note that unless \c bin, \c oct, \c dec, or \c hex is set, the input base is octal if the value starts with 0, hexadecimal if it starts with 0x, binary if it starts with 0b, and decimal otherwise. \sa setf(), unsetf()*//*! \fn int QTextStream::flags( int f ) \overload Sets the stream flags to \a f. Returns the previous stream flags. \sa setf(), unsetf(), flags()*//*! \fn int QTextStream::setf( int bits ) Sets the stream flag bits \a bits. Returns the previous stream flags. Equivalent to \c{flags( flags() | bits )}. \sa setf(), unsetf()*//*! \fn int QTextStream::setf( int bits, int mask ) \overload Sets the stream flag bits \a bits with a bit mask \a mask. Returns the previous stream flags. Equivalent to \c{flags( (flags() & ~mask) | (bits & mask) )}. \sa setf(), unsetf()*//*! \fn int QTextStream::unsetf( int bits ) Clears the stream flag bits \a bits. Returns the previous stream flags. Equivalent to \c{flags( flags() & ~mask )}. \sa setf()*//*! \fn int QTextStream::width() const Returns the field width. The default value is 0.*//*! \fn int QTextStream::width( int w ) \overload Sets the field width to \a w. Returns the previous field width.*//*! \fn int QTextStream::fill() const Returns the fill character. The default value is ' ' (space).*//*! \overload int QTextStream::fill( int f ) Sets the fill character to \a f. Returns the previous fill character.*//*! \fn int QTextStream::precision() const Returns the precision. The default value is 6.*//*! \fn int QTextStream::precision( int p ) \overload Sets the precision to \a p. Returns the previous precision setting.*/ /***************************************************************************** QTextStream manipulators *****************************************************************************/QTextStream &bin( QTextStream &s ){ s.setf(QTS::bin,QTS::basefield); return s;}QTextStream &oct( QTextStream &s ){ s.setf(QTS::oct,QTS::basefield); return s;}QTextStream &dec( QTextStream &s ){ s.setf(QTS::dec,QTS::basefield); return s;}QTextStream &hex( QTextStream &s ){ s.setf(QTS::hex,QTS::basefield); return s;}QTextStream &endl( QTextStream &s ){ return s << '\n';}QTextStream &flush( QTextStream &s ){ if ( s.device() ) s.device()->flush(); return s;}QTextStream &ws( QTextStream &s ){ s.skipWhiteSpace(); return s;}QTextStream &reset( QTextStream &s ){ s.reset(); return s;}/*! \class QTextIStream qtextstream.h \reentrant \brief The QTextIStream class is a convenience class for input streams. \ingroup io \ingroup text This class provides a shorthand for creating simple input \l{QTextStream}s without having to pass a \e mode argument to the constructor. This class makes it easy, for example, to write things like this: \code QString data = "123 456"; int a, b; QTextIStream(&data) >> a >> b; \endcode \sa QTextOStream*//*! \fn QTextIStream::QTextIStream( const QString *s ) Constructs a stream to read from the string \a s.*//*! \fn QTextIStream::QTextIStream( QByteArray ba ) Constructs a stream to read from the array \a ba.*//*! \fn QTextIStream::QTextIStream( FILE *f ) Constructs a stream to read from the file \a f.*//*! \class QTextOStream \reentrant \brief The QTextOStream class is a convenience class for output streams. \ingroup io \ingroup text This class provides a shorthand for creating simple output \l{QTextStream}s without having to pass a \e mode argument to the constructor. This makes it easy for example, to write things like this: \code QString result; QTextOStream(&result) << "pi = " << 3.14; \endcode*//*! \fn QTextOStream::QTextOStream( QString *s ) Constructs a stream to write to string \a s.*//*! \fn QTextOStream::QTextOStream( QByteArray ba ) Constructs a stream to write to the array \a ba.*//*! \fn QTextOStream::QTextOStream( FILE *f ) Constructs a stream to write to the file \a f.*//*! Sets the encoding of this stream to \a e, where \a e is one of the following values: \table \header \i Encoding \i Meaning \row \i Locale \i Uses local file format (Latin1 if locale is not set), but autodetecting Unicode(utf16) on input. \row \i Unicode \i Uses Unicode(utf16) for input and output. Output will be written in the order most efficient for the current platform (i.e. the order used internally in QString). \row \i UnicodeUTF8 \i Using Unicode(utf8) for input and output. If you use it for input it will autodetect utf16 and use it instead of utf8. \row \i Latin1 \i ISO-8859-1. Will not autodetect utf16. \row \i UnicodeNetworkOrder \i Uses network order Unicode(utf16) for input and output. Useful when reading Unicode data that does not start with the byte order marker. \row \i UnicodeReverse \i Uses reverse network order Unicode(utf16) for input and output. Useful when reading Unicode data that does not start with the byte order marker or when writing data that should be read by buggy Windows applications. \row \i RawUnicode \i Like Unicode, but does not write the byte order marker nor does it auto-detect the byte order. Useful only when writing to non-persistent storage used by a single process. \endtable \c Locale and all Unicode encodings, except \c RawUnicode, will look at the first two bytes in an input stream to determine the byte order. The initial byte order marker will be stripped off before data is read. Note that this function should be called before any data is read to or written from the stream. \sa setCodec()*/void QTextStream::setEncoding( Encoding e ){ if ( d->sourceType == QTextStreamPrivate::String ) return; switch ( e ) { case Unicode: mapper = 0; latin1 = FALSE; doUnicodeHeader = TRUE; internalOrder = TRUE; networkOrder = QChar::networkOrdered(); break; case UnicodeUTF8:#ifndef QT_NO_TEXTCODEC mapper = QTextCodec::codecForMib( 106 ); latin1 = FALSE; doUnicodeHeader = TRUE; internalOrder = TRUE; networkOrder = QChar::networkOrdered();#else mapper = 0; latin1 = TRUE; doUnicodeHeader = TRUE;#endif break; case UnicodeNetworkOrder: mapper = 0; latin1 = FALSE; doUnicodeHeader = TRUE; internalOrder = QChar::networkOrdered(); networkOrder = TRUE; break; case UnicodeReverse: mapper = 0; latin1 = FALSE; doUnicodeHeader = TRUE; internalOrder = !QChar::networkOrdered(); networkOrder = FALSE; break; case RawUnicode: mapper = 0; latin1 = FALSE; doUnicodeHeader = FALSE; internalOrder = TRUE; networkOrder = QChar::networkOrdered(); break; case Locale: latin1 = TRUE; // fallback to Latin-1#ifndef QT_NO_TEXTCODEC mapper = QTextCodec::codecForLocale(); // optimized Latin-1 processing#if defined(Q_OS_WIN32) if ( GetACP() == 1252 ) mapper = 0;#endif if ( mapper && mapper->mibEnum() == 4 )#endif mapper = 0; doUnicodeHeader = TRUE; // If it reads as Unicode, accept it break; case Latin1: mapper = 0; doUnicodeHeader = FALSE; latin1 = TRUE; break; }}#ifndef QT_NO_TEXTCODEC/*! Sets the codec for this stream to \a codec. Will not try to autodetect Unicode. Note that this function should be called before any data is read to/written from the stream. \sa setEncoding(), codec()*/void QTextStream::setCodec( QTextCodec *codec ){ if ( d->sourceType == QTextStreamPrivate::String ) return; // QString does not need any codec mapper = codec; latin1 = ( codec->mibEnum() == 4 ); if ( latin1 ) mapper = 0; doUnicodeHeader = FALSE;}/*! Returns the codec actually used for this stream. If Unicode is automatically detected in input, a codec with \link QTextCodec::name() name() \endlink "ISO-10646-UCS-2" is returned. \sa setCodec()*/QTextCodec *QTextStream::codec(){ if ( mapper ) { return mapper; } else { // 4 is "ISO 8859-1", 1000 is "ISO-10646-UCS-2" return QTextCodec::codecForMib( latin1 ? 4 : 1000 ); }}#endif#endif // QT_NO_TEXTSTREAM
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -