[Piglit] [PATCH 1/3] util: Add piglit_split_string_to_array()
Chad Versace
chad.versace at linux.intel.com
Tue Nov 26 12:46:34 PST 2013
This splits a string into an array of strings, which is useful for
splitting the string returned by eglQueryString(EGL_EXTENSIONS).
Signed-off-by: Chad Versace <chad.versace at linux.intel.com>
---
tests/util/piglit-util-gl-common.c | 23 +----------------------
tests/util/piglit-util.c | 30 ++++++++++++++++++++++++++++++
tests/util/piglit-util.h | 3 +++
3 files changed, 34 insertions(+), 22 deletions(-)
diff --git a/tests/util/piglit-util-gl-common.c b/tests/util/piglit-util-gl-common.c
index 02a9bee..4abbd57 100644
--- a/tests/util/piglit-util-gl-common.c
+++ b/tests/util/piglit-util-gl-common.c
@@ -64,32 +64,11 @@ int piglit_get_gl_version(void)
return 10*major+minor;
}
-static const char** split_string(const char *string)
-{
- char **strings, *string_copy;
- int i, length, max_words;
-
- length = strlen(string);
- max_words = length / 2;
- strings = malloc ((sizeof(char*) * (max_words + 1)) +
- (sizeof(char) * (length + 1)));
- assert (strings != NULL);
-
- string_copy = (char*) &strings[max_words + 1];
- strcpy(string_copy, string);
-
- strings[0] = strtok(string_copy, " ");
- for (i = 0; strings[i] != NULL; ++i)
- strings[i + 1] = strtok(NULL, " ");
-
- return (const char**) strings;
-}
-
static const char** gl_extension_array_from_getstring()
{
const char *gl_extensions_string;
gl_extensions_string = (const char *) glGetString(GL_EXTENSIONS);
- return split_string(gl_extensions_string);
+ return piglit_split_string_to_array(gl_extensions_string, " ");
}
#if defined(PIGLIT_USE_OPENGL)
diff --git a/tests/util/piglit-util.c b/tests/util/piglit-util.c
index 9b7a351..6ddc4ee 100644
--- a/tests/util/piglit-util.c
+++ b/tests/util/piglit-util.c
@@ -105,6 +105,36 @@ int asprintf(char **strp, const char *fmt, ...)
#endif /* _WIN32 */
+/**
+ * \brief Split \a string into an array of strings.
+ *
+ * The null-terminated string \a separators is a list of characters at
+ * which to perform the splits. For example, if separators is " ,", then
+ * the function will split the string at each occurence of ' ' and ','.
+ */
+const char**
+piglit_split_string_to_array(const char *string, const char *separators)
+{
+ char **strings, *string_copy;
+ int i, length, max_words;
+
+ length = strlen(string);
+ max_words = length / 2;
+ strings = malloc((sizeof(char*) * (max_words + 1)) +
+ (sizeof(char) * (length + 1)));
+ assert(strings != NULL);
+
+ string_copy = (char*) &strings[max_words + 1];
+ strcpy(string_copy, string);
+
+ strings[0] = strtok(string_copy, separators);
+ for (i = 0; strings[i] != NULL; ++i) {
+ strings[i + 1] = strtok(NULL, separators);
+ }
+
+ return (const char**) strings;
+}
+
bool piglit_is_extension_in_array(const char **haystack, const char *needle)
{
if (needle[0] == 0)
diff --git a/tests/util/piglit-util.h b/tests/util/piglit-util.h
index f9bde74..9409507 100644
--- a/tests/util/piglit-util.h
+++ b/tests/util/piglit-util.h
@@ -184,6 +184,9 @@ piglit_join_paths(char buf[], size_t buf_size, int n, ...);
int64_t
piglit_get_microseconds(void);
+const char**
+piglit_split_string_to_array(const char *string, const char *separators);
+
#ifdef __cplusplus
} /* end extern "C" */
#endif
--
1.8.4
More information about the Piglit
mailing list