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
public void HandleError(ZilErrorBase 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>
public string FindIncludeFile(string name)
{
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
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))
return combined;
}
}
if (!string.IsNullOrEmpty(Path.GetExtension(combined)))
continue;
throw new FileNotFoundException();
}
combined = Path.ChangeExtension(combined, ".zil");
if (FileExists(combined))
return combined;
[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, ".mud");
if (FileExists(combined))
return combined;
// try adding a .zil or .mud extension
bool hasExtension = !string.IsNullOrEmpty(Path.GetExtension(name));
if (!hasExtension)
{
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>
......
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