diff options
-rw-r--r-- | tools/scaninc/c_file.cpp | 97 | ||||
-rw-r--r-- | tools/scaninc/c_file.h | 2 |
2 files changed, 30 insertions, 69 deletions
diff --git a/tools/scaninc/c_file.cpp b/tools/scaninc/c_file.cpp index 2d3f8749a..e4e20e609 100644 --- a/tools/scaninc/c_file.cpp +++ b/tools/scaninc/c_file.cpp @@ -45,8 +45,6 @@ CFile::CFile(std::string path) m_pos = 0; m_lineNum = 1; - - RemoveComments(); } CFile::~CFile() @@ -54,71 +52,6 @@ CFile::~CFile() delete[] m_buffer; } -// Removes comments to simplify further processing. -// It stops upon encountering a null character, -// which may or may not be the end of file marker. -// If it's not, the error will be caught later. -void CFile::RemoveComments() -{ - long pos = 0; - char stringChar = 0; - - for (;;) - { - if (m_buffer[pos] == 0) - return; - - if (stringChar != 0) - { - if (m_buffer[pos] == '\\' && m_buffer[pos + 1] == stringChar) - { - pos += 2; - } - else - { - if (m_buffer[pos] == stringChar) - stringChar = 0; - pos++; - } - } - else if (m_buffer[pos] == '/' && m_buffer[pos + 1] == '/') - { - while (m_buffer[pos] != '\n' && m_buffer[pos] != 0) - m_buffer[pos++] = ' '; - } - else if (m_buffer[pos] == '/' && m_buffer[pos + 1] == '*') - { - m_buffer[pos++] = ' '; - m_buffer[pos++] = ' '; - - for (;;) - { - if (m_buffer[pos] == 0) - return; - - if (m_buffer[pos] == '*' && m_buffer[pos + 1] == '/') - { - m_buffer[pos++] = ' '; - m_buffer[pos++] = ' '; - break; - } - else - { - if (m_buffer[pos] != '\n') - m_buffer[pos] = ' '; - pos++; - } - } - } - else - { - if (m_buffer[pos] == '"' || m_buffer[pos] == '\'') - stringChar = m_buffer[pos]; - pos++; - } - } -} - void CFile::FindIncbins() { char stringChar = 0; @@ -195,9 +128,37 @@ bool CFile::ConsumeNewline() return false; } +bool CFile::ConsumeComment() +{ + if (m_buffer[m_pos] == '/' && m_buffer[m_pos + 1] == '*') + { + m_pos += 2; + while (m_buffer[m_pos] != '*' && m_buffer[m_pos + 1] != '/') + { + if (!ConsumeNewline()) + { + m_pos++; + } + } + m_pos += 2; + return true; + } + else if (m_buffer[m_pos] == '/' && m_buffer[m_pos + 1] == '/') + { + m_pos += 2; + while (!ConsumeNewline()) + { + m_pos++; + } + return true; + } + + return false; +} + void CFile::SkipWhitespace() { - while (ConsumeHorizontalWhitespace() || ConsumeNewline()) + while (ConsumeHorizontalWhitespace() || ConsumeNewline() || ConsumeComment()) ; } diff --git a/tools/scaninc/c_file.h b/tools/scaninc/c_file.h index 38b10f04e..618901b85 100644 --- a/tools/scaninc/c_file.h +++ b/tools/scaninc/c_file.h @@ -44,9 +44,9 @@ private: std::set<std::string> m_incbins; std::set<std::string> m_includes; - void RemoveComments(); bool ConsumeHorizontalWhitespace(); bool ConsumeNewline(); + bool ConsumeComment(); void SkipWhitespace(); bool CheckIdentifier(const std::string& ident); void CheckInclude(); |