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