2 Copyright (C) 1999-2006 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
31 =============================================================================
35 =============================================================================
41 char *buffer,*script_p,*end_p;
45 #define MAX_INCLUDES 8
46 script_t scriptstack[MAX_INCLUDES];
52 qboolean tokenready; // only qtrue if UnGetToken was just called
59 void AddScriptToStack (const char *filename, int index)
64 if (script == &scriptstack[MAX_INCLUDES])
65 Error ("script file exceeded MAX_INCLUDES");
66 strcpy (script->filename, ExpandPath (filename));
68 size = vfsLoadFile (script->filename, (void **)&script->buffer, index);
71 Sys_Printf ("Script file %s was not found\n", script->filename);
75 Sys_Printf ("entering %s (%d)\n", script->filename, index+1);
77 Sys_Printf ("entering %s\n", script->filename);
81 script->script_p = script->buffer;
82 script->end_p = script->buffer + size;
91 void LoadScriptFile (const char *filename, int index)
94 AddScriptToStack (filename, index);
106 void ParseFromMemory (char *buffer, int size)
108 script = scriptstack;
110 if (script == &scriptstack[MAX_INCLUDES])
111 Error ("script file exceeded MAX_INCLUDES");
112 strcpy (script->filename, "memory buffer" );
114 script->buffer = buffer;
116 script->script_p = script->buffer;
117 script->end_p = script->buffer + size;
119 endofscript = qfalse;
128 Signals that the current token was not used, and should be reported
129 for the next GetToken. Note that
135 could cross a line boundary.
138 void UnGetToken (void)
144 qboolean EndOfScript (qboolean crossline)
147 Error ("Line %i is incomplete\n",scriptline);
149 if (!strcmp (script->filename, "memory buffer"))
155 if( script->buffer == NULL )
156 Sys_Printf( "WARNING: Attempt to free already freed script buffer\n" );
158 free( script->buffer );
159 script->buffer = NULL;
160 if (script == scriptstack+1)
166 scriptline = script->line;
167 Sys_Printf ("returning to %s\n", script->filename);
168 return GetToken (crossline);
176 qboolean GetToken (qboolean crossline)
181 /* ydnar: dummy testing */
182 if( script == NULL || script->buffer == NULL )
185 if (tokenready) // is a token already waiting?
191 if ((script->script_p >= script->end_p) || (script->script_p == NULL))
192 return EndOfScript (crossline);
198 while (*script->script_p <= 32)
200 if (script->script_p >= script->end_p)
201 return EndOfScript (crossline);
202 if (*script->script_p++ == '\n')
205 Error ("Line %i is incomplete\n",scriptline);
207 scriptline = script->line;
211 if (script->script_p >= script->end_p)
212 return EndOfScript (crossline);
215 if (*script->script_p == ';' || *script->script_p == '#'
216 || ( script->script_p[0] == '/' && script->script_p[1] == '/') )
219 Error ("Line %i is incomplete\n",scriptline);
220 while (*script->script_p++ != '\n')
221 if (script->script_p >= script->end_p)
222 return EndOfScript (crossline);
224 scriptline = script->line;
229 if (script->script_p[0] == '/' && script->script_p[1] == '*')
232 Error ("Line %i is incomplete\n",scriptline);
234 while (script->script_p[0] != '*' && script->script_p[1] != '/')
236 if ( *script->script_p == '\n' )
239 scriptline = script->line;
242 if (script->script_p >= script->end_p)
243 return EndOfScript (crossline);
245 script->script_p += 2;
254 if (*script->script_p == '"')
258 while (*script->script_p != '"')
260 *token_p++ = *script->script_p++;
261 if (script->script_p == script->end_p)
263 if (token_p == &token[MAXTOKEN])
264 Error ("Token too large on line %i\n",scriptline);
268 else // regular token
269 while ( *script->script_p > 32 && *script->script_p != ';')
271 *token_p++ = *script->script_p++;
272 if (script->script_p == script->end_p)
274 if (token_p == &token[MAXTOKEN])
275 Error ("Token too large on line %i\n",scriptline);
280 if (!strcmp (token, "$include"))
283 AddScriptToStack (token, 0);
284 return GetToken (crossline);
295 Returns qtrue if there is another token on the line
298 qboolean TokenAvailable (void) {
299 int oldLine, oldScriptLine;
303 oldLine = scriptline;
304 oldScriptLine = script->line;
307 r = GetToken( qtrue );
312 if ( oldLine == scriptline ) {
317 //% scriptline = oldLine;
318 //% script->line = oldScriptLine;
324 //=====================================================================
327 void MatchToken( char *match ) {
330 if ( strcmp( token, match ) ) {
331 Error( "MatchToken( \"%s\" ) failed at line %i in file %s", match, scriptline, script->filename);
336 void Parse1DMatrix (int x, vec_t *m) {
341 for (i = 0 ; i < x ; i++) {
349 void Parse2DMatrix (int y, int x, vec_t *m) {
354 for (i = 0 ; i < y ; i++) {
355 Parse1DMatrix (x, m + i * x);
361 void Parse3DMatrix (int z, int y, int x, vec_t *m) {
366 for (i = 0 ; i < z ; i++) {
367 Parse2DMatrix (y, x, m + i * x*y);
374 void Write1DMatrix (FILE *f, int x, vec_t *m) {
378 for (i = 0 ; i < x ; i++) {
379 if (m[i] == (int)m[i] ) {
380 fprintf (f, "%i ", (int)m[i]);
382 fprintf (f, "%f ", m[i]);
388 void Write2DMatrix (FILE *f, int y, int x, vec_t *m) {
392 for (i = 0 ; i < y ; i++) {
393 Write1DMatrix (f, x, m + i*x);
400 void Write3DMatrix (FILE *f, int z, int y, int x, vec_t *m) {
404 for (i = 0 ; i < z ; i++) {
405 Write2DMatrix (f, y, x, m + i*(x*y) );