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
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" );
67 strcpy( script->filename, ExpandPath( filename ) );
69 size = vfsLoadFile( script->filename, &buffer, index );
72 Sys_Printf( "Script file %s was not found\n", script->filename );
78 Sys_Printf( "entering %s (%d)\n", script->filename, index + 1 );
81 Sys_Printf( "entering %s\n", script->filename );
84 script->buffer = buffer;
86 script->script_p = script->buffer;
87 script->end_p = script->buffer + size;
97 void LoadScriptFile( const char *filename, int index ){
99 AddScriptToStack( filename, index );
101 endofscript = qfalse;
104 /* &unload current; for autopacker */
105 void SilentLoadScriptFile( const char *filename, int index ){
108 if ( script->buffer != NULL && !endofscript ) {
109 free( script->buffer );
110 script->buffer = NULL;
113 script = scriptstack;
116 if ( script == &scriptstack[MAX_INCLUDES] ) {
117 Error( "script file exceeded MAX_INCLUDES" );
119 strcpy( script->filename, ExpandPath( filename ) );
121 size = vfsLoadFile( script->filename, (void **)&script->buffer, index );
124 Sys_Printf( "Script file %s was not found\n", script->filename );
127 script->script_p = script->buffer;
128 script->end_p = script->buffer + size;
130 endofscript = qfalse;
139 void ParseFromMemory( char *buffer, int size ){
140 script = scriptstack;
142 if ( script == &scriptstack[MAX_INCLUDES] ) {
143 Error( "script file exceeded MAX_INCLUDES" );
145 strcpy( script->filename, "memory buffer" );
147 script->buffer = buffer;
149 script->script_p = script->buffer;
150 script->end_p = script->buffer + size;
152 endofscript = qfalse;
161 Signals that the current token was not used, and should be reported
162 for the next GetToken. Note that
168 could cross a line boundary.
171 void UnGetToken( void ){
176 qboolean EndOfScript( qboolean crossline ){
178 Error( "Line %i is incomplete\n",scriptline );
181 if ( !strcmp( script->filename, "memory buffer" ) ) {
186 if ( script->buffer == NULL ) {
187 Sys_FPrintf( SYS_WRN, "WARNING: Attempt to free already freed script buffer\n" );
190 free( script->buffer );
192 script->buffer = NULL;
193 if ( script == scriptstack + 1 ) {
198 scriptline = script->line;
199 Sys_Printf( "returning to %s\n", script->filename );
200 return GetToken( crossline );
208 qboolean GetToken( qboolean crossline ){
212 /* ydnar: dummy testing */
213 if ( script == NULL || script->buffer == NULL ) {
217 if ( tokenready ) { // is a token already waiting?
222 if ( ( script->script_p >= script->end_p ) || ( script->script_p == NULL ) ) {
223 return EndOfScript( crossline );
230 while ( *script->script_p <= 32 )
232 if ( script->script_p >= script->end_p ) {
233 return EndOfScript( crossline );
235 if ( *script->script_p++ == '\n' ) {
237 Error( "Line %i is incomplete\n",scriptline );
240 scriptline = script->line;
244 if ( script->script_p >= script->end_p ) {
245 return EndOfScript( crossline );
249 if ( *script->script_p == ';' || *script->script_p == '#'
250 || ( script->script_p[0] == '/' && script->script_p[1] == '/' ) ) {
252 Error( "Line %i is incomplete\n",scriptline );
254 while ( *script->script_p++ != '\n' )
255 if ( script->script_p >= script->end_p ) {
256 return EndOfScript( crossline );
259 scriptline = script->line;
264 if ( script->script_p[0] == '/' && script->script_p[1] == '*' ) {
266 Error( "Line %i is incomplete\n",scriptline );
268 script->script_p += 2;
269 while ( script->script_p[0] != '*' && script->script_p[1] != '/' )
271 if ( *script->script_p == '\n' ) {
273 scriptline = script->line;
276 if ( script->script_p >= script->end_p ) {
277 return EndOfScript( crossline );
280 script->script_p += 2;
289 if ( *script->script_p == '"' ) {
292 while ( *script->script_p != '"' )
294 *token_p++ = *script->script_p++;
295 if ( script->script_p == script->end_p ) {
298 if ( token_p == &token[MAXTOKEN] ) {
299 Error( "Token too large on line %i\n",scriptline );
304 else{ // regular token
305 while ( *script->script_p > 32 && *script->script_p != ';' )
307 *token_p++ = *script->script_p++;
308 if ( script->script_p == script->end_p ) {
311 if ( token_p == &token[MAXTOKEN] ) {
312 Error( "Token too large on line %i\n",scriptline );
319 if ( !strcmp( token, "$include" ) ) {
321 AddScriptToStack( token, 0 );
322 return GetToken( crossline );
333 Returns qtrue if there is another token on the line
336 qboolean TokenAvailable( void ) {
341 oldLine = scriptline;
344 r = GetToken( qtrue );
349 if ( oldLine == scriptline ) {
354 //% scriptline = oldLine;
355 //% script->line = oldScriptLine;
361 //=====================================================================
364 void MatchToken( char *match ) {
367 if ( strcmp( token, match ) ) {
368 Error( "MatchToken( \"%s\" ) failed at line %i in file %s", match, scriptline, script->filename );
373 void Parse1DMatrix( int x, vec_t *m ) {
378 for ( i = 0 ; i < x ; i++ ) {
380 m[i] = atof( token );
386 void Parse2DMatrix( int y, int x, vec_t *m ) {
391 for ( i = 0 ; i < y ; i++ ) {
392 Parse1DMatrix( x, m + i * x );
398 void Parse3DMatrix( int z, int y, int x, vec_t *m ) {
403 for ( i = 0 ; i < z ; i++ ) {
404 Parse2DMatrix( y, x, m + i * x * y );
411 void Write1DMatrix( FILE *f, int x, vec_t *m ) {
415 for ( i = 0 ; i < x ; i++ ) {
416 if ( m[i] == (int)m[i] ) {
417 fprintf( f, "%i ", (int)m[i] );
420 fprintf( f, "%f ", m[i] );
426 void Write2DMatrix( FILE *f, int y, int x, vec_t *m ) {
430 for ( i = 0 ; i < y ; i++ ) {
431 Write1DMatrix( f, x, m + i * x );
438 void Write3DMatrix( FILE *f, int z, int y, int x, vec_t *m ) {
442 for ( i = 0 ; i < z ; i++ ) {
443 Write2DMatrix( f, y, x, m + i * ( x * y ) );