Mercurial > libbuf
annotate buf.h @ 17:2694997c4c90
buf: add a test against SIZE_MAX when doubling
While here, explain more what the code does at some points.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 16 Dec 2020 16:12:31 +0100 |
parents | 9f4f7a266c0b |
children | 939fe74cd80a |
rev | line source |
---|---|
0 | 1 /* |
2 * buf.h -- simple string buffer for C | |
3 * | |
4 * Copyright (c) 2019-2020 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 BUF_H | |
20 #define BUF_H | |
21 | |
22 #include <stdarg.h> | |
23 #include <stdbool.h> | |
24 #include <stddef.h> | |
25 | |
26 #if !defined(BUF_MALLOC) | |
27 # define BUF_MALLOC malloc | |
28 #endif | |
29 | |
30 #if !defined(BUF_REALLOC) | |
31 # define BUF_REALLOC realloc | |
32 #endif | |
33 | |
34 #if !defined(BUF_FREE) | |
35 # define BUF_FREE free | |
36 #endif | |
37 | |
3
9c6b686f797d
misc: add extern C for C++ friends
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
38 #if defined(__cplusplus) |
9c6b686f797d
misc: add extern C for C++ friends
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
39 extern "C" { |
9c6b686f797d
misc: add extern C for C++ friends
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
40 #endif |
9c6b686f797d
misc: add extern C for C++ friends
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
41 |
0 | 42 struct buf { |
15 | 43 char *data; |
44 size_t length; | |
45 size_t capacity; | |
0 | 46 }; |
47 | |
48 void | |
49 buf_init(struct buf *b); | |
50 | |
51 bool | |
52 buf_reserve(struct buf *b, size_t desired); | |
53 | |
54 bool | |
55 buf_resize(struct buf *b, size_t size, char c); | |
56 | |
57 bool | |
58 buf_shrink(struct buf *b); | |
59 | |
60 void | |
61 buf_erase(struct buf *b, size_t pos, size_t count); | |
62 | |
63 bool | |
64 buf_putc(struct buf *b, char c); | |
65 | |
66 bool | |
67 buf_puts(struct buf *b, const char *s); | |
68 | |
69 bool | |
70 buf_printf(struct buf *b, const char *fmt, ...); | |
71 | |
72 bool | |
73 buf_vprintf(struct buf *b, const char *fmt, va_list ap); | |
74 | |
75 bool | |
76 buf_sub(struct buf *b, const struct buf *src, size_t pos, size_t count); | |
77 | |
78 bool | |
79 buf_dup(struct buf *b, const struct buf *src); | |
80 | |
81 void | |
82 buf_clear(struct buf *b); | |
83 | |
84 void | |
85 buf_finish(struct buf *b); | |
86 | |
3
9c6b686f797d
misc: add extern C for C++ friends
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
87 #if defined(__cplusplus) |
9c6b686f797d
misc: add extern C for C++ friends
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
88 } |
9c6b686f797d
misc: add extern C for C++ friends
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
89 #endif |
9c6b686f797d
misc: add extern C for C++ friends
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
90 |
0 | 91 #endif /* !BUF_H */ |