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
28 =============================================================================
32 =============================================================================
38 char *buffer,*script_p,*end_p;
42 #define MAX_INCLUDES 8
43 script_t scriptstack[MAX_INCLUDES];
49 qboolean tokenready; // only true if UnGetToken was just called
56 void AddScriptToStack( char *filename ){
60 if ( script == &scriptstack[MAX_INCLUDES] ) {
61 Error( "script file exceeded MAX_INCLUDES" );
63 strcpy( script->filename, ExpandPath( filename ) );
65 size = LoadFile( script->filename, (void **)&script->buffer );
67 printf( "entering %s\n", script->filename );
71 script->script_p = script->buffer;
72 script->end_p = script->buffer + size;
81 void LoadScriptFile( char *filename ){
83 AddScriptToStack( filename );
95 void ParseFromMemory( char *buffer, int size ){
98 if ( script == &scriptstack[MAX_INCLUDES] ) {
99 Error( "script file exceeded MAX_INCLUDES" );
101 strcpy( script->filename, "memory buffer" );
103 script->buffer = buffer;
105 script->script_p = script->buffer;
106 script->end_p = script->buffer + size;
117 Signals that the current token was not used, and should be reported
118 for the next GetToken. Note that
124 could cross a line boundary.
127 void UnGetToken( void ){
132 qboolean EndOfScript( qboolean crossline ){
134 Error( "Line %i is incomplete\n",scriptline );
137 if ( !strcmp( script->filename, "memory buffer" ) ) {
142 free( script->buffer );
143 if ( script == scriptstack + 1 ) {
148 scriptline = script->line;
149 printf( "returning to %s\n", script->filename );
150 return GetToken( crossline );
158 qboolean GetToken( qboolean crossline ){
161 if ( tokenready ) { // is a token allready waiting?
166 if ( script->script_p >= script->end_p ) {
167 return EndOfScript( crossline );
174 while ( *script->script_p <= 32 )
176 if ( script->script_p >= script->end_p ) {
177 return EndOfScript( crossline );
179 if ( *script->script_p++ == '\n' ) {
181 Error( "Line %i is incomplete\n",scriptline );
183 scriptline = script->line++;
187 if ( script->script_p >= script->end_p ) {
188 return EndOfScript( crossline );
192 if ( *script->script_p == ';' || *script->script_p == '#'
193 || ( script->script_p[0] == '/' && script->script_p[1] == '/' ) ) {
195 Error( "Line %i is incomplete\n",scriptline );
197 while ( *script->script_p++ != '\n' )
198 if ( script->script_p >= script->end_p ) {
199 return EndOfScript( crossline );
205 if ( script->script_p[0] == '/' && script->script_p[1] == '*' ) {
207 Error( "Line %i is incomplete\n",scriptline );
209 script->script_p += 2;
210 while ( script->script_p[0] != '*' && script->script_p[1] != '/' )
213 if ( script->script_p >= script->end_p ) {
214 return EndOfScript( crossline );
217 script->script_p += 2;
226 if ( *script->script_p == '"' ) {
229 while ( *script->script_p != '"' )
231 *token_p++ = *script->script_p++;
232 if ( script->script_p == script->end_p ) {
235 if ( token_p == &token[MAXTOKEN] ) {
236 Error( "Token too large on line %i\n",scriptline );
241 else{ // regular token
242 while ( *script->script_p > 32 && *script->script_p != ';' )
244 *token_p++ = *script->script_p++;
245 if ( script->script_p == script->end_p ) {
248 if ( token_p == &token[MAXTOKEN] ) {
249 Error( "Token too large on line %i\n",scriptline );
256 if ( !strcmp( token, "$include" ) ) {
258 AddScriptToStack( token );
259 return GetToken( crossline );
270 Returns true if there is another token on the line
273 qboolean TokenAvailable( void ){
276 search_p = script->script_p;
278 if ( search_p >= script->end_p ) {
282 while ( *search_p <= 32 )
284 if ( *search_p == '\n' ) {
288 if ( search_p == script->end_p ) {
294 if ( *search_p == ';' ) {