Changeset 561 for trunk/src/corelib/io/qiodevice.cpp
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/corelib/io/qiodevice.cpp
r172 r561 2 2 ** 3 3 ** 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]) 5 6 ** 6 7 ** This file is part of the QtCore module of the Qt Toolkit. … … 21 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 22 23 ** 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. 27 27 ** 28 28 ** GNU General Public License Usage … … 34 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 35 ** 36 ** If you are unsure which license is appropriate for your use, please37 ** contact the sales department at qt-sales@nokia.com.36 ** If you 37 ** @nokia.com. 38 38 ** $QT_END_LICENSE$ 39 39 ** … … 48 48 #include "qstringlist.h" 49 49 #include <limits.h> 50 51 52 53 50 54 51 55 QT_BEGIN_NAMESPACE … … 119 123 QIODevicePrivate::QIODevicePrivate() 120 124 : 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 122 131 { 123 132 } … … 363 372 #if defined QIODEVICE_DEBUG 364 373 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(), 366 375 qPrintable(file ? file->fileName() : QString())); 367 376 #endif … … 376 385 { 377 386 #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()); 379 388 #endif 380 389 } … … 611 620 bool QIODevice::seek(qint64 pos) 612 621 { 613 if (d_func()->openMode == NotOpen) { 622 Q_D(QIODevice); 623 if (d->openMode == NotOpen) { 614 624 qWarning("QIODevice::seek: The device is not open"); 615 625 return false; … … 620 630 } 621 631 622 Q_D(QIODevice);623 632 #if defined QIODEVICE_DEBUG 624 633 printf("%p QIODevice::seek(%d), before: d->pos = %d, d->buffer.size() = %d\n", … … 632 641 } 633 642 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())) 643 645 // When seeking backwards, an operation that is only allowed for 644 646 // random-access devices, the buffer is cleared. The next read … … 646 648 // find that seeking backwards becomes a significant performance hit. 647 649 d->buffer.clear(); 648 } 650 else if (!d->buffer.isEmpty()) 651 d->buffer.skip(int(offset)); 652 649 653 #if defined QIODEVICE_DEBUG 650 654 printf("%p \tafter: d->pos == %d, d->buffer.size() == %d\n", this, int(d->pos), … … 754 758 // Short circuit for getChar() 755 759 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 758 765 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); 772 774 } 773 775 } … … 808 810 809 811 if (readFromDevice < bytesToBuffer) 810 d->buffer.truncate( readFromDevice < 0 ? 0 :int(readFromDevice));812 d->buffer.truncate(int(readFromDevice)); 811 813 if (!d->buffer.isEmpty()) { 812 814 lastReadChunkSize = d->buffer.read(data + readSoFar, maxSize - readSoFar); … … 903 905 { 904 906 Q_D(QIODevice); 905 CHECK_MAXLEN(read, QByteArray());906 QByteArray tmp; 907 qint64 readSoFar = 0;908 char buffer[4096]; 907 ; 908 909 ; 910 909 911 #if defined QIODEVICE_DEBUG 910 912 printf("%p QIODevice::read(%d), d->pos = %d, d->buffer.size() = %d\n", … … 914 916 #endif 915 917 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; 926 946 } 927 947 … … 944 964 #endif 945 965 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); 960 977 } else { 961 978 // 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; 967 990 } 968 991 … … 1031 1054 if (readSoFar) 1032 1055 debugBinaryString(data, int(readSoFar)); 1056 1057 1058 1059 1060 1061 1062 1063 1064 1033 1065 #endif 1034 1066 if (readSoFar && data[readSoFar - 1] == '\n') { … … 1070 1102 1071 1103 if (d->openMode & Text) { 1104 1105 1106 1107 1108 1109 1072 1110 if (readSoFar > 1 && data[readSoFar - 1] == '\n' && data[readSoFar - 2] == '\r') { 1073 1111 data[readSoFar - 2] = '\n'; … … 1098 1136 { 1099 1137 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); 1105 1141 1106 1142 #if defined QIODEVICE_DEBUG … … 1111 1147 #endif 1112 1148 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(); 1129 1177 else 1130 tmp.resize(int(readSoFar)); 1131 return tmp; 1178 result.resize(readBytes); 1179 1180 return result; 1132 1181 } 1133 1182 … … 1361 1410 { 1362 1411 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 1389 1414 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)); 1396 1416 } 1397 1417 … … 1450 1470 1451 1471 /*! 1452 Blocks until data is available for reading and the readyRead()1472 Blocks until data is available for reading and the readyRead() 1453 1473 signal has been emitted, or until \a msecs milliseconds have 1454 1474 passed. If msecs is -1, this function will not time out. 1455 1475 1456 Returns true if data is available for reading; otherwise returns1476 Returns true if data is available for reading; otherwise returns 1457 1477 false (if the operation timed out or if an error occurred). 1458 1478 … … 1741 1761 qSort(modeList); 1742 1762 debug << modeList.join(QLatin1String("|")); 1743 debug << ")";1763 debug << ; 1744 1764 return debug; 1745 1765 }
Note:
See TracChangeset
for help on using the changeset viewer.