1 /* Copyright (c) 2008 The NetBSD Foundation, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
26 #include "atf-c/detail/dynstr.h"
35 #include "atf-c/detail/sanity.h"
36 #include "atf-c/detail/text.h"
37 #include "atf-c/error.h"
39 /* ---------------------------------------------------------------------
40 * Auxiliary functions.
41 * --------------------------------------------------------------------- */
45 resize(atf_dynstr_t *ad, size_t newsize)
50 PRE(newsize > ad->m_datasize);
52 newdata = (char *)malloc(newsize);
53 if (newdata == NULL) {
54 err = atf_no_memory_error();
56 strcpy(newdata, ad->m_data);
59 ad->m_datasize = newsize;
68 prepend_or_append(atf_dynstr_t *ad, const char *fmt, va_list ap,
77 err = atf_text_format_ap(&aux, fmt, ap2);
79 if (atf_is_error(err))
81 newlen = ad->m_length + strlen(aux);
83 if (newlen + sizeof(char) > ad->m_datasize) {
84 err = resize(ad, newlen + sizeof(char));
85 if (atf_is_error(err))
90 memmove(ad->m_data + strlen(aux), ad->m_data, ad->m_length + 1);
91 memcpy(ad->m_data, aux, strlen(aux));
93 strcpy(ad->m_data + ad->m_length, aux);
94 ad->m_length = newlen;
103 /* ---------------------------------------------------------------------
104 * The "atf_dynstr" type.
105 * --------------------------------------------------------------------- */
111 const size_t atf_dynstr_npos = SIZE_MAX;
114 * Constructors and destructors.
118 atf_dynstr_init(atf_dynstr_t *ad)
122 ad->m_data = (char *)malloc(sizeof(char));
123 if (ad->m_data == NULL) {
124 err = atf_no_memory_error();
128 ad->m_data[0] = '\0';
131 err = atf_no_error();
138 atf_dynstr_init_ap(atf_dynstr_t *ad, const char *fmt, va_list ap)
142 ad->m_datasize = strlen(fmt) + 1;
150 ad->m_data = (char *)malloc(ad->m_datasize);
151 if (ad->m_data == NULL) {
152 err = atf_no_memory_error();
157 ret = vsnprintf(ad->m_data, ad->m_datasize, fmt, ap2);
161 err = atf_libc_error(errno, "Cannot format string");
166 if ((size_t)ret >= ad->m_datasize) {
171 } while (ad->m_length >= ad->m_datasize);
173 err = atf_no_error();
175 POST(atf_is_error(err) || ad->m_data != NULL);
180 atf_dynstr_init_fmt(atf_dynstr_t *ad, const char *fmt, ...)
186 err = atf_dynstr_init_ap(ad, fmt, ap);
193 atf_dynstr_init_raw(atf_dynstr_t *ad, const void *mem, size_t memlen)
197 if (memlen >= SIZE_MAX - 1) {
198 err = atf_no_memory_error();
202 ad->m_data = (char *)malloc(memlen + 1);
203 if (ad->m_data == NULL) {
204 err = atf_no_memory_error();
208 ad->m_datasize = memlen + 1;
209 memcpy(ad->m_data, mem, memlen);
210 ad->m_data[memlen] = '\0';
211 ad->m_length = strlen(ad->m_data);
212 INV(ad->m_length <= memlen);
213 err = atf_no_error();
220 atf_dynstr_init_rep(atf_dynstr_t *ad, size_t len, char ch)
224 if (len == SIZE_MAX) {
225 err = atf_no_memory_error();
229 ad->m_datasize = (len + 1) * sizeof(char);
230 ad->m_data = (char *)malloc(ad->m_datasize);
231 if (ad->m_data == NULL) {
232 err = atf_no_memory_error();
236 memset(ad->m_data, ch, len);
237 ad->m_data[len] = '\0';
239 err = atf_no_error();
246 atf_dynstr_init_substr(atf_dynstr_t *ad, const atf_dynstr_t *src,
247 size_t beg, size_t end)
249 if (beg > src->m_length)
252 if (end == atf_dynstr_npos || end > src->m_length)
255 return atf_dynstr_init_raw(ad, src->m_data + beg, end - beg);
259 atf_dynstr_copy(atf_dynstr_t *dest, const atf_dynstr_t *src)
263 dest->m_data = (char *)malloc(src->m_datasize);
264 if (dest->m_data == NULL)
265 err = atf_no_memory_error();
267 memcpy(dest->m_data, src->m_data, src->m_datasize);
268 dest->m_datasize = src->m_datasize;
269 dest->m_length = src->m_length;
270 err = atf_no_error();
277 atf_dynstr_fini(atf_dynstr_t *ad)
279 INV(ad->m_data != NULL);
284 atf_dynstr_fini_disown(atf_dynstr_t *ad)
286 INV(ad->m_data != NULL);
295 atf_dynstr_cstring(const atf_dynstr_t *ad)
301 atf_dynstr_length(const atf_dynstr_t *ad)
307 atf_dynstr_rfind_ch(const atf_dynstr_t *ad, char ch)
311 for (pos = ad->m_length; pos > 0 && ad->m_data[pos - 1] != ch; pos--)
314 return pos == 0 ? atf_dynstr_npos : pos - 1;
322 atf_dynstr_append_ap(atf_dynstr_t *ad, const char *fmt, va_list ap)
328 err = prepend_or_append(ad, fmt, ap2, false);
335 atf_dynstr_append_fmt(atf_dynstr_t *ad, const char *fmt, ...)
341 err = prepend_or_append(ad, fmt, ap, false);
348 atf_dynstr_clear(atf_dynstr_t *ad)
350 ad->m_data[0] = '\0';
355 atf_dynstr_prepend_ap(atf_dynstr_t *ad, const char *fmt, va_list ap)
361 err = prepend_or_append(ad, fmt, ap2, true);
368 atf_dynstr_prepend_fmt(atf_dynstr_t *ad, const char *fmt, ...)
374 err = prepend_or_append(ad, fmt, ap, true);
385 atf_equal_dynstr_cstring(const atf_dynstr_t *ad, const char *str)
387 return strcmp(ad->m_data, str) == 0;
391 atf_equal_dynstr_dynstr(const atf_dynstr_t *s1, const atf_dynstr_t *s2)
393 return strcmp(s1->m_data, s2->m_data) == 0;