testrtf.pp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt, member of the
  4. Free Pascal development team
  5. This program demonstrates the RTF parser object.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. Program testrtf;
  13. uses rtfpars,classes;
  14. type
  15. TRTFDemo = class(TObject)
  16. FFileName : string;
  17. FParser : TRTFParser;
  18. Procedure DoDestination;
  19. procedure dospecial;
  20. procedure doctrl;
  21. Procedure Dowrite;
  22. Procedure Start;
  23. procedure handleerror ( s : string);
  24. end;
  25. Var
  26. RTFDemo : TRTFDemo;
  27. procedure TRTFDemo.DoDestination;
  28. {
  29. skip all special destinations.
  30. }
  31. begin
  32. FParser.skipgroup;
  33. end;
  34. procedure TRTFDemo.dospecial;
  35. {
  36. Don't do anything special.
  37. }
  38. begin
  39. if FParser.RTFMinor=rtfpar then
  40. Writeln;
  41. end;
  42. procedure TRTFDemo.doctrl;
  43. begin
  44. case Fparser.rtfmajor of
  45. rtfdestination : dodestination;
  46. rtfspecialchar : dospecial;
  47. end;
  48. end;
  49. Procedure TRTFDemo.Dowrite;
  50. begin
  51. { RTFmajor contains the character ASCII Code, we just dump it }
  52. Write (chr(FParser.RTFMajor));
  53. end;
  54. procedure TRTFDemo.Start;
  55. var Thestream : TFilestream;
  56. begin
  57. Thestream:=TFileStream.Create(FFileName,fmopenread);
  58. FParser:=TRTFParser.Create(TheStream);
  59. FParser.classcallbacks[rtfText]:=@dowrite;
  60. FParser.classcallbacks[rtfcontrol]:=@doctrl;
  61. FParser.onrtferror:=@handleerror;
  62. FParser.StartReading;
  63. Fparser.Free;
  64. Thestream.free;
  65. end;
  66. procedure TRTFDemo.handleerror ( s : string);
  67. begin
  68. Writeln (stderr,s);
  69. end;
  70. VAr Name : String;
  71. begin
  72. RTFDemo:=TRTFDemo.Create;
  73. If Paramstr(1)='' then
  74. begin
  75. Write ('Enter filename to process: ');
  76. Readln (name);
  77. end
  78. else
  79. Name:=Paramstr(1);
  80. RTFDemo.FFileName:=Name;
  81. RTFDemo.Start;
  82. RTFDemo.Free;
  83. end.