2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
25 #include "scriplib.h"
\r
28 =============================================================================
\r
32 =============================================================================
\r
37 char filename[1024];
\r
38 char *buffer,*script_p,*end_p;
\r
42 #define MAX_INCLUDES 8
\r
43 script_t scriptstack[MAX_INCLUDES];
\r
47 char token[MAXTOKEN];
\r
48 qboolean endofscript;
\r
49 qboolean tokenready; // only true if UnGetToken was just called
\r
56 void AddScriptToStack (char *filename)
\r
61 if (script == &scriptstack[MAX_INCLUDES])
\r
62 Error ("script file exceeded MAX_INCLUDES");
\r
63 strcpy (script->filename, ExpandPath (filename) );
\r
65 size = LoadFile (script->filename, (void **)&script->buffer);
\r
67 printf ("entering %s\n", script->filename);
\r
71 script->script_p = script->buffer;
\r
72 script->end_p = script->buffer + size;
\r
81 void LoadScriptFile (char *filename)
\r
83 script = scriptstack;
\r
84 AddScriptToStack (filename);
\r
86 endofscript = false;
\r
96 void ParseFromMemory (char *buffer, int size)
\r
98 script = scriptstack;
\r
100 if (script == &scriptstack[MAX_INCLUDES])
\r
101 Error ("script file exceeded MAX_INCLUDES");
\r
102 strcpy (script->filename, "memory buffer" );
\r
104 script->buffer = buffer;
\r
106 script->script_p = script->buffer;
\r
107 script->end_p = script->buffer + size;
\r
109 endofscript = false;
\r
110 tokenready = false;
\r
118 Signals that the current token was not used, and should be reported
\r
119 for the next GetToken. Note that
\r
125 could cross a line boundary.
\r
128 void UnGetToken (void)
\r
134 qboolean EndOfScript (qboolean crossline)
\r
137 Error ("Line %i is incomplete\n",scriptline);
\r
139 if (!strcmp (script->filename, "memory buffer"))
\r
141 endofscript = true;
\r
145 free (script->buffer);
\r
146 if (script == scriptstack+1)
\r
148 endofscript = true;
\r
152 scriptline = script->line;
\r
153 printf ("returning to %s\n", script->filename);
\r
154 return GetToken (crossline);
\r
162 qboolean GetToken (qboolean crossline)
\r
166 if (tokenready) // is a token allready waiting?
\r
168 tokenready = false;
\r
172 if (script->script_p >= script->end_p)
\r
173 return EndOfScript (crossline);
\r
179 while (*script->script_p <= 32)
\r
181 if (script->script_p >= script->end_p)
\r
182 return EndOfScript (crossline);
\r
183 if (*script->script_p++ == '\n')
\r
186 Error ("Line %i is incomplete\n",scriptline);
\r
187 scriptline = script->line++;
\r
191 if (script->script_p >= script->end_p)
\r
192 return EndOfScript (crossline);
\r
195 if (*script->script_p == ';' || *script->script_p == '#'
\r
196 || ( script->script_p[0] == '/' && script->script_p[1] == '/') )
\r
199 Error ("Line %i is incomplete\n",scriptline);
\r
200 while (*script->script_p++ != '\n')
\r
201 if (script->script_p >= script->end_p)
\r
202 return EndOfScript (crossline);
\r
207 if (script->script_p[0] == '/' && script->script_p[1] == '*')
\r
210 Error ("Line %i is incomplete\n",scriptline);
\r
211 script->script_p+=2;
\r
212 while (script->script_p[0] != '*' && script->script_p[1] != '/')
\r
214 script->script_p++;
\r
215 if (script->script_p >= script->end_p)
\r
216 return EndOfScript (crossline);
\r
218 script->script_p += 2;
\r
227 if (*script->script_p == '"')
\r
230 script->script_p++;
\r
231 while (*script->script_p != '"')
\r
233 *token_p++ = *script->script_p++;
\r
234 if (script->script_p == script->end_p)
\r
236 if (token_p == &token[MAXTOKEN])
\r
237 Error ("Token too large on line %i\n",scriptline);
\r
239 script->script_p++;
\r
241 else // regular token
\r
242 while ( *script->script_p > 32 && *script->script_p != ';')
\r
244 *token_p++ = *script->script_p++;
\r
245 if (script->script_p == script->end_p)
\r
247 if (token_p == &token[MAXTOKEN])
\r
248 Error ("Token too large on line %i\n",scriptline);
\r
253 if (!strcmp (token, "$include"))
\r
256 AddScriptToStack (token);
\r
257 return GetToken (crossline);
\r
268 Returns true if there is another token on the line
\r
271 qboolean TokenAvailable (void)
\r
275 search_p = script->script_p;
\r
277 if (search_p >= script->end_p)
\r
280 while ( *search_p <= 32)
\r
282 if (*search_p == '\n')
\r
285 if (search_p == script->end_p)
\r
290 if (*search_p == ';')
\r