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"
22 CScriptParser::CScriptParser(void):
24 m_pScriptSection(NULL),
25 m_pLastScriptSection(NULL),
30 CScriptParser::~CScriptParser(void) {
34 void CScriptParser::ClearBuffer(void) {
43 m_pScriptSection = NULL;
44 m_pLastScriptSection = NULL;
45 memset(m_breakChars, 0, sizeof(m_breakChars));
48 const char* CScriptParser::MakeToken(const char* pToken) {
58 int len = static_cast<int>(strlen(pToken));
60 m_pToken = new char[len + 1];
62 strcpy(m_pToken, pToken);
67 #define MAX_TOKEN_STRING 1024
68 // Should NEVER return NULL
69 const char* CScriptParser::GetToken(bool bAllowLinebreaks) {
71 char token[MAX_TOKEN_STRING];
72 bool bNewLines = false;
74 m_pLastScriptSection = m_pScriptSection;
79 if(!m_pScript || !m_pScriptSection) {
80 return MakeToken(token);
84 SkipWhitespace( &bNewLines );
85 if ( !*m_pScriptSection ) {
86 return MakeToken(token);
88 if ( bNewLines && !bAllowLinebreaks ) {
89 return MakeToken(token);
92 c = *m_pScriptSection;
94 if ( c == '/' && m_pScriptSection[1] == '/' ) { // C style comments
95 m_pScriptSection += 2;
96 while (*m_pScriptSection && *m_pScriptSection != '\n') {
99 } else if ( c=='/' && m_pScriptSection[1] == '*' ) { // C++ style comments
100 m_pScriptSection += 2;
101 while ( *m_pScriptSection && ( *m_pScriptSection != '*' || m_pScriptSection[1] != '/' ) ) {
104 if ( *m_pScriptSection ) {
105 m_pScriptSection += 2;
115 c = *m_pScriptSection++;
118 return MakeToken(token);
120 if (len < MAX_TOKEN_STRING) {
128 if(len > 0 && IsBreakChar(*m_pScriptSection)) {
132 if (len < MAX_TOKEN_STRING) {
142 c = *m_pScriptSection;
145 if (len == MAX_TOKEN_STRING) {
150 return MakeToken(token);
153 void CScriptParser::SkipWhitespace(bool* pbNewLines) {
156 if(!m_pScript || !m_pScriptSection) {
160 while( (c = *m_pScriptSection) <= ' ') {
171 void CScriptParser::SkipBracedSection(void) {
177 token = GetToken( true );
178 if( token[1] == 0 ) {
179 if( *token == '{' ) {
181 } else if( *token == '}' ) {
185 } while( depth && *m_pScriptSection );
188 void CScriptParser::SkipRestOfLine(void) {
192 p = m_pScriptSection;
193 while ( (c = *p++) != 0 ) {
198 m_pScriptSection = p;
201 void CScriptParser::UndoGetToken(void) {
202 if(!m_pLastScriptSection) {
205 m_pScriptSection = m_pLastScriptSection;
206 m_pLastScriptSection = NULL;
209 void CScriptParser::ResetParseSession(void) {
214 m_pScriptSection = m_pScript;
215 m_pLastScriptSection = NULL;
218 char* CScriptParser::GetBufferCopy(void) {
223 int len = static_cast<int>(strlen(m_pScript));
224 char* pBuffer = new char[len + 1];
225 strcpy(pBuffer, m_pScript);
229 int CScriptParser::GetTokenOffset(void) {
230 if(!m_pScript || !m_pScriptSection) {
234 return static_cast<int>(m_pScriptSection - m_pScript);
237 void CScriptParser::LoadScript(const char* pScript) {
240 int len = static_cast<int>(strlen(pScript));
245 m_pScript = new char[len + 1];
246 m_pScript[len] = '\0';
248 strcpy(m_pScript, pScript);
249 m_pScriptSection = m_pScript;
252 void CScriptParser::AddBreakChar(char c) {
253 for(int i = 0; i < SP_MAX_BREAKCHARS; i++) {
254 if(!m_breakChars[i]) {
260 // TODO: Error: max break chars hit
263 bool CScriptParser::IsBreakChar(char c) {
264 for(int i = 0; i < SP_MAX_BREAKCHARS; i++) {
265 if(!m_breakChars[i]) {
268 if(m_breakChars[i] == c) {
275 void CScriptParser::SetScript(char* pScript) {
278 int len = static_cast<int>(strlen(pScript));
284 m_pScriptSection = m_pScript;