[Piglit] [PATCH 3/9] util: Add piglit_env_var_as_boolean

Jordan Justen jordan.l.justen at intel.com
Sat Jun 9 05:48:01 UTC 2018


This is copied from Mesa's env_var_as_boolean.

Signed-off-by: Jordan Justen <jordan.l.justen at intel.com>
---
 tests/util/piglit-util.c | 27 +++++++++++++++++++++++++++
 tests/util/piglit-util.h |  9 +++++++++
 2 files changed, 36 insertions(+)

diff --git a/tests/util/piglit-util.c b/tests/util/piglit-util.c
index ed7d21c95..3a8f9bcfb 100644
--- a/tests/util/piglit-util.c
+++ b/tests/util/piglit-util.c
@@ -867,3 +867,30 @@ piglit_free_aligned(void *p)
 	free(p);
 #endif
 }
+
+
+/**
+ * \brief Reads an environment variable and interprets its value as a boolean.
+ *
+ * Recognizes 0/false/no and 1/true/yes.  Other values result in the
+ * \a default_value.
+ */
+bool
+piglit_env_var_as_boolean(const char *var_name, bool default_value)
+{
+   const char *str = getenv(var_name);
+   if (str == NULL)
+      return default_value;
+
+   if (strcmp(str, "1") == 0 ||
+       strcasecmp(str, "true") == 0 ||
+       strcasecmp(str, "yes") == 0) {
+      return true;
+   } else if (strcmp(str, "0") == 0 ||
+              strcasecmp(str, "false") == 0 ||
+              strcasecmp(str, "no") == 0) {
+      return false;
+   } else {
+      return default_value;
+   }
+}
diff --git a/tests/util/piglit-util.h b/tests/util/piglit-util.h
index 0a5eab810..53cffedbb 100644
--- a/tests/util/piglit-util.h
+++ b/tests/util/piglit-util.h
@@ -392,6 +392,15 @@ piglit_source_dir(void);
 size_t
 piglit_join_paths(char buf[], size_t buf_size, int n, ...);
 
+/**
+ * \brief Reads an environment variable and interprets its value as a boolean.
+ *
+ * Recognizes 0/false/no and 1/true/yes.  Other values result in the
+ * \a default_value.
+ */
+bool
+piglit_env_var_as_boolean(const char *var_name, bool default_value);
+
 /**
  * \brief Whether piglit_time_get* return monotonically increasing time.
  *
-- 
2.17.1



More information about the Piglit mailing list