Commit | Line | Data |
---|---|---|
1479465f GJ |
1 | /* |
2 | * libdpkg - Debian packaging suite library routines | |
3 | * varbuf.h - variable length expandable buffer handling | |
4 | * | |
5 | * Copyright © 1994, 1995 Ian Jackson <ijackson@chiark.greenend.org.uk> | |
6 | * Copyright © 2008-2015 Guillem Jover <guillem@debian.org> | |
7 | * | |
8 | * This is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #ifndef LIBDPKG_VARBUF_H | |
23 | #define LIBDPKG_VARBUF_H | |
24 | ||
25 | #include <stddef.h> | |
26 | #include <stdarg.h> | |
27 | #include <string.h> | |
28 | ||
29 | #include <dpkg/macros.h> | |
30 | ||
31 | DPKG_BEGIN_DECLS | |
32 | ||
33 | /** | |
34 | * @defgroup varbuf Variable length buffers | |
35 | * @ingroup dpkg-public | |
36 | * @{ | |
37 | */ | |
38 | ||
39 | /** | |
40 | * varbuf_init must be called exactly once before the use of each varbuf | |
41 | * (including before any call to varbuf_destroy), or the variable must be | |
42 | * initialized with VARBUF_INIT. | |
43 | * | |
44 | * However, varbufs allocated ‘static’ are properly initialized anyway and | |
45 | * do not need varbuf_init; multiple consecutive calls to varbuf_init before | |
46 | * any use are allowed. | |
47 | * | |
48 | * varbuf_destroy must be called after a varbuf is finished with, if anything | |
49 | * other than varbuf_init has been done. After this you are allowed but | |
50 | * not required to call varbuf_init again if you want to start using the | |
51 | * varbuf again. | |
52 | * | |
53 | * Callers using C++ need not worry about any of this. | |
54 | */ | |
55 | struct varbuf { | |
56 | size_t used, size; | |
57 | char *buf; | |
58 | ||
59 | #ifdef __cplusplus | |
60 | varbuf(size_t _size = 0); | |
61 | ~varbuf(); | |
62 | void init(size_t _size = 0); | |
63 | void reset(); | |
64 | void destroy(); | |
65 | int fmt(const char *_fmt, ...) DPKG_ATTR_PRINTF(2); | |
66 | int vfmt(const char *_fmt, va_list va) DPKG_ATTR_VPRINTF(2); | |
67 | void operator()(int c); | |
68 | void operator()(const char *s); | |
69 | const char *string(); | |
70 | #endif | |
71 | }; | |
72 | ||
73 | #define VARBUF_INIT { 0, 0, NULL } | |
74 | ||
75 | void varbuf_init(struct varbuf *v, size_t size); | |
76 | void varbuf_grow(struct varbuf *v, size_t need_size); | |
77 | void varbuf_trunc(struct varbuf *v, size_t used_size); | |
78 | char *varbuf_detach(struct varbuf *v); | |
79 | void varbuf_reset(struct varbuf *v); | |
80 | void varbuf_destroy(struct varbuf *v); | |
81 | ||
82 | void varbuf_add_char(struct varbuf *v, int c); | |
83 | void varbuf_dup_char(struct varbuf *v, int c, size_t n); | |
84 | void varbuf_map_char(struct varbuf *v, int c_src, int c_dst); | |
85 | #define varbuf_add_str(v, s) varbuf_add_buf(v, s, strlen(s)) | |
86 | void varbuf_add_buf(struct varbuf *v, const void *s, size_t size); | |
87 | void varbuf_end_str(struct varbuf *v); | |
88 | const char *varbuf_get_str(struct varbuf *v); | |
89 | ||
90 | int varbuf_printf(struct varbuf *v, const char *fmt, ...) DPKG_ATTR_PRINTF(2); | |
91 | int varbuf_vprintf(struct varbuf *v, const char *fmt, va_list va) | |
92 | DPKG_ATTR_VPRINTF(2); | |
93 | ||
94 | struct varbuf_state { | |
95 | size_t used; | |
96 | }; | |
97 | ||
98 | void varbuf_snapshot(struct varbuf *v, struct varbuf_state *vs); | |
99 | void varbuf_rollback(struct varbuf *v, struct varbuf_state *vs); | |
100 | ||
101 | /** @} */ | |
102 | ||
103 | DPKG_END_DECLS | |
104 | ||
105 | #ifdef __cplusplus | |
106 | inline | |
107 | varbuf::varbuf(size_t _size) | |
108 | { | |
109 | varbuf_init(this, _size); | |
110 | } | |
111 | ||
112 | inline | |
113 | varbuf::~varbuf() | |
114 | { | |
115 | varbuf_destroy(this); | |
116 | } | |
117 | ||
118 | inline void | |
119 | varbuf::init(size_t _size) | |
120 | { | |
121 | varbuf_init(this, _size); | |
122 | } | |
123 | ||
124 | inline void | |
125 | varbuf::reset() | |
126 | { | |
127 | used = 0; | |
128 | } | |
129 | ||
130 | inline void | |
131 | varbuf::destroy() | |
132 | { | |
133 | varbuf_destroy(this); | |
134 | } | |
135 | ||
136 | inline int | |
137 | varbuf::fmt(const char *_fmt, ...) | |
138 | { | |
139 | va_list args; | |
140 | int rc; | |
141 | ||
142 | va_start(args, _fmt); | |
143 | rc = varbuf_vprintf(this, _fmt, args); | |
144 | va_end(args); | |
145 | ||
146 | return rc; | |
147 | } | |
148 | ||
149 | inline int | |
150 | varbuf::vfmt(const char *_fmt, va_list va) | |
151 | { | |
152 | return varbuf_vprintf(this, _fmt, va); | |
153 | } | |
154 | ||
155 | inline void | |
156 | varbuf::operator()(int c) | |
157 | { | |
158 | varbuf_add_char(this, c); | |
159 | } | |
160 | ||
161 | inline void | |
162 | varbuf::operator()(const char *s) | |
163 | { | |
164 | varbuf_add_str(this, s); | |
165 | } | |
166 | ||
167 | inline const char * | |
168 | varbuf::string() | |
169 | { | |
170 | return varbuf_get_str(this); | |
171 | } | |
172 | #endif | |
173 | ||
174 | #endif /* LIBDPKG_VARBUF_H */ |