Commit c4938f53 authored by Jesse McGrew's avatar Jesse McGrew

Change include file searching to try a lowercase version of the filename.

parent 4d4d06ff127a
...@@ -708,30 +708,59 @@ namespace Zilf.Interpreter ...@@ -708,30 +708,59 @@ namespace Zilf.Interpreter
public void HandleError(ZilErrorBase ex) => public void HandleError(ZilErrorBase ex) =>
DiagnosticManager.Handle(ex.Diagnostic ?? throw new ArgumentException("Missing diagnostic", nameof(ex))); DiagnosticManager.Handle(ex.Diagnostic ?? throw new ArgumentException("Missing diagnostic", nameof(ex)));
private static readonly string[] IncludeFileExtensions = { ".zil", ".mud", ".ZIL", ".MUD" };
/// <exception cref="FileNotFoundException">The file wasn't found in any include path.</exception> /// <exception cref="FileNotFoundException">The file wasn't found in any include path.</exception>
public string FindIncludeFile(string name) public string FindIncludeFile(string name)
{ {
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
foreach (var path in IncludePaths) foreach (var path in IncludePaths)
{ {
var combined = Path.Combine(path, name); foreach (var nameVariant in GetIncludeFileNameVariants(name))
{
var combined = Path.Combine(path, nameVariant);
if (FileExists(combined)) if (FileExists(combined))
return combined; return combined;
}
}
if (!string.IsNullOrEmpty(Path.GetExtension(combined))) throw new FileNotFoundException();
continue; }
[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase",
Justification = "This method looks for a lowercase filename when the source specifies an uppercase filename.")]
private static IEnumerable<string> GetIncludeFileNameVariants(string name)
{
// try the name as-is
yield return name;
combined = Path.ChangeExtension(combined, ".zil"); // try adding a .zil or .mud extension
if (FileExists(combined)) bool hasExtension = !string.IsNullOrEmpty(Path.GetExtension(name));
return combined;
combined = Path.ChangeExtension(combined, ".mud"); if (!hasExtension)
if (FileExists(combined)) {
return combined; foreach (var ext in IncludeFileExtensions)
{
yield return Path.ChangeExtension(name, ext);
}
} }
throw new FileNotFoundException(); // try lowercasing the name
var lower = name.ToLowerInvariant();
if (lower != name)
{
yield return lower;
// try adding a .zil or .mud extension
if (!hasExtension)
{
foreach (var ext in IncludeFileExtensions)
{
yield return Path.ChangeExtension(lower, ext);
}
}
}
} }
/// <summary> /// <summary>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment