From 048d021f4030210d2b7174d79bbc05921f4c6770 Mon Sep 17 00:00:00 2001 From: Stefan Knoblich Date: Sat, 6 Sep 2008 20:41:50 +0000 Subject: [PATCH] Change zap_buffer_read_loop() to always try to read the full amount of bytes we wanted to get; add zap_buffer_seek() to skip n bytes from the beginning of the buffer (zap_buffer_toss works relative to the current position only) git-svn-id: http://svn.openzap.org/svn/openzap/trunk@539 a93c3328-9c30-0410-af19-c9cd2b2d52af --- libs/openzap/src/include/zap_buffer.h | 7 +++++++ libs/openzap/src/zap_buffer.c | 27 ++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/libs/openzap/src/include/zap_buffer.h b/libs/openzap/src/include/zap_buffer.h index eaa71524f9..958d2fc8e7 100644 --- a/libs/openzap/src/include/zap_buffer.h +++ b/libs/openzap/src/include/zap_buffer.h @@ -122,6 +122,13 @@ void zap_buffer_zero(zap_buffer_t *buffer); */ void zap_buffer_destroy(zap_buffer_t **buffer); +/*! \brief Seek to offset from the beginning of the buffer + * \param buffer buffer to seek + * \param datalen offset in bytes + * \return new position + */ +zap_size_t zap_buffer_seek(zap_buffer_t *buffer, zap_size_t datalen); + /** @} */ zap_size_t zap_buffer_zwrite(zap_buffer_t *buffer, const void *data, zap_size_t datalen); diff --git a/libs/openzap/src/zap_buffer.c b/libs/openzap/src/zap_buffer.c index d5d7f1ef7d..1051f716cc 100644 --- a/libs/openzap/src/zap_buffer.c +++ b/libs/openzap/src/zap_buffer.c @@ -108,6 +108,27 @@ zap_size_t zap_buffer_inuse(zap_buffer_t *buffer) return buffer->used; } +zap_size_t zap_buffer_seek(zap_buffer_t *buffer, zap_size_t datalen) +{ + zap_size_t reading = 0; + + assert(buffer != NULL); + + if (buffer->used < 1) { + buffer->used = 0; + return 0; + } else if (buffer->used >= datalen) { + reading = datalen; + } else { + reading = buffer->used; + } + + buffer->used = buffer->actually_used - reading; + buffer->head = buffer->data + reading; + + return reading; +} + zap_size_t zap_buffer_toss(zap_buffer_t *buffer, zap_size_t datalen) { zap_size_t reading = 0; @@ -137,13 +158,13 @@ void zap_buffer_set_loops(zap_buffer_t *buffer, int loops) zap_size_t zap_buffer_read_loop(zap_buffer_t *buffer, void *data, zap_size_t datalen) { zap_size_t len; - if ((len = zap_buffer_read(buffer, data, datalen)) == 0) { + if ((len = zap_buffer_read(buffer, data, datalen)) < datalen) { if (buffer->loops == 0) { - return 0; + return len; } buffer->head = buffer->data; buffer->used = buffer->actually_used; - len = zap_buffer_read(buffer, data, datalen); + len = zap_buffer_read(buffer, data + len, datalen - len); buffer->loops--; } return len;