Mercurial > code
changeset 47:fea13579acbe
Some change in buffer
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 07 Oct 2011 16:10:33 +0200 |
parents | b71851428849 |
children | cdae3689f1b6 |
files | buffer.c buffer.h |
diffstat | 2 files changed, 32 insertions(+), 39 deletions(-) [+] |
line wrap: on
line diff
--- a/buffer.c Wed Oct 05 19:46:31 2011 +0200 +++ b/buffer.c Fri Oct 07 16:10:33 2011 +0200 @@ -26,11 +26,13 @@ static int buffer_grow(struct buffer *, size_t); /* - * Initialize a new buffer with the optional `str' as begin of buffer. + * Create a new buffer with initial size `length'. If type is set to BUFFER_AUTO + * the buffer will grow automatically increasing the size by `length' bytes. On + * BUFFER_FIXED type the buffer won't grow. */ struct buffer * -buffer_new(const char *str, size_t bsize, int flags) +buffer_new(enum buffer_type type, int length) { struct buffer *buf; @@ -38,20 +40,15 @@ return NULL; memset(buf, 0, sizeof (struct buffer)); - buf->bsize = (bsize == 0) ? BUFFER_DEFAULT_BSIZE : bsize; + buf->bsize = (length == 0) ? BUFFER_DEFAULT_BSIZE : length; buf->size = buf->bsize + 1; - buf->flags = flags; + buf->type = type; if ((buf->data = calloc(buf->size, 1)) == NULL) { free(buf); return NULL; } - if (str != NULL && buffer_strcat(buf, str) < 0) { - free(buf); - return NULL; - } - return buf; } @@ -68,7 +65,7 @@ if (buffer_grow(buf, length) < 0) return -1; - if (!(buf->flags & BUFFER_AUTO)) + if (buf->type == BUFFER_AUTO) length = buf->size - buf->length - 1; strncpy(buf->data + buf->length, str, length); @@ -78,7 +75,7 @@ } /* - * Append the character c to the end of buffer. + * Append the character `c' to the end of buffer. */ int @@ -87,7 +84,7 @@ if (buffer_grow(buf, 1) < 0) return -1; - if (!(buf->flags & BUFFER_AUTO) && buf->size - buf->length - 1 <= 2) + if (buf->type != BUFFER_AUTO && buf->size - buf->length - 1 <= 2) return -1; buf->data[buf->length++] = c; @@ -107,7 +104,7 @@ return -1; /* Do not truncate void pointer */ - if (!(buf->flags & BUFFER_AUTO) && size > (buf->size - buf->length - 1)) + if (buf->type == BUFFER_AUTO && size > (buf->size - buf->length - 1)) return -1; memcpy(buf->data + buf->length, data, size); @@ -130,7 +127,7 @@ * vsnprintf on windows returns -1 instead of the number of bytes that * should be printed... */ - if (buf->flags & BUFFER_AUTO) { + if (buf->type == BUFFER_AUTO) { do { va_copy(ap_save, ap); nb = vsnprintf(buf->data + buf->length, @@ -238,7 +235,7 @@ if ((buf->size - buf->length) > needed) return 0; - if (buf->flags & BUFFER_AUTO) { + if (buf->type == BUFFER_AUTO) { newlen = buf->size; while (newlen - buf->length - 1 <= needed) newlen += buf->bsize;
--- a/buffer.h Wed Oct 05 19:46:31 2011 +0200 +++ b/buffer.h Fri Oct 07 16:10:33 2011 +0200 @@ -21,34 +21,30 @@ #define BUFFER_DEFAULT_BSIZE 512 -#define BUFFER_FIXED 0x00 -#define BUFFER_AUTO 0x01 - #include <stdarg.h> -struct buffer { - char *data; /* string buffer */ - size_t length; /* string's length */ - - int flags; /* buffer's flags (default AUTO) */ - - /* Private should not be modified by user */ - size_t size; /* current size */ - size_t bsize; /* block size */ +enum buffer_type { + BUFFER_FIXED = 0, + BUFFER_AUTO = 1 }; -struct buffer *buffer_new(const char *, size_t, int); -#define buffer_new_auto(size) buffer_new(NULL, size, BUFFER_AUTO) -#define buffer_new_fixed(max) buffer_new(NULL, max, BUFFER_FIXED) +struct buffer { + enum buffer_type type; /* buffer type (default AUTO) */ + char *data; /* string buffer */ + size_t length; /* string's real length */ + size_t size; /* current allocated size */ + int bsize; /* initial length */ +}; -int buffer_strcat(struct buffer *, const char *); -int buffer_putc(struct buffer *buf, char); -int buffer_bcat(struct buffer *, const void *, size_t); -int buffer_vprintf(struct buffer *, const char *, va_list); -int buffer_printf(struct buffer *, const char *, ...); -int buffer_shrink(struct buffer *); -char *buffer_end(struct buffer *); -void buffer_clear(struct buffer *); -void buffer_free(struct buffer *); +struct buffer *buffer_new(enum buffer_type, int); +int buffer_strcat(struct buffer *, const char *); +int buffer_putc(struct buffer *buf, char); +int buffer_bcat(struct buffer *, const void *, size_t); +int buffer_vprintf(struct buffer *, const char *, va_list); +int buffer_printf(struct buffer *, const char *, ...); +int buffer_shrink(struct buffer *); +char *buffer_end(struct buffer *); +void buffer_clear(struct buffer *); +void buffer_free(struct buffer *); #endif /* _BUFFER_H_ */