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/text.h"
33 #include "atf-c/detail/dynstr.h"
34 #include "atf-c/detail/sanity.h"
35 #include "atf-c/error.h"
38 atf_text_for_each_word(const char *instr, const char *sep,
39 atf_error_t (*func)(const char *, void *),
43 char *str, *str2, *last;
47 err = atf_no_memory_error();
52 str2 = strtok_r(str, sep, &last);
53 while (str2 != NULL && !atf_is_error(err)) {
54 err = func(str2, data);
55 str2 = strtok_r(NULL, sep, &last);
64 atf_text_format(char **dest, const char *fmt, ...)
70 err = atf_text_format_ap(dest, fmt, ap);
77 atf_text_format_ap(char **dest, const char *fmt, va_list ap)
84 err = atf_dynstr_init_ap(&tmp, fmt, ap2);
86 if (!atf_is_error(err))
87 *dest = atf_dynstr_fini_disown(&tmp);
93 atf_text_split(const char *str, const char *delim, atf_list_t *words)
99 err = atf_list_init(words);
100 if (atf_is_error(err))
103 end = str + strlen(str);
110 ptr = strstr(iter, delim);
118 err = atf_dynstr_init_raw(&word, iter, ptr - iter);
119 if (atf_is_error(err))
122 err = atf_list_append(words, atf_dynstr_fini_disown(&word), true);
123 if (atf_is_error(err))
127 iter = ptr + strlen(delim);
130 INV(!atf_is_error(err));
134 atf_list_fini(words);
140 atf_text_to_bool(const char *str, bool *b)
144 if (strcasecmp(str, "yes") == 0 ||
145 strcasecmp(str, "true") == 0) {
147 err = atf_no_error();
148 } else if (strcasecmp(str, "no") == 0 ||
149 strcasecmp(str, "false") == 0) {
151 err = atf_no_error();
153 /* XXX Not really a libc error. */
154 err = atf_libc_error(EINVAL, "Cannot convert string '%s' "
162 atf_text_to_long(const char *str, long *l)
169 tmp = strtol(str, &endptr, 10);
170 if (str[0] == '\0' || *endptr != '\0')
171 err = atf_libc_error(EINVAL, "'%s' is not a number", str);
172 else if (errno == ERANGE || (tmp == LONG_MAX || tmp == LONG_MIN))
173 err = atf_libc_error(ERANGE, "'%s' is out of range", str);
176 err = atf_no_error();