Add PathTools.Combine

This commit is contained in:
Alex Barney 2019-05-20 15:01:14 -05:00
parent b92750688e
commit f7b983ccb2
9 changed files with 33 additions and 16 deletions

View file

@ -38,7 +38,7 @@ namespace LibHac.Nand
DirectoryEntryType type = isDir ? DirectoryEntryType.Directory : DirectoryEntryType.File;
long length = isDir ? 0 : entry.FileSystem.GetFileLength(entry.FullName);
yield return new DirectoryEntry(entry.Name, FullPath + '/' + entry.Name, type, length)
yield return new DirectoryEntry(entry.Name, PathTools.Combine(FullPath, entry.Name), type, length)
{
Attributes = entry.Attributes.ToNxAttributes()
};

View file

@ -18,8 +18,8 @@ namespace LibHac.Fs
foreach (DirectoryEntry entry in source.Read())
{
string subSrcPath = PathTools.Normalize(source.FullPath + '/' + entry.Name);
string subDstPath = PathTools.Normalize(dest.FullPath + '/' + entry.Name);
string subSrcPath = PathTools.Normalize(PathTools.Combine(source.FullPath, entry.Name));
string subDstPath = PathTools.Normalize(PathTools.Combine(dest.FullPath, entry.Name));
if (entry.Type == DirectoryEntryType.Directory)
{
@ -95,7 +95,7 @@ namespace LibHac.Fs
if (entry.Type != DirectoryEntryType.Directory || !recurse) continue;
IDirectory subDir = fs.OpenDirectory(directory.FullPath + '/' + entry.Name, OpenDirectoryMode.All);
IDirectory subDir = fs.OpenDirectory(PathTools.Combine(directory.FullPath, entry.Name), OpenDirectoryMode.All);
foreach (DirectoryEntry subEntry in subDir.EnumerateEntries(searchPattern, searchOptions))
{
@ -189,9 +189,10 @@ namespace LibHac.Fs
foreach (DirectoryEntry entry in directory.Read())
{
string subPath = PathTools.Combine(directory.FullPath, entry.Name);
if (entry.Type == DirectoryEntryType.Directory)
{
string subPath = directory.FullPath + '/' + entry.Name;
IDirectory subDir = fs.OpenDirectory(subPath, OpenDirectoryMode.All);
CleanDirectoryRecursivelyGeneric(subDir);
@ -199,7 +200,7 @@ namespace LibHac.Fs
}
else if (entry.Type == DirectoryEntryType.File)
{
fs.DeleteFile(directory.FullPath + '/' + entry.Name);
fs.DeleteFile(subPath);
}
}
}

View file

@ -34,7 +34,7 @@ namespace LibHac.Fs
DirectoryEntryType type = isDir ? DirectoryEntryType.Directory : DirectoryEntryType.File;
long length = isDir ? 0 : ((FileInfo)entry).Length;
yield return new DirectoryEntry(entry.Name, FullPath + '/' + entry.Name, type, length)
yield return new DirectoryEntry(entry.Name, PathTools.Combine(FullPath, entry.Name), type, length)
{
Attributes = entry.Attributes.ToNxAttributes()
};

View file

@ -24,7 +24,7 @@ namespace LibHac.Fs
internal string ResolveLocalPath(string path)
{
return Path.Combine(BasePath, path.TrimStart('/'));
return PathTools.Combine(BasePath, path);
}
public NxFileAttributes GetFileAttributes(string path)

View file

@ -217,6 +217,23 @@ namespace LibHac.Fs
return path[rootLength] == DirectorySeparator && path.Length > rootLength + 1;
}
public static string Combine(string path1, string path2)
{
if(path1 == null || path2 == null) throw new NullReferenceException();
if (string.IsNullOrEmpty(path1)) return path2;
if (string.IsNullOrEmpty(path2)) return path1;
bool hasSeparator = IsDirectorySeparator(path1[path1.Length - 1]) || IsDirectorySeparator(path2[0]);
if (hasSeparator)
{
return path1 + path2;
}
return path1 + DirectorySeparator + path2;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsDirectorySeparator(char c)
{

View file

@ -29,7 +29,7 @@ namespace LibHac.Fs.RomFs
{
while (tab.FindNextDirectory(ref position, out string name))
{
yield return new DirectoryEntry(name, FullPath + '/' + name, DirectoryEntryType.Directory, 0);
yield return new DirectoryEntry(name, PathTools.Combine(FullPath, name), DirectoryEntryType.Directory, 0);
}
}
@ -37,7 +37,7 @@ namespace LibHac.Fs.RomFs
{
while (tab.FindNextFile(ref position, out RomFileInfo info, out string name))
{
yield return new DirectoryEntry(name, FullPath + '/' + name, DirectoryEntryType.File, info.Length);
yield return new DirectoryEntry(name, PathTools.Combine(FullPath, name), DirectoryEntryType.File, info.Length);
}
}
}

View file

@ -29,7 +29,7 @@ namespace LibHac.Fs.Save
{
while (tab.FindNextDirectory(ref position, out string name))
{
yield return new DirectoryEntry(name, FullPath + '/' + name, DirectoryEntryType.Directory, 0);
yield return new DirectoryEntry(name, PathTools.Combine(FullPath, name), DirectoryEntryType.Directory, 0);
}
}
@ -37,7 +37,7 @@ namespace LibHac.Fs.Save
{
while (tab.FindNextFile(ref position, out SaveFileInfo info, out string name))
{
yield return new DirectoryEntry(name, FullPath + '/' + name, DirectoryEntryType.File, info.Length);
yield return new DirectoryEntry(name, PathTools.Combine(FullPath, name), DirectoryEntryType.File, info.Length);
}
}
}

View file

@ -9,8 +9,7 @@ namespace LibHac.Fs
private string ResolveFullPath(string path)
{
//todo
return RootPath + path;
return PathTools.Combine(RootPath, path);
}
public SubdirectoryFileSystem(IFileSystem fs, string rootPath)

View file

@ -22,7 +22,7 @@ namespace LibHac.Fs
{
foreach (DirectoryEntry entry in BaseDirectory.Read())
{
yield return new DirectoryEntry(entry.Name, FullPath + '/' + entry.Name, entry.Type, entry.Size);
yield return new DirectoryEntry(entry.Name, PathTools.Combine(FullPath, entry.Name), entry.Type, entry.Size);
}
}