diff --git a/SabreTools.Library/DatFiles/DatFile.cs b/SabreTools.Library/DatFiles/DatFile.cs index c39a93af..c6090ec3 100644 --- a/SabreTools.Library/DatFiles/DatFile.cs +++ b/SabreTools.Library/DatFiles/DatFile.cs @@ -828,7 +828,7 @@ namespace SabreTools.Library.DatFiles } // Determine the output path for the DAT - string interOutDir = PathExtensions.GetOutputPath(outDir, path, inplace); + string interOutDir = path.GetOutputPath(outDir, inplace); // Once we're done, try writing out intDat.Write(interOutDir, overwrite: inplace); @@ -878,7 +878,7 @@ namespace SabreTools.Library.DatFiles }); // Determine the output path for the DAT - string interOutDir = PathExtensions.GetOutputPath(outDir, path, inplace); + string interOutDir = path.GetOutputPath(outDir, inplace); // Once we're done, try writing out intDat.Write(interOutDir, overwrite: inplace); @@ -907,7 +907,7 @@ namespace SabreTools.Library.DatFiles DatFile[] outDatsArray = new DatFile[inputs.Count]; Parallel.For(0, inputs.Count, Globals.ParallelOptions, j => { - string innerpost = $" ({j} - {PathExtensions.GetNormalizedFileName(inputs[j], true)} Only)"; + string innerpost = $" ({j} - {inputs[j].GetNormalizedFileName(true)} Only)"; DatFile diffData; // If we're in inplace mode or the output directory is set, take the appropriate DatData object already stored @@ -964,7 +964,7 @@ namespace SabreTools.Library.DatFiles Parallel.For((skip ? 1 : 0), inputs.Count, Globals.ParallelOptions, j => { - string path = PathExtensions.GetOutputPath(outDir, inputs[j], inplace); + string path = inputs[j].GetOutputPath(outDir, inplace); // Try to output the file outDats[j].Write(path, overwrite: inplace); @@ -1030,7 +1030,7 @@ namespace SabreTools.Library.DatFiles Parallel.For(0, inputs.Count, Globals.ParallelOptions, j => { - string innerpost = $" ({j} - {PathExtensions.GetNormalizedFileName(inputs[j], true)} Only)"; + string innerpost = $" ({j} - {inputs[j].GetNormalizedFileName(true)} Only)"; DatFile diffData = Create(DatHeader); diffData.DatHeader.FileName += innerpost; diffData.DatHeader.Name += innerpost; @@ -1110,7 +1110,7 @@ namespace SabreTools.Library.DatFiles { Parallel.For(0, inputs.Count, Globals.ParallelOptions, j => { - string path = PathExtensions.GetOutputPath(outDir, inputs[j], false /* inplace */); + string path = inputs[j].GetOutputPath(outDir, false /* inplace */); // Try to output the file outDats[j].Write(path, overwrite: false); @@ -1179,7 +1179,7 @@ namespace SabreTools.Library.DatFiles filter.FilterDatFile(innerDatdata, false /* useTags */); // Get the correct output path - string realOutDir = PathExtensions.GetOutputPath(outDir, file, inplace); + string realOutDir = file.GetOutputPath(outDir, inplace); // Try to output the file, overwriting only if it's not in the current directory innerDatdata.Write(realOutDir, overwrite: inplace); @@ -2559,7 +2559,7 @@ namespace SabreTools.Library.DatFiles Parse(file); // Get the output directory - outDir = PathExtensions.GetOutputPath(outDir, file, inplace); + outDir = file.GetOutputPath(outDir, inplace); // Split and write the DAT if (splittingMode.HasFlag(SplittingMode.Extension)) diff --git a/SabreTools.Library/Tools/ParentablePath.cs b/SabreTools.Library/Tools/ParentablePath.cs index 0a2857be..0ab64690 100644 --- a/SabreTools.Library/Tools/ParentablePath.cs +++ b/SabreTools.Library/Tools/ParentablePath.cs @@ -1,17 +1,127 @@ -namespace SabreTools.Library.Tools +using System; +using System.IO; + +namespace SabreTools.Library.Tools { /// /// A path that optionally contains a parent root /// public class ParentablePath { - public string CurrentPath { get; set; } - public string ParentPath { get; set; } + /// + /// Current full path represented + /// + public string CurrentPath { get; private set; } + + /// + /// Possible parent path represented (may be null or empty) + /// + public string ParentPath { get; private set; } public ParentablePath(string currentPath, string parentPath = null) { CurrentPath = currentPath; ParentPath = parentPath; } + + /// + /// Get the proper filename (with subpath) from the file and parent combination + /// + /// True if path separators should be converted to '-', false otherwise + /// Subpath for the file + public string GetNormalizedFileName(bool sanitize) + { + // Check that we have a combined path first + if (string.IsNullOrWhiteSpace(ParentPath)) + { + string filename = Path.GetFileName(CurrentPath); + if (sanitize) + filename = filename.Replace(Path.DirectorySeparatorChar, '-').Replace(Path.AltDirectorySeparatorChar, '-'); + + 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; + } + } + + /// + /// Get the proper output path for a given input file and output directory + /// + /// Output directory to use + /// True if the output file should go to the same input folder, false otherwise + /// Complete output path + public string GetOutputPath(string outDir, bool inplace) + { + // First, we need to ensure the output directory + outDir = DirectoryExtensions.Ensure(outDir); + + // Check if we have a split path or not + 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 (inplace) + { + outDir = Path.GetDirectoryName(CurrentPath); + } + + // 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 + else if (CurrentPath.Length != ParentPath.Length && outDir == Environment.CurrentDirectory) + { + outDir = Path.GetDirectoryName(Path.Combine(outDir, CurrentPath.Remove(0, Path.GetDirectoryName(ParentPath).Length + 1))); + } + + // If we are processing a path that is coming from a directory, we want to get the subfolder to write to + else if (CurrentPath.Length != ParentPath.Length) + { + outDir = Path.GetDirectoryName(Path.Combine(outDir, CurrentPath.Remove(0, ParentPath.Length + 1))); + } + + // 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; + } } } diff --git a/SabreTools.Library/Tools/PathExtensions.cs b/SabreTools.Library/Tools/PathExtensions.cs index f9940bee..d8c98e10 100644 --- a/SabreTools.Library/Tools/PathExtensions.cs +++ b/SabreTools.Library/Tools/PathExtensions.cs @@ -1,5 +1,4 @@ -using System; -using System.IO; +using System.IO; using SabreTools.Library.Data; @@ -34,108 +33,6 @@ namespace SabreTools.Library.Tools return ext; } - /// - /// Get the proper filename (with subpath) from the file and parent combination - /// - /// Input combined path to use - /// True if path separators should be converted to '-', false otherwise - /// Subpath for the file - public static string GetNormalizedFileName(ParentablePath path, bool sanitize) - { - // Check that we have a combined path first - if (string.IsNullOrWhiteSpace(path.ParentPath)) - { - string filename = Path.GetFileName(path.CurrentPath); - if (sanitize) - filename = filename.Replace(Path.DirectorySeparatorChar, '-').Replace(Path.AltDirectorySeparatorChar, '-'); - - return filename; - } - - // If the parts are the same, return the filename from the first part - if (string.Equals(path.CurrentPath, path.ParentPath, StringComparison.Ordinal)) - { - string filename = Path.GetFileName(path.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 = path.CurrentPath.Remove(0, path.ParentPath.Length + 1); - if (sanitize) - filename = filename.Replace(Path.DirectorySeparatorChar, '-').Replace(Path.AltDirectorySeparatorChar, '-'); - - return filename; - } - } - - /// - /// Get the proper output path for a given input file and output directory - /// - /// Output directory to use - /// Input path to create output for - /// True if the output file should go to the same input folder, false otherwise - /// Complete output path - public static string GetOutputPath(string outDir, ParentablePath inputPath, bool inplace) - { - // First, we need to ensure the output directory - outDir = DirectoryExtensions.Ensure(outDir); - - // Check if we have a split path or not - bool splitpath = !string.IsNullOrWhiteSpace(inputPath.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 (inplace) - { - outDir = Path.GetDirectoryName(inputPath.CurrentPath); - } - - // 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 - else if (inputPath.CurrentPath.Length != inputPath.ParentPath.Length && outDir == Environment.CurrentDirectory) - { - outDir = Path.GetDirectoryName(Path.Combine(outDir, inputPath.CurrentPath.Remove(0, Path.GetDirectoryName(inputPath.ParentPath).Length + 1))); - } - - // If we are processing a path that is coming from a directory, we want to get the subfolder to write to - else if (inputPath.CurrentPath.Length != inputPath.ParentPath.Length) - { - outDir = Path.GetDirectoryName(Path.Combine(outDir, inputPath.CurrentPath.Remove(0, inputPath.ParentPath.Length + 1))); - } - - // 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(inputPath.CurrentPath); - } - - // Otherwise, just use the supplied output directory - else - { - // No-op - } - } - - // Finally, return the output directory - return outDir; - } - /// /// Get a proper romba sub path ///