]> icculus.org git repositories - divverent/darkplaces.git/blob - cvar.c
qsockets are now dynamically allocated/freed, this drops memory use by about 2mb
[divverent/darkplaces.git] / cvar.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.  
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // cvar.c -- dynamic variable tracking
21
22 #include "quakedef.h"
23
24 cvar_t  *cvar_vars;
25 char    *cvar_null_string = "";
26
27 /*
28 ============
29 Cvar_FindVar
30 ============
31 */
32 cvar_t *Cvar_FindVar (char *var_name)
33 {
34         cvar_t  *var;
35
36         for (var=cvar_vars ; var ; var=var->next)
37                 if (!strcmp (var_name, var->name))
38                         return var;
39
40         return NULL;
41 }
42
43 /*
44 ============
45 Cvar_VariableValue
46 ============
47 */
48 float   Cvar_VariableValue (char *var_name)
49 {
50         cvar_t  *var;
51
52         var = Cvar_FindVar (var_name);
53         if (!var)
54                 return 0;
55         return atof (var->string);
56 }
57
58
59 /*
60 ============
61 Cvar_VariableString
62 ============
63 */
64 char *Cvar_VariableString (char *var_name)
65 {
66         cvar_t *var;
67
68         var = Cvar_FindVar (var_name);
69         if (!var)
70                 return cvar_null_string;
71         return var->string;
72 }
73
74
75 /*
76 ============
77 Cvar_CompleteVariable
78 ============
79 */
80 char *Cvar_CompleteVariable (char *partial)
81 {
82         cvar_t          *cvar;
83         int                     len;
84
85         len = strlen(partial);
86
87         if (!len)
88                 return NULL;
89
90 // check functions
91         for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
92                 if (!strncmp (partial,cvar->name, len))
93                         return cvar->name;
94
95         return NULL;
96 }
97
98
99 /*
100         CVar_CompleteCountPossible
101
102         New function for tab-completion system
103         Added by EvilTypeGuy
104         Thanks to Fett erich@heintz.com
105
106 */
107 int
108 Cvar_CompleteCountPossible (char *partial)
109 {
110         cvar_t  *cvar;
111         int             len;
112         int             h;
113
114         h = 0;
115         len = strlen(partial);
116
117         if (!len)
118                 return  0;
119
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))
123                         h++;
124
125         return h;
126 }
127
128 /*
129         CVar_CompleteBuildList
130
131         New function for tab-completion system
132         Added by EvilTypeGuy
133         Thanks to Fett erich@heintz.com
134         Thanks to taniwha
135
136 */
137 char    **
138 Cvar_CompleteBuildList (char *partial)
139 {
140         cvar_t  *cvar;
141         int             len = 0;
142         int             bpos = 0;
143         int             sizeofbuf = (Cvar_CompleteCountPossible (partial) + 1) * sizeof (char *);
144         char    **buf;
145
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;
152
153         buf[bpos] = NULL;
154         return buf;
155 }
156
157 /*
158 ============
159 Cvar_Set
160 ============
161 */
162 void Cvar_Set (char *var_name, char *value)
163 {
164         cvar_t  *var;
165         qboolean changed;
166
167         var = Cvar_FindVar (var_name);
168         if (!var)
169         {       // there is an error in C code if this happens
170                 Con_Printf ("Cvar_Set: variable %s not found\n", var_name);
171                 return;
172         }
173
174         changed = strcmp(var->string, value);
175
176         Z_Free (var->string);   // free the old value string
177
178         var->string = Z_Malloc (strlen(value)+1);
179         strcpy (var->string, value);
180         var->value = atof (var->string);
181         var->integer = (int) var->value;
182         if ((var->flags & CVAR_NOTIFY) && changed)
183         {
184                 if (sv.active)
185                         SV_BroadcastPrintf ("\"%s\" changed to \"%s\"\n", var->name, var->string);
186         }
187 }
188
189 /*
190 ============
191 Cvar_SetValue
192 ============
193 */
194 void Cvar_SetValue (char *var_name, float value)
195 {
196         char    val[32];
197
198         // LordHavoc: changed from %f to %g to use shortest representation
199         sprintf (val, "%g",value);
200         Cvar_Set (var_name, val);
201 }
202
203 /*
204 ============
205 Cvar_RegisterVariable
206
207 Adds a freestanding variable to the variable list.
208 ============
209 */
210 void Cvar_RegisterVariable (cvar_t *variable)
211 {
212         char    *oldstr;
213
214 // first check to see if it has already been defined
215         if (Cvar_FindVar (variable->name))
216         {
217                 Con_Printf ("Can't register variable %s, already defined\n", variable->name);
218                 return;
219         }
220
221 // check for overlap with a command
222         if (Cmd_Exists (variable->name))
223         {
224                 Con_Printf ("Cvar_RegisterVariable: %s is a command\n", variable->name);
225                 return;
226         }
227
228 // copy the value off, because future sets will Z_Free it
229         oldstr = variable->string;
230         variable->string = Z_Malloc (strlen(variable->string)+1);
231         strcpy (variable->string, oldstr);
232         variable->value = atof (variable->string);
233         variable->integer = (int) variable->value;
234
235 // link the variable in
236         variable->next = cvar_vars;
237         cvar_vars = variable;
238 }
239
240 /*
241 ============
242 Cvar_Command
243
244 Handles variable inspection and changing from the console
245 ============
246 */
247 qboolean        Cvar_Command (void)
248 {
249         cvar_t                  *v;
250
251 // check variables
252         v = Cvar_FindVar (Cmd_Argv(0));
253         if (!v)
254                 return false;
255
256 // perform a variable print or set
257         if (Cmd_Argc() == 1)
258         {
259                 Con_Printf ("\"%s\" is \"%s\"\n", v->name, v->string);
260                 return true;
261         }
262
263         Cvar_Set (v->name, Cmd_Argv(1));
264         return true;
265 }
266
267
268 /*
269 ============
270 Cvar_WriteVariables
271
272 Writes lines containing "set variable value" for all variables
273 with the archive flag set to true.
274 ============
275 */
276 void Cvar_WriteVariables (QFile *f)
277 {
278         cvar_t  *var;
279
280         for (var = cvar_vars ; var ; var = var->next)
281                 if (var->flags & CVAR_SAVE)
282                         Qprintf (f, "%s \"%s\"\n", var->name, var->string);
283 }
284
285
286 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
287 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
288 /*
289 =========
290 Cvar_List
291 =========
292 */
293 void Cvar_List_f (void)
294 {
295         cvar_t  *cvar;
296         char    *partial;
297         int             len;
298         int             count;
299
300         if (Cmd_Argc() > 1) {
301                 partial = Cmd_Argv (1);
302                 len = strlen(partial);
303         } else {
304                 partial = NULL;
305                 len = 0;
306         }
307
308         count = 0;
309         for (cvar = cvar_vars; cvar; cvar = cvar->next) {
310                 if (partial && strncmp (partial,cvar->name,len))
311                         continue;
312
313                 Con_Printf ("%s is \"%s\"\n", cvar->name, cvar->string);
314                 count++;
315         }
316
317         Con_Printf ("%i cvar(s)", count);
318         if (partial)
319                 Con_Printf (" beginning with \"%s\"", partial);
320         Con_Printf ("\n");
321 }
322 // 2000-01-09 CvarList command by Maddes