2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 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.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 =============================================================================
33 =============================================================================
39 char *buffer,*script_p,*end_p;
43 #define MAX_INCLUDES 8
44 script_t scriptstack[MAX_INCLUDES];
50 qboolean tokenready; // only true if UnGetScriptToken was just called
57 void AddScriptToStack (char *filename)
62 if (script == &scriptstack[MAX_INCLUDES])
63 Error ("script file exceeded MAX_INCLUDES");
64 strcpy (script->filename, ExpandPath (filename) );
66 size = LoadFile (script->filename, (void **)&script->buffer);
68 printf ("entering %s\n", script->filename);
72 script->script_p = script->buffer;
73 script->end_p = script->buffer + size;
82 void LoadScriptFile (char *filename)
85 AddScriptToStack (filename);
97 void ParseFromMemory (char *buffer, int size)
101 if (script == &scriptstack[MAX_INCLUDES])
102 Error ("script file exceeded MAX_INCLUDES");
103 strcpy (script->filename, "memory buffer" );
105 script->buffer = buffer;
107 script->script_p = script->buffer;
108 script->end_p = script->buffer + size;
119 Signals that the current token was not used, and should be reported
120 for the next GetScriptToken. Note that
122 GetScriptToken (true);
124 GetScriptToken (false);
126 could cross a line boundary.
129 void UnGetScriptToken (void)
135 qboolean EndOfScript (qboolean crossline)
138 Error ("Line %i is incomplete\n",scriptline);
140 if (!strcmp (script->filename, "memory buffer"))
146 free (script->buffer);
147 if (script == scriptstack+1)
153 scriptline = script->line;
154 printf ("returning to %s\n", script->filename);
155 return GetScriptToken (crossline);
163 qboolean GetScriptToken (qboolean crossline)
167 if (tokenready) // is a token allready waiting?
173 if (script->script_p >= script->end_p)
174 return EndOfScript (crossline);
180 while (*script->script_p <= 32)
182 if (script->script_p >= script->end_p)
183 return EndOfScript (crossline);
184 if (*script->script_p++ == '\n')
187 Error ("Line %i is incomplete\n",scriptline);
188 scriptline = script->line++;
192 if (script->script_p >= script->end_p)
193 return EndOfScript (crossline);
196 if (*script->script_p == ';' || *script->script_p == '#'
197 || ( script->script_p[0] == '/' && script->script_p[1] == '/') )
200 Error ("Line %i is incomplete\n",scriptline);
201 while (*script->script_p++ != '\n')
202 if (script->script_p >= script->end_p)
203 return EndOfScript (crossline);
208 if (script->script_p[0] == '/' && script->script_p[1] == '*')
211 Error ("Line %i is incomplete\n",scriptline);
213 while (script->script_p[0] != '*' && script->script_p[1] != '/')
216 if (script->script_p >= script->end_p)
217 return EndOfScript (crossline);
219 script->script_p += 2;
228 if (*script->script_p == '"')
232 while (*script->script_p != '"')
234 *token_p++ = *script->script_p++;
235 if (script->script_p == script->end_p)
237 if (token_p == &token[MAXTOKEN])
238 Error ("Token too large on line %i\n",scriptline);
242 else // regular token
243 while ( *script->script_p > 32 && *script->script_p != ';')
245 *token_p++ = *script->script_p++;
246 if (script->script_p == script->end_p)
248 if (token_p == &token[MAXTOKEN])
249 Error ("Token too large on line %i\n",scriptline);
254 if (!strcmp (token, "$include"))
256 GetScriptToken (false);
257 AddScriptToStack (token);
258 return GetScriptToken (crossline);
269 Returns true if there is another token on the line
272 qboolean ScriptTokenAvailable (void)
276 search_p = script->script_p;
278 if (search_p >= script->end_p)
281 while ( *search_p <= 32)
283 if (*search_p == '\n')
286 if (search_p == script->end_p)
291 if (*search_p == ';')