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 struct varbuf *varbuf_new(size_t size);
76 void varbuf_init(struct varbuf *v, size_t size);
77 void varbuf_grow(struct varbuf *v, size_t need_size);
78 void varbuf_trunc(struct varbuf *v, size_t used_size);
79 char *varbuf_detach(struct varbuf *v);
80 void varbuf_reset(struct varbuf *v);
81 void varbuf_destroy(struct varbuf *v);
82 void varbuf_free(struct varbuf *v);
83
84 void varbuf_add_char(struct varbuf *v, int c);
85 void varbuf_dup_char(struct varbuf *v, int c, size_t n);
86 void varbuf_map_char(struct varbuf *v, int c_src, int c_dst);
87 #define varbuf_add_str(v, s) varbuf_add_buf(v, s, strlen(s))
88 void varbuf_add_buf(struct varbuf *v, const void *s, size_t size);
89 void varbuf_end_str(struct varbuf *v);
90 const char *varbuf_get_str(struct varbuf *v);
91
92 int varbuf_printf(struct varbuf *v, const char *fmt, ...) DPKG_ATTR_PRINTF(2);
93 int varbuf_vprintf(struct varbuf *v, const char *fmt, va_list va)
94 DPKG_ATTR_VPRINTF(2);
95
96 struct varbuf_state {
97 size_t used;
98 };
99
100 void varbuf_snapshot(struct varbuf *v, struct varbuf_state *vs);
101 void varbuf_rollback(struct varbuf *v, struct varbuf_state *vs);
102
103 /** @} */
104
105 DPKG_END_DECLS
106
107 #ifdef __cplusplus
108 inline
varbuf(size_t _size)109 varbuf::varbuf(size_t _size)
110 {
111 varbuf_init(this, _size);
112 }
113
114 inline
~varbuf()115 varbuf::~varbuf()
116 {
117 varbuf_destroy(this);
118 }
119
120 inline void
init(size_t _size)121 varbuf::init(size_t _size)
122 {
123 varbuf_init(this, _size);
124 }
125
126 inline void
reset()127 varbuf::reset()
128 {
129 used = 0;
130 }
131
132 inline void
destroy()133 varbuf::destroy()
134 {
135 varbuf_destroy(this);
136 }
137
138 inline int
fmt(const char * _fmt,...)139 varbuf::fmt(const char *_fmt, ...)
140 {
141 va_list args;
142 int rc;
143
144 va_start(args, _fmt);
145 rc = varbuf_vprintf(this, _fmt, args);
146 va_end(args);
147
148 return rc;
149 }
150
151 inline int
vfmt(const char * _fmt,va_list va)152 varbuf::vfmt(const char *_fmt, va_list va)
153 {
154 return varbuf_vprintf(this, _fmt, va);
155 }
156
157 inline void
operator()158 varbuf::operator()(int c)
159 {
160 varbuf_add_char(this, c);
161 }
162
163 inline void
operator()164 varbuf::operator()(const char *s)
165 {
166 varbuf_add_str(this, s);
167 }
168
169 inline const char *
string()170 varbuf::string()
171 {
172 return varbuf_get_str(this);
173 }
174 #endif
175
176 #endif /* LIBDPKG_VARBUF_H */
177