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
29 =============================================================================
33 =============================================================================
39 char *buffer,*script_p,*end_p;
43 #define MAX_INCLUDES 8
44 script_t scriptstack[MAX_INCLUDES];
50 qboolean tokenready; // only true if UnGetScriptToken was just called
57 void AddScriptToStack( char *filename ){
61 if ( script == &scriptstack[MAX_INCLUDES] ) {
62 Error( "script file exceeded MAX_INCLUDES" );
64 strcpy( script->filename, ExpandPath( filename ) );
66 size = LoadFile( script->filename, (void **)&script->buffer );
68 printf( "entering %s\n", script->filename );
72 script->script_p = script->buffer;
73 script->end_p = script->buffer + size;
82 void LoadScriptFile( char *filename ){
84 AddScriptToStack( filename );
96 void ParseFromMemory( char *buffer, int size ){
99 if ( script == &scriptstack[MAX_INCLUDES] ) {
100 Error( "script file exceeded MAX_INCLUDES" );
102 strcpy( script->filename, "memory buffer" );
104 script->buffer = buffer;
106 script->script_p = script->buffer;
107 script->end_p = script->buffer + size;
118 Signals that the current token was not used, and should be reported
119 for the next GetScriptToken. Note that
121 GetScriptToken (true);
123 GetScriptToken (false);
125 could cross a line boundary.
128 void UnGetScriptToken( void ){
133 qboolean EndOfScript( qboolean crossline ){
135 Error( "Line %i is incomplete\n",scriptline );
138 if ( !strcmp( script->filename, "memory buffer" ) ) {
143 free( script->buffer );
144 if ( script == scriptstack + 1 ) {
149 scriptline = script->line;
150 printf( "returning to %s\n", script->filename );
151 return GetScriptToken( crossline );
159 qboolean GetScriptToken( qboolean crossline ){
162 if ( tokenready ) { // is a token allready waiting?
167 if ( script->script_p >= script->end_p ) {
168 return EndOfScript( crossline );
175 while ( *script->script_p <= 32 )
177 if ( script->script_p >= script->end_p ) {
178 return EndOfScript( crossline );
180 if ( *script->script_p++ == '\n' ) {
182 Error( "Line %i is incomplete\n",scriptline );
184 scriptline = script->line++;
188 if ( script->script_p >= script->end_p ) {
189 return EndOfScript( crossline );
193 if ( *script->script_p == ';' || *script->script_p == '#'
194 || ( script->script_p[0] == '/' && script->script_p[1] == '/' ) ) {
196 Error( "Line %i is incomplete\n",scriptline );
198 while ( *script->script_p++ != '\n' )
199 if ( script->script_p >= script->end_p ) {
200 return EndOfScript( crossline );
206 if ( script->script_p[0] == '/' && script->script_p[1] == '*' ) {
208 Error( "Line %i is incomplete\n",scriptline );
210 script->script_p += 2;
211 while ( script->script_p[0] != '*' && script->script_p[1] != '/' )
214 if ( script->script_p >= script->end_p ) {
215 return EndOfScript( crossline );
218 script->script_p += 2;
227 if ( *script->script_p == '"' ) {
230 while ( *script->script_p != '"' )
232 *token_p++ = *script->script_p++;
233 if ( script->script_p == script->end_p ) {
236 if ( token_p == &token[MAXTOKEN] ) {
237 Error( "Token too large on line %i\n",scriptline );
242 else{ // regular token
243 while ( *script->script_p > 32 && *script->script_p != ';' )
245 *token_p++ = *script->script_p++;
246 if ( script->script_p == script->end_p ) {
249 if ( token_p == &token[MAXTOKEN] ) {
250 Error( "Token too large on line %i\n",scriptline );
257 if ( !strcmp( token, "$include" ) ) {
258 GetScriptToken( false );
259 AddScriptToStack( token );
260 return GetScriptToken( crossline );
271 Returns true if there is another token on the line
274 qboolean ScriptTokenAvailable( void ){
277 search_p = script->script_p;
279 if ( search_p >= script->end_p ) {
283 while ( *search_p <= 32 )
285 if ( *search_p == '\n' ) {
289 if ( search_p == script->end_p ) {
295 if ( *search_p == ';' ) {