2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // cvar.c -- dynamic variable tracking
24 cvar_t *cvar_vars = NULL;
25 char *cvar_null_string = "";
32 cvar_t *Cvar_FindVar (char *var_name)
36 for (var = cvar_vars;var;var = var->next)
37 if (!strcmp (var_name, var->name))
43 cvar_t *Cvar_FindVarAfter (char *prev_var_name, int neededflags)
49 var = Cvar_FindVar (prev_var_name);
57 // search for the next cvar matching the needed flags
60 if ((var->flags & neededflags) || !neededflags)
72 float Cvar_VariableValue (char *var_name)
76 var = Cvar_FindVar (var_name);
79 return atof (var->string);
88 char *Cvar_VariableString (char *var_name)
92 var = Cvar_FindVar (var_name);
94 return cvar_null_string;
101 Cvar_CompleteVariable
104 char *Cvar_CompleteVariable (char *partial)
109 len = strlen(partial);
115 for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
116 if (!strncmp (partial,cvar->name, len))
124 CVar_CompleteCountPossible
126 New function for tab-completion system
128 Thanks to Fett erich@heintz.com
132 Cvar_CompleteCountPossible (char *partial)
139 len = strlen(partial);
144 // Loop through the cvars and count all possible matches
145 for (cvar = cvar_vars; cvar; cvar = cvar->next)
146 if (!strncasecmp(partial, cvar->name, len))
153 CVar_CompleteBuildList
155 New function for tab-completion system
157 Thanks to Fett erich@heintz.com
162 Cvar_CompleteBuildList (char *partial)
167 int sizeofbuf = (Cvar_CompleteCountPossible (partial) + 1) * sizeof (char *);
170 len = strlen(partial);
171 buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (char *));
172 // Loop through the alias list and print all matches
173 for (cvar = cvar_vars; cvar; cvar = cvar->next)
174 if (!strncasecmp(partial, cvar->name, len))
175 buf[bpos++] = cvar->name;
186 void Cvar_SetQuick (cvar_t *var, char *value)
192 Con_Printf("Cvar_SetQuick: var == NULL\n");
196 changed = strcmp(var->string, value);
197 // LordHavoc: don't reallocate when there is no change
201 // LordHavoc: don't reallocate when the buffer is the same size
202 if (!var->string || strlen(var->string) != strlen(value))
204 Z_Free (var->string); // free the old value string
206 var->string = Z_Malloc (strlen(value)+1);
208 strcpy (var->string, value);
209 var->value = atof (var->string);
210 var->integer = (int) var->value;
211 if ((var->flags & CVAR_NOTIFY) && changed)
214 SV_BroadcastPrintf ("\"%s\" changed to \"%s\"\n", var->name, var->string);
218 void Cvar_Set (char *var_name, char *value)
221 var = Cvar_FindVar (var_name);
224 // there is an error in C code if this happens
225 Con_Printf ("Cvar_Set: variable %s not found\n", var_name);
229 Cvar_SetQuick(var, value);
237 void Cvar_SetValueQuick (cvar_t *var, float value)
241 // LordHavoc: changed from %f to %g to use shortest representation
242 sprintf (val, "%g",value);
243 Cvar_SetQuick (var, val);
246 void Cvar_SetValue (char *var_name, float value)
250 // LordHavoc: changed from %f to %g to use shortest representation
251 sprintf (val, "%g",value);
252 Cvar_Set (var_name, val);
257 Cvar_RegisterVariable
259 Adds a freestanding variable to the variable list.
262 void Cvar_RegisterVariable (cvar_t *variable)
266 // first check to see if it has already been defined
267 if (Cvar_FindVar (variable->name))
269 Con_Printf ("Can't register variable %s, already defined\n", variable->name);
273 // check for overlap with a command
274 if (Cmd_Exists (variable->name))
276 Con_Printf ("Cvar_RegisterVariable: %s is a command\n", variable->name);
280 // copy the value off, because future sets will Z_Free it
281 oldstr = variable->string;
282 variable->string = Z_Malloc (strlen(variable->string)+1);
283 strcpy (variable->string, oldstr);
284 variable->value = atof (variable->string);
285 variable->integer = (int) variable->value;
287 // link the variable in
288 variable->next = cvar_vars;
289 cvar_vars = variable;
296 Handles variable inspection and changing from the console
299 qboolean Cvar_Command (void)
304 v = Cvar_FindVar (Cmd_Argv(0));
308 // perform a variable print or set
311 Con_Printf ("\"%s\" is \"%s\"\n", v->name, v->string);
315 Cvar_Set (v->name, Cmd_Argv(1));
324 Writes lines containing "set variable value" for all variables
325 with the archive flag set to true.
328 void Cvar_WriteVariables (QFile *f)
332 for (var = cvar_vars ; var ; var = var->next)
333 if (var->flags & CVAR_SAVE)
334 Qprintf (f, "%s \"%s\"\n", var->name, var->string);
338 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
339 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
345 void Cvar_List_f (void)
352 if (Cmd_Argc() > 1) {
353 partial = Cmd_Argv (1);
354 len = strlen(partial);
361 for (cvar = cvar_vars; cvar; cvar = cvar->next) {
362 if (partial && strncmp (partial,cvar->name,len))
365 Con_Printf ("%s is \"%s\"\n", cvar->name, cvar->string);
369 Con_Printf ("%i cvar(s)", count);
371 Con_Printf (" beginning with \"%s\"", partial);
374 // 2000-01-09 CvarList command by Maddes