2 * Copyright © 2016 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file parser_utils.h
27 * These are a bunch of plain-text parsing utilities, most of them
29 * boolean-like parse_foo(input-string, output-foo, output-string)
31 * If the input is a well-formed string representation of a "foo"
32 * value, as many characters will be read from the string as they are
33 * needed to initialize the "foo" object returned via the first output
34 * argument, and the boolean return value will evaluate to true. If
35 * the output string argument is not NULL, a pointer one past the last
36 * character consumed to parse a "foo" value will be returned to the
37 * caller so that the rest of the document can be processed (e.g. by
38 * passing the output string as input string of another parse
41 * If the input cannot be parsed as a "foo" object, the boolean return
42 * value will evaluate to false and the input string will be returned
43 * as output string as-is (which mimics the behavior of the C
44 * standard library strto* functions). The "foo" output argument will
45 * be left in an undefined state in that case.
47 #ifndef PIGLIT_PARSER_UTILS_H
48 #define PIGLIT_PARSER_UTILS_H
51 #include "piglit-util-gl.h"
54 * Parse one or more whitespace characters (other than newline) from
58 parse_whitespace(const char *s
, const char **rest
);
61 * Parse an exact match of string \p lit, optionally preceded by
65 parse_str(const char *s
, const char *lit
, const char **rest
);
68 * Parse up to \p n whitespace-separated signed integer values. The
69 * number of values actually parsed is returned as result.
72 parse_ints(const char *s
, int *i
, unsigned n
, const char **rest
);
75 parse_int(const char *s
, int *i
, const char **rest
)
77 return parse_ints(s
, i
, 1, rest
);
81 * Parse up to \p n whitespace-separated unsigned integer values. The
82 * number of values actually parsed is returned as result.
85 parse_uints(const char *s
, unsigned *u
, unsigned n
, const char **rest
);
88 parse_uint(const char *s
, unsigned *u
, const char **rest
)
90 return parse_uints(s
, u
, 1, rest
);
94 * Parse up to \p n whitespace-separated signed 64-bit integer values.
95 * The number of values actually parsed is returned as result.
98 parse_int64s(const char *s
, int64_t *i
, unsigned n
, const char **rest
);
101 parse_int64(const char *s
, int64_t *i
, const char **rest
)
103 return parse_int64s(s
, i
, 1, rest
);
107 * Parse up to \p n whitespace-separated unsigned 64-bit integer
108 * values. The number of values actually parsed is returned as
112 parse_uint64s(const char *s
, uint64_t *u
, unsigned n
, const char **rest
);
115 parse_uint64(const char *s
, uint64_t *u
, const char **rest
)
117 return parse_uint64s(s
, u
, 1, rest
);
121 * Parse up to \p n whitespace-separated floating point values. The
122 * number of values actually parsed is returned as result.
125 parse_floats(const char *s
, float *f
, unsigned n
, const char **rest
);
128 parse_float(const char *s
, float *f
, const char **rest
)
130 return parse_floats(s
, f
, 1, rest
);
134 * Parse up to \p n whitespace-separated double-precision floating
135 * point values. The number of values actually parsed is returned as
139 parse_doubles(const char *s
, double *d
, unsigned n
, const char **rest
);
142 parse_double(const char *s
, double *d
, const char **rest
)
144 return parse_doubles(s
, d
, 1, rest
);
148 * Parse a single non-empty whitespace-separated token. On success \p
149 * t and \p rest will respectively point at the first and one past the
150 * last character of the result.
153 parse_word(const char *s
, const char **t
, const char **rest
);
156 * Like parse_word(), but the result is copied into the fixed-size
157 * buffer pointed to by \p t and null-terminated.
159 * The parse is considered to fail if the size of the result
160 * (including the terminating null character) would have exceeded the
161 * number of characters allocated for it in the buffer as given by the
165 parse_word_copy(const char *s
, char *t
, unsigned n
, const char **rest
);
168 * Parse a GL_* symbolic constant.
171 parse_enum_gl(const char *s
, GLenum
*e
, const char **rest
);
173 struct string_to_enum
{
179 * Parse a whitespace-delimited symbolic constant from the set
180 * specified in the \p tab argument pointing to a zero-terminated
181 * array of string-value pairs.
184 parse_enum_tab(const struct string_to_enum
*tab
,
185 const char *s
, unsigned *e
, const char **rest
);
188 * Parse a texture target symbolic constant.
191 parse_tex_target(const char *s
, GLenum
*t
, const char **rest
);
203 * Parse a binary comparison operator.
205 bool parse_comparison_op(const char *s
, enum comparison
*t
, const char **rest
);
208 * Abort the Piglit test with failure status if the boolean expression
209 * (typically the result of a chain of parse function calls) evaluates
212 #define REQUIRE(b, ...) do { \
214 fprintf(stderr, __VA_ARGS__); \
215 piglit_report_result(PIGLIT_FAIL); \