summaryrefslogtreecommitdiff
path: root/tools/scaninc/c_file.cpp
diff options
context:
space:
mode:
authoryenatch <yenatch@gmail.com>2017-07-23 22:34:49 -0400
committeryenatch <yenatch@gmail.com>2017-07-23 22:34:49 -0400
commite73ec108adb15cda40c17ebe891d25460c3c42b3 (patch)
tree6add0d130afc2a64a08a1879111304a3e2b7f8c5 /tools/scaninc/c_file.cpp
parent89c9c19446843c978485f67eeaa3d55ac26a15ec (diff)
scaninc: skipping comments seems to be faster than removing them
Diffstat (limited to 'tools/scaninc/c_file.cpp')
-rw-r--r--tools/scaninc/c_file.cpp97
1 files changed, 29 insertions, 68 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())
;
}