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
21 #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 #define 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') {
100 } else if ( c=='/' && m_pScriptSection[1] == '*' ) { // C++ style comments
101 m_pScriptSection += 2;
102 while ( *m_pScriptSection && ( *m_pScriptSection != '*' || m_pScriptSection[1] != '/' ) ) {
105 if ( *m_pScriptSection ) {
106 m_pScriptSection += 2;
116 c = *m_pScriptSection++;
119 return MakeToken(token);
121 if (len < MAX_TOKEN_STRING) {
129 if(len > 0 && IsBreakChar(*m_pScriptSection)) {
133 if (len < MAX_TOKEN_STRING) {
143 c = *m_pScriptSection;
146 if (len == MAX_TOKEN_STRING) {
151 return MakeToken(token);
154 void CScriptParser::SkipWhitespace(bool* pbNewLines) {
157 if(!m_pScript || !m_pScriptSection) {
161 while( (c = *m_pScriptSection) <= ' ') {
172 void CScriptParser::SkipBracedSection(void) {
178 token = GetToken( true );
179 if( token[1] == 0 ) {
180 if( *token == '{' ) {
182 } else if( *token == '}' ) {
186 } while( depth && *m_pScriptSection );
189 void CScriptParser::SkipRestOfLine(void) {
193 p = m_pScriptSection;
194 while ( (c = *p++) != 0 ) {
199 m_pScriptSection = p;
202 void CScriptParser::UndoGetToken(void) {
203 if(!m_pLastScriptSection) {
206 m_pScriptSection = m_pLastScriptSection;
207 m_pLastScriptSection = NULL;
210 void CScriptParser::ResetParseSession(void) {
215 m_pScriptSection = m_pScript;
216 m_pLastScriptSection = NULL;
219 char* CScriptParser::GetBufferCopy(void) {
224 int len = static_cast<int>(strlen(m_pScript));
225 char* pBuffer = new char[len + 1];
226 strcpy(pBuffer, m_pScript);
230 int CScriptParser::GetTokenOffset(void) {
231 if(!m_pScript || !m_pScriptSection) {
235 return static_cast<int>(m_pScriptSection - m_pScript);
238 void CScriptParser::LoadScript(const char* pScript) {
241 int len = static_cast<int>(strlen(pScript));
246 m_pScript = new char[len + 1];
247 m_pScript[len] = '\0';
249 strcpy(m_pScript, pScript);
250 m_pScriptSection = m_pScript;
253 void CScriptParser::AddBreakChar(char c) {
254 for(int i = 0; i < SP_MAX_BREAKCHARS; i++) {
255 if(!m_breakChars[i]) {
261 // TODO: Error: max break chars hit
264 bool CScriptParser::IsBreakChar(char c) {
265 for(int i = 0; i < SP_MAX_BREAKCHARS; i++) {
266 if(!m_breakChars[i]) {
269 if(m_breakChars[i] == c) {
276 void CScriptParser::SetScript(char* pScript) {
279 int len = static_cast<int>(strlen(pScript));
285 m_pScriptSection = m_pScript;