ptop.pp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. {$mode objfpc}
  2. {$H+}
  3. Program PtoP;
  4. {
  5. This file is part of the Free Pascal run time library.
  6. Copyright (c) 1999-2002 by Michael Van Canneyt, member of
  7. the Free Pascal development team
  8. Pascal pretty print program
  9. See the file COPYING.FPC, included in this distribution,
  10. for details about the copyright.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. **********************************************************************}
  15. Uses SysUtils,Classes,PtoPu,CustApp, bufstream;
  16. ResourceString
  17. Version = 'Version 1.2';
  18. ATitle = 'PToP';
  19. Copyright = 'Copyright (c) 1999-2005 by the Free Pascal Development Team';
  20. SErrNoInputOutput = 'No input and output file given';
  21. Type
  22. TPToP = Class(TCustomApplication)
  23. Private
  24. Infilename,
  25. OutFileName,
  26. ConfigFile : String;
  27. BeVerbose : Boolean;
  28. TheIndent,
  29. TheBufSize,
  30. TheLineSize : Integer;
  31. Procedure Usage(ECode : Word);
  32. Procedure GenOpts;
  33. Procedure ProcessOpts;
  34. Procedure DoVerbose(Sender : TObject; Const Msg : String);
  35. Public
  36. Procedure DoRun; override;
  37. end;
  38. Procedure TPToP.DoVerbose(Sender : TObject; Const Msg : String);
  39. begin
  40. Writeln(StdErr,Msg);
  41. end;
  42. Procedure TPToP.Usage(ECode : Word);
  43. begin
  44. Writeln ('ptop : Usage : ');
  45. Writeln ('ptop [-v] [-i indent] [-b bufsize ][-c optsfile][-l linesize] infile outfile');
  46. Writeln (' converts infile to outfile.');
  47. Writeln (' -c : read options from optsfile');
  48. Writeln (' -i : Set number of indent spaces.');
  49. Writeln (' -l : Set maximum output linesize.');
  50. Writeln (' -b : Use buffers of size bufsize');
  51. Writeln (' -v : be verbose');
  52. writeln ('ptop -g ofile');
  53. writeln (' generate default options file');
  54. Writeln ('ptop -h : This help');
  55. halt(Ecode);
  56. end;
  57. Procedure TPToP.Genopts;
  58. Var S : TFileStream;
  59. begin
  60. S:=TFileStream.Create(ConfigFile,fmCreate);
  61. Try
  62. GeneratecfgFile(S);
  63. Finally
  64. S.Free;
  65. end;
  66. end;
  67. Procedure TPToP.ProcessOpts;
  68. Var
  69. S : String;
  70. begin
  71. { Set defaults }
  72. Infilename:='';
  73. OutFileName:='';
  74. ConfigFile:='';
  75. TheIndent:=2;
  76. TheBufSize:=255;
  77. TheLineSize:=DefLineSize;
  78. BeVerbose:=False;
  79. S:=CheckOptions('icglbhv','');
  80. If (S<>'') then
  81. begin
  82. Writeln(stderr,S);
  83. Usage(1);
  84. end;
  85. if HasOption('h') then
  86. usage(0);
  87. TheIndent:=StrToIntDef(GetOptionValue('i',''),2);
  88. TheBufSize:=StrToIntDef(GetOptionValue('b',''),255);
  89. TheLineSize:=StrToIntDef(GetOptionValue('l',''),DefLineSize);
  90. If HasOption('g') then
  91. begin
  92. ConfigFile:=GetOptionValue('g','');
  93. GenOpts;
  94. halt(0);
  95. end;
  96. ConfigFile:=GetOptionValue('c','');
  97. BeVerbose:=HasOption('v');
  98. If (ParamCount>1) then
  99. begin
  100. InFileName:=paramstr(ParamCount-1);
  101. OutFilename:=Paramstr(ParamCount);
  102. end;
  103. end; { Of ProcessOpts }
  104. Procedure TPToP.DoRun;
  105. Var
  106. F,InS,OutS,cfgS : TSTream;
  107. PPrinter : TPrettyPrinter;
  108. begin
  109. ProcessOpts;
  110. if BeVerbose then
  111. begin
  112. writeln(Title+' '+Version);
  113. writeln(Copyright);
  114. Writeln;
  115. end;
  116. If (Length(InfileName)=0) or (Length(OutFileName)=0) Then
  117. begin
  118. Writeln(stderr,SErrNoInputOutput);
  119. Usage(1);
  120. end;
  121. Ins:=TMemoryStream.Create;
  122. try
  123. F:=TFileStream.Create(InFileName,fmOpenRead);
  124. Try
  125. Ins.CopyFrom(F,0);
  126. Ins.Position:=0;
  127. Finally
  128. F.Free;
  129. end;
  130. OutS:=TwriteBufStream.Create(TFileStream.Create(OutFileName,fmCreate));
  131. Try
  132. If ConfigFile<>'' then
  133. CfgS:=TFileStream.Create(ConfigFile,fmOpenRead)
  134. else
  135. CfgS:=Nil;
  136. try
  137. PPrinter:=TPrettyPrinter.Create;
  138. Try
  139. PPrinter.Indent:=TheIndent;
  140. PPrinter.LineSize:=TheLineSize;
  141. PPrinter.Source:=Ins;
  142. PPrinter.Dest:=OutS;
  143. PPrinter.Config:=CfgS;
  144. If BeVerbose then
  145. PPrinter.OnVerbose:=@DoVerbose;
  146. PPrinter.PrettyPrint;
  147. Finally
  148. FreeAndNil(PPrinter);
  149. end;
  150. Finally
  151. FreeAndNil(CfgS);
  152. end;
  153. Finally
  154. FreeAndNil(OutS);
  155. end;
  156. Finally
  157. FreeAndNil(Ins);
  158. end;
  159. Terminate;
  160. end;
  161. begin
  162. With TPToP.Create(Nil) do
  163. Try
  164. Title:= ATitle;
  165. StopOnException:=True;
  166. Initialize;
  167. Run;
  168. Finally
  169. Free;
  170. end;
  171. end.