protected void Execute(bool isSubFilter) { string tempStr; int arg1CharPos = (int)CmdLine.GetArg(0).Value; int arg2CharPos = (int)CmdLine.GetArg(1).Value; int resultPos = CmdLine.GetIntSwitch("/I", 0); int numericWidth = CmdLine.GetIntSwitch("/W", 6); int noOfDecimals = CmdLine.GetIntSwitch("/D", 2); bool sciNotation = CmdLine.GetBooleanSwitch("/S"); CheckIntRange(arg1CharPos, 1, int.MaxValue, "Char. position", CmdLine.GetArg(0).CharPos); CheckIntRange(arg2CharPos, 1, int.MaxValue, "Char. position", CmdLine.GetArg(1).CharPos); if (resultPos != 0) { CheckIntRange(resultPos, 1, int.MaxValue, "Char. position", CmdLine.GetSwitchPos("/I")); } CheckIntRange(numericWidth, 0, int.MaxValue, "Numeric width", CmdLine.GetSwitchPos("/W")); CheckIntRange(noOfDecimals, 0, int.MaxValue, "No. of decimals", CmdLine.GetSwitchPos("/D")); Open(); try { while (!EndOfText) { string line = ReadLine(); int num = arg1CharPos - 1; tempStr = ScanDecimal(line, ref num); try { double value1 = double.Parse(tempStr); num = arg2CharPos - 1; tempStr = ScanDecimal(line, ref num); try { double value2 = double.Parse(tempStr); double resultValue; if (isSubFilter) { resultValue = value1 - value2; } else { resultValue = value1 / value2; } // Build the result string: if (!sciNotation) { tempStr = resultValue.ToString("0." + new string('0', noOfDecimals)).PadLeft(numericWidth); } else { tempStr = resultValue.ToString("#." + new string('#', noOfDecimals) + "e+00").PadLeft(numericWidth) + ' '; } if (resultPos > 0) { // Inserting the result back into source. tempStr += ' '; // Pad the source line to the result column: while (line.Length < resultPos - 1) { line += ' '; } // Insert the result string: line = line.Insert(resultPos - 1, tempStr); WriteText(line); } else { // Returning only the result string. WriteText(tempStr); } } catch (FormatException) { // Numeric value is invalid. ThrowException("Numeric value found on text line " + TextLineNo.ToString() + ", character position " + CmdLine.GetArg(1).Value.ToString() + " is invalid.", CmdLine.GetArg(1).CharPos); } } catch (FormatException) { // Numeric value is invalid. ThrowException("Numeric value found on text line " + TextLineNo.ToString() + ", character position " + CmdLine.GetArg(0).Value.ToString() + " is invalid.", CmdLine.GetArg(0).CharPos); } } } finally { Close(); } }
protected void Execute(bool isAddFilter) { double resultValue; string tempStr; int resultPos = CmdLine.GetIntSwitch("/I", 0); int numericWidth = CmdLine.GetIntSwitch("/W", 6); int noOfDecimals = CmdLine.GetIntSwitch("/D", 2); bool sciNotation = CmdLine.GetBooleanSwitch("/S"); if (resultPos != 0) { CheckIntRange(resultPos, 1, int.MaxValue, "Char. position", CmdLine.GetSwitchPos("/I")); } CheckIntRange(numericWidth, 0, int.MaxValue, "Numeric width", CmdLine.GetSwitchPos("/W")); CheckIntRange(noOfDecimals, 0, int.MaxValue, "No. of decimals", CmdLine.GetSwitchPos("/D")); Open(); try { while (!EndOfText) { string line = ReadLine(); // Initialize the result: if (isAddFilter) { resultValue = 0.0; } else { resultValue = 1.0; } if (CmdLine.ArgCount > 0) { // Adding (or multiplying) the numbers located as specific character positions. for (int i = 0; i < CmdLine.ArgCount; i++) { int charPos = (int)CmdLine.GetArg(i).Value; CheckIntRange(charPos, 1, int.MaxValue, "Char. position", CmdLine.GetArg(i).CharPos); int num = charPos - 1; tempStr = ScanDecimal(line, ref num); try { double theValue = double.Parse(tempStr); if (isAddFilter) { resultValue += theValue; } else { resultValue *= theValue; } } catch (FormatException) { // Numeric value is invalid. ThrowException("Numeric value found on text line " + TextLineNo.ToString() + ", character position " + CmdLine.GetArg(i).Value.ToString() + " is invalid.", CmdLine.GetArg(i).CharPos); } } // Build the result string: if (!sciNotation) { tempStr = resultValue.ToString("0." + new string('0', noOfDecimals)).PadLeft(numericWidth); } else { tempStr = resultValue.ToString("#." + new string('#', noOfDecimals) + "e+00").PadLeft(numericWidth) + ' '; } } else { // Adding (or multiplying) all numbers found on each line separated by whitespace. int countOfScannedValues = 0; int charIndex = 0; while (charIndex < line.Length) { countOfScannedValues++; tempStr = ScanDecimal(line, ref charIndex); try { double theValue = double.Parse(tempStr); if (isAddFilter) { resultValue += theValue; } else { resultValue *= theValue; } } catch (FormatException) { // Numeric value is invalid. ThrowException("Numeric value " + countOfScannedValues.ToString() + "found on text line " + TextLineNo.ToString() + " is invalid."); } } if (countOfScannedValues > 0) { // Build the result string: if (!sciNotation) { tempStr = resultValue.ToString("0." + new string('0', noOfDecimals)).PadLeft(numericWidth); } else { tempStr = resultValue.ToString("#.########e+00"); } } else { // Build the result string: tempStr = "n/a"; } } if (resultPos > 0) { // Inserting the result back into source. tempStr += ' '; // Pad the source line to the result column: while (line.Length < resultPos - 1) { line += ' '; } // Insert the result string: line = line.Insert(resultPos - 1, tempStr); WriteText(line); } else { // Returning only the result string. WriteText(tempStr); } } } finally { Close(); } }
public override void Execute() { string tempStr; double theValue; string line; List <double> totals = new List <double>(); int numericWidth = CmdLine.GetIntSwitch("/W", 6); int noOfDecimals = CmdLine.GetIntSwitch("/D", 2); bool appendToEnd = CmdLine.GetBooleanSwitch("/A"); bool sciNotation = CmdLine.GetBooleanSwitch("/S"); CheckIntRange(numericWidth, 0, int.MaxValue, "Numeric width", CmdLine.GetSwitchPos("/W")); CheckIntRange(noOfDecimals, 0, int.MaxValue, "No. of decimals", CmdLine.GetSwitchPos("/D")); for (int i = 1; i <= CmdLine.ArgCount; i++) { // Initialize the totals: totals.Add(0.0); } Open(); try { while (!EndOfText) { line = ReadLine(); if (appendToEnd) { WriteText(line); } // Total this line: for (int i = 0; i < CmdLine.ArgCount; i++) { int charPos = (int)CmdLine.GetArg(i).Value; CheckIntRange(charPos, 1, int.MaxValue, "Char. position", CmdLine.GetArg(i).CharPos); int n = charPos - 1; tempStr = ScanDecimal(line, ref n); try { theValue = double.Parse(tempStr); totals[i] += theValue; } catch { // Numeric value is invalid. ThrowException("Numeric value on text line " + TextLineNo.ToString() + " is invalid."); } } } // Output the totals: line = string.Empty; for (int i = 0; i < totals.Count; i++) { theValue = totals[i]; int charPos = (int)CmdLine.GetArg(i).Value; // Build the result string: if (!sciNotation) { tempStr = theValue.ToString("0." + new string('0', noOfDecimals)).PadLeft(numericWidth) + ' '; } else { tempStr = theValue.ToString("#." + new string('#', noOfDecimals) + "e+00").PadLeft(numericWidth) + ' '; } // Pad the source line to the result column: while (line.Length < charPos - 1) { line += ' '; } // Insert the result string: line = line.Insert(charPos - 1, tempStr); } WriteText(line); } finally { Close(); } }
public override void Execute() { bool ignoringCase = CmdLine.GetBooleanSwitch("/I"); numericWidth = CmdLine.GetIntSwitch("/W", 6); bool paddingWithZeros = CmdLine.GetBooleanSwitch("/Z"); string delimiterStr = CmdLine.GetStrSwitch("/D", string.Empty); explicitCounts = CmdLine.GetBooleanSwitch("/E"); joinLinesOpt = CmdLine.GetIntSwitch("/J", 0); joiningLines = (joinLinesOpt == 1) || (joinLinesOpt == 2); rangeIsGiven = CmdLine.ArgCount > 0; bool delimSpecified = delimiterStr != string.Empty; CheckIntRange(numericWidth, 0, int.MaxValue, "Numeric width", CmdLine.GetSwitchPos("/W")); CheckIntRange(joinLinesOpt, 0, 2, "Join option", CmdLine.GetSwitchPos("/J")); char delimiter = '\0'; if (delimSpecified) { delimiter = delimiterStr[0]; } begPos = 0; endPos = 0; if (rangeIsGiven) { begPos = (int)CmdLine.GetArg(0).Value; endPos = (int)CmdLine.GetArg(1).Value; CheckIntRange(begPos, 1, int.MaxValue, "Char. position", CmdLine.GetArg(0).CharPos); CheckIntRange(endPos, 1, int.MaxValue, "Char. position", CmdLine.GetArg(1).CharPos); } if (begPos > endPos) { // Oops. ThrowException("Ending position must be >= begin position.", CmdLine.GetArg(1).CharPos); } if (paddingWithZeros) { padChar = '0'; } else { padChar = ' '; } savedLine = null; accumLine = string.Empty; count = 0; Open(); try { while (!EndOfText) { string line = ReadLine(); int countVal = GetCount(line); StripCount(ref line); if ((delimSpecified && StringsCompare(line, savedLine, ignoringCase, delimiter)) || (!delimSpecified && StringsCompare(line, savedLine, ignoringCase, begPos, endPos))) { // The line compares with the last one. switch (countVal) { case -1: // Numeric value is invalid. ThrowException("Numeric value on text line " + TextLineNo.ToString() + " is invalid."); break; case -2: // Numeric value not found. ThrowException("Numeric value on text line " + TextLineNo.ToString() + " not found."); break; default: // Accumulate the count value: count += countVal; if (joiningLines) { // Accumulate the line: accumLine += line; } break; } } else { // The line does not compare to the last one. OutputLine(); savedLine = line; if (joiningLines) { accumLine = savedLine; } count = countVal; } } OutputLine(); } finally { Close(); } }
public override void Execute() { //LoggingEnabled = true; int radix = (int)CmdLine.GetArg(0).Value; int entryPos = CmdLine.GetIntSwitch("/I", 0); int scanPos = CmdLine.GetIntSwitch("/S", 1); int numericWidth = CmdLine.GetIntSwitch("/W", 6); bool paddingWithZeros = CmdLine.GetBooleanSwitch("/Z"); CheckIntRange(radix, 2, 62, "Radix", CmdLine.GetArg(0).CharPos); if (entryPos != 0) { CheckIntRange(entryPos, 1, int.MaxValue, "Char. position", CmdLine.GetSwitchPos("/I")); } CheckIntRange(scanPos, 1, int.MaxValue, "Char. position", CmdLine.GetSwitchPos("/S")); CheckIntRange(numericWidth, 0, int.MaxValue, "Numeric width", CmdLine.GetSwitchPos("/W")); Open(); try { while (!EndOfText) { string line = ReadLine(); int charPos = scanPos - 1; string validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; string tempStr = ScanToken(line, ref charPos, validChars); if (tempStr != string.Empty) { ulong num = Base2Decimal(tempStr, radix); tempStr = num.ToString(); char padChar; if (paddingWithZeros) { padChar = '0'; } else { padChar = ' '; } tempStr = tempStr.PadLeft(numericWidth, padChar); if (entryPos > 0) { // Insert the result back into source: tempStr += ' '; while (line.Length < entryPos - 1) { line += ' '; } line = line.Insert(entryPos - 1, tempStr); WriteText(line); } else { // Only return the result: WriteText(tempStr); } } else { // No value found to convert. ThrowException("No value was found on text line " + TextLineNo.ToString() + " to convert."); } } } finally { Close(); } }
public override void Execute() { int radix = (int)CmdLine.GetArg(0).Value; int entryPos = CmdLine.GetIntSwitch("/I", 0); int scanPos = CmdLine.GetIntSwitch("/S", 1); int numericWidth = CmdLine.GetIntSwitch("/W", 6); bool paddingWithZeros = CmdLine.GetBooleanSwitch("/Z"); CheckIntRange(radix, 2, 62, "Radix", CmdLine.GetArg(0).CharPos); if (entryPos != 0) { CheckIntRange(entryPos, 1, int.MaxValue, "Char. position", CmdLine.GetSwitchPos("/I")); } CheckIntRange(scanPos, 1, int.MaxValue, "Char. position", CmdLine.GetSwitchPos("/S")); CheckIntRange(numericWidth, 0, int.MaxValue, "Numeric width", CmdLine.GetSwitchPos("/W")); Open(); try { while (!EndOfText) { string line = ReadLine(); int charPos = scanPos - 1; string tempStr = ScanInteger(line, ref charPos); if (tempStr != string.Empty) { try { ulong num = ulong.Parse(tempStr); // The decimal value converted ok. tempStr = DecimalToBase(num, radix); char padChar; if (paddingWithZeros) { padChar = '0'; } else { padChar = ' '; } tempStr = tempStr.PadLeft(numericWidth, padChar); if (entryPos > 0) { // Insert the result back into source: tempStr += ' '; while (line.Length < entryPos - 1) { line += ' '; } line = line.Insert(entryPos - 1, tempStr); WriteText(line); } else { // Only return the result: WriteText(tempStr); } } catch (FormatException) { // Numeric value is invalid. ThrowException("Numeric value on text line " + TextLineNo.ToString() + " is invalid."); } } else { // Numeric value not found. ThrowException("No numeric value was found on text line " + TextLineNo.ToString() + " to convert."); } } } finally { Close(); } }