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

📄 qtextstream.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    CHECK_VALID_STREAM(QString());    if (maxlen <= 0)        return QString("");     // empty, not null    const QChar *readPtr;    int length;    if (!d->scan(&readPtr, &length, int(maxlen), QTextStreamPrivate::EndOfFile))        return QString();    QString tmp = QString(readPtr, length);    d->consumeLastToken();    return tmp;}/*! \internal*/QTextStreamPrivate::NumberParsingStatus QTextStreamPrivate::getNumber(qulonglong *ret){    scan(0, 0, 0, NotSpace);    consumeLastToken();    // detect int encoding    int base = integerBase;    if (base == 0) {        QChar ch;        if (!getChar(&ch))            return npsInvalidPrefix;        if (ch == QLatin1Char('0')) {            QChar ch2;            if (!getChar(&ch2)) {                // Result is the number 0                *ret = 0;                return npsOk;            }            ch2 = ch2.toLower();            if (ch2 == QLatin1Char('x')) {                base = 16;            } else if (ch2 == QLatin1Char('b')) {                base = 2;            } else if (ch2.isDigit() && ch2.digitValue() >= 0 && ch2.digitValue() <= 7) {                base = 8;            } else {                base = 10;            }            ungetChar(ch2);        } else if (ch == QLatin1Char('-') || ch == QLatin1Char('+') || ch.isDigit()) {            base = 10;        } else {            ungetChar(ch);            return npsInvalidPrefix;        }        ungetChar(ch);        // State of the stream is now the same as on entry        // (cursor is at prefix),        // and local variable 'base' has been set appropriately.    }    qulonglong val=0;    switch (base) {    case 2: {        QChar pf1, pf2, dig;        // Parse prefix '0b'        if (!getChar(&pf1) || pf1 != QLatin1Char('0'))            return npsInvalidPrefix;        if (!getChar(&pf2) || pf2.toLower() != QLatin1Char('b'))            return npsInvalidPrefix;        // Parse digits        int ndigits = 0;        while (getChar(&dig)) {            int n = dig.toLower().unicode();            if (n == '0' || n == '1') {                val <<= 1;                val += n - '0';            } else {                ungetChar(dig);                break;            }            ndigits++;        }        if (ndigits == 0) {            // Unwind the prefix and abort            ungetChar(pf2);            ungetChar(pf1);            return npsMissingDigit;        }        break;    }    case 8: {        QChar pf, dig;        // Parse prefix '0'        if (!getChar(&pf) || pf != QLatin1Char('0'))            return npsInvalidPrefix;        // Parse digits        int ndigits = 0;        while (getChar(&dig)) {            int n = dig.toLower().unicode();            if (n >= '0' && n <= '7') {                val *= 8;                val += n - '0';            } else {                ungetChar(dig);                break;            }            ndigits++;        }        if (ndigits == 0) {            // Unwind the prefix and abort            ungetChar(pf);            return npsMissingDigit;        }        break;    }    case 10: {        // Parse sign (or first digit)        QChar sign;        int ndigits = 0;        if (!getChar(&sign))            return npsMissingDigit;        if (sign != QLatin1Char('-') && sign != QLatin1Char('+')) {            if (!sign.isDigit()) {                ungetChar(sign);                return npsMissingDigit;            }            val += sign.digitValue();            ndigits++;        }        // Parse digits        QChar ch;        while (getChar(&ch)) {            if (ch.isDigit()) {                val *= 10;                val += ch.digitValue();            } else {                ungetChar(ch);                break;            }            ndigits++;        }        if (ndigits == 0)            return npsMissingDigit;        if (sign == QLatin1Char('-')) {            qlonglong ival = qlonglong(val);            if (ival > 0)                ival = -ival;            val = qulonglong(ival);        }        break;    }    case 16: {        QChar pf1, pf2, dig;        // Parse prefix ' 0x'        if (!getChar(&pf1) || pf1 != QLatin1Char('0'))            return npsInvalidPrefix;        if (!getChar(&pf2) || pf2.toLower() != QLatin1Char('x'))            return npsInvalidPrefix;        // Parse digits        int ndigits = 0;        while (getChar(&dig)) {            int n = dig.toLower().unicode();            if (n >= '0' && n <= '9') {                val <<= 4;                val += n - '0';            } else if (n >= 'a' && n <= 'f') {                val <<= 4;                val += 10 + (n - 'a');            } else {                ungetChar(dig);                break;            }            ndigits++;        }        if (ndigits == 0) {            return npsMissingDigit;        }        break;    }    default:        // Unsupported integerBase        return npsInvalidPrefix;    }    if (ret)        *ret = val;    return npsOk;}/*! \internal    (hihi)*/bool QTextStreamPrivate::getReal(double *f){    // We use a table-driven FSM to parse floating point numbers    // strtod() cannot be used directly since we may be reading from a    // QIODevice.    enum ParserState {        Init = 0,        Sign = 1,        Mantissa = 2,        Dot = 3,        Abscissa = 4,        ExpMark = 5,        ExpSign = 6,        Exponent = 7,        Done = 8    };    enum InputToken {        None = 0,        InputSign = 1,        InputDigit = 2,        InputDot = 3,        InputExp = 4    };    static uchar table[8][5] = {        // None InputSign InputDigit InputDot InputExp        { 0,    Sign,     Mantissa,  Dot,     0        }, // Init        { 0,    0,        Mantissa,  Dot,     0        }, // Sign        { Done, Done,     Mantissa,  Dot,     ExpMark  }, // Mantissa        { 0,    0,        Abscissa,  0,       0        }, // Dot        { Done, Done,     Abscissa,  Done,    ExpMark  }, // Abscissa        { 0,    ExpSign,  Exponent,  0,       0        }, // ExpMark        { 0,    0,        Exponent,  0,       0        }, // ExpSign        { Done, Done,     Exponent,  Done,    Done     }  // Exponent    };    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;        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;    buf[i] = '\0';    if (f)        *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 casted 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 casted to    the correct type. If no real number is detect on the stream, \a f    is set to 0.0.    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()

⌨️ 快捷键说明

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