[Piglit] [PATCH] util: Refactor extension string search code

Ian Romanick idr at freedesktop.org
Fri Dec 9 14:55:14 PST 2011


From: Ian Romanick <ian.d.romanick at intel.com>

Move the strstr-based code from glx_ext_import_context/common.c to a
new utility function called piglit_is_extension_in_string.  Use this
function to implement both piglit_is_extension_supported and
piglit_is_glx_extension_supported.

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
---
This patch should be applied after the glx_ext_import_context series.

 tests/spec/glx_ext_import_context/common.c |   26 ++++---------------
 tests/util/piglit-glx-util.c               |   13 ++-------
 tests/util/piglit-util.c                   |   37 ++++++++++++++++++----------
 tests/util/piglit-util.h                   |   12 +++++++++
 4 files changed, 45 insertions(+), 43 deletions(-)

diff --git a/tests/spec/glx_ext_import_context/common.c b/tests/spec/glx_ext_import_context/common.c
index eeac7ca..d3318bf 100644
--- a/tests/spec/glx_ext_import_context/common.c
+++ b/tests/spec/glx_ext_import_context/common.c
@@ -100,27 +100,13 @@ void GLX_EXT_import_context_setup(void)
 	 */
 	vendor = glXGetClientString(dpy, GLX_VENDOR);
 	if (strcmp("NVIDIA Corporation", vendor) == 0) {
-		const char needle[] = "GLX_EXT_import_context";
-		const unsigned needle_len = sizeof(needle) - 1;
-		const char *haystack;
-		bool found = false;
-
-		haystack = glXGetClientString(dpy, GLX_EXTENSIONS);
-		while (haystack != NULL) {
-			const char *s = strstr(haystack, needle);
-
-			if (s != NULL
-			    && (s[needle_len] == ' '
-				|| s[needle_len] == '\0')) {
-				found = true;
-				break;
-			}
-
-			haystack = s;
-		}
+		const char *const client_extensions =
+			glXGetClientString(dpy, GLX_EXTENSIONS);
 
-		if (!found) {
-			fprintf(stderr, "Test requires %s\n", needle);
+		if (!piglit_is_extension_in_string(client_extensions,
+						   "GLX_EXT_import_context")) {
+			fprintf(stderr,
+				"Test requires GLX_EXT_import_context.\n");
 			piglit_report_result(PIGLIT_SKIP);
 		}
 	} else {
diff --git a/tests/util/piglit-glx-util.c b/tests/util/piglit-glx-util.c
index e9177ee..cb01962 100644
--- a/tests/util/piglit-glx-util.c
+++ b/tests/util/piglit-glx-util.c
@@ -138,18 +138,11 @@ piglit_get_glx_window(Display *dpy, XVisualInfo *visinfo)
 bool
 piglit_is_glx_extension_supported(Display *dpy, const char *name)
 {
-	const char *glx_extension_list;
 	int screen = DefaultScreen(dpy);
+	const char *const glx_extension_list =
+		glXQueryExtensionsString(dpy, screen);
 
-	/* This is a bogus way of checking for the extension.
-	 * Needs more GLEW.
-	 */
-	glx_extension_list = glXQueryExtensionsString(dpy, screen);
-	if (strstr(glx_extension_list, name) == NULL) {
-		return false;
-	}
-
-	return true;
+	return piglit_is_extension_in_string(glx_extension_list, name);
 }
 
 void
diff --git a/tests/util/piglit-util.c b/tests/util/piglit-util.c
index 0f7810e..ad430d0 100644
--- a/tests/util/piglit-util.c
+++ b/tests/util/piglit-util.c
@@ -138,22 +138,33 @@ int piglit_get_gl_version()
 	return 10*major+minor;
 }
 
-bool piglit_is_extension_supported(const char *name)
+bool piglit_is_extension_in_string(const char *haystack, const char *needle)
 {
-	char *extensions;
-	bool found = false;
-	char *i;
-
-	assert(name != NULL);
-	extensions = strdup((const char*) glGetString(GL_EXTENSIONS));
-	for (i = strtok(extensions, " "); i != NULL; i = strtok(NULL, " ")) {
-		if (strcmp(name, i) == 0) {
-			found = true;
-			break;
+	const unsigned needle_len = strlen(needle);
+
+	if (needle_len == 0)
+		return false;
+
+	while (haystack != NULL) {
+		const char *const s = strstr(haystack, needle);
+
+		if (s != NULL
+		    && (s[needle_len] == ' ' || s[needle_len] == '\0')) {
+			return true;
 		}
+
+		haystack = s;
 	}
-	free(extensions);
-	return found;
+
+	return false;
+}
+
+bool piglit_is_extension_supported(const char *name)
+{
+	const char *const extensions =
+		(const char*) glGetString(GL_EXTENSIONS);
+
+	return piglit_is_extension_in_string(extensions, name);
 }
 
 void piglit_require_gl_version(int required_version_times_10)
diff --git a/tests/util/piglit-util.h b/tests/util/piglit-util.h
index 95aedfd..a64b295 100755
--- a/tests/util/piglit-util.h
+++ b/tests/util/piglit-util.h
@@ -140,6 +140,18 @@ bool piglit_is_gles();
 int piglit_get_gl_version();
 
 /**
+ * Determine if an extension is listed in an extension string
+ *
+ * \param haystack   List of all extensions to be searched
+ * \param needle     Extension whose presens is to be detected
+ *
+ * \precondition \c haystack is not null
+ *
+ * \sa piglit_is_extension_supported, piglit_is_glx_extension_supported
+ */
+bool piglit_is_extension_in_string(const char *haystack, const char *needle);
+
+/**
  * \precondition name is not null
  */
 bool piglit_is_extension_supported(const char *name);
-- 
1.7.6.4



More information about the Piglit mailing list