compat: fix clang-analyzer warning
[svn-fe.git] / compat / strbuf.h
blob631e1cc99b2deb3601aa9ec8521483b7668d920e
1 /*
2 * From git 1.7.3.1
3 * License: GPL-2
5 * Modifications (2010-10-19):
6 * - add license header.
7 * - remove unneeded functions.
8 * Modifications (2012-05-18):
9 * - restore functions needed for downstream changes.
12 #ifndef STRBUF_H
13 #define STRBUF_H
16 * Strbuf's can be use in many ways: as a byte array, or to store arbitrary
17 * long, overflow safe strings.
19 * Strbufs has some invariants that are very important to keep in mind:
21 * 1. the ->buf member is always malloc-ed, hence strbuf's can be used to
22 * build complex strings/buffers whose final size isn't easily known.
24 * It is NOT legal to copy the ->buf pointer away.
26 * 2. the ->buf member is a byte array that has at least ->len + 1 bytes
27 * allocated. The extra byte is used to store a '\0', allowing the ->buf
28 * member to be a valid C-string. Every strbuf function ensures this
29 * invariant is preserved.
31 * Note that it is OK to "play" with the buffer directly if you work it
32 * that way:
34 * strbuf_grow(sb, SOME_SIZE);
35 * ... Here, the memory array starting at sb->buf, and of length
36 * ... strbuf_avail(sb) is all yours, and you are sure that
37 * ... strbuf_avail(sb) is at least SOME_SIZE.
38 * strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE);
40 * Doing so is safe, though if it has to be done in many places, adding the
41 * missing API to the strbuf module is the way to go.
43 * XXX: do _not_ assume that the area that is yours is of size ->alloc - 1
44 * even if it's true in the current implementation. Alloc is somehow a
45 * "private" member that should not be messed with.
48 #include <assert.h>
50 extern char strbuf_slopbuf[];
51 struct strbuf {
52 size_t alloc;
53 size_t len;
54 char *buf;
57 #define STRBUF_INIT { 0, 0, strbuf_slopbuf }
59 /*----- strbuf life cycle -----*/
60 extern void strbuf_init(struct strbuf *, size_t);
61 extern void strbuf_release(struct strbuf *);
62 static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
63 struct strbuf tmp = *a;
64 *a = *b;
65 *b = tmp;
68 /*----- strbuf size related -----*/
69 extern void strbuf_grow(struct strbuf *, size_t);
71 static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
72 if (!sb->alloc)
73 strbuf_grow(sb, 0);
74 assert(len < sb->alloc);
75 assert(sb->buf);
76 sb->len = len;
77 sb->buf[len] = '\0';
79 #define strbuf_reset(sb) strbuf_setlen(sb, 0)
81 /*----- add data in your buffer -----*/
82 static inline void strbuf_addch(struct strbuf *sb, int c) {
83 strbuf_grow(sb, 1);
84 sb->buf[sb->len++] = c;
85 sb->buf[sb->len] = '\0';
88 extern void strbuf_add(struct strbuf *, const void *, size_t);
89 static inline void strbuf_addstr(struct strbuf *sb, const char *s) {
90 strbuf_add(sb, s, strlen(s));
93 extern void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t);
94 extern void strbuf_remove(struct strbuf *, size_t pos, size_t len);
96 /* splice pos..pos+len with given data */
97 extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,
98 const void *, size_t);
100 extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
102 #endif /* STRBUF_H */