コード例 #1
0
ファイル: MemSize.cs プロジェクト: jazmatician/BizArk3
 /// <summary>
 /// Parses the string to determine a memory size. NOTE: Using a floating point number will likely result in a small amount of imprecision. Do not rely on the byte count being exact.
 /// </summary>
 /// <param name="fmt">A string that starts with a number and ends with one of the supported format strings. Ex 123GiB</param>
 /// <param name="outSz">Output parameter.</param>
 /// <returns></returns>
 public static bool TryParse(string fmt, out MemSize outSz)
 {
     return(TryParse(fmt, false, out outSz));
 }
コード例 #2
0
ファイル: MemSize.cs プロジェクト: jazmatician/BizArk3
        /// <summary>
        /// Parses the string to determine a memory size. NOTE: Using a floating point number will likely result in a small amount of imprecision. Do not rely on the byte count being exact.
        /// </summary>
        /// <param name="fmt">A string that starts with a number and ends with one of the supported format strings. Ex 123GiB</param>
        /// <param name="useIEC">If true, always uses IEC format. So sending in 1KB is interpreted as 1KiB.</param>
        /// <param name="outSz">Output parameter.</param>
        /// <returns></returns>
        public static bool TryParse(string fmt, bool useIEC, out MemSize outSz)
        {
            outSz = null;

            if (fmt.IsEmpty())
            {
                return(false);
            }

            var re = new Regex(@"^(?<sz>\d+(\.\d+)?)(\s?(?<fmt>[A-Z]+))?$", RegexOptions.IgnoreCase);
            var m  = re.Match(fmt);

            if (!m.Success)
            {
                return(false);
            }

            if (decimal.TryParse(m.Groups["sz"].Value, out var fmtSz))
            {
                var fmtDesignator = m.Groups["fmt"].Value ?? "";

                switch (fmtDesignator.ToUpperInvariant())
                {
                case "":
                case "B":
                case "BYTE":
                case "BYTES":
                    ;                             // Leave sz alone
                    break;

                case "TIB":
                    fmtSz *= cNumBytesInTebibyte;
                    break;

                case "GIB":
                    fmtSz *= cNumBytesInGibibyte;
                    break;

                case "MIB":
                    fmtSz *= cNumBytesInMebibyte;
                    break;

                case "KIB":
                    fmtSz *= cNumBytesInKibibyte;
                    break;

                case "TB":
                    fmtSz *= useIEC ? cNumBytesInTebibyte : cNumBytesInTerabyte;
                    break;

                case "GB":
                    fmtSz *= useIEC ? cNumBytesInGibibyte : cNumBytesInGigabyte;
                    break;

                case "MB":
                    fmtSz *= useIEC ? cNumBytesInMebibyte : cNumBytesInMegabyte;
                    break;

                case "KB":
                    fmtSz *= useIEC ? cNumBytesInKibibyte : cNumBytesInKilobyte;
                    break;

                default:
                    return(false);
                }

                outSz = new MemSize((int)Math.Round(fmtSz, 0));
                return(true);
            }

            return(false);
        }
コード例 #3
0
ファイル: MemSize.cs プロジェクト: jazmatician/BizArk3
        /// <summary>
        /// Returns a string representation of the size.
        /// </summary>
        /// <param name="totalBytes"></param>
        /// <param name="format">Can be IEC, SI (see http://en.wikipedia.org/wiki/Binary_prefix), KB, MB, GB, TB, KiB, MiB, GiB, or TiB. If null, IEC is assumed. Any other values will show the total number of bytes. If you want an IEC value with an SI prefix (old style), add a * to the format (ex, "IEC*"). This is ignored if using an SI format.</param>
        /// <returns></returns>
        public static string GetString(long totalBytes, string format)
        {
            var sz = new MemSize(totalBytes);

            return(sz.ToString(format));
        }