testrtf.pp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. {$mode objfpc}{$h+}
  14. uses rtfpars,classes;
  15. type
  16. TRTFDemo = class(TObject)
  17. FFileName : string;
  18. FParser : TRTFParser;
  19. Procedure DoDestination;
  20. procedure dospecial;
  21. procedure doctrl;
  22. Procedure Dowrite;
  23. Procedure Start;
  24. procedure handleerror ( s : shortstring);
  25. end;
  26. Var
  27. RTFDemo : TRTFDemo;
  28. procedure TRTFDemo.DoDestination;
  29. {
  30. skip all special destinations.
  31. }
  32. begin
  33. FParser.skipgroup;
  34. end;
  35. procedure TRTFDemo.dospecial;
  36. {
  37. Don't do anything special.
  38. }
  39. begin
  40. if FParser.RTFMinor=rtfpar then
  41. Writeln;
  42. end;
  43. procedure TRTFDemo.doctrl;
  44. begin
  45. case Fparser.rtfmajor of
  46. rtfdestination : dodestination;
  47. rtfspecialchar : dospecial;
  48. end;
  49. end;
  50. Procedure TRTFDemo.Dowrite;
  51. begin
  52. { RTFmajor contains the character ASCII Code, we just dump it }
  53. Write (chr(FParser.RTFMajor));
  54. end;
  55. procedure TRTFDemo.Start;
  56. var Thestream : TFilestream;
  57. begin
  58. Thestream:=TFileStream.Create(FFileName,fmopenread);
  59. FParser:=TRTFParser.Create(TheStream);
  60. FParser.classcallbacks[rtfText]:=@dowrite;
  61. FParser.classcallbacks[rtfcontrol]:=@doctrl;
  62. FParser.onrtferror:=@handleerror;
  63. FParser.StartReading;
  64. Fparser.Free;
  65. Thestream.free;
  66. end;
  67. procedure TRTFDemo.handleerror ( s : shortstring);
  68. begin
  69. Writeln (stderr,s);
  70. end;
  71. VAr Name : String;
  72. begin
  73. RTFDemo:=TRTFDemo.Create;
  74. If Paramstr(1)='' then
  75. begin
  76. Write ('Enter filename to process: ');
  77. Readln (name);
  78. end
  79. else
  80. Name:=Paramstr(1);
  81. RTFDemo.FFileName:=Name;
  82. RTFDemo.Start;
  83. RTFDemo.Free;
  84. end.