diff --git a/SabreTools.Core/Tools/TextHelper.cs b/SabreTools.Core/Tools/TextHelper.cs index 986cc5d1..d085122d 100644 --- a/SabreTools.Core/Tools/TextHelper.cs +++ b/SabreTools.Core/Tools/TextHelper.cs @@ -1,3 +1,6 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; using System.Text.RegularExpressions; namespace SabreTools.Core.Tools @@ -22,6 +25,18 @@ namespace SabreTools.Core.Tools return input; } + // + /// Remove all chars that are considered path unsafe + /// + public static string? RemovePathUnsafeCharacters(string? input) + { + if (string.IsNullOrWhiteSpace(input)) + return input; + + List invalidPath = Path.GetInvalidPathChars().ToList(); + return new string(input.Where(c => !invalidPath.Contains(c)).ToArray()); + } + #region Helpers /// diff --git a/SabreTools.Core/Tools/Utilities.cs b/SabreTools.Core/Tools/Utilities.cs index 0940b17f..f4a1acf3 100644 --- a/SabreTools.Core/Tools/Utilities.cs +++ b/SabreTools.Core/Tools/Utilities.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; namespace SabreTools.Core.Tools { @@ -175,18 +173,7 @@ namespace SabreTools.Core.Tools return array == null || array.Length == 0; } - /// - /// Remove all chars that are considered path unsafe - /// - /// Input string to clean - /// Cleaned string - public static string RemovePathUnsafeCharacters(string s) - { - List invalidPath = Path.GetInvalidPathChars().ToList(); - return new string(s.Where(c => !invalidPath.Contains(c)).ToArray()); - } - - /// + //// /// Returns if the first byte array starts with the second array /// /// First byte array to compare diff --git a/SabreTools.DatItems/DatItem.cs b/SabreTools.DatItems/DatItem.cs index 6a28cfe9..199d7fbb 100644 --- a/SabreTools.DatItems/DatItem.cs +++ b/SabreTools.DatItems/DatItem.cs @@ -767,14 +767,14 @@ namespace SabreTools.DatItems // If item types match, more refinement is needed if (x.ItemType == y.ItemType) { - string xDirectoryName = Path.GetDirectoryName(Utilities.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)); - string yDirectoryName = Path.GetDirectoryName(Utilities.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty)); + string xDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)); + string yDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty)); // If item directory names match, more refinement is needed if (xDirectoryName == yDirectoryName) { - string xName = Path.GetFileName(Utilities.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)); - string yName = Path.GetFileName(Utilities.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty)); + string xName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)); + string yName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty)); // If item names match, then compare on machine or source, depending on the flag if (xName == yName) diff --git a/SabreTools.FileTypes/Archives/SevenZipArchive.cs b/SabreTools.FileTypes/Archives/SevenZipArchive.cs index a36e27d9..867f2b02 100644 --- a/SabreTools.FileTypes/Archives/SevenZipArchive.cs +++ b/SabreTools.FileTypes/Archives/SevenZipArchive.cs @@ -414,7 +414,7 @@ namespace SabreTools.FileTypes.Archives inputStream.Seek(0, SeekOrigin.Begin); // Get the output archive name from the first rebuild rom - string archiveFileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(baseFile.Parent) + (baseFile.Parent.EndsWith(".7z") ? string.Empty : ".7z")); + string archiveFileName = Path.Combine(outDir, TextHelper.RemovePathUnsafeCharacters(baseFile.Parent) + (baseFile.Parent.EndsWith(".7z") ? string.Empty : ".7z")); // Set internal variables Stream writeStream = null; @@ -611,7 +611,7 @@ namespace SabreTools.FileTypes.Archives } // Get the output archive name from the first rebuild rom - string archiveFileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(baseFiles[0].Parent) + (baseFiles[0].Parent.EndsWith(".7z") ? string.Empty : ".7z")); + string archiveFileName = Path.Combine(outDir, TextHelper.RemovePathUnsafeCharacters(baseFiles[0].Parent) + (baseFiles[0].Parent.EndsWith(".7z") ? string.Empty : ".7z")); // Set internal variables Stream writeStream = null; diff --git a/SabreTools.FileTypes/Archives/TapeArchive.cs b/SabreTools.FileTypes/Archives/TapeArchive.cs index fcc63a61..da848b74 100644 --- a/SabreTools.FileTypes/Archives/TapeArchive.cs +++ b/SabreTools.FileTypes/Archives/TapeArchive.cs @@ -274,7 +274,7 @@ namespace SabreTools.FileTypes.Archives return success; // Get the output archive name from the first rebuild rom - string archiveFileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(baseFile.Parent) + (baseFile.Parent.EndsWith(".tar") ? string.Empty : ".tar")); + string archiveFileName = Path.Combine(outDir, TextHelper.RemovePathUnsafeCharacters(baseFile.Parent) + (baseFile.Parent.EndsWith(".tar") ? string.Empty : ".tar")); // Set internal variables TarArchive oldTarFile = TarArchive.Create(); @@ -418,7 +418,7 @@ namespace SabreTools.FileTypes.Archives } // Get the output archive name from the first rebuild rom - string archiveFileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(baseFiles[0].Parent) + (baseFiles[0].Parent.EndsWith(".tar") ? string.Empty : ".tar")); + string archiveFileName = Path.Combine(outDir, TextHelper.RemovePathUnsafeCharacters(baseFiles[0].Parent) + (baseFiles[0].Parent.EndsWith(".tar") ? string.Empty : ".tar")); // Set internal variables TarArchive oldTarFile = TarArchive.Create(); diff --git a/SabreTools.FileTypes/Archives/ZipArchive.cs b/SabreTools.FileTypes/Archives/ZipArchive.cs index 568fa62e..7a6fd01c 100644 --- a/SabreTools.FileTypes/Archives/ZipArchive.cs +++ b/SabreTools.FileTypes/Archives/ZipArchive.cs @@ -427,7 +427,7 @@ namespace SabreTools.FileTypes.Archives inputStream.Seek(0, SeekOrigin.Begin); // Get the output archive name from the first rebuild rom - string archiveFileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(baseFile.Parent) + (baseFile.Parent.EndsWith(".zip") ? string.Empty : ".zip")); + string archiveFileName = Path.Combine(outDir, TextHelper.RemovePathUnsafeCharacters(baseFile.Parent) + (baseFile.Parent.EndsWith(".zip") ? string.Empty : ".zip")); // Set internal variables Stream writeStream = null; @@ -624,7 +624,7 @@ namespace SabreTools.FileTypes.Archives } // Get the output archive name from the first rebuild rom - string archiveFileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(baseFiles[0].Parent) + (baseFiles[0].Parent.EndsWith(".zip") ? string.Empty : ".zip")); + string archiveFileName = Path.Combine(outDir, TextHelper.RemovePathUnsafeCharacters(baseFiles[0].Parent) + (baseFiles[0].Parent.EndsWith(".zip") ? string.Empty : ".zip")); // Set internal variables Stream writeStream = null; diff --git a/SabreTools.FileTypes/Folder.cs b/SabreTools.FileTypes/Folder.cs index ee661814..4e2d7b27 100644 --- a/SabreTools.FileTypes/Folder.cs +++ b/SabreTools.FileTypes/Folder.cs @@ -318,9 +318,9 @@ namespace SabreTools.FileTypes // Get the output folder name from the first rebuild rom string fileName; if (writeToParent) - fileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(baseFile.Filename)); + fileName = Path.Combine(outDir, TextHelper.RemovePathUnsafeCharacters(baseFile.Filename)); else - fileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(baseFile.Parent), Utilities.RemovePathUnsafeCharacters(baseFile.Filename)); + fileName = Path.Combine(outDir, TextHelper.RemovePathUnsafeCharacters(baseFile.Parent), TextHelper.RemovePathUnsafeCharacters(baseFile.Filename)); try {