QRingBuffer: add append(const char *, qint64) function
This allows to remove a code duplication in several places. Change-Id: I49f56e951682dbd2968923654a12cba5199a2502 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
This commit is contained in:
parent
9fb5ff56c3
commit
57ca755d2e
@ -562,11 +562,7 @@ qint64 QFileDevice::writeData(const char *data, qint64 len)
|
||||
}
|
||||
|
||||
// Write to the buffer.
|
||||
char *writePointer = d->writeBuffer.reserve(len);
|
||||
if (len == 1)
|
||||
*writePointer = *data;
|
||||
else if (len)
|
||||
::memcpy(writePointer, data, len);
|
||||
d->writeBuffer.append(data, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
|
@ -111,6 +111,7 @@ public:
|
||||
inline qint64 read(char *data, qint64 maxLength) { return (m_buf ? m_buf->read(data, maxLength) : Q_INT64_C(0)); }
|
||||
inline QByteArray read() { return (m_buf ? m_buf->read() : QByteArray()); }
|
||||
inline qint64 peek(char *data, qint64 maxLength, qint64 pos = 0) const { return (m_buf ? m_buf->peek(data, maxLength, pos) : Q_INT64_C(0)); }
|
||||
inline void append(const char *data, qint64 size) { Q_ASSERT(m_buf); m_buf->append(data, size); }
|
||||
inline void append(const QByteArray &qba) { Q_ASSERT(m_buf); m_buf->append(qba); }
|
||||
inline qint64 skip(qint64 length) { return (m_buf ? m_buf->skip(length) : Q_INT64_C(0)); }
|
||||
inline qint64 readLine(char *data, qint64 maxLength) { return (m_buf ? m_buf->readLine(data, maxLength) : Q_INT64_C(-1)); }
|
||||
|
@ -2040,24 +2040,7 @@ qint64 QProcess::writeData(const char *data, qint64 len)
|
||||
}
|
||||
#endif
|
||||
|
||||
if (len == 1) {
|
||||
d->writeBuffer.putChar(*data);
|
||||
#ifdef Q_OS_WIN
|
||||
if (!d->stdinWriteTrigger->isActive())
|
||||
d->stdinWriteTrigger->start();
|
||||
#else
|
||||
if (d->stdinChannel.notifier)
|
||||
d->stdinChannel.notifier->setEnabled(true);
|
||||
#endif
|
||||
#if defined QPROCESS_DEBUG
|
||||
qDebug("QProcess::writeData(%p \"%s\", %lld) == 1 (written to buffer)",
|
||||
data, qt_prettyDebug(data, len, 16).constData(), len);
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *dest = d->writeBuffer.reserve(len);
|
||||
memcpy(dest, data, len);
|
||||
d->writeBuffer.append(data, len);
|
||||
#ifdef Q_OS_WIN
|
||||
if (!d->stdinWriteTrigger->isActive())
|
||||
d->stdinWriteTrigger->start();
|
||||
|
@ -314,6 +314,20 @@ qint64 QRingBuffer::peek(char *data, qint64 maxLength, qint64 pos) const
|
||||
return readSoFar;
|
||||
}
|
||||
|
||||
/*!
|
||||
\internal
|
||||
|
||||
Append bytes from data to the end
|
||||
*/
|
||||
void QRingBuffer::append(const char *data, qint64 size)
|
||||
{
|
||||
char *writePointer = reserve(size);
|
||||
if (size == 1)
|
||||
*writePointer = *data;
|
||||
else if (size)
|
||||
::memcpy(writePointer, data, size);
|
||||
}
|
||||
|
||||
/*!
|
||||
\internal
|
||||
|
||||
|
@ -126,6 +126,7 @@ public:
|
||||
Q_CORE_EXPORT qint64 read(char *data, qint64 maxLength);
|
||||
Q_CORE_EXPORT QByteArray read();
|
||||
Q_CORE_EXPORT qint64 peek(char *data, qint64 maxLength, qint64 pos = 0) const;
|
||||
Q_CORE_EXPORT void append(const char *data, qint64 size);
|
||||
Q_CORE_EXPORT void append(const QByteArray &qba);
|
||||
|
||||
inline qint64 skip(qint64 length) {
|
||||
|
@ -2481,8 +2481,7 @@ qint64 QAbstractSocket::writeData(const char *data, qint64 size)
|
||||
d->setError(d->socketEngine->error(), d->socketEngine->errorString());
|
||||
} else if (written < size) {
|
||||
// Buffer what was not written yet
|
||||
char *ptr = d->writeBuffer.reserve(size - written);
|
||||
memcpy(ptr, data + written, size - written);
|
||||
d->writeBuffer.append(data + written, size - written);
|
||||
written = size;
|
||||
d->socketEngine->setWriteNotificationEnabled(true);
|
||||
}
|
||||
@ -2515,12 +2514,7 @@ qint64 QAbstractSocket::writeData(const char *data, qint64 size)
|
||||
// We just write to our write buffer and enable the write notifier
|
||||
// The write notifier then flush()es the buffer.
|
||||
|
||||
char *ptr = d->writeBuffer.reserve(size);
|
||||
if (size == 1)
|
||||
*ptr = *data;
|
||||
else
|
||||
memcpy(ptr, data, size);
|
||||
|
||||
d->writeBuffer.append(data, size);
|
||||
qint64 written = size;
|
||||
|
||||
if (d->socketEngine && !d->writeBuffer.isEmpty())
|
||||
|
@ -2004,8 +2004,7 @@ qint64 QSslSocket::writeData(const char *data, qint64 len)
|
||||
if (d->mode == UnencryptedMode && !d->autoStartHandshake)
|
||||
return d->plainSocket->write(data, len);
|
||||
|
||||
char *writePtr = d->writeBuffer.reserve(len);
|
||||
::memcpy(writePtr, data, len);
|
||||
d->writeBuffer.append(data, len);
|
||||
|
||||
// make sure we flush to the plain socket's buffer
|
||||
QMetaObject::invokeMethod(this, "_q_flushWriteBuffer", Qt::QueuedConnection);
|
||||
|
@ -670,8 +670,7 @@ void QSslSocketBackendPrivate::transmit()
|
||||
}
|
||||
|
||||
if (readBytes) {
|
||||
char *const ptr = buffer.reserve(readBytes);
|
||||
std::copy(data.data(), data.data() + readBytes, ptr);
|
||||
buffer.append(data.constData(), readBytes);
|
||||
if (readyReadEmittedPointer)
|
||||
*readyReadEmittedPointer = true;
|
||||
emit q->readyRead();
|
||||
|
@ -940,8 +940,7 @@ void QSslSocketBackendPrivate::transmit()
|
||||
#ifdef QSSLSOCKET_DEBUG
|
||||
qCDebug(lcSsl) << "QSslSocketBackendPrivate::transmit: decrypted" << readBytes << "bytes";
|
||||
#endif
|
||||
char *ptr = buffer.reserve(readBytes);
|
||||
::memcpy(ptr, data.data(), readBytes);
|
||||
buffer.append(data.constData(), readBytes);
|
||||
|
||||
if (readyReadEmittedPointer)
|
||||
*readyReadEmittedPointer = true;
|
||||
|
Loading…
x
Reference in New Issue
Block a user