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
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))
48 float Cvar_VariableValue (char *var_name)
52 var = Cvar_FindVar (var_name);
55 return atof (var->string);
64 char *Cvar_VariableString (char *var_name)
68 var = Cvar_FindVar (var_name);
70 return cvar_null_string;
80 char *Cvar_CompleteVariable (char *partial)
85 len = strlen(partial);
91 for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
92 if (!strncmp (partial,cvar->name, len))
100 CVar_CompleteCountPossible
102 New function for tab-completion system
104 Thanks to Fett erich@heintz.com
108 Cvar_CompleteCountPossible (char *partial)
115 len = strlen(partial);
120 // Loop through the cvars and count all possible matches
121 for (cvar = cvar_vars; cvar; cvar = cvar->next)
122 if (!strncasecmp(partial, cvar->name, len))
129 CVar_CompleteBuildList
131 New function for tab-completion system
133 Thanks to Fett erich@heintz.com
138 Cvar_CompleteBuildList (char *partial)
143 int sizeofbuf = (Cvar_CompleteCountPossible (partial) + 1) * sizeof (char *);
146 len = strlen(partial);
147 buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (char *));
148 // Loop through the alias list and print all matches
149 for (cvar = cvar_vars; cvar; cvar = cvar->next)
150 if (!strncasecmp(partial, cvar->name, len))
151 buf[bpos++] = cvar->name;
162 void Cvar_SetQuick (cvar_t *var, char *value)
168 Con_Printf("Cvar_SetQuick: var == NULL\n");
172 changed = strcmp(var->string, value);
173 // LordHavoc: don't reallocate when there is no change
177 // LordHavoc: don't reallocate when the buffer is the same size
178 if (!var->string || strlen(var->string) != strlen(value))
180 Z_Free (var->string); // free the old value string
182 var->string = Z_Malloc (strlen(value)+1);
184 strcpy (var->string, value);
185 var->value = atof (var->string);
186 var->integer = (int) var->value;
187 if ((var->flags & CVAR_NOTIFY) && changed)
190 SV_BroadcastPrintf ("\"%s\" changed to \"%s\"\n", var->name, var->string);
194 void Cvar_Set (char *var_name, char *value)
197 var = Cvar_FindVar (var_name);
200 // there is an error in C code if this happens
201 Con_Printf ("Cvar_Set: variable %s not found\n", var_name);
205 Cvar_SetQuick(var, value);
213 void Cvar_SetValueQuick (cvar_t *var, float value)
217 // LordHavoc: changed from %f to %g to use shortest representation
218 sprintf (val, "%g",value);
219 Cvar_SetQuick (var, val);
222 void Cvar_SetValue (char *var_name, float value)
226 // LordHavoc: changed from %f to %g to use shortest representation
227 sprintf (val, "%g",value);
228 Cvar_Set (var_name, val);
233 Cvar_RegisterVariable
235 Adds a freestanding variable to the variable list.
238 void Cvar_RegisterVariable (cvar_t *variable)
242 // first check to see if it has already been defined
243 if (Cvar_FindVar (variable->name))
245 Con_Printf ("Can't register variable %s, already defined\n", variable->name);
249 // check for overlap with a command
250 if (Cmd_Exists (variable->name))
252 Con_Printf ("Cvar_RegisterVariable: %s is a command\n", variable->name);
256 // copy the value off, because future sets will Z_Free it
257 oldstr = variable->string;
258 variable->string = Z_Malloc (strlen(variable->string)+1);
259 strcpy (variable->string, oldstr);
260 variable->value = atof (variable->string);
261 variable->integer = (int) variable->value;
263 // link the variable in
264 variable->next = cvar_vars;
265 cvar_vars = variable;
272 Handles variable inspection and changing from the console
275 qboolean Cvar_Command (void)
280 v = Cvar_FindVar (Cmd_Argv(0));
284 // perform a variable print or set
287 Con_Printf ("\"%s\" is \"%s\"\n", v->name, v->string);
291 Cvar_Set (v->name, Cmd_Argv(1));
300 Writes lines containing "set variable value" for all variables
301 with the archive flag set to true.
304 void Cvar_WriteVariables (QFile *f)
308 for (var = cvar_vars ; var ; var = var->next)
309 if (var->flags & CVAR_SAVE)
310 Qprintf (f, "%s \"%s\"\n", var->name, var->string);
314 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
315 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
321 void Cvar_List_f (void)
328 if (Cmd_Argc() > 1) {
329 partial = Cmd_Argv (1);
330 len = strlen(partial);
337 for (cvar = cvar_vars; cvar; cvar = cvar->next) {
338 if (partial && strncmp (partial,cvar->name,len))
341 Con_Printf ("%s is \"%s\"\n", cvar->name, cvar->string);
345 Con_Printf ("%i cvar(s)", count);
347 Con_Printf (" beginning with \"%s\"", partial);
350 // 2000-01-09 CvarList command by Maddes