mesa: Simplify logic in get_extension_override()
authorChad Versace <chad.versace@intel.com>
Thu, 27 Jan 2011 09:40:43 +0000 (01:40 -0800)
committerChad Versace <chad.versace@intel.com>
Thu, 27 Jan 2011 00:37:45 +0000 (16:37 -0800)
* Reduce max indentation level from 7 to 3.
* Eliminate counter variables.
* Remove function append().

src/mesa/main/extensions.c

index 35839ae..a7e98cd 100644 (file)
@@ -731,78 +731,57 @@ _mesa_extension_is_enabled( struct gl_context *ctx, const char *name )
 
 
 /**
- * Append string 'b' onto string 'a'.  Free 'a' and return new string.
- */
-static char *
-append(const char *a, const char *b)
-{
-   const GLuint aLen = a ? strlen(a) : 0;
-   const GLuint bLen = b ? strlen(b) : 0;
-   char *s = calloc(1, aLen + bLen + 1);
-   if (s) {
-      if (a)
-         memcpy(s, a, aLen);
-      if (b)
-         memcpy(s + aLen, b, bLen);
-      s[aLen + bLen] = '\0';
-   }
-   if (a)
-      free((void *) a);
-   return s;
-}
-
-
-/**
- * Check the MESA_EXTENSION_OVERRIDE env var.
- * For extension names that are recognized, turn them on.  For extension
- * names that are recognized and prefixed with '-', turn them off.
- * Return a string of the unknown/leftover names.
+ * \brief Apply the \c MESA_EXTENSION_OVERRIDE environment variable.
+ *
+ * \c MESA_EXTENSION_OVERRIDE is a space-separated list of extensions to
+ * enable or disable. The list is processed thus:
+ *    - Disable recognized extension names that are prefixed with '-'.
+ *    - Enable recognized extension names that are not prefixed.
+ *    - Collect unrecognized extension names in a new string.
  *
- * Returnd string needs to be freed.
+ * \return Space-separated list of unrecognized extension names (which must
+ *    be freed). Does not return \c NULL.
  */
 static char *
 get_extension_override( struct gl_context *ctx )
 {
-   const char *envExt = _mesa_getenv("MESA_EXTENSION_OVERRIDE");
-   char *extraExt = NULL;
-   char ext[1000];
-   GLuint extLen = 0;
-   GLuint i;
-   GLboolean disableExt = GL_FALSE;
-
-   if (!envExt)
-      return NULL;
+   const char *env_const= _mesa_getenv("MESA_EXTENSION_OVERRIDE");
+   if (env_const == NULL) {
+      /* Return the empty string rather than NULL. This simplifies the logic
+       * of client functions. */
+      return calloc(1, sizeof(char));
+   }
 
-   for (i = 0; ; i++) {
-      if (envExt[i] == '\0' || envExt[i] == ' ') {
-         /* terminate/process 'ext' if extLen > 0 */
-         if (extLen > 0) {
-            assert(extLen < sizeof(ext));
-            /* enable extension named by 'ext' */
-            ext[extLen] = 0;
-            if (!set_extension(ctx, ext, !disableExt)) {
-               /* unknown extension name, append it to extraExt */
-               if (extraExt) {
-                  extraExt = append(extraExt, " ");
-               }
-               extraExt = append(extraExt, ext);
-            }
-            extLen = 0;
-            disableExt = GL_FALSE;
-         }
-         if (envExt[i] == '\0')
-            break;
-      }
-      else if (envExt[i] == '-') {
-         disableExt = GL_TRUE;
+   /* extra_exts: List of unrecognized extensions. */
+   char *extra_exts = calloc(strlen(env_const), sizeof(char));
+
+   /* Copy env_const because strtok() is destructive. */
+   char *env = strdup(env_const);
+   char *ext;
+   for (ext = strtok(env, " "); ext != NULL; ext = strtok(NULL, " ")) {
+      int enable;
+      switch (ext[0]) {
+      case '-':
+         enable = 0;
+         ++ext;
+         break;
+      default:
+         enable = 1;
+         break;
       }
-      else {
-         /* accumulate this non-space character */
-         ext[extLen++] = envExt[i];
+      int recognized = set_extension(ctx, ext, enable);
+      if (!recognized) {
+         strcat(extra_exts, ext);
+         strcat(extra_exts, " ");
       }
    }
 
-   return extraExt;
+   /* Remove trailing space. */
+   int len  = strlen(extra_exts);
+   if (extra_exts[len - 1] == ' ')
+      extra_exts[len - 1] = '\0';
+
+   return extra_exts;
 }