using
System;
using
System.IO;
namespace
Microsoft.Xna.Framework.Utilities
{
internal
static
class
FileHelpers
{
public
static
readonly
char
ForwardSlash =
'/'
;
public
static
readonly
string
ForwardSlashString =
new
string
(ForwardSlash, 1);
public
static
readonly
char
BackwardSlash =
'\\'
;
#if WINRT
public
static
readonly
char
NotSeparator = ForwardSlash;
public
static
readonly
char
Separator = BackwardSlash;
#else
public
static
readonly
char
NotSeparator = Path.DirectorySeparatorChar == BackwardSlash ? ForwardSlash : BackwardSlash;
public
static
readonly
char
Separator = Path.DirectorySeparatorChar;
#endif
public
static
string
NormalizeFilePathSeparators(
string
name)
{
return
name.Replace(NotSeparator, Separator);
}
/// <summary>
/// Combines the filePath and relativeFile based on relativeFile being a file in the same location as filePath.
/// Relative directory operators (..) are also resolved
/// </summary>
/// <example>"A\B\C.txt","D.txt" becomes "A\B\D.txt"</example>
/// <example>"A\B\C.txt","..\D.txt" becomes "A\D.txt"</example>
/// <param name="filePath">Path to the file we are starting from</param>
/// <param name="relativeFile">Relative location of another file to resolve the path to</param>
public
static
string
ResolveRelativePath(
string
filePath,
string
relativeFile)
{
filePath = filePath.Replace(BackwardSlash, ForwardSlash);
bool
hasForwardSlash = filePath.StartsWith(ForwardSlashString);
if
(!hasForwardSlash)
filePath = ForwardSlashString + filePath;
var src =
new
Uri(
"file://"
+ filePath);
var dst =
new
Uri(src, relativeFile);
var localPath = dst.LocalPath;
if
(!hasForwardSlash && localPath.StartsWith(
"/"
))
localPath = localPath.Substring(1);
return
NormalizeFilePathSeparators(localPath);
}
}
}