- Added missing files

This commit is contained in:
koennecke
2009-02-17 08:50:23 +00:00
parent e7a985d7e2
commit 03166512fd
2 changed files with 131 additions and 0 deletions

85
rwpuffer.c Normal file
View File

@ -0,0 +1,85 @@
/**
* This is a buffer to store bytes for reading and writing.
*
* copyright: see file COPYRIGHT
*
* Mark Koennecke, January 2009
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#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;
}
}

46
rwpuffer.h Normal file
View File

@ -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_ */