public override void Execute() { int numericWidth = CmdLine.GetIntSwitch("/W", 6); bool paddingWithZeros = CmdLine.GetBooleanSwitch("/Z"); char padChar; CheckIntRange(numericWidth, 0, int.MaxValue, "Numeric width", CmdLine.GetSwitchPos("/W")); if (paddingWithZeros) { padChar = '0'; } else { padChar = ' '; } Open(); try { int lineCount = Host.TextLineCount; WriteText(lineCount.ToString().PadLeft(numericWidth, padChar)); } finally { Close(); } }
public override void Execute() { string breakChars = CmdLine.GetStrSwitch("/B", string.Empty); int jaggednessAllowed = CmdLine.GetIntSwitch("/J", 25); int charPos = (int)CmdLine.GetArg(0).Value; CheckIntRange(charPos, 1, int.MaxValue, "Char. position", CmdLine.GetArg(0).CharPos); CheckIntRange(jaggednessAllowed, 0, 100, "% jaggedness", CmdLine.GetSwitchPos("/J")); Open(); try { while (!EndOfText) { string line = ReadLine(); WriteText(Wrap(line, charPos, breakChars, jaggednessAllowed)); } } finally { Close(); } }
public override void Execute() { int charPos = CmdLine.GetIntSwitch("/P", 1); bool reverseSort = CmdLine.GetBooleanSwitch("/R"); CheckIntRange(charPos, 1, int.MaxValue, "Char. position", CmdLine.GetSwitchPos("/P")); // Copy the input text to the output text object: File.Copy(((Filter)Host).InText, ((Filter)Host).OutText, true); // Sort it: SortFile(((Filter)Host).OutText, charPos, reverseSort); }
public override void Execute() { int noOfSets = CmdLine.GetIntSwitch("/S", 0); if (noOfSets != 0) { CheckIntRange(noOfSets, 1, int.MaxValue, "Set size", CmdLine.GetSwitchPos("/S")); } Open(); try { while (!EndOfText) { string line = ReadLine(); int setNo; if (noOfSets <= 0) { setNo = TextLineNo; } else { setNo = (TextLineNo - 1) % noOfSets + 1; } if (InRange(setNo)) { WriteText(line); } } } 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() { bool countGiven = CmdLine.GetBooleanSwitch("/C"); int insertCharPos = CmdLine.GetIntSwitch("/P", 0); if (insertCharPos != 0) { CheckIntRange(insertCharPos, 1, int.MaxValue, "Char. position", CmdLine.GetSwitchPos("/P")); } Open(); try { while (!EndOfText) { string line = ReadLine(); string newLine = string.Empty; if (!countGiven) { // pos/pos pairs are supplied. for (int i = 0; i < (CmdLine.ArgCount / 2); i++) { int j = (i * 2); int begCharPos = (int)CmdLine.GetArg(j).Value; int endCharPos = (int)CmdLine.GetArg(j + 1).Value; CheckIntRange(begCharPos, 1, int.MaxValue, "Begin char. position", CmdLine.GetArg(j).CharPos); CheckIntRange(endCharPos, 1, int.MaxValue, "End char. position", CmdLine.GetArg(j + 1).CharPos); if (endCharPos >= begCharPos) { while (line.Length < endCharPos) { line += ' '; } int len = endCharPos - begCharPos + 1; string tempStr = line.Substring(begCharPos - 1, len); newLine += tempStr; } else { // Begin/End pair is reversed. ThrowException("End position must be >= begin position.", CmdLine.GetArg(j + 1).CharPos); } } } else { // pos/count pairs are supplied. for (int i = 0; i < (CmdLine.ArgCount / 2); i++) { int j = (i * 2); int begCharPos = (int)CmdLine.GetArg(j).Value; int numOfChars = (int)CmdLine.GetArg(j + 1).Value; int endCharPos = begCharPos + numOfChars - 1; CheckIntRange(begCharPos, 1, int.MaxValue, "Begin char. position", CmdLine.GetArg(j).CharPos); CheckIntRange(numOfChars, 1, int.MaxValue, "No. of characters", CmdLine.GetArg(j + 1).CharPos); while (line.Length < endCharPos) { line += ' '; } string tempStr = line.Substring(begCharPos - 1, numOfChars); newLine += tempStr; } } if (insertCharPos > 0) { // Insert the resulting text back into the line: while (line.Length < insertCharPos) { line += ' '; } newLine = line.Insert(insertCharPos - 1, newLine); } WriteText(newLine); } } finally { Close(); } }
public override void Execute() { string delimiterStr = CmdLine.GetStrSwitch("/D", " "); string placeHolderStr = CmdLine.GetStrSwitch("/P", "%"); bool ignoringCase = CmdLine.GetBooleanSwitch("/I"); int begPos = CmdLine.GetIntSwitch("/B", 0); int endPos = CmdLine.GetIntSwitch("/E", 0); bool isRegEx = CmdLine.GetBooleanSwitch("/R"); RegexOptions defaultOptions = RegexOptions.Compiled | RegexOptions.Multiline; if (delimiterStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetSwitchPos("/D")); } if (placeHolderStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetSwitchPos("/P")); } if (begPos != 0) { CheckIntRange(begPos, 1, int.MaxValue, "Char. position", CmdLine.GetSwitchPos("/B")); } if (endPos != 0) { CheckIntRange(endPos, 1, int.MaxValue, "Char. position", CmdLine.GetSwitchPos("/E")); } char delimiter = delimiterStr[0]; Open(); try { while (!EndOfText) { // Read a line of the source text: string text = ReadLine(); // Perform text replacements to line: for (int i = 0; i < CmdLine.ArgCount / 2; i++) { string oldStr = (string)CmdLine.GetArg(i * 2).Value; if (oldStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetArg(i * 2).CharPos); } string newStr; if (begPos == 0) { // Use the specified replace string literally. newStr = (string)CmdLine.GetArg((i * 2) + 1).Value; } else { // Use the specified replace string as a template only. // The real replace string originates from the input text. string replaceStr = GetReplString(text, begPos, endPos, delimiter); string templateStr = (string)CmdLine.GetArg((i * 2) + 1).Value; newStr = templateStr.Replace(placeHolderStr, replaceStr); } if (isRegEx) { if (ignoringCase) { text = Regex.Replace(text, oldStr, newStr, defaultOptions | RegexOptions.IgnoreCase); } else { text = Regex.Replace(text, oldStr, newStr, defaultOptions); } } else { text = ReplaceString(text, oldStr, newStr, ignoringCase); } } // Write the edited line to the output file: WriteText(text); } } finally { Close(); } }
public override void Execute() { string quoteStr = CmdLine.GetStrSwitch("/Q", "\""); string delimiterStr = CmdLine.GetStrSwitch("/D", ","); unquoting = CmdLine.GetBooleanSwitch("/U"); usingBackslashQuote = CmdLine.GetBooleanSwitch("/B"); int noOfSets = CmdLine.GetIntSwitch("/S", 0); int option = CmdLine.GetIntSwitch("/O", 0); if (quoteStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetSwitchPos("/Q")); } if (delimiterStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetSwitchPos("/D")); } if (noOfSets != 0) { CheckIntRange(noOfSets, 1, int.MaxValue, "Set size", CmdLine.GetSwitchPos("/S")); } CheckIntRange(option, 0, 2, "Quote option", CmdLine.GetSwitchPos("/O")); quoteChar = quoteStr[0]; delimiterChar = delimiterStr[0]; int setNo = 0; bool inRange = false; int rangeIndex = 0; int prevLine = 0; string line = string.Empty; Open(); try { while (!EndOfText) { int j = rangeIndex * 2; int begLine = (int)CmdLine.GetArg(j).Value; int endLine = (int)CmdLine.GetArg(j + 1).Value; CheckIntRange(begLine, 1, int.MaxValue, "Line no.", CmdLine.GetArg(j).CharPos); CheckIntRange(endLine, 1, int.MaxValue, "Line no.", CmdLine.GetArg(j + 1).CharPos); if (begLine > prevLine) { prevLine = begLine; if (endLine >= begLine) { // Scan thru lines prior to range: while ((!EndOfText) && (!inRange)) { line = ReadLine(); inRange = (setNo + 1 >= begLine) && (setNo + 1 <= endLine); if (!inRange) { WriteText(line); setNo++; if (noOfSets > 0) { setNo = setNo % noOfSets; } } } // Scan thru lines in range: do { QuoteLine(line, option); setNo++; if (noOfSets > 0) { setNo = setNo % noOfSets; } inRange = (setNo + 1 >= begLine) && (setNo + 1 <= endLine); if ((!EndOfText) && inRange) { line = ReadLine(); if (EndOfText) { QuoteLine(line, option); } } }while ((!EndOfText) && inRange); if (rangeIndex == (CmdLine.ArgCount / 2) - 1) { // Last range - Scan to first set of next group: while ((!EndOfText) && (setNo != 0)) { line = ReadLine(); WriteText(line); setNo += 1; if (noOfSets > 0) { setNo = setNo % noOfSets; } } prevLine = 0; } rangeIndex = (rangeIndex + 1) % (CmdLine.ArgCount / 2); } else { // Begin/End pairs are reversed. ThrowException("End line no. must be >= begin line no.", CmdLine.GetArg(j + 1).CharPos); } } else { // Begin/End pairs are not ascending. ThrowException("Begin/End pairs must be in ascending order.", CmdLine.GetArg(j).CharPos); } } } finally { Close(); } }
public override void Execute() { string theStr = (string)CmdLine.GetArg(0).Value; bool ignoringCase = CmdLine.GetBooleanSwitch("/I"); int noOfRotates = CmdLine.GetIntSwitch("/N", 1); bool isRegEx = CmdLine.GetBooleanSwitch("/R"); CheckIntRange(noOfRotates, 1, int.MaxValue, "No. of rotates", CmdLine.GetSwitchPos("/N")); if (theStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetArg(0).CharPos); } Open(); try { while (!EndOfText) { string line = ReadLine(); if (line.Length > 0) { string matchStr = string.Empty; for (int j = 1; j <= noOfRotates; j++) { if (j > 1) { // Move previous match found to end of line: line = line.Substring(matchStr.Length) + matchStr; } // Find the next match: int charPos = StringPos(theStr, line, ignoringCase, isRegEx, out matchStr); if (charPos >= 0) { // Found it. if (charPos > 0) { // Rotate to it: string front = line.Substring(0, charPos); string back = line.Substring(charPos, line.Length - charPos); line = back + front; } } else { // No match found. Quit: break; } } } WriteText(line); } } finally { Close(); } }
public override void Execute() { int numericWidth = CmdLine.GetIntSwitch("/W", 6); bool paddingWithZeros = CmdLine.GetBooleanSwitch("/Z"); bool perLineCount = CmdLine.GetBooleanSwitch("/L"); int insertPos = CmdLine.GetIntSwitch("/I", 0); char padChar; long count = 0; CheckIntRange(numericWidth, 0, int.MaxValue, "Numeric width", CmdLine.GetSwitchPos("/W")); if (insertPos != 0) { CheckIntRange(insertPos, 1, int.MaxValue, "Char. position", CmdLine.GetSwitchPos("/I")); } if (paddingWithZeros) { padChar = '0'; } else { padChar = ' '; } Open(); try { while (!EndOfText) { string line = ReadLine(); int lineLen = line.Length; if (perLineCount) { if (insertPos > 0) { // Inserting the count back into the line. // Pad the source line to the insertion point: while (line.Length < insertPos - 1) { line += ' '; } // Insert the count: WriteText(line.Insert(insertPos - 1, lineLen.ToString().PadLeft(numericWidth, padChar))); } else { // Outputting only the count. WriteText(lineLen.ToString().PadLeft(numericWidth, padChar)); } } else { count += lineLen; } } if (!perLineCount) { WriteText(count.ToString().PadLeft(numericWidth, padChar)); } } 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() { 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(); } }
protected void Execute(bool left) { int Len; string Source; int SetIndex; string PadStr = (string)CmdLine.GetArg(0).Value; int Width = CmdLine.GetIntSwitch("/W", 0); int SetSize = CmdLine.GetIntSwitch("/S", 1); if (PadStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetArg(0).CharPos); } CheckIntRange(Width, 0, int.MaxValue, "Numeric width", CmdLine.GetSwitchPos("/W")); CheckIntRange(SetSize, 1, int.MaxValue, "Set size", CmdLine.GetSwitchPos("/S")); Open(); try { if (Width > 0) { // Padding to a specified width. while (!EndOfText) { Source = ReadLine(); PadString(ref Source, Width, PadStr, left); WriteText(Source); } } else { // The width is unspecified. Padding all lines // to the width of the longest line IN ITS SET. // Clear the line lengths: int[] LongestLineLengths = new int[SetSize]; for (int i = 0; i < SetSize; i++) { LongestLineLengths[i] = 0; } // Determine the longest line in each set: while (!EndOfText) { Source = ReadLine(); Len = Source.Length; SetIndex = (TextLineNo - 1) % SetSize; if (Len > LongestLineLengths[SetIndex]) { LongestLineLengths[SetIndex] = Len; } } // Re-position to top of text: Reset(); // Pad each line to the longest line in its set: while (!EndOfText) { Source = ReadLine(); SetIndex = (TextLineNo - 1) % SetSize; Width = LongestLineLengths[SetIndex]; PadString(ref Source, Width, PadStr, left); WriteText(Source); } } } 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(); } }
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(); } }
public override void Execute() { char padChar; string tempStr; int initLineNo = CmdLine.GetIntSwitch("/L", 1); int lineNoIncr = CmdLine.GetIntSwitch("/I", 1); int lineNoPos = CmdLine.GetIntSwitch("/P", 1); int lineNoSetSize = CmdLine.GetIntSwitch("/S", 0); bool paddingWithZeros = CmdLine.GetBooleanSwitch("/Z"); int numericWidth = CmdLine.GetIntSwitch("/W", 6); CheckIntRange(lineNoPos, 1, int.MaxValue, "Char. position", CmdLine.GetSwitchPos("/P")); if (lineNoSetSize != 0) { CheckIntRange(lineNoSetSize, 1, int.MaxValue, "Set size", CmdLine.GetSwitchPos("/S")); } CheckIntRange(numericWidth, 0, int.MaxValue, "Numeric width", CmdLine.GetSwitchPos("/W")); int currLineNo = initLineNo; int count = 1; Open(); try { while (!EndOfText) { string source = ReadLine(); if (paddingWithZeros) { padChar = '0'; } else { padChar = ' '; } if (currLineNo < 0) { // The line # is negative. if (paddingWithZeros) { tempStr = (-currLineNo).ToString().PadLeft(numericWidth, padChar); if (tempStr[0] == '0') { tempStr = '-' + tempStr.Substring(1, tempStr.Length - 1); } } else { tempStr = currLineNo.ToString().PadLeft(numericWidth, padChar); } } else { // The line # is positive. tempStr = currLineNo.ToString().PadLeft(numericWidth, padChar); } while (source.Length < lineNoPos - 1) { source += ' '; } source = source.Insert(lineNoPos - 1, tempStr); WriteText(source); if (lineNoSetSize > 0) { // Repeating line number sequence over and over. if (count == lineNoSetSize) { // Restart the sequence: currLineNo = initLineNo; count = 1; } else { // Increment the current line number and the set count: currLineNo += lineNoIncr; count++; } } else { // Not repeating. Just increment the current line number: currLineNo += lineNoIncr; } } } 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(); } }