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 #include "parser_utils.h"
28 parse_whitespace(const char *s
, const char **rest
)
31 for (; *end
&& *end
!= '\n' && isspace(*end
); end
++);
40 parse_str(const char *s
, const char *lit
, const char **rest
)
43 parse_whitespace(s
, &t
);
44 const bool ret
= strncmp(t
, lit
, strlen(lit
)) == 0;
47 *rest
= (ret
? t
+ strlen(lit
) : s
);
53 parse_ints(const char *s
, int *i
, unsigned n
, const char **rest
)
58 for (j
= 0; j
< n
; j
++) {
59 int v
= strtoll(s
= end
, (char **)&end
, 0);
72 parse_uints(const char *s
, unsigned *u
, unsigned n
, const char **rest
)
77 for (j
= 0; j
< n
; j
++) {
78 unsigned v
= strtoul(s
= end
, (char **)&end
, 0);
91 parse_int64s(const char *s
, int64_t *i
, unsigned n
, const char **rest
)
96 for (j
= 0; j
< n
; j
++) {
100 while (isspace(s
[0]))
103 /* If the user specified a raw hex value, just parse the raw
104 * bit pattern. Hex values that represent negative numbers
105 * would be clamped to INT64_MAX by strtoll (and errno would
109 if (strncmp("0x", s
, 2) == 0)
110 v
= (int64_t) strtoull(s
, (char **)&end
, 0);
112 v
= strtoll(s
, (char **)&end
, 0);
128 parse_uint64s(const char *s
, uint64_t *u
, unsigned n
, const char **rest
)
133 for (j
= 0; j
< n
; j
++) {
134 uint64_t v
= strtoull(s
= end
, (char **)&end
, 0);
147 parse_floats(const char *s
, float *f
, unsigned n
, const char **rest
)
152 for (j
= 0; j
< n
; j
++) {
153 float v
= strtof_hex(s
= end
, (char **)&end
);
166 parse_doubles(const char *s
, double *d
, unsigned n
, const char **rest
)
171 for (j
= 0; j
< n
; j
++) {
172 double v
= strtod_hex(s
= end
, (char **)&end
);
185 parse_word(const char *s
, const char **t
, const char **rest
)
187 parse_whitespace(s
, t
);
189 const char *end
= *t
;
190 for (; *end
&& !isspace(*end
); end
++);
193 *rest
= (*t
!= end
? end
: s
);
199 parse_word_copy(const char *s
, char *t
, unsigned n
, const char **rest
)
201 const char *start
, *end
;
202 const bool ret
= parse_word(s
, &start
, &end
) && end
- start
< n
;
205 memcpy(t
, start
, end
- start
);
209 *rest
= (ret
? end
: s
);
215 parse_enum_gl(const char *s
, GLenum
*e
, const char **rest
)
218 const bool ret
= parse_word_copy(s
, name
, sizeof(name
), rest
);
219 *e
= (ret
? piglit_get_gl_enum_from_name(name
) : GL_NONE
);
224 parse_enum_tab(const struct string_to_enum
*tab
,
225 const char *s
, unsigned *e
, const char **rest
)
228 bool ret
= parse_word(s
, &s
, &end
);
232 for (i
= 0; tab
[i
].name
; i
++) {
233 if (!strncmp(tab
[i
].name
, s
, end
- s
) &&
234 !tab
[i
].name
[end
- s
])
243 *rest
= (ret
? end
: s
);
249 parse_tex_target(const char *s
, unsigned *t
, const char **rest
)
251 static const struct string_to_enum tab
[] = {
252 { "1D", GL_TEXTURE_1D
},
253 { "2D", GL_TEXTURE_2D
},
254 { "3D", GL_TEXTURE_3D
},
255 { "Rect", GL_TEXTURE_RECTANGLE
},
256 { "Cube", GL_TEXTURE_CUBE_MAP
},
257 { "1DArray", GL_TEXTURE_1D_ARRAY
},
258 { "2DArray", GL_TEXTURE_2D_ARRAY
},
259 { "CubeArray", GL_TEXTURE_CUBE_MAP_ARRAY
},
262 return parse_enum_tab(tab
, s
, t
, rest
);
266 parse_comparison_op(const char *s
, enum comparison
*t
, const char **rest
)
268 if (parse_str(s
, "==", rest
)) {
271 } else if (parse_str(s
, "!=", rest
)) {
274 } else if (parse_str(s
, "<=", rest
)) {
277 } else if (parse_str(s
, "<", rest
)) {
280 } else if (parse_str(s
, ">=", rest
)) {
283 } else if (parse_str(s
, ">", rest
)) {