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 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() { //LoggingEnabled = true; int state = 1; string line; string newLine; string begStr = (string)CmdLine.GetArg(0).Value; string endStr = CmdLine.GetStrSwitch("/E", string.Empty); bool ignoringCase = CmdLine.GetBooleanSwitch("/I"); bool isRegEx = CmdLine.GetBooleanSwitch("/R"); 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("End position must be >= begin position.", CmdLine.GetArg(2).CharPos); } // Create/open a text file to contain the diverted text: string divText = Path.GetTempFileName(); StreamWriter divTextWriter = new StreamWriter(divText); try { // Open the input/output files: Open(); try { // Process the text: while (!EndOfText) { line = ReadLine(); if (rangeGiven) { newLine = line.Substring(begPos - 1, endPos - begPos + 1); } else { newLine = line; } if (endStr != string.Empty) { // The block of text is defined as the *first* encountered // group of lines bounded by the two given boundary strings. switch (state) { case 1: if (!StringMatched(begStr, newLine, ignoringCase, isRegEx)) { // Save line to "top" of diverted text file: divTextWriter.WriteLine(line); } else { // Output the line: WriteText(line); // Write a boundary marker between the "top" // and "bottom" of the diverted text: divTextWriter.WriteLine("<rekram yradnuob>"); // TODO: Need to remove the need for this boundary marker (likely by using an additional list). if (StringMatched(endStr, newLine, ignoringCase, isRegEx)) { state = 3; } else { state = 2; } } break; case 2: // Looking for ending string. Output the line: WriteText(line); if (StringMatched(endStr, newLine, ignoringCase, isRegEx)) { state = 3; } break; case 3: // Save line to "bottom" of the diverted text file: divTextWriter.WriteLine(line); break; } } else { // The isolated block of text is defined as the *first* encountered // group of lines that contain the given string (optionally within the // given range of character positions). switch (state) { case 1: // Collecting "top" lines. if (StringMatched(begStr, newLine, ignoringCase, isRegEx)) { // Output the line: WriteText(line); // Write a boundary marker between the "top" // and "bottom" of the diverted text: divTextWriter.WriteLine("<rekram yradnuob>"); state = 2; } else { // Save line to "top" of diverted text file: divTextWriter.WriteLine(line); } break; case 2: // Collecting the "isolated" lines. if (StringMatched(begStr, newLine, ignoringCase, isRegEx)) { // Output the line: WriteText(line); } else { // Save line to "bottom" of the diverted text file: divTextWriter.WriteLine(line); state = 3; } break; case 3: // Save line to "bottom" of the diverted text file: divTextWriter.WriteLine(line); break; } } } // Push the diverted text filename onto the stack: ((Filter)Host).DivTextStack.Push(divText); } finally { Close(); } } finally { divTextWriter.Close(); } }
public override void Execute() { int i; string Source; string Token; char QuoteChar; char DelimiterChar; int State; char Ch; bool Done; string QuoteStr = CmdLine.GetStrSwitch("/Q", "\""); string DelimiterStr = CmdLine.GetStrSwitch("/D", ","); if (QuoteStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetSwitchPos("/Q")); } if (DelimiterStr == string.Empty) { ThrowException("String cannot be empty.", CmdLine.GetSwitchPos("/D")); } QuoteChar = QuoteStr[0]; DelimiterChar = DelimiterStr[0]; Open(); try { while (!EndOfText) { Source = ReadLine(); i = 0; State = 1; Token = string.Empty; Done = false; do { Ch = GetAChar(ref i, ref Source); if (Ch != '\0') { switch (State) { case 1: if (Ch == QuoteChar) { Token += Ch; State = 2; } else { if (Ch != DelimiterChar) { if (Char.IsWhiteSpace(Ch)) { State = 8; } else { Token += Ch; State = 5; } } else { State = 4; } } break; case 2: Token += Ch; if (Ch == QuoteChar) { State = 3; } break; case 3: if (Ch == QuoteChar) { Token += Ch; State = 2; } else { if (Char.IsWhiteSpace(Ch)) { State = 8; } else { State = 4; } } break; case 4: WriteText(Token); Token = string.Empty; if (Ch == QuoteChar) { Token += Ch; State = 2; } else { if (Ch != DelimiterChar) { if (!Char.IsWhiteSpace(Ch)) { Token += Ch; State = 5; } else { State = 8; } } } break; case 5: if (Ch == DelimiterChar) { State = 4; } else { if (Char.IsWhiteSpace(Ch)) { State = 8; } else { Token += Ch; } } break; case 8: if (Ch == DelimiterChar) { State = 4; } else { if (Ch == QuoteChar) { Token += Ch; State = 2; } else { if (!Char.IsWhiteSpace(Ch)) { Token += Ch; State = 5; } } } break; } } else { if (Token != string.Empty) { WriteText(Token); } string TempStr = Source.Trim(); if (TempStr[TempStr.Length - 1] == DelimiterChar) { WriteText(string.Empty); } Done = true; } }while (!Done); } } 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(); } }