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 ){
63 if ( script == &scriptstack[MAX_INCLUDES] ) {
64 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 );
76 Sys_Printf( "entering %s (%d)\n", script->filename, index + 1 );
79 Sys_Printf( "entering %s\n", script->filename );
84 script->script_p = script->buffer;
85 script->end_p = script->buffer + size;
94 void LoadScriptFile( const char *filename, int index ){
96 AddScriptToStack( filename, index );
108 void ParseFromMemory( char *buffer, int size ){
109 script = scriptstack;
111 if ( script == &scriptstack[MAX_INCLUDES] ) {
112 Error( "script file exceeded MAX_INCLUDES" );
114 strcpy( script->filename, "memory buffer" );
116 script->buffer = buffer;
118 script->script_p = script->buffer;
119 script->end_p = script->buffer + size;
121 endofscript = qfalse;
130 Signals that the current token was not used, and should be reported
131 for the next GetToken. Note that
137 could cross a line boundary.
140 void UnGetToken( void ){
145 qboolean EndOfScript( qboolean crossline ){
147 Error( "Line %i is incomplete\n",scriptline );
150 if ( !strcmp( script->filename, "memory buffer" ) ) {
155 if ( script->buffer == NULL ) {
156 Sys_Printf( "WARNING: Attempt to free already freed script buffer\n" );
159 free( script->buffer );
161 script->buffer = NULL;
162 if ( script == scriptstack + 1 ) {
167 scriptline = script->line;
168 Sys_Printf( "returning to %s\n", script->filename );
169 return GetToken( crossline );
177 qboolean GetToken( qboolean crossline ){
181 /* ydnar: dummy testing */
182 if ( script == NULL || script->buffer == NULL ) {
186 if ( tokenready ) { // is a token already waiting?
191 if ( ( script->script_p >= script->end_p ) || ( script->script_p == NULL ) ) {
192 return EndOfScript( crossline );
199 while ( *script->script_p <= 32 )
201 if ( script->script_p >= script->end_p ) {
202 return EndOfScript( crossline );
204 if ( *script->script_p++ == '\n' ) {
206 Error( "Line %i is incomplete\n",scriptline );
209 scriptline = script->line;
213 if ( script->script_p >= script->end_p ) {
214 return EndOfScript( crossline );
218 if ( *script->script_p == ';' || *script->script_p == '#'
219 || ( script->script_p[0] == '/' && script->script_p[1] == '/' ) ) {
221 Error( "Line %i is incomplete\n",scriptline );
223 while ( *script->script_p++ != '\n' )
224 if ( script->script_p >= script->end_p ) {
225 return EndOfScript( crossline );
228 scriptline = script->line;
233 if ( script->script_p[0] == '/' && script->script_p[1] == '*' ) {
235 Error( "Line %i is incomplete\n",scriptline );
237 script->script_p += 2;
238 while ( script->script_p[0] != '*' && script->script_p[1] != '/' )
240 if ( *script->script_p == '\n' ) {
242 scriptline = script->line;
245 if ( script->script_p >= script->end_p ) {
246 return EndOfScript( crossline );
249 script->script_p += 2;
258 if ( *script->script_p == '"' ) {
261 while ( *script->script_p != '"' )
263 *token_p++ = *script->script_p++;
264 if ( script->script_p == script->end_p ) {
267 if ( token_p == &token[MAXTOKEN] ) {
268 Error( "Token too large on line %i\n",scriptline );
273 else{ // regular token
274 while ( *script->script_p > 32 && *script->script_p != ';' )
276 *token_p++ = *script->script_p++;
277 if ( script->script_p == script->end_p ) {
280 if ( token_p == &token[MAXTOKEN] ) {
281 Error( "Token too large on line %i\n",scriptline );
288 if ( !strcmp( token, "$include" ) ) {
290 AddScriptToStack( token, 0 );
291 return GetToken( crossline );
302 Returns qtrue if there is another token on the line
305 qboolean TokenAvailable( void ) {
310 oldLine = scriptline;
313 r = GetToken( qtrue );
318 if ( oldLine == scriptline ) {
323 //% scriptline = oldLine;
324 //% script->line = oldScriptLine;
330 //=====================================================================
333 void MatchToken( char *match ) {
336 if ( strcmp( token, match ) ) {
337 Error( "MatchToken( \"%s\" ) failed at line %i in file %s", match, scriptline, script->filename );
342 void Parse1DMatrix( int x, vec_t *m ) {
347 for ( i = 0 ; i < x ; i++ ) {
349 m[i] = atof( token );
355 void Parse2DMatrix( int y, int x, vec_t *m ) {
360 for ( i = 0 ; i < y ; i++ ) {
361 Parse1DMatrix( x, m + i * x );
367 void Parse3DMatrix( int z, int y, int x, vec_t *m ) {
372 for ( i = 0 ; i < z ; i++ ) {
373 Parse2DMatrix( y, x, m + i * x * y );
380 void Write1DMatrix( FILE *f, int x, vec_t *m ) {
384 for ( i = 0 ; i < x ; i++ ) {
385 if ( m[i] == (int)m[i] ) {
386 fprintf( f, "%i ", (int)m[i] );
389 fprintf( f, "%f ", m[i] );
395 void Write2DMatrix( FILE *f, int y, int x, vec_t *m ) {
399 for ( i = 0 ; i < y ; i++ ) {
400 Write1DMatrix( f, x, m + i * x );
407 void Write3DMatrix( FILE *f, int z, int y, int x, vec_t *m ) {
411 for ( i = 0 ; i < z ; i++ ) {
412 Write2DMatrix( f, y, x, m + i * ( x * y ) );