+/*
+============
+Cvar_Get
+
+Adds a newly allocated variable to the variable list or sets its value.
+============
+*/
+cvar_t *Cvar_Get (const char *name, const char *value, int flags)
+{
+ int hashindex;
+ cvar_t *current, *next, *cvar;
+ size_t alloclen;
+
+ if (developer.integer >= 100)
+ Con_Printf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
+
+// first check to see if it has already been defined
+ cvar = Cvar_FindVar (name);
+ if (cvar)
+ {
+ cvar->flags |= flags;
+ Cvar_SetQuick_Internal (cvar, value);
+ return cvar;
+ }
+
+// check for overlap with a command
+ if (Cmd_Exists (name))
+ {
+ Con_Printf("Cvar_Get: %s is a command\n", name);
+ return NULL;
+ }
+
+// allocate a new cvar, cvar name, and cvar string
+// TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
+// FIXME: these never get Z_Free'd
+ cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
+ cvar->flags = flags | CVAR_ALLOCATED;
+ alloclen = strlen(name) + 1;
+ cvar->name = (char *)Z_Malloc(alloclen);
+ memcpy(cvar->name, name, alloclen);
+ alloclen = strlen(value) + 1;
+ cvar->string = (char *)Z_Malloc(alloclen);
+ memcpy(cvar->string, value, alloclen);
+ cvar->defstring = (char *)Z_Malloc(alloclen);
+ memcpy(cvar->defstring, value, alloclen);
+ cvar->value = atof (cvar->string);
+ cvar->integer = (int) cvar->value;
+ cvar->description = "custom cvar";
+
+// link the variable in
+// alphanumerical order
+ for( current = NULL, next = cvar_vars ; next && strcmp( next->name, cvar->name ) < 0 ; current = next, next = next->next )
+ ;
+ if( current )
+ current->next = cvar;
+ else
+ cvar_vars = cvar;
+ cvar->next = next;
+
+ // link to head of list in this hash table index
+ hashindex = CRC_Block((const unsigned char *)cvar->name, strlen(cvar->name));
+ cvar->nextonhashchain = cvar_hashtable[hashindex];
+ cvar_hashtable[hashindex] = cvar;
+
+ return cvar;
+}
+
+