git-svn-id: http://bladebattles.com/kurok/SVN@11 20cd92bb-ff49-0410-b73e-96a06e42c3b9
[kurok.git] / cvar.c
blob5548691b4c1956fe95c8812c1c2b40cc3579f841
1 /*
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
22 #include "quakedef.h"
24 cvar_t *cvar_vars;
25 char *cvar_null_string = "";
28 ============
29 Cvar_FindVar
30 ============
32 cvar_t *Cvar_FindVar (char *var_name)
34 cvar_t *var;
36 for (var=cvar_vars ; var ; var=var->next)
37 if (!Q_strcmp (var_name, var->name))
38 return var;
40 return NULL;
44 ============
45 Cvar_VariableValue
46 ============
48 float Cvar_VariableValue (char *var_name)
50 cvar_t *var;
52 var = Cvar_FindVar (var_name);
53 if (!var)
54 return 0;
55 return Q_atof (var->string);
60 ============
61 Cvar_VariableString
62 ============
64 char *Cvar_VariableString (char *var_name)
66 cvar_t *var;
68 var = Cvar_FindVar (var_name);
69 if (!var)
70 return cvar_null_string;
71 return var->string;
76 ============
77 Cvar_CompleteVariable
78 ============
80 char *Cvar_CompleteVariable (char *partial)
82 cvar_t *cvar;
83 int len;
85 len = Q_strlen(partial);
87 if (!len)
88 return NULL;
90 // check functions
91 for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
92 if (!Q_strncmp (partial,cvar->name, len))
93 return cvar->name;
95 return NULL;
100 ============
101 Cvar_Set
102 ============
104 void Cvar_Set (char *var_name, char *value)
106 cvar_t *var;
107 qboolean changed;
109 var = Cvar_FindVar (var_name);
110 if (!var)
111 { // there is an error in C code if this happens
112 Con_Printf ("Cvar_Set: variable %s not found\n", var_name);
113 return;
116 changed = Q_strcmp(var->string, value);
118 Z_Free (var->string); // free the old value string
120 var->string = Z_Malloc (Q_strlen(value)+1);
121 Q_strcpy (var->string, value);
122 var->value = Q_atof (var->string);
123 if (var->server && changed)
125 if (sv.active)
126 SV_BroadcastPrintf ("\"%s\" changed to \"%s\"\n", var->name, var->string);
131 ============
132 Cvar_SetValue
133 ============
135 void Cvar_SetValue (char *var_name, float value)
137 char val[32];
139 if (value == (int)value)
140 sprintf (val, "%d", (int)value);
141 else
142 sprintf (val, "%f", value);
143 Cvar_Set (var_name, val);
148 ============
149 Cvar_RegisterVariable
151 Adds a freestanding variable to the variable list.
152 ============
154 void Cvar_RegisterVariable (cvar_t *variable)
156 char *oldstr;
158 // first check to see if it has allready been defined
159 if (Cvar_FindVar (variable->name))
161 Con_Printf ("Can't register variable %s, allready defined\n", variable->name);
162 return;
165 // check for overlap with a command
166 if (Cmd_Exists (variable->name))
168 Con_Printf ("Cvar_RegisterVariable: %s is a command\n", variable->name);
169 return;
172 // copy the value off, because future sets will Z_Free it
173 oldstr = variable->string;
174 variable->string = Z_Malloc (Q_strlen(variable->string)+1);
175 Q_strcpy (variable->string, oldstr);
176 variable->value = Q_atof (variable->string);
178 // link the variable in
179 variable->next = cvar_vars;
180 cvar_vars = variable;
184 ============
185 Cvar_Command
187 Handles variable inspection and changing from the console
188 ============
190 qboolean Cvar_Command (void)
192 cvar_t *v;
194 // check variables
195 v = Cvar_FindVar (Cmd_Argv(0));
196 if (!v)
197 return false;
199 // perform a variable print or set
200 if (Cmd_Argc() == 1)
202 Con_Printf ("\"%s\" is \"%s\"\n", v->name, v->string);
203 return true;
206 Cvar_Set (v->name, Cmd_Argv(1));
207 return true;
212 ============
213 Cvar_WriteVariables
215 Writes lines containing "set variable value" for all variables
216 with the archive flag set to true.
217 ============
219 void Cvar_WriteVariables (FILE *f)
221 cvar_t *var;
223 for (var = cvar_vars ; var ; var = var->next)
224 if (var->archive)
225 fprintf (f, "%s \"%s\"\n", var->name, var->string);