Files
duplicati/Duplicati/Library/Utility/Sizeparser.cs
T

113 lines
4.6 KiB
C#
Raw Normal View History

2025-01-07 09:40:39 +01:00
// Copyright (C) 2025, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2024-12-03 13:04:27 +01:00
// DEALINGS IN THE SOFTWARE.
2024-02-28 15:45:30 +01:00
2009-01-14 17:29:30 +00:00
using System;
using System.Collections.Generic;
using System.Text;
namespace Duplicati.Library.Utility
2009-01-14 17:29:30 +00:00
{
public static class Sizeparser
{
public static long ParseSize(string size, string defaultSuffix)
{
2009-02-18 19:08:05 +00:00
if (string.IsNullOrEmpty(size))
return 0;
size = size.Trim();
2009-02-18 19:08:05 +00:00
if (size.EndsWith("tb", StringComparison.OrdinalIgnoreCase) ||
2024-12-03 13:04:27 +01:00
size.EndsWith("tib", StringComparison.OrdinalIgnoreCase) ||
size.EndsWith("gb", StringComparison.OrdinalIgnoreCase) ||
2024-12-03 13:04:27 +01:00
size.EndsWith("gib", StringComparison.OrdinalIgnoreCase) ||
size.EndsWith("mb", StringComparison.OrdinalIgnoreCase) ||
2024-12-03 13:04:27 +01:00
size.EndsWith("mib", StringComparison.OrdinalIgnoreCase) ||
size.EndsWith("kb", StringComparison.OrdinalIgnoreCase) ||
2024-12-03 13:04:27 +01:00
size.EndsWith("kib", StringComparison.OrdinalIgnoreCase) ||
size.EndsWith("b", StringComparison.OrdinalIgnoreCase))
2009-01-14 17:29:30 +00:00
return ParseSize(size);
else
return ParseSize(size + " " + defaultSuffix);
}
public static long ParseSize(string size)
{
2009-02-18 19:08:05 +00:00
if (string.IsNullOrEmpty(size))
2009-01-14 17:29:30 +00:00
return 0;
2009-02-18 19:08:05 +00:00
2009-01-14 17:29:30 +00:00
string origsize = size;
size = size.Trim();
2024-12-03 13:04:27 +01:00
2009-01-14 17:29:30 +00:00
long factor = 1;
2024-12-04 22:43:47 +01:00
if (size.EndsWith("tb", StringComparison.OrdinalIgnoreCase))
2016-08-31 14:21:54 +02:00
{
factor = 1024L * 1024 * 1024 * 1024;
size = size.Substring(0, size.Length - 2).Trim();
}
2024-12-04 22:43:47 +01:00
if (size.EndsWith("tib", StringComparison.OrdinalIgnoreCase))
{
factor = 1024L * 1024 * 1024 * 1024;
size = size.Substring(0, size.Length - 3).Trim();
}
else if (size.EndsWith("gb", StringComparison.OrdinalIgnoreCase))
2009-01-14 17:29:30 +00:00
{
factor = 1024 * 1024 * 1024;
size = size.Substring(0, size.Length - 2).Trim();
}
2024-12-04 22:43:47 +01:00
else if (size.EndsWith("gib", StringComparison.OrdinalIgnoreCase))
{
factor = 1024 * 1024 * 1024;
size = size.Substring(0, size.Length - 3).Trim();
}
else if (size.EndsWith("mb", StringComparison.OrdinalIgnoreCase))
2009-01-14 17:29:30 +00:00
{
factor = 1024 * 1024;
size = size.Substring(0, size.Length - 2).Trim();
}
2024-12-04 22:43:47 +01:00
else if (size.EndsWith("mib", StringComparison.OrdinalIgnoreCase))
{
factor = 1024 * 1024;
size = size.Substring(0, size.Length - 3).Trim();
}
else if (size.EndsWith("kb", StringComparison.OrdinalIgnoreCase))
2009-01-14 17:29:30 +00:00
{
factor = 1024;
size = size.Substring(0, size.Length - 2).Trim();
}
2024-12-04 22:43:47 +01:00
else if (size.EndsWith("kib", StringComparison.OrdinalIgnoreCase))
{
factor = 1024;
size = size.Substring(0, size.Length - 3).Trim();
}
else if (size.EndsWith("b", StringComparison.OrdinalIgnoreCase))
2009-01-14 17:29:30 +00:00
size = size.Substring(0, size.Length - 1).Trim();
long r;
if (!long.TryParse(size, out r))
throw new Exception(Strings.Sizeparser.InvalidSizeValueError(origsize));
2009-01-14 17:29:30 +00:00
else
return factor * r;
}
}
}