Smarter XML comment skipping (fixes #37)

This commit is contained in:
Matt Nadareski
2021-07-18 12:48:59 -07:00
parent ef2b51a36a
commit 4bd6fd6fcd

View File

@@ -167,23 +167,65 @@ namespace SabreTools.DatTools
// Get the first two non-whitespace, non-comment lines to check, if possible // Get the first two non-whitespace, non-comment lines to check, if possible
string first = string.Empty, second = string.Empty; string first = string.Empty, second = string.Empty;
// TODO: Add handling of multi-line comments
try try
{ {
using StreamReader sr = File.OpenText(filename); using StreamReader sr = File.OpenText(filename);
first = sr.ReadLine().ToLowerInvariant(); first = sr.ReadLine().ToLowerInvariant().Trim();
while ((string.IsNullOrWhiteSpace(first) || first.StartsWith("<!--")) bool inComment = first.StartsWith("<!--");
&& !sr.EndOfStream)
while ((string.IsNullOrWhiteSpace(first) || inComment) && !sr.EndOfStream)
{ {
first = sr.ReadLine().ToLowerInvariant(); if (first.StartsWith("<!--") && first.EndsWith("-->"))
{
inComment = false;
first = sr.ReadLine().ToLowerInvariant().Trim();
}
else if (first.StartsWith("<!--"))
{
inComment = true;
first = sr.ReadLine().ToLowerInvariant().Trim();
}
else if (inComment && first.EndsWith("-->"))
{
first = sr.ReadLine().ToLowerInvariant().Trim();
inComment = first.StartsWith("<!--");
}
else if (string.IsNullOrWhiteSpace(first) || inComment)
{
first = sr.ReadLine().ToLowerInvariant().Trim();
inComment |= first.StartsWith("<!--");
}
} }
if (!sr.EndOfStream) if (!sr.EndOfStream)
{ {
second = sr.ReadLine().ToLowerInvariant(); second = sr.ReadLine().ToLowerInvariant().Trim();
while (string.IsNullOrWhiteSpace(second) || second.StartsWith("<!--") inComment = second.StartsWith("<!--");
&& !sr.EndOfStream)
while ((string.IsNullOrWhiteSpace(second) || inComment) && !sr.EndOfStream)
{ {
second = sr.ReadLine().ToLowerInvariant(); if (second.StartsWith("<!--") && second.EndsWith("-->"))
{
inComment = false;
second = sr.ReadLine().ToLowerInvariant().Trim();
}
else if (second.StartsWith("<!--"))
{
inComment = true;
second = sr.ReadLine().ToLowerInvariant().Trim();
}
else if (inComment && second.EndsWith("-->"))
{
second = sr.ReadLine().ToLowerInvariant().Trim();
inComment = second.StartsWith("<!--");
}
else if (string.IsNullOrWhiteSpace(second) || inComment)
{
second = sr.ReadLine().ToLowerInvariant().Trim();
inComment |= second.StartsWith("<!--");
}
} }
} }
} }