mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Make next line finding a helper method
This commit is contained in:
@@ -167,67 +167,11 @@ 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().Trim();
|
first = FindNextLine(sr);
|
||||||
bool inComment = first.StartsWith("<!--");
|
second = FindNextLine(sr);
|
||||||
|
|
||||||
while ((string.IsNullOrWhiteSpace(first) || inComment) && !sr.EndOfStream)
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
second = sr.ReadLine().ToLowerInvariant().Trim();
|
|
||||||
inComment = second.StartsWith("<!--");
|
|
||||||
|
|
||||||
while ((string.IsNullOrWhiteSpace(second) || inComment) && !sr.EndOfStream)
|
|
||||||
{
|
|
||||||
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("<!--");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
|
|
||||||
@@ -289,5 +233,63 @@ namespace SabreTools.DatTools
|
|||||||
else
|
else
|
||||||
return DatFormat.ClrMamePro;
|
return DatFormat.ClrMamePro;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Find the next non-whitespace, non-comment line from an input
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sr">StreamReader representing the input</param>
|
||||||
|
/// <returns>The next complete line, if possible</returns>
|
||||||
|
private static string FindNextLine(StreamReader sr)
|
||||||
|
{
|
||||||
|
// If we're at the end of the stream, we can't do anything
|
||||||
|
if (sr.EndOfStream)
|
||||||
|
return string.Empty;
|
||||||
|
|
||||||
|
// Find the first line that's not whitespace or an XML comment
|
||||||
|
string line = sr.ReadLine().ToLowerInvariant().Trim();
|
||||||
|
bool inComment = line.StartsWith("<!--");
|
||||||
|
while ((string.IsNullOrWhiteSpace(line) || inComment) && !sr.EndOfStream)
|
||||||
|
{
|
||||||
|
// Self-contained comment lines
|
||||||
|
if (line.StartsWith("<!--") && line.EndsWith("-->"))
|
||||||
|
{
|
||||||
|
inComment = false;
|
||||||
|
line = sr.ReadLine().ToLowerInvariant().Trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start of block comments
|
||||||
|
else if (line.StartsWith("<!--"))
|
||||||
|
{
|
||||||
|
inComment = true;
|
||||||
|
line = sr.ReadLine().ToLowerInvariant().Trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
// End of block comments
|
||||||
|
else if (inComment && line.EndsWith("-->"))
|
||||||
|
{
|
||||||
|
line = sr.ReadLine().ToLowerInvariant().Trim();
|
||||||
|
inComment = line.StartsWith("<!--");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Empty lines are just skipped
|
||||||
|
else if (string.IsNullOrWhiteSpace(line))
|
||||||
|
{
|
||||||
|
line = sr.ReadLine().ToLowerInvariant().Trim();
|
||||||
|
inComment |= line.StartsWith("<!--");
|
||||||
|
}
|
||||||
|
|
||||||
|
// In-comment lines
|
||||||
|
else if (inComment)
|
||||||
|
{
|
||||||
|
line = sr.ReadLine().ToLowerInvariant().Trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we ended in a comment, return an empty string
|
||||||
|
if (inComment)
|
||||||
|
return string.Empty;
|
||||||
|
|
||||||
|
return line;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user