grep2msg.pas 2.6 KB

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