]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - contrib/bobtoolz/ScriptParser.cpp
more eol-style
[xonotic/netradiant.git] / contrib / bobtoolz / ScriptParser.cpp
index c94175e131924d59d0e05649515aa91f51683f5f..012e531a822be3683715e6b695f60ef39473b277 100644 (file)
-#include "StdAfx.h"\r
-#include "ScriptParser.h"\r
-\r
-CScriptParser::CScriptParser(void): \r
-       m_pScript(NULL),\r
-       m_pScriptSection(NULL),\r
-       m_pLastScriptSection(NULL),\r
-       m_pToken(NULL) {\r
-       ClearBuffer();\r
-}\r
-\r
-CScriptParser::~CScriptParser(void) {\r
-       ClearBuffer();\r
-}\r
-\r
-void CScriptParser::ClearBuffer(void) {\r
-       if(m_pScript) {\r
-               delete[] m_pScript;\r
-               m_pScript = NULL;\r
-       }\r
-       if(m_pToken) {\r
-               delete[] m_pToken;\r
-               m_pToken = NULL;\r
-       }\r
-       m_pScriptSection = NULL;\r
-       m_pLastScriptSection = NULL;\r
-       memset(m_breakChars, 0, sizeof(m_breakChars));\r
-}\r
-\r
-const char* CScriptParser::MakeToken(const char* pToken) {\r
-       if(m_pToken) {\r
-               delete[] m_pToken;\r
-               m_pToken = NULL;\r
-       }\r
-\r
-       if(!pToken) {\r
-               pToken = "";\r
-       }\r
-\r
-       int len = static_cast<int>(strlen(pToken));\r
-\r
-       m_pToken = new char[len + 1];\r
-       m_pToken[len] = '\0';\r
-       strcpy(m_pToken, pToken);\r
-\r
-       return m_pToken;\r
-}\r
-\r
-#define MAX_TOKEN_STRING 1024\r
-// Should NEVER return NULL\r
-const char* CScriptParser::GetToken(bool bAllowLinebreaks) {\r
-       int c = 0, len;\r
-       char token[MAX_TOKEN_STRING];\r
-       bool bNewLines = false;\r
-\r
-       m_pLastScriptSection = m_pScriptSection;\r
-\r
-       len = 0;\r
-       *token = '\0';\r
-\r
-       if(!m_pScript || !m_pScriptSection) {\r
-               return MakeToken(token);\r
-       }\r
-\r
-       while ( true ) {\r
-               SkipWhitespace( &bNewLines );\r
-               if ( !*m_pScriptSection ) {\r
-                       return MakeToken(token);\r
-               }\r
-               if ( bNewLines && !bAllowLinebreaks ) {\r
-                       return MakeToken(token);\r
-               }\r
-\r
-               c = *m_pScriptSection;\r
-               \r
-               if ( c == '/' && m_pScriptSection[1] == '/' ) { // C style comments\r
-                       m_pScriptSection += 2;\r
-                       while (*m_pScriptSection && *m_pScriptSection != '\n') {\r
-                               m_pScriptSection++;\r
-                       }\r
-               } else if ( c=='/' && m_pScriptSection[1] == '*' ) { // C++ style comments\r
-                       m_pScriptSection += 2;\r
-                       while ( *m_pScriptSection && ( *m_pScriptSection != '*' || m_pScriptSection[1] != '/' ) ) {\r
-                               m_pScriptSection++;\r
-                       }\r
-                       if ( *m_pScriptSection ) {\r
-                               m_pScriptSection += 2;\r
-                       }\r
-               } else {\r
-                       break;\r
-               }\r
-       }\r
-\r
-       if (c == '\"') {\r
-               m_pScriptSection++;\r
-               while ( true ) {\r
-                       c = *m_pScriptSection++;\r
-                       if (c=='\"' || !c) {\r
-                               token[len] = 0;\r
-                               return MakeToken(token);\r
-                       }\r
-                       if (len < MAX_TOKEN_STRING) {\r
-                               token[len] = c;\r
-                               len++;\r
-                       }\r
-               }\r
-       }\r
-\r
-       do {\r
-               if(len > 0 && IsBreakChar(*m_pScriptSection)) {\r
-                       break;\r
-               }\r
-\r
-               if (len < MAX_TOKEN_STRING) {\r
-                       token[len] = c;\r
-                       len++;\r
-               }\r
-               m_pScriptSection++;\r
-\r
-               if(IsBreakChar(c)) {\r
-                       break;\r
-               }\r
-\r
-               c = *m_pScriptSection;\r
-       } while (c > 32);\r
-\r
-       if (len == MAX_TOKEN_STRING) {\r
-               len = 0;\r
-       }\r
-       token[len] = 0;\r
-\r
-       return MakeToken(token);\r
-}\r
-\r
-void CScriptParser::SkipWhitespace(bool* pbNewLines) {\r
-       int c;\r
-\r
-       if(!m_pScript || !m_pScriptSection) {\r
-               return;\r
-       }\r
-\r
-       while( (c = *m_pScriptSection) <= ' ') {\r
-               if( !c ) {\r
-                       return;\r
-               }\r
-               if( c == '\n' ) {\r
-                       *pbNewLines = true;\r
-               }\r
-               m_pScriptSection++;\r
-       }\r
-}\r
-\r
-void CScriptParser::SkipBracedSection(void) {\r
-       const char              *token;\r
-       int                             depth;\r
-\r
-       depth = 0;\r
-       do {\r
-               token = GetToken( true );\r
-               if( token[1] == 0 ) {\r
-                       if( *token == '{' ) {\r
-                               depth++;\r
-                       } else if( *token == '}' ) {\r
-                               depth--;\r
-                       }\r
-               }\r
-       } while( depth && *m_pScriptSection );\r
-}\r
-\r
-void CScriptParser::SkipRestOfLine(void) {\r
-       char    *p;\r
-       int             c;\r
-\r
-       p = m_pScriptSection;\r
-       while ( (c = *p++) != 0 ) {\r
-               if ( c == '\n' ) {\r
-                       break;\r
-               }\r
-       }\r
-       m_pScriptSection = p;\r
-}\r
-\r
-void CScriptParser::UndoGetToken(void) {\r
-       if(!m_pLastScriptSection) {\r
-               return;\r
-       }\r
-       m_pScriptSection = m_pLastScriptSection;\r
-       m_pLastScriptSection = NULL;\r
-}\r
-\r
-void CScriptParser::ResetParseSession(void) {\r
-       if(!m_pScript) {\r
-               return;\r
-       }\r
-\r
-       m_pScriptSection = m_pScript;\r
-       m_pLastScriptSection = NULL;\r
-}\r
-\r
-char* CScriptParser::GetBufferCopy(void) {\r
-       if(!m_pScript) {\r
-               return NULL;\r
-       }\r
-\r
-       int len = static_cast<int>(strlen(m_pScript));\r
-       char* pBuffer = new char[len + 1];\r
-       strcpy(pBuffer, m_pScript);\r
-       return pBuffer;\r
-}\r
-\r
-int CScriptParser::GetTokenOffset(void) {\r
-       if(!m_pScript || !m_pScriptSection) {\r
-               return 0;\r
-       }\r
-\r
-       return static_cast<int>(m_pScriptSection - m_pScript);\r
-}\r
-\r
-void CScriptParser::LoadScript(const char* pScript) {\r
-       ClearBuffer();\r
-\r
-       int len = static_cast<int>(strlen(pScript));\r
-       if(len <= 0) {\r
-               return;\r
-       }\r
-\r
-       m_pScript = new char[len + 1];\r
-       m_pScript[len] = '\0';\r
-\r
-       strcpy(m_pScript, pScript);\r
-       m_pScriptSection = m_pScript;\r
-}\r
-\r
-void CScriptParser::AddBreakChar(char c) {\r
-       for(int i = 0; i < SP_MAX_BREAKCHARS; i++) {\r
-               if(!m_breakChars[i]) {\r
-                       m_breakChars[i] = c;\r
-                       return;\r
-               }\r
-       }\r
-\r
-       // TODO: Error: max break chars hit\r
-}\r
-\r
-bool CScriptParser::IsBreakChar(char c) {\r
-       for(int i = 0; i < SP_MAX_BREAKCHARS; i++) {\r
-               if(!m_breakChars[i]) {\r
-                       return false;\r
-               }\r
-               if(m_breakChars[i] == c) {\r
-                       return true;\r
-               }\r
-       }\r
-       return false;\r
-}\r
-\r
-void CScriptParser::SetScript(char* pScript) {\r
-       ClearBuffer();\r
-\r
-       int len = static_cast<int>(strlen(pScript));\r
-       if(len <= 0) {\r
-               return;\r
-       }\r
-\r
-       m_pScript = pScript;\r
-       m_pScriptSection = m_pScript;\r
-}\r
+#include "StdAfx.h"
+#include "ScriptParser.h"
+
+CScriptParser::CScriptParser(void): 
+       m_pScript(NULL),
+       m_pScriptSection(NULL),
+       m_pLastScriptSection(NULL),
+       m_pToken(NULL) {
+       ClearBuffer();
+}
+
+CScriptParser::~CScriptParser(void) {
+       ClearBuffer();
+}
+
+void CScriptParser::ClearBuffer(void) {
+       if(m_pScript) {
+               delete[] m_pScript;
+               m_pScript = NULL;
+       }
+       if(m_pToken) {
+               delete[] m_pToken;
+               m_pToken = NULL;
+       }
+       m_pScriptSection = NULL;
+       m_pLastScriptSection = NULL;
+       memset(m_breakChars, 0, sizeof(m_breakChars));
+}
+
+const char* CScriptParser::MakeToken(const char* pToken) {
+       if(m_pToken) {
+               delete[] m_pToken;
+               m_pToken = NULL;
+       }
+
+       if(!pToken) {
+               pToken = "";
+       }
+
+       int len = static_cast<int>(strlen(pToken));
+
+       m_pToken = new char[len + 1];
+       m_pToken[len] = '\0';
+       strcpy(m_pToken, pToken);
+
+       return m_pToken;
+}
+
+#define MAX_TOKEN_STRING 1024
+// Should NEVER return NULL
+const char* CScriptParser::GetToken(bool bAllowLinebreaks) {
+       int c = 0, len;
+       char token[MAX_TOKEN_STRING];
+       bool bNewLines = false;
+
+       m_pLastScriptSection = m_pScriptSection;
+
+       len = 0;
+       *token = '\0';
+
+       if(!m_pScript || !m_pScriptSection) {
+               return MakeToken(token);
+       }
+
+       while ( true ) {
+               SkipWhitespace( &bNewLines );
+               if ( !*m_pScriptSection ) {
+                       return MakeToken(token);
+               }
+               if ( bNewLines && !bAllowLinebreaks ) {
+                       return MakeToken(token);
+               }
+
+               c = *m_pScriptSection;
+               
+               if ( c == '/' && m_pScriptSection[1] == '/' ) { // C style comments
+                       m_pScriptSection += 2;
+                       while (*m_pScriptSection && *m_pScriptSection != '\n') {
+                               m_pScriptSection++;
+                       }
+               } else if ( c=='/' && m_pScriptSection[1] == '*' ) { // C++ style comments
+                       m_pScriptSection += 2;
+                       while ( *m_pScriptSection && ( *m_pScriptSection != '*' || m_pScriptSection[1] != '/' ) ) {
+                               m_pScriptSection++;
+                       }
+                       if ( *m_pScriptSection ) {
+                               m_pScriptSection += 2;
+                       }
+               } else {
+                       break;
+               }
+       }
+
+       if (c == '\"') {
+               m_pScriptSection++;
+               while ( true ) {
+                       c = *m_pScriptSection++;
+                       if (c=='\"' || !c) {
+                               token[len] = 0;
+                               return MakeToken(token);
+                       }
+                       if (len < MAX_TOKEN_STRING) {
+                               token[len] = c;
+                               len++;
+                       }
+               }
+       }
+
+       do {
+               if(len > 0 && IsBreakChar(*m_pScriptSection)) {
+                       break;
+               }
+
+               if (len < MAX_TOKEN_STRING) {
+                       token[len] = c;
+                       len++;
+               }
+               m_pScriptSection++;
+
+               if(IsBreakChar(c)) {
+                       break;
+               }
+
+               c = *m_pScriptSection;
+       } while (c > 32);
+
+       if (len == MAX_TOKEN_STRING) {
+               len = 0;
+       }
+       token[len] = 0;
+
+       return MakeToken(token);
+}
+
+void CScriptParser::SkipWhitespace(bool* pbNewLines) {
+       int c;
+
+       if(!m_pScript || !m_pScriptSection) {
+               return;
+       }
+
+       while( (c = *m_pScriptSection) <= ' ') {
+               if( !c ) {
+                       return;
+               }
+               if( c == '\n' ) {
+                       *pbNewLines = true;
+               }
+               m_pScriptSection++;
+       }
+}
+
+void CScriptParser::SkipBracedSection(void) {
+       const char              *token;
+       int                             depth;
+
+       depth = 0;
+       do {
+               token = GetToken( true );
+               if( token[1] == 0 ) {
+                       if( *token == '{' ) {
+                               depth++;
+                       } else if( *token == '}' ) {
+                               depth--;
+                       }
+               }
+       } while( depth && *m_pScriptSection );
+}
+
+void CScriptParser::SkipRestOfLine(void) {
+       char    *p;
+       int             c;
+
+       p = m_pScriptSection;
+       while ( (c = *p++) != 0 ) {
+               if ( c == '\n' ) {
+                       break;
+               }
+       }
+       m_pScriptSection = p;
+}
+
+void CScriptParser::UndoGetToken(void) {
+       if(!m_pLastScriptSection) {
+               return;
+       }
+       m_pScriptSection = m_pLastScriptSection;
+       m_pLastScriptSection = NULL;
+}
+
+void CScriptParser::ResetParseSession(void) {
+       if(!m_pScript) {
+               return;
+       }
+
+       m_pScriptSection = m_pScript;
+       m_pLastScriptSection = NULL;
+}
+
+char* CScriptParser::GetBufferCopy(void) {
+       if(!m_pScript) {
+               return NULL;
+       }
+
+       int len = static_cast<int>(strlen(m_pScript));
+       char* pBuffer = new char[len + 1];
+       strcpy(pBuffer, m_pScript);
+       return pBuffer;
+}
+
+int CScriptParser::GetTokenOffset(void) {
+       if(!m_pScript || !m_pScriptSection) {
+               return 0;
+       }
+
+       return static_cast<int>(m_pScriptSection - m_pScript);
+}
+
+void CScriptParser::LoadScript(const char* pScript) {
+       ClearBuffer();
+
+       int len = static_cast<int>(strlen(pScript));
+       if(len <= 0) {
+               return;
+       }
+
+       m_pScript = new char[len + 1];
+       m_pScript[len] = '\0';
+
+       strcpy(m_pScript, pScript);
+       m_pScriptSection = m_pScript;
+}
+
+void CScriptParser::AddBreakChar(char c) {
+       for(int i = 0; i < SP_MAX_BREAKCHARS; i++) {
+               if(!m_breakChars[i]) {
+                       m_breakChars[i] = c;
+                       return;
+               }
+       }
+
+       // TODO: Error: max break chars hit
+}
+
+bool CScriptParser::IsBreakChar(char c) {
+       for(int i = 0; i < SP_MAX_BREAKCHARS; i++) {
+               if(!m_breakChars[i]) {
+                       return false;
+               }
+               if(m_breakChars[i] == c) {
+                       return true;
+               }
+       }
+       return false;
+}
+
+void CScriptParser::SetScript(char* pScript) {
+       ClearBuffer();
+
+       int len = static_cast<int>(strlen(pScript));
+       if(len <= 0) {
+               return;
+       }
+
+       m_pScript = pScript;
+       m_pScriptSection = m_pScript;
+}