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 );
101 /* &unload current; for autopacker */
102 void SilentLoadScriptFile( const char *filename, int index ){
105 if ( script->buffer != NULL && !endofscript ) {
106 free( script->buffer );
107 script->buffer = NULL;
110 script = scriptstack;
113 if ( script == &scriptstack[MAX_INCLUDES] ) {
114 Error( "script file exceeded MAX_INCLUDES" );
116 strcpy( script->filename, ExpandPath( filename ) );
118 size = vfsLoadFile( script->filename, (void **)&script->buffer, index );
121 Sys_Printf( "Script file %s was not found\n", script->filename );
124 script->script_p = script->buffer;
125 script->end_p = script->buffer + size;
127 endofscript = qfalse;
136 void ParseFromMemory( char *buffer, int size ){
137 script = scriptstack;
139 if ( script == &scriptstack[MAX_INCLUDES] ) {
140 Error( "script file exceeded MAX_INCLUDES" );
142 strcpy( script->filename, "memory buffer" );
144 script->buffer = buffer;
146 script->script_p = script->buffer;
147 script->end_p = script->buffer + size;
149 endofscript = qfalse;
158 Signals that the current token was not used, and should be reported
159 for the next GetToken. Note that
165 could cross a line boundary.
168 void UnGetToken( void ){
173 qboolean EndOfScript( qboolean crossline ){
175 Error( "Line %i is incomplete\nFile location be: %s\n", scriptline, g_strLoadedFileLocation );
178 if ( !strcmp( script->filename, "memory buffer" ) ) {
183 if ( script->buffer == NULL ) {
184 Sys_Printf( "WARNING: Attempt to free already freed script buffer\n" );
187 free( script->buffer );
189 script->buffer = NULL;
190 if ( script == scriptstack + 1 ) {
195 scriptline = script->line;
196 Sys_Printf( "returning to %s\n", script->filename );
197 return GetToken( crossline );
205 qboolean GetToken( qboolean crossline ){
209 /* ydnar: dummy testing */
210 if ( script == NULL || script->buffer == NULL ) {
214 if ( tokenready ) { // is a token already waiting?
219 if ( ( script->script_p >= script->end_p ) || ( script->script_p == NULL ) ) {
220 return EndOfScript( crossline );
227 while ( *script->script_p <= 32 )
229 if ( script->script_p >= script->end_p ) {
230 return EndOfScript( crossline );
232 if ( *script->script_p++ == '\n' ) {
234 Error( "Line %i is incomplete\nFile location be: %s\n", scriptline, g_strLoadedFileLocation );
237 scriptline = script->line;
241 if ( script->script_p >= script->end_p ) {
242 return EndOfScript( crossline );
246 if ( *script->script_p == ';' || *script->script_p == '#'
247 || ( script->script_p[0] == '/' && script->script_p[1] == '/' ) ) {
249 Error( "Line %i is incomplete\nFile location be: %s\n", scriptline, g_strLoadedFileLocation );
251 while ( *script->script_p++ != '\n' )
252 if ( script->script_p >= script->end_p ) {
253 return EndOfScript( crossline );
256 scriptline = script->line;
261 if ( script->script_p[0] == '/' && script->script_p[1] == '*' ) {
263 Error( "Line %i is incomplete\nFile location be: %s\n", scriptline, g_strLoadedFileLocation );
265 script->script_p += 2;
266 while ( script->script_p[0] != '*' || script->script_p[1] != '/' )
268 if ( *script->script_p == '\n' ) {
270 scriptline = script->line;
273 if ( script->script_p >= script->end_p ) {
274 return EndOfScript( crossline );
277 script->script_p += 2;
286 if ( *script->script_p == '"' ) {
289 while ( *script->script_p != '"' )
291 *token_p++ = *script->script_p++;
292 if ( script->script_p == script->end_p ) {
295 if ( token_p == &token[MAXTOKEN] ) {
296 Error( "Token too large on line %i\nFile location be: %s\n", scriptline, g_strLoadedFileLocation );
301 else{ // regular token
302 while ( *script->script_p > 32 && *script->script_p != ';' )
304 *token_p++ = *script->script_p++;
305 if ( script->script_p == script->end_p ) {
308 if ( token_p == &token[MAXTOKEN] ) {
309 Error( "Token too large on line %i\nFile location be: %s\n", scriptline, g_strLoadedFileLocation );
316 if ( !strcmp( token, "$include" ) ) {
318 AddScriptToStack( token, 0 );
319 return GetToken( crossline );
330 Returns qtrue if there is another token on the line
333 qboolean TokenAvailable( void ) {
338 oldLine = scriptline;
341 r = GetToken( qtrue );
346 if ( oldLine == scriptline ) {
351 //% scriptline = oldLine;
352 //% script->line = oldScriptLine;
358 //=====================================================================
361 void MatchToken( char *match ) {
364 if ( strcmp( token, match ) ) {
365 Error( "MatchToken( \"%s\" ) failed at line %i in file %s", match, scriptline, script->filename );
370 void Parse1DMatrix( int x, vec_t *m ) {
375 for ( i = 0 ; i < x ; i++ ) {
377 m[i] = atof( token );
383 void Parse2DMatrix( int y, int x, vec_t *m ) {
388 for ( i = 0 ; i < y ; i++ ) {
389 Parse1DMatrix( x, m + i * x );
395 void Parse3DMatrix( int z, int y, int x, vec_t *m ) {
400 for ( i = 0 ; i < z ; i++ ) {
401 Parse2DMatrix( y, x, m + i * x * y );
408 void Write1DMatrix( FILE *f, int x, vec_t *m ) {
412 for ( i = 0 ; i < x ; i++ ) {
413 if ( m[i] == (int)m[i] ) {
414 fprintf( f, "%i ", (int)m[i] );
417 fprintf( f, "%f ", m[i] );
423 void Write2DMatrix( FILE *f, int y, int x, vec_t *m ) {
427 for ( i = 0 ; i < y ; i++ ) {
428 Write1DMatrix( f, x, m + i * x );
435 void Write3DMatrix( FILE *f, int z, int y, int x, vec_t *m ) {
439 for ( i = 0 ; i < z ; i++ ) {
440 Write2DMatrix( f, y, x, m + i * ( x * y ) );