ptop.pp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. Title = '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. P : String;
  109. i : longint;
  110. begin
  111. ProcessOpts;
  112. if BeVerbose then
  113. begin
  114. writeln(Title+' '+Version);
  115. writeln(Copyright);
  116. Writeln;
  117. end;
  118. If (Length(InfileName)=0) or (Length(OutFileName)=0) Then
  119. begin
  120. Writeln(stderr,SErrNoInputOutput);
  121. Usage(1);
  122. end;
  123. Ins:=TMemoryStream.Create;
  124. try
  125. F:=TFileStream.Create(InFileName,fmOpenRead);
  126. Try
  127. Ins.CopyFrom(F,0);
  128. Ins.Position:=0;
  129. Finally
  130. F.Free;
  131. end;
  132. OutS:=TwriteBufStream.Create(TFileStream.Create(OutFileName,fmCreate));
  133. Try
  134. If ConfigFile<>'' then
  135. CfgS:=TFileStream.Create(ConfigFile,fmOpenRead)
  136. else
  137. CfgS:=Nil;
  138. try
  139. PPrinter:=TPrettyPrinter.Create;
  140. Try
  141. PPrinter.Indent:=TheIndent;
  142. PPrinter.LineSize:=TheLineSize;
  143. PPrinter.Source:=Ins;
  144. PPrinter.Dest:=OutS;
  145. PPrinter.Config:=CfgS;
  146. If BeVerbose then
  147. PPrinter.OnVerbose:=@DoVerbose;
  148. PPrinter.PrettyPrint;
  149. Finally
  150. FreeAndNil(PPrinter);
  151. end;
  152. Finally
  153. FreeAndNil(CfgS);
  154. end;
  155. Finally
  156. FreeAndNil(OutS);
  157. end;
  158. Finally
  159. FreeAndNil(Ins);
  160. end;
  161. Terminate;
  162. end;
  163. begin
  164. With TPToP.Create(Nil) do
  165. Try
  166. StopOnException:=True;
  167. Initialize;
  168. Run;
  169. Finally
  170. Free;
  171. end;
  172. end.