mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Add size string parsing back to long filters
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using SabreTools.Logging;
|
||||
|
||||
@@ -10,6 +9,36 @@ namespace SabreTools.Filtering
|
||||
/// </summary>
|
||||
public abstract class Filter
|
||||
{
|
||||
#region Constants
|
||||
|
||||
#region Byte (1000-based) size comparisons
|
||||
|
||||
private const long KiloByte = 1000;
|
||||
private readonly static long MegaByte = (long)Math.Pow(KiloByte, 2);
|
||||
private readonly static long GigaByte = (long)Math.Pow(KiloByte, 3);
|
||||
private readonly static long TeraByte = (long)Math.Pow(KiloByte, 4);
|
||||
private readonly static long PetaByte = (long)Math.Pow(KiloByte, 5);
|
||||
private readonly static long ExaByte = (long)Math.Pow(KiloByte, 6);
|
||||
private readonly static long ZettaByte = (long)Math.Pow(KiloByte, 7);
|
||||
private readonly static long YottaByte = (long)Math.Pow(KiloByte, 8);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Byte (1024-based) size comparisons
|
||||
|
||||
private const long KibiByte = 1024;
|
||||
private readonly static long MibiByte = (long)Math.Pow(KibiByte, 2);
|
||||
private readonly static long GibiByte = (long)Math.Pow(KibiByte, 3);
|
||||
private readonly static long TibiByte = (long)Math.Pow(KibiByte, 4);
|
||||
private readonly static long PibiByte = (long)Math.Pow(KibiByte, 5);
|
||||
private readonly static long ExiByte = (long)Math.Pow(KibiByte, 6);
|
||||
private readonly static long ZittiByte = (long)Math.Pow(KibiByte, 7);
|
||||
private readonly static long YittiByte = (long)Math.Pow(KibiByte, 8);
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region Logging
|
||||
|
||||
/// <summary>
|
||||
@@ -148,7 +177,8 @@ namespace SabreTools.Filtering
|
||||
operation = null;
|
||||
|
||||
string valueString = value.TrimStart('>', '<', '=');
|
||||
if (!Int64.TryParse(valueString, out long valueLong))
|
||||
long? valueLong = ToSize(valueString);
|
||||
if (valueLong == null)
|
||||
return;
|
||||
|
||||
// Equal
|
||||
@@ -203,6 +233,65 @@ namespace SabreTools.Filtering
|
||||
filterItem.PositiveSet.Add(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the multiplier to be used with the size given
|
||||
/// </summary>
|
||||
/// <param name="sizestring">String with possible size with extension</param>
|
||||
/// <returns>Tuple of multiplier to use on final size and fixed size string</returns>
|
||||
private static long? ToSize(string sizestring)
|
||||
{
|
||||
// If the string is null or empty, we return -1
|
||||
if (string.IsNullOrWhiteSpace(sizestring))
|
||||
return null;
|
||||
|
||||
// Make sure the string is in lower case
|
||||
sizestring = sizestring.ToLowerInvariant();
|
||||
|
||||
// Get any trailing size identifiers
|
||||
long multiplier = 1;
|
||||
if (sizestring.EndsWith("k") || sizestring.EndsWith("kb"))
|
||||
multiplier = KiloByte;
|
||||
else if (sizestring.EndsWith("ki") || sizestring.EndsWith("kib"))
|
||||
multiplier = KibiByte;
|
||||
else if (sizestring.EndsWith("m") || sizestring.EndsWith("mb"))
|
||||
multiplier = MegaByte;
|
||||
else if (sizestring.EndsWith("mi") || sizestring.EndsWith("mib"))
|
||||
multiplier = MibiByte;
|
||||
else if (sizestring.EndsWith("g") || sizestring.EndsWith("gb"))
|
||||
multiplier = GigaByte;
|
||||
else if (sizestring.EndsWith("gi") || sizestring.EndsWith("gib"))
|
||||
multiplier = GibiByte;
|
||||
else if (sizestring.EndsWith("t") || sizestring.EndsWith("tb"))
|
||||
multiplier = TeraByte;
|
||||
else if (sizestring.EndsWith("ti") || sizestring.EndsWith("tib"))
|
||||
multiplier = TibiByte;
|
||||
else if (sizestring.EndsWith("p") || sizestring.EndsWith("pb"))
|
||||
multiplier = PetaByte;
|
||||
else if (sizestring.EndsWith("pi") || sizestring.EndsWith("pib"))
|
||||
multiplier = PibiByte;
|
||||
else if (sizestring.EndsWith("e") || sizestring.EndsWith("eb"))
|
||||
multiplier = ExaByte;
|
||||
else if (sizestring.EndsWith("ei") || sizestring.EndsWith("eib"))
|
||||
multiplier = ExiByte;
|
||||
else if (sizestring.EndsWith("z") || sizestring.EndsWith("zb"))
|
||||
multiplier = ZettaByte;
|
||||
else if (sizestring.EndsWith("zi") || sizestring.EndsWith("zib"))
|
||||
multiplier = ZittiByte;
|
||||
else if (sizestring.EndsWith("y") || sizestring.EndsWith("yb"))
|
||||
multiplier = YottaByte;
|
||||
else if (sizestring.EndsWith("yi") || sizestring.EndsWith("yib"))
|
||||
multiplier = YittiByte;
|
||||
|
||||
// Remove any trailing identifiers
|
||||
sizestring = sizestring.TrimEnd(new char[] { 'k', 'm', 'g', 't', 'p', 'e', 'z', 'y', 'i', 'b', ' ' });
|
||||
|
||||
// Now try to get the size from the string
|
||||
if (!Int64.TryParse(sizestring, out long size))
|
||||
return null;
|
||||
else
|
||||
return size * multiplier;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filter Running
|
||||
|
||||
Reference in New Issue
Block a user