Debian Codegraph @

prboom2/src/sc_man.c

raw
1 //**************************************************************************
2 //**
3 //** sc_man.c : Heretic 2 : Raven Software, Corp.
4 //**
5 //** $RCSfile: sc_man.c,v $
6 //** $Revision: 1.3 $
7 //** $Date: 96/01/06 03:23:43 $
8 //** $Author: bgokey $
9 //**
10 //**************************************************************************
11
12 // HEADER FILES ------------------------------------------------------------
13
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include "doomtype.h"
18 #include "w_wad.h"
19 #include "m_misc.h"
20 #include "z_zone.h"
21 #include "lprintf.h"
22
23 #include "sc_man.h"
24
25 // MACROS ------------------------------------------------------------------
26
27 #define MAX_STRING_SIZE 256
28 #define ASCII_COMMENT (';')
29 #define ASCII_QUOTE (34)
30
31 // TYPES -------------------------------------------------------------------
32
33 // EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
34
35 // PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
36
37 // PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
38
39 static void CheckOpen(void);
40 static void OpenScript(void);
41 static void OpenScriptByName(const char *name);
42 static void OpenScriptByNum(int lump);
43
44 // EXTERNAL DATA DECLARATIONS ----------------------------------------------
45
46 // PUBLIC DATA DEFINITIONS -------------------------------------------------
47
48 char *sc_String;
49 int sc_Number;
50 int sc_Line;
51 dboolean sc_End;
52 dboolean sc_Crossed;
53
54 // PRIVATE DATA DEFINITIONS ------------------------------------------------
55
56 static char ScriptName[16];
57 static int ScriptLump;
58 static const char *ScriptBuffer;
59 static const char *ScriptPtr;
60 static const char *ScriptEndPtr;
61 static char StringBuffer[MAX_STRING_SIZE];
62 static dboolean ScriptOpen = false;
63 static int ScriptSize;
64 static dboolean AlreadyGot = false;
65
66 // CODE --------------------------------------------------------------------
67
68 //==========================================================================
69 //
70 // SC_OpenLump
71 //
72 // Loads a script (from the WAD files) and prepares it for parsing.
73 //
74 //==========================================================================
75
76 void SC_OpenLump(const char *name)
77 {
78 OpenScriptByName(name);
79 }
80
81 void SC_OpenLumpByNum(int lump)
82 {
83 OpenScriptByNum(lump);
84 }
85
86 //==========================================================================
87 //
88 // OpenScript
89 //
90 //==========================================================================
91
92 static void OpenScript(void)
93 {
94 ScriptBuffer = W_LumpByNum(ScriptLump);
95 ScriptSize = W_LumpLength(ScriptLump);
96
97 ScriptPtr = ScriptBuffer;
98 ScriptEndPtr = ScriptPtr + ScriptSize;
99 sc_Line = 1;
100 sc_End = false;
101 ScriptOpen = true;
102 sc_String = StringBuffer;
103 AlreadyGot = false;
104 }
105
106 static void OpenScriptByName(const char *name)
107 {
108 SC_Close();
109
110 // Lump script
111 ScriptLump = W_GetNumForName(name);
112 strcpy(ScriptName, name);
113
114 OpenScript();
115 }
116
117 static void OpenScriptByNum(int lump)
118 {
119 SC_Close();
120
121 // Lump script
122 ScriptLump = lump;
123 strcpy(ScriptName, W_GetLumpInfoByNum(ScriptLump)->name);
124
125 OpenScript();
126 }
127
128 //==========================================================================
129 //
130 // SC_Close
131 //
132 //==========================================================================
133
134 void SC_Close(void)
135 {
136 if (ScriptOpen)
137 {
138 ScriptOpen = false;
139 }
140 }
141
142 //==========================================================================
143 //
144 // SC_GetString
145 //
146 //==========================================================================
147
148 dboolean SC_GetString(void)
149 {
150 char *text;
151 dboolean foundToken;
152
153 CheckOpen();
154 if (AlreadyGot)
155 {
156 AlreadyGot = false;
157 return true;
158 }
159 foundToken = false;
160 sc_Crossed = false;
161 if (ScriptPtr >= ScriptEndPtr)
162 {
163 sc_End = true;
164 return false;
165 }
166 while (foundToken == false)
167 {
168 while (*ScriptPtr <= 32)
169 {
170 if (ScriptPtr >= ScriptEndPtr)
171 {
172 sc_End = true;
173 return false;
174 }
175 if (*ScriptPtr++ == '\n')
176 {
177 sc_Line++;
178 sc_Crossed = true;
179 }
180 }
181 if (ScriptPtr >= ScriptEndPtr)
182 {
183 sc_End = true;
184 return false;
185 }
186 if (*ScriptPtr != ASCII_COMMENT)
187 { // Found a token
188 foundToken = true;
189 }
190 else
191 { // Skip comment
192 while (*ScriptPtr++ != '\n')
193 {
194 if (ScriptPtr >= ScriptEndPtr)
195 {
196 sc_End = true;
197 return false;
198 }
199 }
200 sc_Line++;
201 sc_Crossed = true;
202 }
203 }
204 text = sc_String;
205 if (*ScriptPtr == ASCII_QUOTE)
206 { // Quoted string
207 ScriptPtr++;
208 while (*ScriptPtr != ASCII_QUOTE)
209 {
210 *text++ = *ScriptPtr++;
211 if (ScriptPtr == ScriptEndPtr || text == &sc_String[MAX_STRING_SIZE - 1])
212 {
213 break;
214 }
215 }
216 ScriptPtr++;
217 }
218 else
219 { // Normal string
220 while ((*ScriptPtr > 32) && (*ScriptPtr != ASCII_COMMENT))
221 {
222 *text++ = *ScriptPtr++;
223 if(ScriptPtr == ScriptEndPtr || text == &sc_String[MAX_STRING_SIZE-1])
224 {
225 break;
226 }
227 }
228 }
229 *text = 0;
230 return true;
231 }
232
233 //==========================================================================
234 //
235 // SC_MustGetString
236 //
237 //==========================================================================
238
239 void SC_MustGetString(void)
240 {
241 if (SC_GetString() == false)
242 {
243 SC_ScriptError("Missing string.");
244 }
245 }
246
247 //==========================================================================
248 //
249 // SC_MustGetStringName
250 //
251 //==========================================================================
252
253 void SC_MustGetStringName(const char *name)
254 {
255 SC_MustGetString();
256 if (SC_Compare(name) == false)
257 {
258 SC_ScriptError(NULL);
259 }
260 }
261
262 //==========================================================================
263 //
264 // SC_GetNumber
265 //
266 //==========================================================================
267
268 dboolean SC_GetNumber(void)
269 {
270 char *stopper;
271
272 CheckOpen();
273 if (SC_GetString())
274 {
275 sc_Number = strtol(sc_String, &stopper, 0);
276 if (*stopper != 0)
277 {
278 I_Error("SC_GetNumber: Bad numeric constant \"%s\".\n"
279 "Script %s, Line %d", sc_String, ScriptName, sc_Line);
280 }
281 return true;
282 }
283 else
284 {
285 return false;
286 }
287 }
288
289 //==========================================================================
290 //
291 // SC_MustGetNumber
292 //
293 //==========================================================================
294
295 void SC_MustGetNumber(void)
296 {
297 if (SC_GetNumber() == false)
298 {
299 SC_ScriptError("Missing integer.");
300 }
301 }
302
303 //==========================================================================
304 //
305 // SC_UnGet
306 //
307 // Assumes there is a valid string in sc_String.
308 //
309 //==========================================================================
310
311 void SC_UnGet(void)
312 {
313 AlreadyGot = true;
314 }
315
316 //==========================================================================
317 //
318 // SC_Check
319 //
320 // Returns true if another token is on the current line.
321 //
322 //==========================================================================
323
324
325 dboolean SC_Check(void)
326 {
327 const char *text;
328
329 CheckOpen();
330 text = ScriptPtr;
331 if (text >= ScriptEndPtr)
332 {
333 return false;
334 }
335 while (*text <= 32)
336 {
337 if (*text == '\n')
338 {
339 return false;
340 }
341 text++;
342 if(text == ScriptEndPtr)
343 {
344 return false;
345 }
346 }
347 if (*text == ASCII_COMMENT)
348 {
349 return false;
350 }
351 return true;
352 }
353
354
355 //==========================================================================
356 //
357 // SC_MatchString
358 //
359 // Returns the index of the first match to sc_String from the passed
360 // array of strings, or -1 if not found.
361 //
362 //==========================================================================
363
364 int SC_MatchString(const char **strings)
365 {
366 int i;
367
368 for (i = 0; *strings != NULL; i++)
369 {
370 if (SC_Compare(*strings++))
371 {
372 return i;
373 }
374 }
375 return -1;
376 }
377
378 //==========================================================================
379 //
380 // SC_MustMatchString
381 //
382 //==========================================================================
383
384 int SC_MustMatchString(const char **strings)
385 {
386 int i;
387
388 i = SC_MatchString(strings);
389 if (i == -1)
390 {
391 SC_ScriptError(NULL);
392 }
393 return i;
394 }
395
396 //==========================================================================
397 //
398 // SC_Compare
399 //
400 //==========================================================================
401
402 dboolean SC_Compare(const char *text)
403 {
404 if (strcasecmp(text, sc_String) == 0)
405 {
406 return true;
407 }
408 return false;
409 }
410
411 //==========================================================================
412 //
413 // SC_ScriptError
414 //
415 //==========================================================================
416
417 void SC_ScriptError(const char *message)
418 {
419 if (message == NULL)
420 {
421 message = "Bad syntax.";
422 }
423 I_Error("Script error, \"%s\" line %d: %s", ScriptName, sc_Line, message);
424 }
425
426 //==========================================================================
427 //
428 // CheckOpen
429 //
430 //==========================================================================
431
432 static void CheckOpen(void)
433 {
434 if (ScriptOpen == false)
435 {
436 I_Error("SC_ call before SC_Open*().");
437 }
438 }