Mercurial > code
view buffer.h @ 15:520939b40002
Do not increate buf->size on realloc failure
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 08 Sep 2011 13:34:13 +0200 |
parents | 9ee89108528b |
children | ae4128d16c92 |
line wrap: on
line source
/* * buffer.h -- safe unlimited size string * * Copyright (c) 2011, David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifndef _BUFFER_H_ #define _BUFFER_H_ #define BUFFER_DEFAULT_BSIZE 512 #include <stdarg.h> struct buffer { char *data; /* string buffer */ size_t length; /* string's length */ #define BUFFER_AUTO 0x00000000 #define BUFFER_FIXED 0x00000001 int flags; /* buffer's flags (default AUTO) */ /* Private should not be modified by user */ size_t size; /* current size */ size_t bsize; /* block size */ }; 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) 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_ */