home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D12 / UTILDEMO.ZIP / GREP2MSG.PAS next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  2.5 KB  |  94 lines

  1. {************************************************}
  2. {                                                }
  3. {   Grep message filter example                  }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. program Grep2Msg;
  9.  
  10. { Message filters read input from the target program (in this case, GREP)
  11.   by way of StdIn (by using Read or ReadLn), filter the input, then write
  12.   output back to StdOut (using Write or WriteLn). The IDE takes care of
  13.   redirecting the transfer program's output to the filter program, as well
  14.   as redirecting the filter program's output back to the IDE itself. 
  15. }
  16.  
  17. {$I-,S-}
  18.  
  19. var
  20.   LineNo, E: Word;
  21.   Line: String;
  22.   InputBuffer: array[0..4095] of Char;
  23.   OutputBuffer: array[0..4095] of Char;
  24.  
  25.  
  26. { The first data passed back to the IDE by a message filter must always 
  27.   be the string 'BI#PIP#OK', followed by a null terminator.
  28. }
  29. procedure WriteHeader;
  30. begin
  31.   Write('BI#PIP#OK'#0);
  32. end;
  33.  
  34. { The beginning of a new file is marked by a #0, the file's name, terminated
  35.   by a #0 character.
  36. }
  37. procedure WriteNewFile(const FileName: String);
  38. begin
  39.   Write(#0, FileName, #0);
  40. end;
  41.  
  42. { Each message line begins with a #1, followed the line number (in low/high 
  43.   order), followed by the column number (in low/high order), then the
  44.   message text itself, terminated with a #0 character.
  45. }
  46. procedure WriteMessage(Line, Col: Word; const Message: String);
  47. begin
  48.   Write(#1, Chr(Lo(Line)), Chr(Hi(Line)), Chr(Lo(Col)), Chr(Hi(Col)),
  49.     Message, #0);
  50. end;
  51.  
  52. { The end of the input stream is marked by a #127 character }
  53. procedure WriteEnd;
  54. begin
  55.   Write(#127);
  56. end;
  57.  
  58. function TrimLeft(S:String): String;
  59. var
  60.   i: Integer;
  61.   n: String;
  62. begin
  63.   i := 1;
  64.   while (i <= Length(s)) and (s[i] = #32) do Inc(i);
  65.   if i <= Length(s) then
  66.   begin
  67.     Move(s[i], n[1], Length(s) - i + 1);
  68.     n[0] := Char(Length(s) - i + 1);
  69.   end
  70.   else n[0] := #0;
  71.   TrimLeft := n;
  72. end;
  73.  
  74. begin
  75.   SetTextBuf(Input, InputBuffer);
  76.   SetTextBuf(Output, OutputBuffer);
  77.   WriteHeader;
  78.   while not Eof do
  79.   begin
  80.     ReadLn(Line);
  81.     if Line <> '' then
  82.     begin
  83.       if Copy(Line, 1, 5) = 'File ' then
  84.         WriteNewFile(Copy(Line, 6, Length(Line) - 6))
  85.       else
  86.       begin
  87.         Val(Copy(Line, 1, Pos(' ', Line) - 1), LineNo, E);
  88.         if E = 0 then WriteMessage(LineNo, 1, TrimLeft(Copy(Line, 9, 132)));
  89.       end;
  90.     end;
  91.   end;
  92.   WriteEnd;
  93. end.
  94.