Debian Codegraph @

code/botlib/l_libvar.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 /*****************************************************************************
24 * name: l_libvar.c
25 *
26 * desc: bot library variables
27 *
28 * $Archive: /MissionPack/code/botlib/l_libvar.c $
29 *
30 *****************************************************************************/
31
32 #include "../qcommon/q_shared.h"
33 #include "l_memory.h"
34 #include "l_libvar.h"
35
36 //list with library variables
37 libvar_t *libvarlist = NULL;
38
39 //===========================================================================
40 //
41 // Parameter: -
42 // Returns: -
43 // Changes Globals: -
44 //===========================================================================
45 float LibVarStringValue(const char *string)
46 {
47 int dotfound = 0;
48 float value = 0;
49
50 while(*string)
51 {
52 if (*string < '0' || *string > '9')
53 {
54 if (dotfound || *string != '.')
55 {
56 return 0;
57 } //end if
58 else
59 {
60 dotfound = 10;
61 string++;
62 } //end if
63 } //end if
64 if (dotfound)
65 {
66 value = value + (float) (*string - '0') / (float) dotfound;
67 dotfound *= 10;
68 } //end if
69 else
70 {
71 value = value * 10.0 + (float) (*string - '0');
72 } //end else
73 string++;
74 } //end while
75 return value;
76 } //end of the function LibVarStringValue
77 //===========================================================================
78 //
79 // Parameter: -
80 // Returns: -
81 // Changes Globals: -
82 //===========================================================================
83 libvar_t *LibVarAlloc(const char *var_name)
84 {
85 libvar_t *v;
86
87 v = (libvar_t *) GetMemory(sizeof(libvar_t));
88 Com_Memset(v, 0, sizeof(libvar_t));
89 v->name = (char *) GetMemory(strlen(var_name)+1);
90 strcpy(v->name, var_name);
91 //add the variable in the list
92 v->next = libvarlist;
93 libvarlist = v;
94 return v;
95 } //end of the function LibVarAlloc
96 //===========================================================================
97 //
98 // Parameter: -
99 // Returns: -
100 // Changes Globals: -
101 //===========================================================================
102 void LibVarDeAlloc(libvar_t *v)
103 {
104 if (v->string) FreeMemory(v->string);
105 FreeMemory(v->name);
106 FreeMemory(v);
107 } //end of the function LibVarDeAlloc
108 //===========================================================================
109 //
110 // Parameter: -
111 // Returns: -
112 // Changes Globals: -
113 //===========================================================================
114 void LibVarDeAllocAll(void)
115 {
116 libvar_t *v;
117
118 for (v = libvarlist; v; v = libvarlist)
119 {
120 libvarlist = libvarlist->next;
121 LibVarDeAlloc(v);
122 } //end for
123 libvarlist = NULL;
124 } //end of the function LibVarDeAllocAll
125 //===========================================================================
126 //
127 // Parameter: -
128 // Returns: -
129 // Changes Globals: -
130 //===========================================================================
131 libvar_t *LibVarGet(const char *var_name)
132 {
133 libvar_t *v;
134
135 for (v = libvarlist; v; v = v->next)
136 {
137 if (!Q_stricmp(v->name, var_name))
138 {
139 return v;
140 } //end if
141 } //end for
142 return NULL;
143 } //end of the function LibVarGet
144 //===========================================================================
145 //
146 // Parameter: -
147 // Returns: -
148 // Changes Globals: -
149 //===========================================================================
150 char *LibVarGetString(const char *var_name)
151 {
152 libvar_t *v;
153
154 v = LibVarGet(var_name);
155 if (v)
156 {
157 return v->string;
158 } //end if
159 else
160 {
161 return "";
162 } //end else
163 } //end of the function LibVarGetString
164 //===========================================================================
165 //
166 // Parameter: -
167 // Returns: -
168 // Changes Globals: -
169 //===========================================================================
170 float LibVarGetValue(const char *var_name)
171 {
172 libvar_t *v;
173
174 v = LibVarGet(var_name);
175 if (v)
176 {
177 return v->value;
178 } //end if
179 else
180 {
181 return 0;
182 } //end else
183 } //end of the function LibVarGetValue
184 //===========================================================================
185 //
186 // Parameter: -
187 // Returns: -
188 // Changes Globals: -
189 //===========================================================================
190 libvar_t *LibVar(const char *var_name, const char *value)
191 {
192 libvar_t *v;
193 v = LibVarGet(var_name);
194 if (v) return v;
195 //create new variable
196 v = LibVarAlloc(var_name);
197 //variable string
198 v->string = (char *) GetMemory(strlen(value) + 1);
199 strcpy(v->string, value);
200 //the value
201 v->value = LibVarStringValue(v->string);
202 //variable is modified
203 v->modified = qtrue;
204 //
205 return v;
206 } //end of the function LibVar
207 //===========================================================================
208 //
209 // Parameter: -
210 // Returns: -
211 // Changes Globals: -
212 //===========================================================================
213 char *LibVarString(const char *var_name, const char *value)
214 {
215 libvar_t *v;
216
217 v = LibVar(var_name, value);
218 return v->string;
219 } //end of the function LibVarString
220 //===========================================================================
221 //
222 // Parameter: -
223 // Returns: -
224 // Changes Globals: -
225 //===========================================================================
226 float LibVarValue(const char *var_name, const char *value)
227 {
228 libvar_t *v;
229
230 v = LibVar(var_name, value);
231 return v->value;
232 } //end of the function LibVarValue
233 //===========================================================================
234 //
235 // Parameter: -
236 // Returns: -
237 // Changes Globals: -
238 //===========================================================================
239 void LibVarSet(const char *var_name, const char *value)
240 {
241 libvar_t *v;
242
243 v = LibVarGet(var_name);
244 if (v)
245 {
246 FreeMemory(v->string);
247 } //end if
248 else
249 {
250 v = LibVarAlloc(var_name);
251 } //end else
252 //variable string
253 v->string = (char *) GetMemory(strlen(value) + 1);
254 strcpy(v->string, value);
255 //the value
256 v->value = LibVarStringValue(v->string);
257 //variable is modified
258 v->modified = qtrue;
259 } //end of the function LibVarSet
260 //===========================================================================
261 //
262 // Parameter: -
263 // Returns: -
264 // Changes Globals: -
265 //===========================================================================
266 qboolean LibVarChanged(const char *var_name)
267 {
268 libvar_t *v;
269
270 v = LibVarGet(var_name);
271 if (v)
272 {
273 return v->modified;
274 } //end if
275 else
276 {
277 return qfalse;
278 } //end else
279 } //end of the function LibVarChanged
280 //===========================================================================
281 //
282 // Parameter: -
283 // Returns: -
284 // Changes Globals: -
285 //===========================================================================
286 void LibVarSetNotModified(const char *var_name)
287 {
288 libvar_t *v;
289
290 v = LibVarGet(var_name);
291 if (v)
292 {
293 v->modified = qfalse;
294 } //end if
295 } //end of the function LibVarSetNotModified