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),
32 CScriptParser::~CScriptParser(void)
37 void CScriptParser::ClearBuffer(void)
47 m_pScriptSection = NULL;
48 m_pLastScriptSection = NULL;
49 memset(m_breakChars, 0, sizeof(m_breakChars));
52 const char *CScriptParser::MakeToken(const char *pToken)
63 int len = static_cast<int>( strlen(pToken));
65 m_pToken = new char[len + 1];
67 strcpy(m_pToken, pToken);
72 const int MAX_TOKEN_STRING = 1024;
74 // Should NEVER return NULL
75 const char *CScriptParser::GetToken(bool bAllowLinebreaks)
78 char token[MAX_TOKEN_STRING];
79 bool bNewLines = false;
81 m_pLastScriptSection = m_pScriptSection;
86 if (!m_pScript || !m_pScriptSection) {
87 return MakeToken(token);
91 SkipWhitespace(&bNewLines);
92 if (!*m_pScriptSection) {
93 return MakeToken(token);
95 if (bNewLines && !bAllowLinebreaks) {
96 return MakeToken(token);
99 c = *m_pScriptSection;
101 if (c == '/' && m_pScriptSection[1] == '/') { // C style comments
102 m_pScriptSection += 2;
103 while (*m_pScriptSection && *m_pScriptSection != '\n') {
106 } else if (c == '/' && m_pScriptSection[1] == '*') { // C++ style comments
107 m_pScriptSection += 2;
108 while (*m_pScriptSection && (*m_pScriptSection != '*' || m_pScriptSection[1] != '/')) {
111 if (*m_pScriptSection) {
112 m_pScriptSection += 2;
122 c = *m_pScriptSection++;
123 if (c == '\"' || !c) {
125 return MakeToken(token);
127 if (len < MAX_TOKEN_STRING) {
135 if (len > 0 && IsBreakChar(*m_pScriptSection)) {
139 if (len < MAX_TOKEN_STRING) {
145 if (IsBreakChar(c)) {
149 c = *m_pScriptSection;
152 if (len == MAX_TOKEN_STRING) {
157 return MakeToken(token);
160 void CScriptParser::SkipWhitespace(bool *pbNewLines)
164 if (!m_pScript || !m_pScriptSection) {
168 while ((c = *m_pScriptSection) <= ' ') {
179 void CScriptParser::SkipBracedSection(void)
186 token = GetToken(true);
190 } else if (*token == '}') {
194 } while (depth && *m_pScriptSection);
197 void CScriptParser::SkipRestOfLine(void)
202 p = m_pScriptSection;
203 while ((c = *p++) != 0) {
208 m_pScriptSection = p;
211 void CScriptParser::UndoGetToken(void)
213 if (!m_pLastScriptSection) {
216 m_pScriptSection = m_pLastScriptSection;
217 m_pLastScriptSection = NULL;
220 void CScriptParser::ResetParseSession(void)
226 m_pScriptSection = m_pScript;
227 m_pLastScriptSection = NULL;
230 char *CScriptParser::GetBufferCopy(void)
236 int len = static_cast<int>( strlen(m_pScript));
237 char *pBuffer = new char[len + 1];
238 strcpy(pBuffer, m_pScript);
242 int CScriptParser::GetTokenOffset(void)
244 if (!m_pScript || !m_pScriptSection) {
248 return static_cast<int>( m_pScriptSection - m_pScript );
251 void CScriptParser::LoadScript(const char *pScript)
255 int len = static_cast<int>( strlen(pScript));
260 m_pScript = new char[len + 1];
261 m_pScript[len] = '\0';
263 strcpy(m_pScript, pScript);
264 m_pScriptSection = m_pScript;
267 void CScriptParser::AddBreakChar(char c)
269 for (int i = 0; i < SP_MAX_BREAKCHARS; i++) {
270 if (!m_breakChars[i]) {
276 // TODO: Error: max break chars hit
279 bool CScriptParser::IsBreakChar(char c)
281 for (int i = 0; i < SP_MAX_BREAKCHARS; i++) {
282 if (!m_breakChars[i]) {
285 if (m_breakChars[i] == c) {
292 void CScriptParser::SetScript(char *pScript)
296 int len = static_cast<int>( strlen(pScript));
302 m_pScriptSection = m_pScript;