From 7767312143537bf38fd622f18d0918f629926ca2 Mon Sep 17 00:00:00 2001 From: fc_botelho Date: Wed, 19 Apr 2006 18:44:29 +0000 Subject: [PATCH] buffer_entry.c and buffer_entry.h added --- src/buffer_entry.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++ src/buffer_entry.h | 14 ++++++++ 2 files changed, 100 insertions(+) create mode 100644 src/buffer_entry.c create mode 100644 src/buffer_entry.h diff --git a/src/buffer_entry.c b/src/buffer_entry.c new file mode 100644 index 0000000..bd9f82d --- /dev/null +++ b/src/buffer_entry.c @@ -0,0 +1,86 @@ +#include "buffer_entry.h" +#include +#include +#include + +struct __buffer_entry_t +{ + FILE *fd; + cmph_uint8 * buff; + cmph_uint32 capacity, // buffer entry capacity + nbytes, // buffer entry used bytes + pos; // current read position in buffer entry + cmph_uint8 eof; // flag to indicate end of file +}; + +buffer_entry_t * buffer_entry_new(cmph_uint32 capacity) +{ + buffer_entry_t *buff_entry = (buffer_entry_t *)malloc(sizeof(buffer_entry_t)); + assert(buff_entry); + buff_entry->fd = NULL; + buff_entry->buff = NULL; + buff_entry->capacity = capacity; + buff_entry->nbytes = capacity; + buff_entry->pos = capacity; + buff_entry->eof = 0; + return buff_entry; +} + +void buffer_entry_open(buffer_entry_t * buffer_entry, char * filename) +{ + buffer_entry->fd = fopen(filename, "rb"); +} + +void buffer_entry_set_capacity(buffer_entry_t * buffer_entry, cmph_uint32 capacity) +{ + buffer_entry->capacity = capacity; +} + + +cmph_uint32 buffer_entry_get_capacity(buffer_entry_t * buffer_entry) +{ + return buffer_entry->capacity; +} + +void buffer_entry_load(buffer_entry_t * buffer_entry) +{ + free(buffer_entry->buff); + buffer_entry->buff = (cmph_uint8 *)calloc(buffer_entry->capacity, sizeof(cmph_uint8)); + buffer_entry->nbytes = fread(buffer_entry->buff, 1, buffer_entry->capacity, buffer_entry->fd); + if (buffer_entry->nbytes != buffer_entry->capacity) buffer_entry->eof = 1; + buffer_entry->pos = 0; +} + +cmph_uint8 * buffer_entry_read_key(buffer_entry_t * buffer_entry) +{ + cmph_uint8 * buf = (cmph_uint8 *)malloc(BUFSIZ); + cmph_uint32 buf_pos = 0; + cmph_uint8 c; + while(1) + { + if(buffer_entry->eof && (buffer_entry->pos == buffer_entry->nbytes)) // end + { + free(buf); + return NULL; + } + if(buffer_entry->pos == buffer_entry->nbytes) buffer_entry_load(buffer_entry); + c = buffer_entry->buff[(buffer_entry->pos)++]; + buf[buf_pos++] = c; + if(c == '\0') break; + if(buf_pos % BUFSIZ == 0) buf = (cmph_uint8 *)realloc(buf, buf_pos + BUFSIZ); + } + return buf; +} + +void buffer_entry_destroy(buffer_entry_t * buffer_entry) +{ + fclose(buffer_entry->fd); + buffer_entry->fd = NULL; + free(buffer_entry->buff); + buffer_entry->buff = NULL; + buffer_entry->capacity = 0; + buffer_entry->nbytes = 0; + buffer_entry->pos = 0; + buffer_entry->eof = 0; + free(buffer_entry); +} diff --git a/src/buffer_entry.h b/src/buffer_entry.h new file mode 100644 index 0000000..6f94924 --- /dev/null +++ b/src/buffer_entry.h @@ -0,0 +1,14 @@ +#ifndef __CMPH_BUFFER_ENTRY_H__ +#define __CMPH_BUFFER_ENTRY_H__ + +#include "cmph_types.h" +#include +typedef struct __buffer_entry_t buffer_entry_t; + +buffer_entry_t * buffer_entry_new(cmph_uint32 capacity); +void buffer_entry_set_capacity(buffer_entry_t * buffer_entry, cmph_uint32 capacity); +cmph_uint32 buffer_entry_get_capacity(buffer_entry_t * buffer_entry); +void buffer_entry_open(buffer_entry_t * buffer_entry, char * filename); +cmph_uint8 * buffer_entry_read_key(buffer_entry_t * buffer_entry); +void buffer_entry_destroy(buffer_entry_t * buffer_entry); +#endif