public override void Interpret(FileSizeContext value, out string[] strComponents) { strComponents = null; // Go through each defined expression and see if we can match it with a known unit // Compute next smaller unit (eg. convert from Gb to Mb) and keep going until we hit bytes foreach (FileSizeExpression exp in this.expressionTree) { string[] strOutComponents = null; // Adjust value to next smaller unit (if any) exp.Interpret(value, out strOutComponents); // Found a match and converted OK -> Return to sender if (exp.InterpretOK == true) { // return recognized and cleaned string components from input if (this.InterpretOK == false) { strComponents = strOutComponents; } this.InterpretOK = true; } } // Attempt 'bytes conversion' as last resort if (this.InterpretOK == false) { this.InterpretAsBytes(value); } }
/// <summary> /// Application Logic to convert unparsed string (such as "100Kb") and /// output number in bytes as UInt64 /// </summary> /// <param name="value"></param> /// <param name="numberOfBytes"></param> /// <returns></returns> public static bool ConvertUnparsedSizeToBytesInt(object value, out ulong numberOfBytes, out string[] sInputComponents) { string stringInput = value as string; sInputComponents = null; numberOfBytes = 0; if (stringInput != null) { if (!string.IsNullOrEmpty(value.ToString())) { var ctx = new FileSizeContext((string)value); // "10Mb" var parser = new FileSizeParser(); parser.Interpret(ctx, out sInputComponents); numberOfBytes = ctx.Output; return(parser.InterpretOK); } } return(false); }
/// <summary> /// Interpret input string and set corresponding <seealso cref="InterpretOK"/> property /// /// See also <seealso cref="InterpretAsBytes"/> method in <seealso cref="FileSizeExpression"/> class. /// </summary> /// <param name="value"></param> public override void Interpret(FileSizeContext value, out string[] strComponents) { strComponents = null; string inputNumber = string.Empty; try { string thisPattern = this.ThisPattern(); if (value.Input.EndsWith(thisPattern)) { this.InterpretOK = false; try { // Mask out string portion that is not considered to be a number inputNumber = value.Input.Replace(this.ThisPattern(), string.Empty); // Attempt tp parse the input number portion of the string as double double amount = double.Parse(inputNumber); // Compute file size in bytes ulong fileSize = (ulong)(amount * 1024); value.Input = string.Format("{0}{1}", fileSize, this.NextPattern()); // Set file size in bytes to output value.Output = fileSize; strComponents = new string[2]; // Pass back portions of recognized string strComponents[0] = inputNumber.Trim(); strComponents[1] = this.ThisPattern(); this.InterpretOK = true; } catch (FormatException) { // These are caught for debugging purposes... this.InterpretOK = false; value.Output = 0; Console.WriteLine("FormatException on '{0}' -> '{1}'", value.Input, inputNumber); } catch (OverflowException) { // These are caught for debugging purposes... this.InterpretOK = false; value.Output = 0; Console.WriteLine("OverflowException on '{0}' -> '{1}'", value.Input, inputNumber); } } } catch (Exception exp) { this.InterpretOK = false; value.Output = 0; Console.WriteLine("Exception on '{0}' -> '{1}'\n{2}", value.Input, inputNumber, exp.ToString()); } }
/// <summary> /// See also <seealso cref="Interpret"/> method in <seealso cref="TerminalFileSizeExpression"/> class. /// </summary> /// <param name="value"></param> public void InterpretAsBytes(FileSizeContext value) { string inputNumber = string.Empty; try { string thisPattern = "bytes"; if (value.Input.EndsWith(thisPattern)) { this.InterpretOK = false; try { // Mask out string portion that is not considered to be a number inputNumber = value.Input.Replace(thisPattern, string.Empty); // Attempt tp parse the input number portion of the string double amount = double.Parse(inputNumber); value.Input = string.Format("{0}{1}", amount, thisPattern); // Set file size in bytes to output value.Output = (ulong)amount; this.InterpretOK = true; } catch (FormatException) { // These are caught for debugging purposes... this.InterpretOK = false; value.Output = 0; Console.WriteLine("FormatException on '{0}' -> '{1}'", value.Input, inputNumber); } catch (OverflowException) { // These are caught for debugging purposes... this.InterpretOK = false; value.Output = 0; Console.WriteLine("OverflowException on '{0}' -> '{1}'", value.Input, inputNumber); } } } catch (Exception exp) { this.InterpretOK = false; value.Output = 0; Console.WriteLine("Exception on '{0}' -> '{1}'\n{2}", value.Input, inputNumber, exp.ToString()); } }
/// <summary> /// Abstract Interpret method /// </summary> /// <param name="value"></param> public abstract void Interpret(FileSizeContext value, out string[] strComponents);