public override void Execute() { int NoOfLines = (int)CmdLine.GetArg(0).Value; CheckIntRange(NoOfLines, 1, int.MaxValue, "No. of lines", CmdLine.GetArg(0).CharPos); Open(); try { while (!EndOfText) { string line = ReadLine(); if (TextLineNo >= TextLineCount - NoOfLines + 1) { WriteText(line); } } } finally { Close(); } }
public override void Execute() { int width = (int)CmdLine.GetArg(0).Value; CheckIntRange(width, 1, int.MaxValue, "Width", CmdLine.GetArg(0).CharPos); Open(); try { while (!EndOfText) { string line = ReadLine(); int i = (width - line.Length) / 2; if (i > 0) { WriteText(string.Empty.PadRight(i) + line); } else { WriteText(line); } } } finally { Close(); } }
public override void Execute() { int noOfChars = (int)CmdLine.GetArg(0).Value; CheckIntRange(noOfChars, 1, int.MaxValue, "No. of characters", CmdLine.GetArg(0).CharPos); Open(); try { while (!EndOfText) { string line = ReadLine(); if (line.Length > noOfChars) { line = line.Substring(0, noOfChars); } WriteText(line); } } finally { Close(); } }
protected void Execute(bool rotatingLeft) { int noOfChars = (int)CmdLine.GetArg(0).Value; CheckIntRange(noOfChars, 1, int.MaxValue, "No. of characters", CmdLine.GetArg(0).CharPos); Open(); try { while (!EndOfText) { string line = ReadLine(); if (line.Length > 0) { RotateStr(ref line, rotatingLeft, noOfChars); } WriteText(line); } } finally { Close(); } }
private string GetArg(int argIndex, bool inString) { string tempStr; string methodName = "Pipe.GetArg"; if ((argIndex > -1) && (argIndex < CmdLine.ArgCount)) { // The argument index is in range. tempStr = CmdLine.GetArg(argIndex).Value.ToString(); if (CmdLine.GetArg(argIndex).Value is string) { // Quote the string: if (!inString) { tempStr = "'" + tempStr + "'"; } } LogWrite(methodName + ": Args[" + argIndex.ToString() + "] = \"" + tempStr + "\""); } else { // The argument index is out of range. throw new PipeWrenchCompileException("Placeholder index is out of range."); } return(tempStr); }
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 noOfChars = (int)CmdLine.GetArg(0).Value; CheckIntRange(noOfChars, 1, int.MaxValue, "No. of characters", CmdLine.GetArg(0).CharPos); Open(); try { while (!EndOfText) { string source = ReadLine(); int len = source.Length; if (len >= noOfChars) { string tempStr = source.Substring(len - noOfChars, noOfChars); WriteText(tempStr); } else { WriteText(source); } } } finally { Close(); } }
public override void Execute() { bool cullingAll; string begCharStr; string endCharStr; bool ignoringCase; bool isRegEx; begCharStr = (string)CmdLine.GetArg(0).Value; endCharStr = (string)CmdLine.GetArg(1).Value; cullingAll = CmdLine.GetBooleanSwitch("/A"); ignoringCase = CmdLine.GetBooleanSwitch("/I"); isRegEx = CmdLine.GetBooleanSwitch("/R"); Open(); try { while (!EndOfText) { string line = ReadLine(); if (StringMatched(begCharStr, line, ignoringCase, isRegEx)) { // Found the first string. Omit lines until after the second string is found: while (!EndOfText && !StringMatched(endCharStr, line, ignoringCase, isRegEx)) { // Omit the line && get the next one: line = ReadLine(); } if (!cullingAll) { // Output the remaining lines: while (!EndOfText) { line = ReadLine(); WriteText(line); } } } else { // This line is outside of an omitted segment. Output it: WriteText(line); } } } finally { Close(); } }
// The pipe that is being called. /// <summary> /// Compiles the filter. /// </summary> public override void Compile(string cmdLineText, int initialCharPos) { // Parse the filter's command line: LoggingEnabled = true; LogWrite("Call.Compile: begin"); CmdLine.Text = cmdLineText; CmdLine.IgnoringExtraneousText = true; CmdLine.Parse(initialCharPos, true); // Load and compile the contents of the saved-to-disk pipe: string pipePath = (string)CmdLine.GetArg(0).Value; LogWrite("Call.Compile: Called pipe: \"" + pipePath + "\""); if (System.IO.File.Exists(pipePath)) { // The pipe file exists. Create a new pipe object from the pipe file's contents: string tempStr = System.IO.Path.GetFullPath(pipePath); string pipeFolder = System.IO.Path.GetDirectoryName(tempStr); string pipeName = System.IO.Path.GetFileName(tempStr); string pipeScript = System.IO.File.ReadAllText(pipePath); CalledPipe = new Pipe(pipeScript, (Engine)Eng, pipeFolder, pipeName, ((Filter)Host).CoreLoggingEnabled, false); CalledPipe.DebugFile = ((Filter)Host).DebugFile; CalledPipe.Errors = ((Filter)Host).Errors; // Save the current folder because Pipe.Compile() may change it: string savedFolder = System.Environment.CurrentDirectory; // Compile the pipe using the CALL filter's // "extraneous" command line parameters: CalledPipe.Compile(CmdLine.Text, ((CommandLine)CmdLine).CmdLinePtr, Source, PipeLineNo); // Restore the prior-saved current folder: Directory.SetCurrentDirectory(savedFolder); } else { // Pipe file doesn't exist. PipeWrenchCompileException ex = new PipeWrenchCompileException("Pipe file not found."); ex.Data.Add("CharPos", ((CommandLine)CmdLine).CmdLinePtr); ex.Data.Add("CmdLine", CmdLine.Text); throw ex; } LogWrite("Call.Compile: end"); }
public override void Execute() { Open(); try { while (!EndOfText) { int offset = 0; int prevPos = 0; string text = ReadLine(); // Delete characters at each character position: for (int j = 0; j < CmdLine.ArgCount / 2; j++) { int charPos = (int)CmdLine.GetArg(j * 2).Value; CheckIntRange(charPos, 1, int.MaxValue, "Char. position", CmdLine.GetArg(j * 2).CharPos); if (charPos > prevPos) { int noOfChars = (int)CmdLine.GetArg((j * 2) + 1).Value; CheckIntRange(noOfChars, 1, int.MaxValue, "No. of characters", CmdLine.GetArg((j * 2) + 1).CharPos); while (text.Length < charPos - offset - 1 + noOfChars) { text += ' '; } text = text.Remove(charPos - offset - 1, noOfChars); offset += noOfChars; prevPos = charPos + noOfChars - 1; } else { // Character position arguments are not ascending. ThrowException("Character position arguments must be non-overlapping " + "and in ascending order.", CmdLine.GetArg(j * 2).CharPos); } } WriteText(text); } } finally { Close(); } }
public override void Execute() { Open(); try { while (!EndOfText) { string line = ReadLine(); int offset = 0; int priorPos = 0; 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); if (charPos > priorPos) { priorPos = charPos; int offsetPos = charPos - offset; if (offsetPos <= line.Length) { WriteText(line.Substring(0, offsetPos - 1)); line = line.Remove(0, offsetPos - 1); offset += offsetPos - 1; } } else { // Character positions are not ascending. ThrowException("Character positions must be in ascending order.", CmdLine.GetArg(i).CharPos); } } WriteText(line); } } finally { Close(); } }
public override void Execute() { bool extractingAll; string begCharStr; string endCharStr; bool done = false; bool ignoringCase; bool isRegEx; begCharStr = (string)CmdLine.GetArg(0).Value; endCharStr = (string)CmdLine.GetArg(1).Value; extractingAll = CmdLine.GetBooleanSwitch("/A"); ignoringCase = CmdLine.GetBooleanSwitch("/I"); isRegEx = CmdLine.GetBooleanSwitch("/R"); Open(); try { while (!done && !EndOfText) { string line = ReadLine(); if (StringMatched(begCharStr, line, ignoringCase, isRegEx)) { // Found the first string. Output the line: WriteText(line); // Output lines until the second string is found: while (!EndOfText && !StringMatched(endCharStr, line, ignoringCase, isRegEx)) { line = ReadLine(); WriteText(line); } done = !extractingAll; } } } finally { Close(); } }
private bool InRange(int setNo) { bool result = false; int prevLine = 0; for (int i = 0; i < (CmdLine.ArgCount / 2); i++) { int j = (i * 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) { if ((setNo >= begLine) && (setNo <= endLine)) { result = true; break; } } else { // Begin/End pairs are reversed. ThrowException("End line no. must be >= begin line no.", CmdLine.GetArg(j + 1).CharPos); } } else { // Begin/End pairs must be in ascending order. ThrowException("Begin/End pairs must be in ascending order.", CmdLine.GetArg(j).CharPos); } } return(result); }
public override void Execute() { Open(); try { while (!EndOfText) { string line = ReadLine(); for (int i = 0; i < (CmdLine.ArgCount / 2); i++) { int j = (i * 2); int charPos = (int)CmdLine.GetArg(j).Value; CheckIntRange(charPos, 1, int.MaxValue, "Character position", CmdLine.GetArg(j).CharPos); string charStr = (string)CmdLine.GetArg(j + 1).Value; if (charStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetArg(j + 1).CharPos); } while (line.Length < charPos + charStr.Length - 1) { line += ' '; } line = line.Remove(charPos - 1, charStr.Length); line = line.Insert(charPos - 1, charStr); } WriteText(line); } } finally { Close(); } }
public override void Execute() { LoggingEnabled = true; bool prepending = CmdLine.GetBooleanSwitch("/P"); string appStr = (string)CmdLine.GetArg(0).Value; if (appStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetArg(0).CharPos); } Open(); try { while (!EndOfText) { string line = ReadLine(); string tempStr; if (prepending) { tempStr = appStr + line; } else { tempStr = line + appStr; } WriteText(tempStr); } } finally { Close(); } }
public override void Execute() { int noOfLines = (int)CmdLine.GetArg(0).Value; CheckIntRange(noOfLines, 1, int.MaxValue, "No. of lines", CmdLine.GetArg(0).CharPos); int lineCount = 0; Open(); try { while (!EndOfText && (lineCount < noOfLines)) { lineCount++; WriteText(ReadLine()); } } finally { Close(); } }
protected void Execute(bool excludingLines) { string newSource; bool found; string matchStr = (string)CmdLine.GetArg(0).Value; bool ignoringCase = CmdLine.GetBooleanSwitch("/I"); bool isRegEx = CmdLine.GetBooleanSwitch("/R"); if (matchStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetArg(0).CharPos); } int begPos = 0; int endPos = 0; bool rangeGiven = (CmdLine.ArgCount > 1); if (rangeGiven) { begPos = (int)CmdLine.GetArg(1).Value; endPos = (int)CmdLine.GetArg(2).Value; CheckIntRange(begPos, 1, int.MaxValue, "Begin char. position", CmdLine.GetArg(1).CharPos); CheckIntRange(endPos, 1, int.MaxValue, "End char. position", CmdLine.GetArg(2).CharPos); } if (begPos > endPos) { // Oops. ThrowException("End char. position must be >= begin char. position.", CmdLine.GetArg(2).CharPos); } Open(); try { while (!EndOfText) { string line = ReadLine(); if (rangeGiven) { if (line.Length >= endPos) { newSource = line.Substring(begPos - 1, endPos - begPos + 1); found = StringMatched(matchStr, newSource, ignoringCase, isRegEx); } else { // Range extends past end of line. found = false; } } else { found = StringMatched(matchStr, line, ignoringCase, isRegEx); } if ((!found && excludingLines) || (found && !excludingLines)) { WriteText(line); } } } finally { Close(); } }
public override void Execute() { string spliceFileName = (string)CmdLine.GetArg(0).Value; string delimiterStr = CmdLine.GetStrSwitch("/D", string.Empty); bool mergingText = CmdLine.GetBooleanSwitch("/M"); try { using (TextReader tr = new StreamReader(spliceFileName)) { Open(); try { string tempStr; string source; if (!mergingText) { // Appending each line from splice file to end of input line. while (!EndOfText) { source = ReadLine(); if ((tempStr = tr.ReadLine()) != null) { WriteText(source + delimiterStr + tempStr); } else { WriteText(source); } } while ((tempStr = tr.ReadLine()) != null) { WriteText(tempStr); } } else { // Adding all lines from splice file to end of input text (merging mode). while (!EndOfText) { source = ReadLine(); WriteText(source); } while ((tempStr = tr.ReadLine()) != null) { WriteText(tempStr); } } } finally { Close(); } } } catch (IOException) { // Error reading the splice file. ThrowException("Error reading the splice file."); } }
public override void Execute() { int charPos; string charStr; Open(); try { while (!EndOfText) { int offset = 0; int prevPos = 0; // Read a line of the source text: string text = ReadLine(); // Insert each string argument into it: for (int j = 0; j < CmdLine.ArgCount / 2; j++) { charPos = (int)CmdLine.GetArg((j * 2)).Value + offset; CheckIntRange(charPos, 1, int.MaxValue, "Character position", CmdLine.GetArg((j * 2)).CharPos); if (charPos > prevPos) { prevPos = charPos; charStr = (string)CmdLine.GetArg((j * 2) + 1).Value; if (charStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetArg((j * 2) + 1).CharPos); } while (text.Length < charPos - 1) { text += ' '; } text = text.Insert(charPos - 1, charStr); offset += charStr.Length; } else { // Oops! ThrowException("Character position arguments must be in ascending order.", CmdLine.GetArg((j * 2)).CharPos); } } // Write the edited line to the output file: WriteText(text); } } 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() { string delimiterStr = CmdLine.GetStrSwitch("/D", string.Empty); bool ignoringCase = CmdLine.GetBooleanSwitch("/I"); bool rangeIsGiven = (CmdLine.ArgCount > 0); bool delimSpecified = delimiterStr != string.Empty; char delimiter = '\0'; if (delimSpecified) { delimiter = delimiterStr[0]; } int begPos = 0; int 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("End position must be >= begin position.", CmdLine.GetArg(1).CharPos); } } string oldLine = null; // Must default to null. bool begOfSeq = false; Open(); try { while (!EndOfText) { string line = ReadLine(); if ((delimSpecified && StringsCompare(line, oldLine, ignoringCase, delimiter)) || (!delimSpecified && StringsCompare(line, oldLine, ignoringCase, begPos, endPos))) { // Found a duplicate line. if (begOfSeq) { // Output the previous, (first duplicate) line: WriteText(oldLine); begOfSeq = false; } // Output the current duplicate line: WriteText(line); } else { // The line differs from the previous one. oldLine = line; begOfSeq = true; } } } 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() { bool deletingAll = CmdLine.GetBooleanSwitch("/A"); bool deletingFirst = CmdLine.GetBooleanSwitch("/F"); string delimiterStr = CmdLine.GetStrSwitch("/D", string.Empty); bool ignoringCase = CmdLine.GetBooleanSwitch("/I"); bool rangeIsGiven = CmdLine.ArgCount > 0; bool delimSpecified = delimiterStr != string.Empty; char delimiter = '\0'; if (delimSpecified) { delimiter = delimiterStr[0]; } int begPos = 0; int 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("End position must be >= begin position.", CmdLine.GetArg(1).CharPos); } } string savedLine = null; // Must default to null. int count = 0; Open(); try { if (!deletingFirst) { // Deleting all but one line or all lines of each group. while (!EndOfText) { string line = ReadLine(); if (!((delimSpecified && StringsCompare(line, savedLine, ignoringCase, delimiter)) || (!delimSpecified && StringsCompare(line, savedLine, ignoringCase, begPos, endPos)))) { if ((deletingAll && (count == 1)) || (!deletingAll && (count > 0))) { WriteText(savedLine); } savedLine = line; count = 1; } else { count++; } } if ((deletingAll && (count == 1)) || (!deletingAll && (count > 0))) { WriteText(savedLine); } } else { // Deleting only the FIRST line of each group. To test, use the following: // // pipe: "WrapText 2 | OutDuplLines 2 2 | DelDuplLines 2 2 /f | AppendStr ',' | JoinLines | StripChars 1" // data: 112232435464748495A6B6C6D6E6F6G7 // // After running the WrapText filter, the first char on each line is the line's // sequence #. The second char is the line's data of which only even numbered // data digits are duplicated (per the data digit's value). The expected test // pipe results are as follows: // // 32,64,74,84,B6,C6,D6,E6,F6 // // Note that all odd numbered groups (1, 3, 5 and 7), each of which contains // but a single line, has been removed and that all remaining even numbered // groups (2, 4 and 6) no longer include their first line of data. while (!EndOfText) { string line = ReadLine(); if (!((delimSpecified && StringsCompare(line, savedLine, ignoringCase, delimiter)) || (!delimSpecified && StringsCompare(line, savedLine, ignoringCase, begPos, endPos)))) { if (count == 1) { WriteText(savedLine); } savedLine = line; count = 1; } else { count++; if (count > 1) { WriteText(line); } } } if (count == 1) { WriteText(savedLine); } } } 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(); } }
//private Match match; public override void Execute() { string theMatch; bool ignoringCase = CmdLine.GetBooleanSwitch("/I"); bool isRegEx = CmdLine.GetBooleanSwitch("/R"); Open(); try { while (!EndOfText) { string line = ReadLine(); int begPos = 1; for (int i = 0; i < CmdLine.ArgCount / 2; i++) { int j = (i * 2); int charPos = (int)CmdLine.GetArg(j).Value; string matchStr = (string)CmdLine.GetArg(j + 1).Value; string subSource = line.Substring(begPos - 1, line.Length - begPos + 1); CheckIntRange(charPos, 1, int.MaxValue, "Character position", CmdLine.GetArg(j).CharPos); if (matchStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetArg(j + 1).CharPos); } int p = StringPos(matchStr, subSource, ignoringCase, isRegEx, out theMatch) + 1; if (p > 0) { // Match string was found in the line. int len; if (isRegEx) { len = theMatch.Length; } else { len = matchStr.Length; } int noOfChars; if ((p + begPos - 1) < charPos) { // The match string is located prior to the column position. noOfChars = charPos - (p + begPos - 1); string stringOfBlanks = new string(' ', noOfChars); line = line.Insert(p + begPos - 2, stringOfBlanks); begPos = p + begPos - 1 + len + noOfChars; } else { // The match string is located after the column position. noOfChars = (p + begPos - 1) - charPos; line = line.Remove(charPos - 1, noOfChars); begPos = p + begPos - 1 + len - noOfChars; } } } WriteText(line); } } 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 theStr = (string)CmdLine.GetArg(0).Value; bool ignoringCase = CmdLine.GetBooleanSwitch("/I"); bool isRegEx = CmdLine.GetBooleanSwitch("/R"); if (theStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetArg(0).CharPos); } bool rangeGiven = (CmdLine.ArgCount > 1); int begPos = 0; int endPos = 0; if (rangeGiven) { begPos = (int)CmdLine.GetArg(1).Value; endPos = (int)CmdLine.GetArg(2).Value; CheckIntRange(begPos, 1, int.MaxValue, "Char. position", CmdLine.GetArg(1).CharPos); CheckIntRange(endPos, 1, int.MaxValue, "Char. position", CmdLine.GetArg(2).CharPos); } if (begPos > endPos) { // Oops. ThrowException("Ending position must be >= begin position.", CmdLine.GetArg(2).CharPos); } // Create a temporary text list for storing "grouped" lines: List <string> groupedLines = new List <string>(); Open(); try { // Process the text: while (!EndOfText) { string line = ReadLine(); string newLine; if (rangeGiven) { newLine = line.Substring(begPos - 1, endPos - begPos + 1); } else { newLine = line; } if (StringMatched(theStr, newLine, ignoringCase, isRegEx)) { // string found. Add it to the list of "grouped" lines: groupedLines.Add(line); } else { // string not found in line. Output the line now: WriteText(line); } } // Now, output all of the grouped lines: foreach (string tempStr in groupedLines) { WriteText(tempStr); } } 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() { int noOfLinesToJoin = 0; if (CmdLine.ArgCount > 0) { noOfLinesToJoin = (int)CmdLine.GetArg(0).Value; CheckIntRange(noOfLinesToJoin, 1, int.MaxValue, "No. of lines", CmdLine.GetArg(0).CharPos); } bool paragraphJoin = CmdLine.GetBooleanSwitch("/P"); string line; string newLine = string.Empty; Open(); try { if (noOfLinesToJoin > 0) { // Joining a specified number of lines. int count = 1; while (!EndOfText) { line = ReadLine(); if (count <= noOfLinesToJoin) { // Append the current line to the end of newLine: newLine += line; if ((count == noOfLinesToJoin) || EndOfText) { // Add newLine to the output queue: WriteText(newLine); count = 1; newLine = string.Empty; } else { count++; } } } } else { if (!paragraphJoin) { // Joining all lines, (stripping newlines). while (!EndOfText) { newLine += ReadLine(); } Write(newLine); // This fixes special case where joining 0 input lines was resulting in 1 line. } else { // Joining all lines, (Paragraph Join). while (!EndOfText) { do { line = ReadLine(); if (line.Trim() != string.Empty) { newLine += line; } }while ((line.Trim() != string.Empty) && !EndOfText); if (line.Trim() == string.Empty) { newLine += System.Environment.NewLine; } WriteText(newLine); newLine = string.Empty; } } } } finally { Close(); } }
public override void Execute() { int begPos = 0; int endPos = 0; bool rangeGiven = (CmdLine.ArgCount == 2); if (rangeGiven) { begPos = (int)CmdLine.GetArg(0).Value; endPos = (int)CmdLine.GetArg(1).Value; CheckIntRange(begPos, 1, int.MaxValue, "Begin char. position", CmdLine.GetArg(0).CharPos); CheckIntRange(endPos, 1, int.MaxValue, "End char. position", CmdLine.GetArg(1).CharPos); } if (begPos > endPos) { // Oops. ThrowException("End char. position must be >= begin char. position.", CmdLine.GetArg(1).CharPos); } Open(); try { while (!EndOfText) { string line = ReadLine(); string tempStr = string.Empty; if (rangeGiven) { // Reversing substring of line only. Reverse the range of characters: string subStr = string.Empty; if (line.Length < endPos) { // Range is beyond end of line. if (line.Length >= begPos) { subStr = line.Substring(begPos - 1, line.Length - begPos + 1); } } else { subStr = line.Substring(begPos - 1, endPos - begPos + 1); } string revSubStr = ReverseStr(subStr); string beforeStr; if (line.Length >= begPos) { beforeStr = line.Substring(0, begPos - 1); } else { beforeStr = line; } string afterStr = string.Empty; if (line.Length >= endPos) { afterStr = line.Substring(endPos); } tempStr = beforeStr + revSubStr + afterStr; } else { // Reversing entire line. tempStr = ReverseStr(line); } WriteText(tempStr); } } finally { Close(); } }