Move another text helper method

This commit is contained in:
Matt Nadareski
2023-08-12 01:17:12 -04:00
parent ce6a64d4cd
commit 1752b1a0ac
7 changed files with 28 additions and 26 deletions

View File

@@ -1,3 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace SabreTools.Core.Tools namespace SabreTools.Core.Tools
@@ -22,6 +25,18 @@ namespace SabreTools.Core.Tools
return input; return input;
} }
// <summary>
/// Remove all chars that are considered path unsafe
/// </summary>
public static string? RemovePathUnsafeCharacters(string? input)
{
if (string.IsNullOrWhiteSpace(input))
return input;
List<char> invalidPath = Path.GetInvalidPathChars().ToList();
return new string(input.Where(c => !invalidPath.Contains(c)).ToArray());
}
#region Helpers #region Helpers
/// <summary> /// <summary>

View File

@@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
namespace SabreTools.Core.Tools namespace SabreTools.Core.Tools
{ {
@@ -175,18 +173,7 @@ namespace SabreTools.Core.Tools
return array == null || array.Length == 0; return array == null || array.Length == 0;
} }
/// <summary> //// <summary>
/// Remove all chars that are considered path unsafe
/// </summary>
/// <param name="s">Input string to clean</param>
/// <returns>Cleaned string</returns>
public static string RemovePathUnsafeCharacters(string s)
{
List<char> invalidPath = Path.GetInvalidPathChars().ToList();
return new string(s.Where(c => !invalidPath.Contains(c)).ToArray());
}
/// <summary>
/// Returns if the first byte array starts with the second array /// Returns if the first byte array starts with the second array
/// </summary> /// </summary>
/// <param name="arr1">First byte array to compare</param> /// <param name="arr1">First byte array to compare</param>

View File

@@ -767,14 +767,14 @@ namespace SabreTools.DatItems
// If item types match, more refinement is needed // If item types match, more refinement is needed
if (x.ItemType == y.ItemType) if (x.ItemType == y.ItemType)
{ {
string xDirectoryName = Path.GetDirectoryName(Utilities.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)); string xDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty));
string yDirectoryName = Path.GetDirectoryName(Utilities.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty)); string yDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty));
// If item directory names match, more refinement is needed // If item directory names match, more refinement is needed
if (xDirectoryName == yDirectoryName) if (xDirectoryName == yDirectoryName)
{ {
string xName = Path.GetFileName(Utilities.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)); string xName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty));
string yName = Path.GetFileName(Utilities.RemovePathUnsafeCharacters(y.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 item names match, then compare on machine or source, depending on the flag
if (xName == yName) if (xName == yName)

View File

@@ -414,7 +414,7 @@ namespace SabreTools.FileTypes.Archives
inputStream.Seek(0, SeekOrigin.Begin); inputStream.Seek(0, SeekOrigin.Begin);
// Get the output archive name from the first rebuild rom // 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 // Set internal variables
Stream writeStream = null; Stream writeStream = null;
@@ -611,7 +611,7 @@ namespace SabreTools.FileTypes.Archives
} }
// Get the output archive name from the first rebuild rom // 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 // Set internal variables
Stream writeStream = null; Stream writeStream = null;

View File

@@ -274,7 +274,7 @@ namespace SabreTools.FileTypes.Archives
return success; return success;
// Get the output archive name from the first rebuild rom // 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 // Set internal variables
TarArchive oldTarFile = TarArchive.Create(); TarArchive oldTarFile = TarArchive.Create();
@@ -418,7 +418,7 @@ namespace SabreTools.FileTypes.Archives
} }
// Get the output archive name from the first rebuild rom // 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 // Set internal variables
TarArchive oldTarFile = TarArchive.Create(); TarArchive oldTarFile = TarArchive.Create();

View File

@@ -427,7 +427,7 @@ namespace SabreTools.FileTypes.Archives
inputStream.Seek(0, SeekOrigin.Begin); inputStream.Seek(0, SeekOrigin.Begin);
// Get the output archive name from the first rebuild rom // 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 // Set internal variables
Stream writeStream = null; Stream writeStream = null;
@@ -624,7 +624,7 @@ namespace SabreTools.FileTypes.Archives
} }
// Get the output archive name from the first rebuild rom // 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 // Set internal variables
Stream writeStream = null; Stream writeStream = null;

View File

@@ -318,9 +318,9 @@ namespace SabreTools.FileTypes
// Get the output folder name from the first rebuild rom // Get the output folder name from the first rebuild rom
string fileName; string fileName;
if (writeToParent) if (writeToParent)
fileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(baseFile.Filename)); fileName = Path.Combine(outDir, TextHelper.RemovePathUnsafeCharacters(baseFile.Filename));
else 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 try
{ {