2 * libdpkg - Debian packaging suite library routines
3 * varbuf.h - variable length expandable buffer handling
5 * Copyright © 1994, 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008-2015 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
22 #ifndef LIBDPKG_VARBUF_H
23 #define LIBDPKG_VARBUF_H
30 #include <dpkg/macros.h>
35 * @defgroup varbuf Variable length buffers
36 * @ingroup dpkg-public
41 * varbuf_init must be called exactly once before the use of each varbuf
42 * (including before any call to varbuf_destroy), or the variable must be
43 * initialized with VARBUF_INIT.
45 * However, varbufs allocated ‘static’ are properly initialized anyway and
46 * do not need varbuf_init; multiple consecutive calls to varbuf_init before
47 * any use are allowed.
49 * varbuf_destroy must be called after a varbuf is finished with, if anything
50 * other than varbuf_init has been done. After this you are allowed but
51 * not required to call varbuf_init again if you want to start using the
54 * Callers using C++ need not worry about any of this.
61 explicit varbuf(size_t _size
= 0);
63 void init(size_t _size
= 0);
66 int fmt(const char *_fmt
, ...) DPKG_ATTR_PRINTF(2);
67 int vfmt(const char *_fmt
, va_list va
) DPKG_ATTR_VPRINTF(2);
68 void operator()(int c
);
69 void operator()(const char *s
);
74 #define VARBUF_INIT { 0, 0, NULL }
76 #define VARBUF_OBJECT (struct varbuf)VARBUF_INIT
78 struct varbuf
*varbuf_new(size_t size
);
79 void varbuf_init(struct varbuf
*v
, size_t size
);
80 void varbuf_grow(struct varbuf
*v
, size_t need_size
);
81 void varbuf_trunc(struct varbuf
*v
, size_t used_size
);
82 char *varbuf_detach(struct varbuf
*v
);
83 void varbuf_reset(struct varbuf
*v
);
84 void varbuf_destroy(struct varbuf
*v
);
85 void varbuf_free(struct varbuf
*v
);
87 const char *varbuf_str(struct varbuf
*v
);
89 void varbuf_set_varbuf(struct varbuf
*v
, struct varbuf
*other
);
90 void varbuf_set_buf(struct varbuf
*v
, const void *buf
, size_t size
);
91 #define varbuf_set_str(v, s) varbuf_set_buf(v, s, strlen(s))
92 #define varbuf_set_strn(v, s, n) varbuf_set_buf(v, s, strnlen(s, n))
94 void varbuf_add_varbuf(struct varbuf
*v
, const struct varbuf
*other
);
95 void varbuf_add_char(struct varbuf
*v
, int c
);
96 void varbuf_dup_char(struct varbuf
*v
, int c
, size_t n
);
97 void varbuf_map_char(struct varbuf
*v
, int c_src
, int c_dst
);
98 #define varbuf_add_str(v, s) varbuf_add_buf(v, s, strlen(s))
99 #define varbuf_add_strn(v, s, n) varbuf_add_buf(v, s, strnlen(s, n))
100 void varbuf_add_dir(struct varbuf
*v
, const char *dirname
);
101 void varbuf_add_buf(struct varbuf
*v
, const void *s
, size_t size
);
103 bool varbuf_has_prefix(struct varbuf
*v
, struct varbuf
*prefix
);
104 bool varbuf_has_suffix(struct varbuf
*v
, struct varbuf
*suffix
);
105 void varbuf_trim_varbuf_prefix(struct varbuf
*v
, struct varbuf
*prefix
);
106 void varbuf_trim_char_prefix(struct varbuf
*v
, int prefix
);
108 int varbuf_printf(struct varbuf
*v
, const char *fmt
, ...) DPKG_ATTR_PRINTF(2);
109 int varbuf_vprintf(struct varbuf
*v
, const char *fmt
, va_list va
)
110 DPKG_ATTR_VPRINTF(2);
112 struct varbuf_state
{
117 void varbuf_snapshot(struct varbuf
*v
, struct varbuf_state
*vs
);
118 void varbuf_rollback(struct varbuf_state
*vs
);
119 size_t varbuf_rollback_len(struct varbuf_state
*vs
);
120 const char *varbuf_rollback_start(struct varbuf_state
*vs
);
128 varbuf::varbuf(size_t _size
)
130 varbuf_init(this, _size
);
136 varbuf_destroy(this);
140 varbuf::init(size_t _size
)
142 varbuf_init(this, _size
);
154 varbuf_destroy(this);
158 varbuf::fmt(const char *_fmt
, ...)
163 va_start(args
, _fmt
);
164 rc
= varbuf_vprintf(this, _fmt
, args
);
171 varbuf::vfmt(const char *_fmt
, va_list va
)
173 return varbuf_vprintf(this, _fmt
, va
);
177 varbuf::operator()(int c
)
179 varbuf_add_char(this, c
);
183 varbuf::operator()(const char *s
)
185 varbuf_add_str(this, s
);
191 return varbuf_str(this);
195 #endif /* LIBDPKG_VARBUF_H */