comparison buffer.h @ 0:a20beb164de8

Added buffer.c and buffer.h: Various function to manipulate safely string buffers. It is similar to sbuf(9) on FreeBSD but more portable and simpler.
author David Demelier <markand@malikania.fr>
date Mon, 05 Sep 2011 23:01:29 +0200
parents
children 22d7bb03e569
comparison
equal deleted inserted replaced
-1:000000000000 0:a20beb164de8
1 /*
2 * buffer.h -- safe unlimited size string
3 *
4 * Copyright (c) 2011, David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef HAVE_BUFFER_H
20 #define HAVE_BUFFER_H
21
22 #define BUFFER_DEFAULT_BSIZE 512
23
24 #include <stdarg.h>
25
26 struct buffer {
27 char *data; /* string buffer */
28 size_t length; /* string's length */
29 size_t size; /* current size */
30 size_t bsize; /* block size */
31
32 #define BUFFER_AUTO 0x00000001
33 #define BUFFER_FIXED 0x00000010
34 int flags; /* buffer's flags (default AUTO) */
35 };
36
37 struct buffer *buffer_new(const char *, size_t, int);
38 #define buffer_new_auto(size) buffer_new(NULL, size, BUFFER_AUTO)
39 #define buffer_new_fixed(max) buffer_new(NULL, max, BUFFER_FIXED)
40
41 int buffer_strcat(struct buffer *, const char *);
42 int buffer_bcat(struct buffer *, const void *, size_t);
43 int buffer_vprintf(struct buffer *, const char *, va_list);
44 int buffer_printf(struct buffer *, const char *, ...);
45 int buffer_shrink(struct buffer *);
46 char *buffer_end(struct buffer *);
47 void buffer_clear(struct buffer *);
48 void buffer_free(struct buffer *);
49
50 #endif /* HAVE_BUFFER_H */