Reduce duplicated code in ParentablePath

This commit is contained in:
Matt Nadareski
2021-01-29 10:40:54 -08:00
parent f422c2ac84
commit 6c66b58af2

View File

@@ -35,37 +35,20 @@ namespace SabreTools.IO
if (string.IsNullOrWhiteSpace(CurrentPath)) if (string.IsNullOrWhiteSpace(CurrentPath))
return null; return null;
// Check that we have a combined path first // Assume the current path is the filename
if (string.IsNullOrWhiteSpace(ParentPath))
{
string filename = Path.GetFileName(CurrentPath); string filename = Path.GetFileName(CurrentPath);
// If we have a true ParentPath, remove it from CurrentPath and return the remainder
if (!string.IsNullOrWhiteSpace(ParentPath) && !string.Equals(CurrentPath, ParentPath, StringComparison.Ordinal))
filename = CurrentPath.Remove(0, ParentPath.Length + 1);
// If we're sanitizing the path after, do so
if (sanitize) if (sanitize)
filename = filename.Replace(Path.DirectorySeparatorChar, '-').Replace(Path.AltDirectorySeparatorChar, '-'); filename = filename.Replace(Path.DirectorySeparatorChar, '-').Replace(Path.AltDirectorySeparatorChar, '-');
return filename; return filename;
} }
// If the parts are the same, return the filename from the first part
if (string.Equals(CurrentPath, ParentPath, StringComparison.Ordinal))
{
string filename = Path.GetFileName(CurrentPath);
if (sanitize)
filename = filename.Replace(Path.DirectorySeparatorChar, '-').Replace(Path.AltDirectorySeparatorChar, '-');
return filename;
}
// Otherwise, remove the path.ParentPath from the path.CurrentPath and return the remainder
else
{
string filename = CurrentPath.Remove(0, ParentPath.Length + 1);
if (sanitize)
filename = filename.Replace(Path.DirectorySeparatorChar, '-').Replace(Path.AltDirectorySeparatorChar, '-');
return filename;
}
}
/// <summary> /// <summary>
/// Get the proper output path for a given input file and output directory /// Get the proper output path for a given input file and output directory
/// </summary> /// </summary>
@@ -85,59 +68,27 @@ namespace SabreTools.IO
// Check if we have a split path or not // Check if we have a split path or not
bool splitpath = !string.IsNullOrWhiteSpace(ParentPath); bool splitpath = !string.IsNullOrWhiteSpace(ParentPath);
// If we have a split path, we need to treat the input separately
if (splitpath)
{
// If we have an inplace output, use the directory name from the input path // If we have an inplace output, use the directory name from the input path
if (inplace) if (inplace)
{ return Path.GetDirectoryName(CurrentPath);
outDir = Path.GetDirectoryName(CurrentPath);
} // If the current and parent paths are the same, just use the output directory
if (!splitpath || CurrentPath.Length == ParentPath.Length)
return outDir;
// By default, the working parent directory is the parent path
string workingParent = ParentPath;
// TODO: Should this be the default? Always create a subfolder if a folder is found? // TODO: Should this be the default? Always create a subfolder if a folder is found?
// If we are processing a path that is coming from a directory and we are outputting to the current directory, we want to get the subfolder to write to // If we are processing a path that is coming from a directory and we are outputting to the current directory, we want to get the subfolder to write to
else if (CurrentPath.Length != ParentPath.Length && outDir == Environment.CurrentDirectory) if (outDir == Environment.CurrentDirectory)
{ workingParent = Path.GetDirectoryName(ParentPath);
string nextDir = Path.GetDirectoryName(ParentPath);
int extraLength = nextDir.EndsWith(':')
|| nextDir.EndsWith(Path.DirectorySeparatorChar)
|| nextDir.EndsWith(Path.AltDirectorySeparatorChar) ? 0 : 1;
outDir = Path.GetDirectoryName(Path.Combine(outDir, CurrentPath.Remove(0, nextDir.Length + extraLength)));
}
// If we are processing a path that is coming from a directory, we want to get the subfolder to write to // Determine the correct subfolder based on the working parent directory
else if (CurrentPath.Length != ParentPath.Length) int extraLength = workingParent.EndsWith(':')
{ || workingParent.EndsWith(Path.DirectorySeparatorChar)
int extraLength = ParentPath.EndsWith(':') || workingParent.EndsWith(Path.AltDirectorySeparatorChar) ? 0 : 1;
|| ParentPath.EndsWith(Path.DirectorySeparatorChar) return Path.GetDirectoryName(Path.Combine(outDir, CurrentPath.Remove(0, workingParent.Length + extraLength)));
|| ParentPath.EndsWith(Path.AltDirectorySeparatorChar) ? 0 : 1;
outDir = Path.GetDirectoryName(Path.Combine(outDir, CurrentPath.Remove(0, ParentPath.Length + extraLength)));
}
// If we are processing a single file from the root of a directory, we just use the output directory
else
{
// No-op
}
}
// Otherwise, assume the input path is just a filename
else
{
// If we have an inplace output, use the directory name from the input path
if (inplace)
{
outDir = Path.GetDirectoryName(CurrentPath);
}
// Otherwise, just use the supplied output directory
else
{
// No-op
}
}
// Finally, return the output directory
return outDir;
} }
} }
} }