From 03166512fdcd1df924ab6134bb1d9f709204e9d7 Mon Sep 17 00:00:00 2001 From: koennecke Date: Tue, 17 Feb 2009 08:50:23 +0000 Subject: [PATCH] - Added missing files --- rwpuffer.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ rwpuffer.h | 46 +++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 rwpuffer.c create mode 100644 rwpuffer.h diff --git a/rwpuffer.c b/rwpuffer.c new file mode 100644 index 00000000..a678e700 --- /dev/null +++ b/rwpuffer.c @@ -0,0 +1,85 @@ +/** + * This is a buffer to store bytes for reading and writing. + * + * copyright: see file COPYRIGHT + * + * Mark Koennecke, January 2009 + */ +#include +#include +#include +#include "rwpuffer.h" +/*----------------------------------------------------------------------*/ +typedef struct __RWBuffer { + char *data; + int length; + int startPtr; + int endPtr; +} RWBuffer; +/*----------------------------------------------------------------------*/ +prwBuffer MakeRWPuffer(int size) +{ + prwBuffer self = NULL; + + self = malloc(sizeof(RWBuffer)); + if (self == NULL) { + return NULL; + } + self->data = calloc(size, sizeof(char)); + if (self->data == NULL) { + return NULL; + } + self->length = size; + self->startPtr = 0; + self->endPtr = 0; + return self; +} + +/*------------------------------------------------------------------------*/ +void KillRWBuffer(prwBuffer self) +{ + if (self == NULL) { + return; + } + if (self->data != NULL) { + free(self->data); + } + free(self); +} + +/*------------------------------------------------------------------------*/ +int StoreRWBuffer(prwBuffer self, void *data, int count) +{ + int length; + + length = self->endPtr - self->startPtr; + if (count + length >= self->length ) { + printf("HELP: RWBuffer overrun!!!!\n"); + return 0; + } + if (count + self->endPtr > self->length) { + memmove(self->data, self->data + self->startPtr, length); + self->startPtr = 0; + self->endPtr = length; + } + memcpy(self->data + self->endPtr, data, count); + self->endPtr += count; + return 1; +} + +/*------------------------------------------------------------------------*/ +void *GetRWBufferData(prwBuffer self, int *length) +{ + *length = self->endPtr - self->startPtr; + return (void *) self->data + self->startPtr; +} + +/*-------------------------------------------------------------------------*/ +void RemoveRWBufferData(prwBuffer self, int count) +{ + self->startPtr += count; + if (self->startPtr >= self->endPtr) { + self->startPtr = 0; + self->endPtr = 0; + } +} diff --git a/rwpuffer.h b/rwpuffer.h new file mode 100644 index 00000000..e69795b4 --- /dev/null +++ b/rwpuffer.h @@ -0,0 +1,46 @@ +/** + * This is a buffer to store bytes for reading and writing. + * + * copyright: see file COPYRIGHT + * + * Mark Koennecke, January 2009 + */ +#ifndef RWPUFFER_H_ +#define RWPUFFER_H_ + +typedef struct __RWBuffer *prwBuffer; + +/** + * \brief create a RW buffer. + * \param size The size of the buffer. + * \return NULL on success, else a pointer to t a new rwPuffer + */ +prwBuffer MakeRWPuffer(int size); +/** + * \brief delete a rw buffer. + * \param self The rwPuffer to delete. + */ +void KillRWBuffer(prwBuffer self); +/** + * \brief store some data in the RW buffer + * \param self The rw buffer to store the data in + * \param data pointer to the data to store + * \param count The number of bytes to store + * \return 1 on success, 0 on failure + */ +int StoreRWBuffer(prwBuffer self, void *data, int count); +/** + * \brief Get a pointer to the current buffer data + * \param self the buffer to get the data from + * \param length Will be set to the number of available bytes. + * \return A pointer to the data + */ +void *GetRWBufferData(prwBuffer self, int *length); +/** + * \brief remove data from the buffer + * \param self the buffer to remove data from + * \param count The number of bytes to remove + */ +void RemoveRWBufferData(prwBuffer self, int count); + +#endif /*RWPUFFER_H_ */