parent
5dac5eb451
commit
d7c623b8c7
2 changed files with 15 additions and 15 deletions
16
src/buffer.c
16
src/buffer.c
|
@ -22,9 +22,9 @@
|
||||||
#include "buffer.h"
|
#include "buffer.h"
|
||||||
#include "xalloc.h"
|
#include "xalloc.h"
|
||||||
|
|
||||||
void buffer_compact(buffer_t *buffer, size_t maxsize) {
|
void buffer_compact(buffer_t *buffer, int maxsize) {
|
||||||
if(buffer->len >= maxsize || buffer->offset / 7 > buffer->len / 8) {
|
if(buffer->len >= maxsize || buffer->offset / 7 > buffer->len / 8) {
|
||||||
memmove(buffer->data, buffer->data + buffer->offset, (size_t)(buffer->len - buffer->offset));
|
memmove(buffer->data, buffer->data + buffer->offset, buffer->len - buffer->offset);
|
||||||
buffer->len -= buffer->offset;
|
buffer->len -= buffer->offset;
|
||||||
buffer->offset = 0;
|
buffer->offset = 0;
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ void buffer_compact(buffer_t *buffer, size_t maxsize) {
|
||||||
|
|
||||||
// Make sure we can add size bytes to the buffer, and return a pointer to the start of those bytes.
|
// Make sure we can add size bytes to the buffer, and return a pointer to the start of those bytes.
|
||||||
|
|
||||||
char *buffer_prepare(buffer_t *buffer, size_t size) {
|
char *buffer_prepare(buffer_t *buffer, int size) {
|
||||||
if(!buffer->data) {
|
if(!buffer->data) {
|
||||||
buffer->maxlen = size;
|
buffer->maxlen = size;
|
||||||
buffer->data = xmalloc(size);
|
buffer->data = xmalloc(size);
|
||||||
|
@ -58,7 +58,7 @@ char *buffer_prepare(buffer_t *buffer, size_t size) {
|
||||||
|
|
||||||
// Copy data into the buffer.
|
// Copy data into the buffer.
|
||||||
|
|
||||||
void buffer_add(buffer_t *buffer, const char *data, size_t size) {
|
void buffer_add(buffer_t *buffer, const char *data, int size) {
|
||||||
memcpy(buffer_prepare(buffer, size), data, size);
|
memcpy(buffer_prepare(buffer, size), data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ void buffer_add(buffer_t *buffer, const char *data, size_t size) {
|
||||||
static char *buffer_consume(buffer_t *buffer, int size) {
|
static char *buffer_consume(buffer_t *buffer, int size) {
|
||||||
char *start = buffer->data + buffer->offset;
|
char *start = buffer->data + buffer->offset;
|
||||||
|
|
||||||
buffer->offset += (size_t)size;
|
buffer->offset += size;
|
||||||
|
|
||||||
if(buffer->offset >= buffer->len) {
|
if(buffer->offset >= buffer->len) {
|
||||||
buffer->offset = 0;
|
buffer->offset = 0;
|
||||||
|
@ -85,18 +85,18 @@ char *buffer_readline(buffer_t *buffer) {
|
||||||
if(!newline)
|
if(!newline)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
int len = (int)(newline + 1 - (buffer->data + buffer->offset));
|
int len = newline + 1 - (buffer->data + buffer->offset);
|
||||||
*newline = 0;
|
*newline = 0;
|
||||||
return buffer_consume(buffer, len);
|
return buffer_consume(buffer, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we have enough bytes in the buffer, and if so, return a pointer to the start of them.
|
// Check if we have enough bytes in the buffer, and if so, return a pointer to the start of them.
|
||||||
|
|
||||||
char *buffer_read(buffer_t *buffer, size_t size) {
|
char *buffer_read(buffer_t *buffer, int size) {
|
||||||
if(buffer->len - buffer->offset < size)
|
if(buffer->len - buffer->offset < size)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return buffer_consume(buffer, (int)size);
|
return buffer_consume(buffer, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void buffer_clear(buffer_t *buffer) {
|
void buffer_clear(buffer_t *buffer) {
|
||||||
|
|
14
src/buffer.h
14
src/buffer.h
|
@ -3,16 +3,16 @@
|
||||||
|
|
||||||
typedef struct buffer_t {
|
typedef struct buffer_t {
|
||||||
char *data;
|
char *data;
|
||||||
size_t maxlen;
|
int maxlen;
|
||||||
size_t len;
|
int len;
|
||||||
unsigned int offset;
|
int offset;
|
||||||
} buffer_t;
|
} buffer_t;
|
||||||
|
|
||||||
extern void buffer_compact(buffer_t *buffer, size_t maxsize);
|
extern void buffer_compact(buffer_t *buffer, int maxsize);
|
||||||
extern char *buffer_prepare(buffer_t *buffer, size_t size);
|
extern char *buffer_prepare(buffer_t *buffer, int size);
|
||||||
extern void buffer_add(buffer_t *buffer, const char *data, size_t size);
|
extern void buffer_add(buffer_t *buffer, const char *data, int size);
|
||||||
extern char *buffer_readline(buffer_t *buffer);
|
extern char *buffer_readline(buffer_t *buffer);
|
||||||
extern char *buffer_read(buffer_t *buffer, size_t size);
|
extern char *buffer_read(buffer_t *buffer, int size);
|
||||||
extern void buffer_clear(buffer_t *buffer);
|
extern void buffer_clear(buffer_t *buffer);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue