diff options
author | Alex Trotsenko <alex1973tr@gmail.com> | 2016-01-29 12:46:13 +0200 |
---|---|---|
committer | Alex Trotsenko <alex1973tr@gmail.com> | 2016-02-01 20:45:21 +0000 |
commit | 57ca755d2efbdb7268e8014cb4c50c9ea1c65bfc (patch) | |
tree | ff8459ea7129c501526b7c58cbecde50d6d9f857 /src/corelib | |
parent | 9fb5ff56c35177502791f1e979b2eca1881a70e5 (diff) |
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>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/io/qfiledevice.cpp | 6 | ||||
-rw-r--r-- | src/corelib/io/qiodevice_p.h | 1 | ||||
-rw-r--r-- | src/corelib/io/qprocess.cpp | 19 | ||||
-rw-r--r-- | src/corelib/tools/qringbuffer.cpp | 14 | ||||
-rw-r--r-- | src/corelib/tools/qringbuffer_p.h | 1 |
5 files changed, 18 insertions, 23 deletions
diff --git a/src/corelib/io/qfiledevice.cpp b/src/corelib/io/qfiledevice.cpp index b712fb719f..a2119b74db 100644 --- a/src/corelib/io/qfiledevice.cpp +++ b/src/corelib/io/qfiledevice.cpp @@ -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; } diff --git a/src/corelib/io/qiodevice_p.h b/src/corelib/io/qiodevice_p.h index f2fd0ca069..d264bfd31d 100644 --- a/src/corelib/io/qiodevice_p.h +++ b/src/corelib/io/qiodevice_p.h @@ -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)); } diff --git a/src/corelib/io/qprocess.cpp b/src/corelib/io/qprocess.cpp index 7cda718b04..e9c2cabc66 100644 --- a/src/corelib/io/qprocess.cpp +++ b/src/corelib/io/qprocess.cpp @@ -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(); diff --git a/src/corelib/tools/qringbuffer.cpp b/src/corelib/tools/qringbuffer.cpp index c3cb60a6f0..7e2d909dcd 100644 --- a/src/corelib/tools/qringbuffer.cpp +++ b/src/corelib/tools/qringbuffer.cpp @@ -317,6 +317,20 @@ qint64 QRingBuffer::peek(char *data, qint64 maxLength, qint64 pos) const /*! \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 + Append a new buffer to the end */ void QRingBuffer::append(const QByteArray &qba) diff --git a/src/corelib/tools/qringbuffer_p.h b/src/corelib/tools/qringbuffer_p.h index b1d4401de3..9ff1ec49cf 100644 --- a/src/corelib/tools/qringbuffer_p.h +++ b/src/corelib/tools/qringbuffer_p.h @@ -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) { |