2020-07-15 09:41:59 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
2020-12-08 16:37:08 -08:00
|
|
|
|
namespace SabreTools.Core.Tools
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
|
|
|
|
|
public static class Sanitizer
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get a sanitized size from an input string
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">String to get value from</param>
|
2020-09-04 23:03:27 -07:00
|
|
|
|
/// <returns>Size as a long?, if possible</returns>
|
|
|
|
|
|
public static long? CleanLong(string input)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2020-09-04 23:03:27 -07:00
|
|
|
|
long? size = null;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
if (input != null && input.Contains("0x"))
|
|
|
|
|
|
size = Convert.ToInt64(input, 16);
|
|
|
|
|
|
|
|
|
|
|
|
else if (input != null)
|
2020-09-04 23:03:27 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (Int64.TryParse(input, out long longSize))
|
|
|
|
|
|
size = longSize;
|
|
|
|
|
|
}
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
|
|
|
|
|
return size;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|