2 BobToolz plugin for GtkRadiant
3 Copyright (C) 2001 Gordon Biggans
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "ScriptParser.h"
23 CScriptParser::CScriptParser( void ) :
25 m_pScriptSection( NULL ),
26 m_pLastScriptSection( NULL ),
31 CScriptParser::~CScriptParser( void ) {
35 void CScriptParser::ClearBuffer( void ) {
44 m_pScriptSection = NULL;
45 m_pLastScriptSection = NULL;
46 memset( m_breakChars, 0, sizeof( m_breakChars ) );
49 const char* CScriptParser::MakeToken( const char* pToken ) {
59 int len = static_cast<int>( strlen( pToken ) );
61 m_pToken = new char[len + 1];
63 strcpy( m_pToken, pToken );
68 const int MAX_TOKEN_STRING = 1024;
69 // Should NEVER return NULL
70 const char* CScriptParser::GetToken( bool bAllowLinebreaks ) {
72 char token[MAX_TOKEN_STRING];
73 bool bNewLines = false;
75 m_pLastScriptSection = m_pScriptSection;
80 if ( !m_pScript || !m_pScriptSection ) {
81 return MakeToken( token );
85 SkipWhitespace( &bNewLines );
86 if ( !*m_pScriptSection ) {
87 return MakeToken( token );
89 if ( bNewLines && !bAllowLinebreaks ) {
90 return MakeToken( token );
93 c = *m_pScriptSection;
95 if ( c == '/' && m_pScriptSection[1] == '/' ) { // C style comments
96 m_pScriptSection += 2;
97 while ( *m_pScriptSection && *m_pScriptSection != '\n' ) {
101 else if ( c == '/' && m_pScriptSection[1] == '*' ) { // C++ style comments
102 m_pScriptSection += 2;
103 while ( *m_pScriptSection && ( *m_pScriptSection != '*' || m_pScriptSection[1] != '/' ) ) {
106 if ( *m_pScriptSection ) {
107 m_pScriptSection += 2;
118 c = *m_pScriptSection++;
119 if ( c == '\"' || !c ) {
121 return MakeToken( token );
123 if ( len < MAX_TOKEN_STRING ) {
131 if ( len > 0 && IsBreakChar( *m_pScriptSection ) ) {
135 if ( len < MAX_TOKEN_STRING ) {
141 if ( IsBreakChar( c ) ) {
145 c = *m_pScriptSection;
148 if ( len == MAX_TOKEN_STRING ) {
153 return MakeToken( token );
156 void CScriptParser::SkipWhitespace( bool* pbNewLines ) {
159 if ( !m_pScript || !m_pScriptSection ) {
163 while ( ( c = *m_pScriptSection ) <= ' ' ) {
174 void CScriptParser::SkipBracedSection( void ) {
180 token = GetToken( true );
181 if ( token[1] == 0 ) {
182 if ( *token == '{' ) {
185 else if ( *token == '}' ) {
189 } while ( depth && *m_pScriptSection );
192 void CScriptParser::SkipRestOfLine( void ) {
196 p = m_pScriptSection;
197 while ( ( c = *p++ ) != 0 ) {
202 m_pScriptSection = p;
205 void CScriptParser::UndoGetToken( void ) {
206 if ( !m_pLastScriptSection ) {
209 m_pScriptSection = m_pLastScriptSection;
210 m_pLastScriptSection = NULL;
213 void CScriptParser::ResetParseSession( void ) {
218 m_pScriptSection = m_pScript;
219 m_pLastScriptSection = NULL;
222 char* CScriptParser::GetBufferCopy( void ) {
227 int len = static_cast<int>( strlen( m_pScript ) );
228 char* pBuffer = new char[len + 1];
229 strcpy( pBuffer, m_pScript );
233 int CScriptParser::GetTokenOffset( void ) {
234 if ( !m_pScript || !m_pScriptSection ) {
238 return static_cast<int>( m_pScriptSection - m_pScript );
241 void CScriptParser::LoadScript( const char* pScript ) {
244 int len = static_cast<int>( strlen( pScript ) );
249 m_pScript = new char[len + 1];
250 m_pScript[len] = '\0';
252 strcpy( m_pScript, pScript );
253 m_pScriptSection = m_pScript;
256 void CScriptParser::AddBreakChar( char c ) {
257 for ( int i = 0; i < SP_MAX_BREAKCHARS; i++ ) {
258 if ( !m_breakChars[i] ) {
264 // TODO: Error: max break chars hit
267 bool CScriptParser::IsBreakChar( char c ) {
268 for ( int i = 0; i < SP_MAX_BREAKCHARS; i++ ) {
269 if ( !m_breakChars[i] ) {
272 if ( m_breakChars[i] == c ) {
279 void CScriptParser::SetScript( char* pScript ) {
282 int len = static_cast<int>( strlen( pScript ) );
288 m_pScriptSection = m_pScript;