📄 qtextstream.cpp
字号:
InputN = 6, InputF = 7, InputA = 8, InputT = 9 }; static uchar table[13][10] = { // None InputSign InputDigit InputDot InputExp InputI InputN InputF InputA InputT { 0, Sign, Mantissa, Dot, 0, Inf1, Nan1, 0, 0, 0 }, // 0 Init { 0, 0, Mantissa, Dot, 0, Inf1, Nan1, 0, 0, 0 }, // 1 Sign { Done, Done, Mantissa, Dot, ExpMark, 0, 0, 0, 0, 0 }, // 2 Mantissa { 0, 0, Abscissa, 0, 0, 0, 0, 0, 0, 0 }, // 3 Dot { Done, Done, Abscissa, Done, ExpMark, 0, 0, 0, 0, 0 }, // 4 Abscissa { 0, ExpSign, Exponent, 0, 0, 0, 0, 0, 0, 0 }, // 5 ExpMark { 0, 0, Exponent, 0, 0, 0, 0, 0, 0, 0 }, // 6 ExpSign { Done, Done, Exponent, Done, Done, 0, 0, 0, 0, 0 }, // 7 Exponent { 0, 0, 0, 0, 0, 0, 0, 0, Nan2, 0 }, // 8 Nan1 { 0, 0, 0, 0, 0, 0, NanInf, 0, 0, 0 }, // 9 Nan2 { 0, 0, 0, 0, 0, 0, Inf2, 0, 0, 0 }, // 10 Inf1 { 0, 0, 0, 0, 0, 0, 0, NanInf, 0, 0 }, // 11 Inf2 { Done, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 11 NanInf }; ParserState state = Init; InputToken input = None; scan(0, 0, 0, NotSpace); consumeLastToken(); const int BufferSize = 128; char buf[BufferSize]; int i = 0; QChar c; while (getChar(&c)) { switch (c.unicode()) { case '+': case '-': input = InputSign; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': input = InputDigit; break; case '.': input = InputDot; break; case 'e': case 'E': input = InputExp; break; case 'i': case 'I': input = InputI; break; case 'n': case 'N': input = InputN; break; case 'f': case 'F': input = InputF; break; case 'a': case 'A': input = InputA; break; case 't': case 'T': input = InputT; break; default: input = None; break; } state = ParserState(table[state][input]); if (state == Init || state == Done || i > (BufferSize - 5)) { ungetChar(c); if (i > (BufferSize - 5)) { // ignore rest of digits while (getChar(&c)) { if (!c.isDigit()) { ungetChar(c); break; } } } break; } buf[i++] = c.toLatin1(); } if (i == 0) return false; if (!f) return true; // ### Number parsing should really be handled by QLocale. char c0 = buf[0] | 32; // tolower char c1 = buf[1] | 32; // tolower bool sign = true; if (c0 == '+' || c0 == '-') { sign = (c0 == '+'); c0 = c1; } if (c0 == 'i') { *f = sign ? qInf() : -qInf(); } else if (c0 == 'n') { *f = qQNaN(); } else { buf[i] = '\0'; *f = strtod(buf, 0); } return true;}/*! Reads a character from the stream and stores it in \a c. Returns a reference to the QTextStream, so several operators can be nested. Example: \code QTextStream in(file); QChar ch1, ch2, ch3; in >> ch1 >> ch2 >> ch3; \endcode Whitespace is \e not skipped.*/QTextStream &QTextStream::operator>>(QChar &c){ Q_D(QTextStream); CHECK_VALID_STREAM(*this); d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); if (!d->getChar(&c)) setStatus(ReadPastEnd); return *this;}/*! \overload Reads a character from the stream and stores it in \a c. The character from the stream is converted to ISO-5589-1 before it is stored. \sa QChar::toLatin1()*/QTextStream &QTextStream::operator>>(char &c){ QChar ch; *this >> ch; c = ch.toLatin1(); return *this;}/*! Reads an integer from the stream and stores it in \a i, then returns a reference to the QTextStream. The number is cast to the correct type before it is stored. If no number was detected on the stream, \a i is set to 0. By default, QTextStream will attempt to detect the base of the number using the following rules: \table \header \o Prefix \o Base \row \o "0b" or "0B" \o 2 (binary) \row \o "0" followed by "0-7" \o 8 (octal) \row \o "0" otherwise \o 10 (decimal) \row \o "0x" or "0X" \o 16 (hexadecimal) \row \o "1" to "9" \o 10 (decimal) \endtable By calling setIntegerBase(), you can specify the integer base explicitly. This will disable the auto-detection, and speed up QTextStream slightly. Leading whitespace is skipped.*/QTextStream &QTextStream::operator>>(signed short &i){ IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(signed short);}/*! \overload Stores the integer in the unsigned short \a i.*/QTextStream &QTextStream::operator>>(unsigned short &i){ IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(unsigned short);}/*! \overload Stores the integer in the signed int \a i.*/QTextStream &QTextStream::operator>>(signed int &i){ IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(signed int);}/*! \overload Stores the integer in the unsigned int \a i.*/QTextStream &QTextStream::operator>>(unsigned int &i){ IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(unsigned int);}/*! \overload Stores the integer in the signed long \a i.*/QTextStream &QTextStream::operator>>(signed long &i){ IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(signed long);}/*! \overload Stores the integer in the unsigned long \a i.*/QTextStream &QTextStream::operator>>(unsigned long &i){ IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(unsigned long);}/*! \overload Stores the integer in the qlonglong \a i.*/QTextStream &QTextStream::operator>>(qlonglong &i){ IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(qlonglong);}/*! \overload Stores the integer in the qulonglong \a i.*/QTextStream &QTextStream::operator>>(qulonglong &i){ IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(qulonglong);}/*! Reads a real number from the stream and stores it in \a f, then returns a reference to the QTextStream. The number is cast to the correct type. If no real number is detect on the stream, \a f is set to 0.0. As a special exception, QTextStream allows the strings "nan" and "inf" to represent NAN and INF floats or doubles. Leading whitespace is skipped.*/QTextStream &QTextStream::operator>>(float &f){ IMPLEMENT_STREAM_RIGHT_REAL_OPERATOR(float);}/*! \overload Stores the real number in the double \a f.*/QTextStream &QTextStream::operator>>(double &f){ IMPLEMENT_STREAM_RIGHT_REAL_OPERATOR(double);}/*! Reads a word from the stream and stores it in \a str, then returns a reference to the stream. Words are separated by whitespace (i.e., all characters for which QChar::isSpace() returns true). Leading whitespace is skipped.*/QTextStream &QTextStream::operator>>(QString &str){ Q_D(QTextStream); CHECK_VALID_STREAM(*this); str.clear(); d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); d->consumeLastToken(); const QChar *ptr; int length; if (!d->scan(&ptr, &length, 0, QTextStreamPrivate::Space)) { setStatus(ReadPastEnd); return *this; } str = QString(ptr, length); d->consumeLastToken(); return *this;}/*! \overload Converts the word to ISO-8859-1, then stores it in \a array. \sa QString::toLatin1()*/QTextStream &QTextStream::operator>>(QByteArray &array){ Q_D(QTextStream); CHECK_VALID_STREAM(*this); array.clear(); d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); d->consumeLastToken(); const QChar *ptr; int length; if (!d->scan(&ptr, &length, 0, QTextStreamPrivate::Space)) { setStatus(ReadPastEnd); return *this; } for (int i = 0; i < length; ++i) array += ptr[i].toLatin1(); d->consumeLastToken(); return *this;}/*! \overload Stores the word in \a c, terminated by a '\0' character. If no word is available, only the '\0' character is stored. Warning: Although convenient, this operator is dangerous and must be used with care. QTextStream assumes that \a c points to a buffer with enough space to hold the word. If the buffer is too small, your application may crash. If possible, use the QByteArray operator instead.*/QTextStream &QTextStream::operator>>(char *c){ Q_D(QTextStream); *c = 0; CHECK_VALID_STREAM(*this); d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); d->consumeLastToken(); const QChar *ptr; int length; if (!d->scan(&ptr, &length, 0, QTextStreamPrivate::Space)) { setStatus(ReadPastEnd); return *this; } for (int i = 0; i < length; ++i) *c++ = ptr[i].toLatin1(); *c = '\0'; d->consumeLastToken(); return *this;}/*! \internal */bool QTextStreamPrivate::putNumber(qulonglong number, bool negative){ QString prefix; if (negative) prefix = QLatin1Char('-'); else if (numberFlags & QTextStream::ForceSign) prefix = QLatin1Char('+'); if (numberFlags & QTextStream::ShowBase) { switch (integerBase) { case 2: prefix += QLatin1String("0b"); break; case 8: prefix += QLatin1String("0"); break; case 16: prefix += QLatin1String("0x"); break; default: break; } if (numberFlags & QTextStream::UppercaseBase) prefix = prefix.toUpper(); // ### in-place instead } QString digits = QString::number(number, integerBase ? integerBase : 10); if (numberFlags & QTextStream::UppercaseDigits) digits = digits.toUpper(); // ### in-place instead return putString(prefix + digits);}/*! \internal \overload*/QTextStream &QTextStream::operator<<(QBool b){ return *this << bool(b);}/*! Writes the character \a c to the stream, then returns a reference to the QTextStream. \sa setFieldWidth()*/QTextStream &QTextStream::operator<<(QChar c){ Q_D(QTextStream); CHECK_VALID_STREAM(*this); d->putString(QString(c)); return *this;}/*! \overload Converts \a c from ASCII to a QChar, then writes it to the stream.*/QTextStream &QTextStream::operator<<(char c){ Q_D(QTextStream); CHECK_VALID_STREAM(*this); d->putString(QString(QChar::fromAscii(c))); return *this;}/*! Writes the integer number \a i to the stream, then returns a reference to the QTextStream. By default, the number is stored in decimal form, but you can also set the base by calling setIntegerBase(). \sa setFieldWidth(), setNumberFlags()*/QTextStream &QTextStream::operator<<(signed short i){ Q_D(QTextStream); CHECK_VALID_STREAM(*this); d->putNumber((qulonglong)qAbs(qlonglong(i)), i < 0); return *this;}/*! \overload Writes the unsigned short \a i to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -