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;
111 void ParseFromMemory( char *buffer, int size ){
112 script = scriptstack;
114 if ( script == &scriptstack[MAX_INCLUDES] ) {
115 Error( "script file exceeded MAX_INCLUDES" );
117 strcpy( script->filename, "memory buffer" );
119 script->buffer = buffer;
121 script->script_p = script->buffer;
122 script->end_p = script->buffer + size;
124 endofscript = qfalse;
133 Signals that the current token was not used, and should be reported
134 for the next GetToken. Note that
140 could cross a line boundary.
143 void UnGetToken( void ){
148 qboolean EndOfScript( qboolean crossline ){
150 Error( "Line %i is incomplete\n",scriptline );
153 if ( !strcmp( script->filename, "memory buffer" ) ) {
158 if ( script->buffer == NULL ) {
159 Sys_FPrintf( SYS_WRN, "WARNING: Attempt to free already freed script buffer\n" );
162 free( script->buffer );
164 script->buffer = NULL;
165 if ( script == scriptstack + 1 ) {
170 scriptline = script->line;
171 Sys_Printf( "returning to %s\n", script->filename );
172 return GetToken( crossline );
180 qboolean GetToken( qboolean crossline ){
184 /* ydnar: dummy testing */
185 if ( script == NULL || script->buffer == NULL ) {
189 if ( tokenready ) { // is a token already waiting?
194 if ( ( script->script_p >= script->end_p ) || ( script->script_p == NULL ) ) {
195 return EndOfScript( crossline );
202 while ( *script->script_p <= 32 )
204 if ( script->script_p >= script->end_p ) {
205 return EndOfScript( crossline );
207 if ( *script->script_p++ == '\n' ) {
209 Error( "Line %i is incomplete\n",scriptline );
212 scriptline = script->line;
216 if ( script->script_p >= script->end_p ) {
217 return EndOfScript( crossline );
221 if ( *script->script_p == ';' || *script->script_p == '#'
222 || ( script->script_p[0] == '/' && script->script_p[1] == '/' ) ) {
224 Error( "Line %i is incomplete\n",scriptline );
226 while ( *script->script_p++ != '\n' )
227 if ( script->script_p >= script->end_p ) {
228 return EndOfScript( crossline );
231 scriptline = script->line;
236 if ( script->script_p[0] == '/' && script->script_p[1] == '*' ) {
238 Error( "Line %i is incomplete\n",scriptline );
240 script->script_p += 2;
241 while ( script->script_p[0] != '*' && script->script_p[1] != '/' )
243 if ( *script->script_p == '\n' ) {
245 scriptline = script->line;
248 if ( script->script_p >= script->end_p ) {
249 return EndOfScript( crossline );
252 script->script_p += 2;
261 if ( *script->script_p == '"' ) {
264 while ( *script->script_p != '"' )
266 *token_p++ = *script->script_p++;
267 if ( script->script_p == script->end_p ) {
270 if ( token_p == &token[MAXTOKEN] ) {
271 Error( "Token too large on line %i\n",scriptline );
276 else{ // regular token
277 while ( *script->script_p > 32 && *script->script_p != ';' )
279 *token_p++ = *script->script_p++;
280 if ( script->script_p == script->end_p ) {
283 if ( token_p == &token[MAXTOKEN] ) {
284 Error( "Token too large on line %i\n",scriptline );
291 if ( !strcmp( token, "$include" ) ) {
293 AddScriptToStack( token, 0 );
294 return GetToken( crossline );
305 Returns qtrue if there is another token on the line
308 qboolean TokenAvailable( void ) {
313 oldLine = scriptline;
316 r = GetToken( qtrue );
321 if ( oldLine == scriptline ) {
326 //% scriptline = oldLine;
327 //% script->line = oldScriptLine;
333 //=====================================================================
336 void MatchToken( char *match ) {
339 if ( strcmp( token, match ) ) {
340 Error( "MatchToken( \"%s\" ) failed at line %i in file %s", match, scriptline, script->filename );
345 void Parse1DMatrix( int x, vec_t *m ) {
350 for ( i = 0 ; i < x ; i++ ) {
352 m[i] = atof( token );
358 void Parse2DMatrix( int y, int x, vec_t *m ) {
363 for ( i = 0 ; i < y ; i++ ) {
364 Parse1DMatrix( x, m + i * x );
370 void Parse3DMatrix( int z, int y, int x, vec_t *m ) {
375 for ( i = 0 ; i < z ; i++ ) {
376 Parse2DMatrix( y, x, m + i * x * y );
383 void Write1DMatrix( FILE *f, int x, vec_t *m ) {
387 for ( i = 0 ; i < x ; i++ ) {
388 if ( m[i] == (int)m[i] ) {
389 fprintf( f, "%i ", (int)m[i] );
392 fprintf( f, "%f ", m[i] );
398 void Write2DMatrix( FILE *f, int y, int x, vec_t *m ) {
402 for ( i = 0 ; i < y ; i++ ) {
403 Write1DMatrix( f, x, m + i * x );
410 void Write3DMatrix( FILE *f, int z, int y, int x, vec_t *m ) {
414 for ( i = 0 ; i < z ; i++ ) {
415 Write2DMatrix( f, y, x, m + i * ( x * y ) );