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.
|
// Write to the buffer.
|
||||||
char *writePointer = d->writeBuffer.reserve(len);
|
d->writeBuffer.append(data, len);
|
||||||
if (len == 1)
|
|
||||||
*writePointer = *data;
|
|
||||||
else if (len)
|
|
||||||
::memcpy(writePointer, data, len);
|
|
||||||
return 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 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 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 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 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 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)); }
|
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
|
#endif
|
||||||
|
|
||||||
if (len == 1) {
|
d->writeBuffer.append(data, len);
|
||||||
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);
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
if (!d->stdinWriteTrigger->isActive())
|
if (!d->stdinWriteTrigger->isActive())
|
||||||
d->stdinWriteTrigger->start();
|
d->stdinWriteTrigger->start();
|
||||||
|
@ -314,6 +314,20 @@ qint64 QRingBuffer::peek(char *data, qint64 maxLength, qint64 pos) const
|
|||||||
return readSoFar;
|
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
|
\internal
|
||||||
|
|
||||||
|
@ -126,6 +126,7 @@ public:
|
|||||||
Q_CORE_EXPORT qint64 read(char *data, qint64 maxLength);
|
Q_CORE_EXPORT qint64 read(char *data, qint64 maxLength);
|
||||||
Q_CORE_EXPORT QByteArray read();
|
Q_CORE_EXPORT QByteArray read();
|
||||||
Q_CORE_EXPORT qint64 peek(char *data, qint64 maxLength, qint64 pos = 0) const;
|
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);
|
Q_CORE_EXPORT void append(const QByteArray &qba);
|
||||||
|
|
||||||
inline qint64 skip(qint64 length) {
|
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());
|
d->setError(d->socketEngine->error(), d->socketEngine->errorString());
|
||||||
} else if (written < size) {
|
} else if (written < size) {
|
||||||
// Buffer what was not written yet
|
// Buffer what was not written yet
|
||||||
char *ptr = d->writeBuffer.reserve(size - written);
|
d->writeBuffer.append(data + written, size - written);
|
||||||
memcpy(ptr, data + written, size - written);
|
|
||||||
written = size;
|
written = size;
|
||||||
d->socketEngine->setWriteNotificationEnabled(true);
|
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
|
// We just write to our write buffer and enable the write notifier
|
||||||
// The write notifier then flush()es the buffer.
|
// The write notifier then flush()es the buffer.
|
||||||
|
|
||||||
char *ptr = d->writeBuffer.reserve(size);
|
d->writeBuffer.append(data, size);
|
||||||
if (size == 1)
|
|
||||||
*ptr = *data;
|
|
||||||
else
|
|
||||||
memcpy(ptr, data, size);
|
|
||||||
|
|
||||||
qint64 written = size;
|
qint64 written = size;
|
||||||
|
|
||||||
if (d->socketEngine && !d->writeBuffer.isEmpty())
|
if (d->socketEngine && !d->writeBuffer.isEmpty())
|
||||||
|
@ -2004,8 +2004,7 @@ qint64 QSslSocket::writeData(const char *data, qint64 len)
|
|||||||
if (d->mode == UnencryptedMode && !d->autoStartHandshake)
|
if (d->mode == UnencryptedMode && !d->autoStartHandshake)
|
||||||
return d->plainSocket->write(data, len);
|
return d->plainSocket->write(data, len);
|
||||||
|
|
||||||
char *writePtr = d->writeBuffer.reserve(len);
|
d->writeBuffer.append(data, len);
|
||||||
::memcpy(writePtr, data, len);
|
|
||||||
|
|
||||||
// make sure we flush to the plain socket's buffer
|
// make sure we flush to the plain socket's buffer
|
||||||
QMetaObject::invokeMethod(this, "_q_flushWriteBuffer", Qt::QueuedConnection);
|
QMetaObject::invokeMethod(this, "_q_flushWriteBuffer", Qt::QueuedConnection);
|
||||||
|
@ -670,8 +670,7 @@ void QSslSocketBackendPrivate::transmit()
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (readBytes) {
|
if (readBytes) {
|
||||||
char *const ptr = buffer.reserve(readBytes);
|
buffer.append(data.constData(), readBytes);
|
||||||
std::copy(data.data(), data.data() + readBytes, ptr);
|
|
||||||
if (readyReadEmittedPointer)
|
if (readyReadEmittedPointer)
|
||||||
*readyReadEmittedPointer = true;
|
*readyReadEmittedPointer = true;
|
||||||
emit q->readyRead();
|
emit q->readyRead();
|
||||||
|
@ -940,8 +940,7 @@ void QSslSocketBackendPrivate::transmit()
|
|||||||
#ifdef QSSLSOCKET_DEBUG
|
#ifdef QSSLSOCKET_DEBUG
|
||||||
qCDebug(lcSsl) << "QSslSocketBackendPrivate::transmit: decrypted" << readBytes << "bytes";
|
qCDebug(lcSsl) << "QSslSocketBackendPrivate::transmit: decrypted" << readBytes << "bytes";
|
||||||
#endif
|
#endif
|
||||||
char *ptr = buffer.reserve(readBytes);
|
buffer.append(data.constData(), readBytes);
|
||||||
::memcpy(ptr, data.data(), readBytes);
|
|
||||||
|
|
||||||
if (readyReadEmittedPointer)
|
if (readyReadEmittedPointer)
|
||||||
*readyReadEmittedPointer = true;
|
*readyReadEmittedPointer = true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user