// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tools.DotNETCommon
{
///
/// Represents a case-insensitive name in the filesystem
///
public class FileSystemName
{
///
/// Name as it should be displayed
///
public readonly string DisplayName;
///
/// The canonical form of the name
///
public readonly string CanonicalName;
///
/// Constructor
///
/// The display name
public FileSystemName(string DisplayName)
{
this.DisplayName = DisplayName;
CanonicalName = DisplayName.ToLowerInvariant();
}
///
/// Constructor
///
/// Reference to a file or directory in the filesystem
public FileSystemName(FileSystemReference Reference)
{
int Idx = Reference.FullName.LastIndexOf(Path.DirectorySeparatorChar);
if(Idx == Reference.FullName.Length - 1)
{
DisplayName = Reference.FullName;
CanonicalName = Reference.CanonicalName;
}
else
{
DisplayName = Reference.FullName.Substring(Idx + 1);
CanonicalName = Reference.CanonicalName.Substring(Idx + 1);
}
}
///
/// Compares two filesystem object names for equality. Uses the canonical name representation, not the display name representation.
///
/// First object to compare.
/// Second object to compare.
/// True if the names represent the same object, false otherwise
public static bool operator ==(FileSystemName A, FileSystemName B)
{
if ((object)A == null)
{
return (object)B == null;
}
else
{
return (object)B != null && A.CanonicalName == B.CanonicalName;
}
}
///
/// Compares two filesystem object names for inequality. Uses the canonical name representation, not the display name representation.
///
/// First object to compare.
/// Second object to compare.
/// False if the names represent the same object, true otherwise
public static bool operator !=(FileSystemName A, FileSystemName B)
{
return !(A == B);
}
///
/// Compares against another object for equality.
///
/// other instance to compare.
/// True if the names represent the same object, false otherwise
public override bool Equals(object Obj)
{
return (Obj is FileSystemName) && ((FileSystemName)Obj) == this;
}
public bool HasExtension(string Extension)
{
if(Extension.Length == 0)
{
return CanonicalName.IndexOf('.') == -1;
}
else if(Extension[0] == '.')
{
return CanonicalName.EndsWith(Extension, StringComparison.InvariantCultureIgnoreCase);
}
else
{
return CanonicalName.Length > Extension.Length && CanonicalName[CanonicalName.Length - Extension.Length] == '.' && CanonicalName.EndsWith(Extension, StringComparison.InvariantCultureIgnoreCase);
}
}
///
/// Returns a hash code for this object
///
/// Hash code for this object
public override int GetHashCode()
{
return CanonicalName.GetHashCode();
}
///
/// Returns the display name
///
/// The display name
public override string ToString()
{
return DisplayName;
}
}
}