Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/corelib/io/qiodevice.cpp

    r172 r561  
    22**
    33** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    4 ** Contact: Qt Software Information ([email protected])
     4** All rights reserved.
     5** Contact: Nokia Corporation ([email protected])
    56**
    67** This file is part of the QtCore module of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    23 ** In addition, as a special exception, Nokia gives you certain
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
    26 ** package.
     24** In addition, as a special exception, Nokia gives you certain additional
     25** rights.  These rights are described in the Nokia Qt LGPL Exception
     26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** contact the sales department at qt-sales@nokia.com.
     36** If you
     37** @nokia.com.
    3838** $QT_END_LICENSE$
    3939**
     
    4848#include "qstringlist.h"
    4949#include <limits.h>
     50
     51
     52
     53
    5054
    5155QT_BEGIN_NAMESPACE
     
    119123QIODevicePrivate::QIODevicePrivate()
    120124    : openMode(QIODevice::NotOpen), buffer(QIODEVICE_BUFFERSIZE),
    121       pos(0), devicePos(0), accessMode(Unset)
     125      pos(0), devicePos(0)
     126       , baseReadLineDataCalled(false)
     127       , accessMode(Unset)
     128#ifdef QT_NO_QOBJECT
     129       , q_ptr(0)
     130#endif
    122131{
    123132}
     
    363372#if defined QIODEVICE_DEBUG
    364373    QFile *file = qobject_cast<QFile *>(this);
    365     printf("%p QIODevice::QIODevice(\"%s\") %s\n", this, className(),
     374    printf("%p QIODevice::QIODevice(\"%s\") %s\n", this, className(),
    366375           qPrintable(file ? file->fileName() : QString()));
    367376#endif
     
    376385{
    377386#if defined QIODEVICE_DEBUG
    378     printf("%p QIODevice::QIODevice(%p \"%s\")\n", this, parent, className());
     387    printf("%p QIODevice::QIODevice(%p \"%s\")\n", this, parent, className());
    379388#endif
    380389}
     
    611620bool QIODevice::seek(qint64 pos)
    612621{
    613     if (d_func()->openMode == NotOpen) {
     622    Q_D(QIODevice);
     623    if (d->openMode == NotOpen) {
    614624        qWarning("QIODevice::seek: The device is not open");
    615625        return false;
     
    620630    }
    621631
    622     Q_D(QIODevice);
    623632#if defined QIODEVICE_DEBUG
    624633    printf("%p QIODevice::seek(%d), before: d->pos = %d, d->buffer.size() = %d\n",
     
    632641    }
    633642
    634     if (offset > 0 && !d->buffer.isEmpty()) {
    635         // When seeking forwards, we need to pop bytes off the front of the
    636         // buffer.
    637         do {
    638             int bytesToSkip = int(qMin<qint64>(offset, INT_MAX));
    639             d->buffer.skip(bytesToSkip);
    640             offset -= bytesToSkip;
    641         } while (offset > 0);
    642     } else if (offset < 0) {
     643    if (offset < 0
     644            || offset >= qint64(d->buffer.size()))
    643645        // When seeking backwards, an operation that is only allowed for
    644646        // random-access devices, the buffer is cleared. The next read
     
    646648        // find that seeking backwards becomes a significant performance hit.
    647649        d->buffer.clear();
    648     }
     650    else if (!d->buffer.isEmpty())
     651        d->buffer.skip(int(offset));
     652
    649653#if defined QIODEVICE_DEBUG
    650654    printf("%p \tafter: d->pos == %d, d->buffer.size() == %d\n", this, int(d->pos),
     
    754758    // Short circuit for getChar()
    755759    if (maxSize == 1) {
    756         int chint = d->buffer.getChar();
    757         if (chint != -1) {
     760        int chint;
     761        while ((chint = d->buffer.getChar()) != -1) {
     762            if (!sequential)
     763                ++d->pos;
     764
    758765            char c = char(uchar(chint));
    759             if (c == '\r' && (d->openMode & Text)) {
    760                 d->buffer.ungetChar(c);
    761             } else {
    762                 if (data)
    763                     *data = c;
    764                 if (!sequential)
    765                     ++d->pos;
    766 #if defined QIODEVICE_DEBUG
    767                 printf("%p \tread 0x%hhx (%c) returning 1 (shortcut)\n", this,
    768                        int(c), isprint(c) ? c : '?');
    769 #endif
    770                 return qint64(1);
    771             }
     766            if (c == '\r' && (d->openMode & Text))
     767                continue;
     768            *data = c;
     769#if defined QIODEVICE_DEBUG
     770            printf("%p \tread 0x%hhx (%c) returning 1 (shortcut)\n", this,
     771                   int(c), isprint(c) ? c : '?');
     772#endif
     773            return qint64(1);
    772774        }
    773775    }
     
    808810
    809811                if (readFromDevice < bytesToBuffer)
    810                     d->buffer.truncate(readFromDevice < 0 ? 0 : int(readFromDevice));
     812                    d->buffer.truncate(int(readFromDevice));
    811813                if (!d->buffer.isEmpty()) {
    812814                    lastReadChunkSize = d->buffer.read(data + readSoFar, maxSize - readSoFar);
     
    903905{
    904906    Q_D(QIODevice);
    905     CHECK_MAXLEN(read, QByteArray());
    906     QByteArray tmp;
    907     qint64 readSoFar = 0;
    908     char buffer[4096];
     907    ;
     908
     909    ;
     910
    909911#if defined QIODEVICE_DEBUG
    910912    printf("%p QIODevice::read(%d), d->pos = %d, d->buffer.size() = %d\n",
     
    914916#endif
    915917
    916     do {
    917         qint64 bytesToRead = qMin(int(maxSize - readSoFar), int(sizeof(buffer)));
    918         qint64 readBytes = read(buffer, bytesToRead);
    919         if (readBytes <= 0)
    920             break;
    921         tmp.append(buffer, (int) readBytes);
    922         readSoFar += readBytes;
    923     } while (readSoFar < maxSize && bytesAvailable() > 0);
    924 
    925     return tmp;
     918    if (maxSize != qint64(int(maxSize))) {
     919        qWarning("QIODevice::read: maxSize argument exceeds QByteArray size limit");
     920        maxSize = INT_MAX;
     921    }
     922
     923    qint64 readBytes = 0;
     924    if (maxSize) {
     925        result.resize(int(maxSize));
     926        if (!result.size()) {
     927            // If resize fails, read incrementally.
     928            qint64 readResult;
     929            do {
     930                result.resize(int(qMin(maxSize, result.size() + QIODEVICE_BUFFERSIZE)));
     931                readResult = read(result.data() + readBytes, result.size() - readBytes);
     932                if (readResult > 0 || readBytes == 0)
     933                    readBytes += readResult;
     934            } while (readResult == QIODEVICE_BUFFERSIZE);
     935        } else {
     936            readBytes = read(result.data(), result.size());
     937        }
     938    }
     939
     940    if (readBytes <= 0)
     941        result.clear();
     942    else
     943        result.resize(int(readBytes));
     944
     945    return result;
    926946}
    927947
     
    944964#endif
    945965
    946     QByteArray tmp;
    947     if (d->isSequential() || size() == 0) {
    948         // Read it in chunks, bytesAvailable() is unreliable for sequential
    949         // devices.
    950         const int chunkSize = 4096;
    951         qint64 totalRead = 0;
    952         forever {
    953             tmp.resize(tmp.size() + chunkSize);
    954             qint64 readBytes = read(tmp.data() + totalRead, chunkSize);
    955             tmp.chop(chunkSize - (readBytes < 0 ? 0 : readBytes));
    956             if (readBytes <= 0)
    957                 return tmp;
    958             totalRead += readBytes;
    959         }
     966    QByteArray result;
     967    qint64 readBytes = 0;
     968    if (d->isSequential() || (readBytes = size()) == 0) {
     969        // Size is unknown, read incrementally.
     970        qint64 readResult;
     971        do {
     972            result.resize(result.size() + QIODEVICE_BUFFERSIZE);
     973            readResult = read(result.data() + readBytes, result.size() - readBytes);
     974            if (readResult > 0 || readBytes == 0)
     975                readBytes += readResult;
     976        } while (readResult > 0);
    960977    } else {
    961978        // Read it all in one go.
    962         tmp.resize(int(bytesAvailable()));
    963         qint64 readBytes = read(tmp.data(), tmp.size());
    964         tmp.resize(readBytes < 0 ? 0 : int(readBytes));
    965     }
    966     return tmp;
     979        // If resize fails, don't read anything.
     980        result.resize(int(readBytes - d->pos));
     981        readBytes = read(result.data(), result.size());
     982    }
     983
     984    if (readBytes <= 0)
     985        result.clear();
     986    else
     987        result.resize(int(readBytes));
     988
     989    return result;
    967990}
    968991
     
    10311054        if (readSoFar)
    10321055            debugBinaryString(data, int(readSoFar));
     1056
     1057
     1058
     1059
     1060
     1061
     1062
     1063
     1064
    10331065#endif
    10341066        if (readSoFar && data[readSoFar - 1] == '\n') {
     
    10701102
    10711103    if (d->openMode & Text) {
     1104
     1105
     1106
     1107
     1108
     1109
    10721110        if (readSoFar > 1 && data[readSoFar - 1] == '\n' && data[readSoFar - 2] == '\r') {
    10731111            data[readSoFar - 2] = '\n';
     
    10981136{
    10991137    Q_D(QIODevice);
    1100     CHECK_MAXLEN(readLine, QByteArray());
    1101     QByteArray tmp;
    1102     const int BufferGrowth = 4096;
    1103     qint64 readSoFar = 0;
    1104     qint64 readBytes = 0;
     1138    QByteArray result;
     1139
     1140    CHECK_MAXLEN(readLine, result);
    11051141
    11061142#if defined QIODEVICE_DEBUG
     
    11111147#endif
    11121148
    1113     do {
    1114         if (maxSize != 0)
    1115             tmp.resize(int(readSoFar + qMin(int(maxSize), BufferGrowth)));
    1116         else
    1117             tmp.resize(int(readSoFar + BufferGrowth));
    1118         readBytes = readLine(tmp.data() + readSoFar, tmp.size() - readSoFar);
    1119         if (readBytes <= 0)
    1120             break;
    1121 
    1122         readSoFar += readBytes;
    1123     } while ((!maxSize || readSoFar < maxSize) &&
    1124              readSoFar + 1 == tmp.size() &&   // +1 due to the ending null
    1125              tmp.at(readSoFar - 1) != '\n');
    1126 
    1127     if (readSoFar == 0 && readBytes == -1)
    1128         tmp.clear();            // return Null if we found an error
     1149    if (maxSize > INT_MAX) {
     1150        qWarning("QIODevice::read: maxSize argument exceeds QByteArray size limit");
     1151        maxSize = INT_MAX;
     1152    }
     1153
     1154    result.resize(int(maxSize));
     1155    qint64 readBytes = 0;
     1156    if (!result.size()) {
     1157        // If resize fails or maxSize == 0, read incrementally
     1158        if (maxSize == 0)
     1159            maxSize = INT_MAX;
     1160
     1161        // The first iteration needs to leave an extra byte for the terminating null
     1162        result.resize(1);
     1163
     1164        qint64 readResult;
     1165        do {
     1166            result.resize(int(qMin(maxSize, result.size() + QIODEVICE_BUFFERSIZE)));
     1167            readResult = readLine(result.data() + readBytes, result.size() - readBytes);
     1168            if (readResult > 0 || readBytes == 0)
     1169                readBytes += readResult;
     1170        } while (readResult == QIODEVICE_BUFFERSIZE
     1171                && result[int(readBytes - 1)] != '\n');
     1172    } else
     1173        readBytes = readLine(result.data(), result.size());
     1174
     1175    if (readBytes <= 0)
     1176        result.clear();
    11291177    else
    1130         tmp.resize(int(readSoFar));
    1131     return tmp;
     1178        result.resize(readBytes);
     1179
     1180    return result;
    11321181}
    11331182
     
    13611410{
    13621411    Q_D(QIODevice);
    1363     const OpenMode openMode = d->openMode;
    1364     if (!(openMode & ReadOnly)) {
    1365         if (openMode == NotOpen)
    1366             qWarning("QIODevice::getChar: Closed device");
    1367         else
    1368             qWarning("QIODevice::getChar: WriteOnly device");
    1369         return false;
    1370     }
    1371 
    1372     // Shortcut for QIODevice::read(c, 1)
    1373     QRingBuffer *buffer = &d->buffer;
    1374     const int chint = buffer->getChar();
    1375     if (chint != -1) {
    1376         char ch = char(uchar(chint));
    1377         if ((openMode & Text) && ch == '\r') {
    1378             buffer->ungetChar(ch);
    1379         } else {
    1380             if (c)
    1381                 *c = ch;
    1382             if (!d->isSequential())
    1383                 ++d->pos;
    1384             return true;
    1385         }
    1386     }
    1387 
    1388     // Fall back to read().
     1412    CHECK_READABLE(getChar, false);
     1413
    13891414    char ch;
    1390     if (read(&ch, 1) == 1) {
    1391         if (c)
    1392             *c = ch;
    1393         return true;
    1394     }
    1395     return false;
     1415    return (1 == read(c ? c : &ch, 1));
    13961416}
    13971417
     
    14501470
    14511471/*!
    1452     Blocks until data is available for reading and the readyRead()
     1472    Blocks until data is available for reading and the readyRead()
    14531473    signal has been emitted, or until \a msecs milliseconds have
    14541474    passed. If msecs is -1, this function will not time out.
    14551475
    1456     Returns true if data is available for reading; otherwise returns
     1476    Returns true if data is available for reading; otherwise returns
    14571477    false (if the operation timed out or if an error occurred).
    14581478
     
    17411761    qSort(modeList);
    17421762    debug << modeList.join(QLatin1String("|"));
    1743     debug << ")";
     1763    debug << ;
    17441764    return debug;
    17451765}
Note: See TracChangeset for help on using the changeset viewer.