コード例 #1
0
        /// <summary>
        /// Input unparsed string (such as "100Kb") and
        /// output number in bytes as string ("102400 bytes")
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter">Can contain an array of 2 UInt64 numbers
        /// which are interpreted as Min Max values for resulting number of bytes</param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value,
                              Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return(Binding.DoNothing);
            }

            string stringInput = value as string;

            if (stringInput != null)
            {
                if (!string.IsNullOrEmpty(value.ToString()))
                {
                    ulong    numberOfBytes;
                    string[] inputComponents;
                    if (FileSizeContext.ConvertUnparsedSizeToBytesInt(value, out numberOfBytes, out inputComponents) == true)
                    {
                        ulong[] iMinMax = parameter as ulong[];

                        if (iMinMax != null)
                        {
                            if (iMinMax.Length == 2)
                            {
                                ulong iMin = iMinMax[0];
                                ulong iMax = iMinMax[1];

                                if (numberOfBytes < iMin || numberOfBytes > iMax)
                                {
                                    string  sMin, sMax;
                                    MemUnit sizeUnit;
                                    double  d = NumberToBestSizeConverter.GetHumanReadableSize(iMin, out sizeUnit);
                                    sMin = string.Format("{0} {1}", Math.Round(d, 2).ToString(), sizeUnit);

                                    d    = NumberToBestSizeConverter.GetHumanReadableSize(iMax, out sizeUnit);
                                    sMax = string.Format("{0} {1}", Math.Round(d, 2).ToString(), sizeUnit);

                                    return(string.Format("(Out of range [{0}, {1}])", sMin, sMax));
                                }
                            }
                        }

                        return(string.Format("{0} bytes", numberOfBytes));
                    }
                    else
                    {
                        return(string.Format("Syntax Error: '{0}'", stringInput));
                    }
                }
            }

            return(Binding.DoNothing);
        }
コード例 #2
0
        /// <summary>
        /// Input unparsed string (such as "100Kb") and
        /// output number in bytes as string ("102400 bytes")
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter">Can contain an array of 2 UInt64 numbers
        /// which are interpreted as Min Max values for resulting number of bytes</param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values == null)
            {
                return(Binding.DoNothing);
            }

            string stringInput = string.Empty;

            if (values.Length == 1)
            {
                if (values[0] != null)
                {
                    stringInput = string.Format("{0}", values[0]);
                }
            }
            else
            {
                if (values.Length == 2)
                {
                    if (values[0] != null && values[1] != null)
                    {
                        stringInput = string.Format("{0} {1}", values[0], values[1]);
                    }
                }
            }

            if (stringInput.Length == 0)
            {
                return(Binding.DoNothing);
                ////throw new NotImplementedException(string.Format("One or Two input values are required for conversion, but there are; {0}", values.Length));
            }

            // Convert an iinput string such as, "100 Mb" into a byte string represantation, such as, "1000 bytes"
            ulong numberOfBytes;

            string[] inputComponents;
            if (FileSizeContext.ConvertUnparsedSizeToBytesInt(stringInput, out numberOfBytes, out inputComponents) == true)
            {
                ulong[] iMinMax = parameter as ulong[];

                if (iMinMax != null)
                {
                    if (iMinMax.Length == 2)
                    {
                        ulong iMin = iMinMax[0];
                        ulong iMax = iMinMax[1];

                        if (numberOfBytes < iMin || numberOfBytes > iMax)
                        {
                            string  sMin, sMax;
                            MemUnit sizeUnit;
                            double  d = NumberToBestSizeConverter.GetHumanReadableSize(iMin, out sizeUnit);
                            sMin = string.Format("{0} {1}", Math.Round(d, 2).ToString(), sizeUnit);

                            d    = NumberToBestSizeConverter.GetHumanReadableSize(iMax, out sizeUnit);
                            sMax = string.Format("{0} {1}", Math.Round(d, 2).ToString(), sizeUnit);

                            return(string.Format("(Out of range [{0}, {1}])", sMin, sMax));
                        }
                    }
                }

                return(string.Format("{0} bytes", numberOfBytes));
            }

            return(string.Format("Syntax Error: '{0}'", stringInput));
        }
コード例 #3
0
        /// <summary>
        /// Convert a bytes size string (eg.: "100 bytes") into a 'meaningful'
        /// size string, such as, (eg.: "0.3 Kbytes") - for display purposes
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values == null)
            {
                return(Binding.DoNothing);
            }

            string stringInput = string.Empty;

            if (values.Length == 1)
            {
                if (values[0] != null)
                {
                    stringInput = string.Format("{0}", values[0]);
                }
            }
            else
            {
                if (values.Length == 2)
                {
                    // put value and unit into one string if they were supplied seperately
                    if (values[0] != null && values[1] != null)
                    {
                        stringInput = string.Format("{0} {1}", values[0], values[1]);
                    }
                }
            }

            if (stringInput.Length == 0)
            {
                return(Binding.DoNothing);
                ////throw new NotImplementedException(string.Format("One or Two input values are required for conversion, but there are; {0}", values.Length));
            }

            ulong numberOfBytes;

            string[] inputComponents;
            if (FileSize.FileSizeContext.ConvertUnparsedSizeToBytesInt(stringInput, out numberOfBytes, out inputComponents) == true)
            {
                ulong[] iMinMax = parameter as ulong[];
                MemUnit sizeUnit;
                string  sOutOfRage   = string.Empty;
                bool    isOutOfRange = false;

                if (iMinMax != null)
                {
                    if (iMinMax.Length == 2)
                    {
                        ulong iMin = iMinMax[0];
                        ulong iMax = iMinMax[1];

                        if (numberOfBytes < iMin || numberOfBytes > iMax)
                        {
                            isOutOfRange = true;
                            string sMin, sMax;
                            double d = NumberToBestSizeConverter.GetHumanReadableSize(iMin, out sizeUnit);
                            sMin = string.Format("{0} {1}", Math.Round(d, 2).ToString(), sizeUnit);

                            d    = NumberToBestSizeConverter.GetHumanReadableSize(iMax, out sizeUnit);
                            sMax = string.Format("{0} {1}", Math.Round(d, 2).ToString(), sizeUnit);

                            sOutOfRage = string.Format(" (Out of range [{0}, {1}])", sMin, sMax);
                        }
                    }
                }

                double doubleVal    = NumberToBestSizeConverter.GetHumanReadableSize(numberOfBytes, out sizeUnit);
                string sOutput      = string.Format("{0} {1}", Math.Round(doubleVal, 2).ToString(), sizeUnit);
                string sFinalOutput = string.Format("{0} {1}", sOutput, sOutOfRage);
                string sInput       = string.Empty;

                // return ouput only if best size unit does actually differ from input size unit
                if (inputComponents != null)
                {
                    if (inputComponents.Length == 2)
                    {
                        sInput = string.Format("{0} {1}", inputComponents[0], inputComponents[1]);

                        if (sInput != sOutput)
                        {
                            return(sFinalOutput);
                        }
                        else
                        {
                            if (isOutOfRange == false)
                            {
                                return(string.Empty);
                            }
                            else
                            {
                                return(sFinalOutput);
                            }
                        }
                    }
                }

                // We should never get here but we can return the output even if input components are not available
                return(sFinalOutput);
            }

            return(string.Format("Syntax Error: '{0}'", stringInput));
        }