ptop.pp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. Program PtoP;
  2. {
  3. $Id$
  4. This file is part of the Free Pascal run time library.
  5. Copyright (c) 1999 by Michael Van Canneyt, member of
  6. the Free Pascal development team
  7. Pascal pretty print program
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. Uses PtoPu,Objects,getopts;
  15. Var
  16. Infilename,OutFileName,ConfigFile : String;
  17. BeVerbose : Boolean;
  18. TheIndent,TheBufSize,TheLineSize : Integer;
  19. Function StrToInt(Const S : String) : Integer;
  20. Var Code : integer;
  21. begin
  22. Val(S,StrToInt,Code);
  23. If Code<>0 then StrToInt:=0;
  24. end;
  25. Procedure Usage;
  26. begin
  27. Writeln ('ptop : Usage : ');
  28. Writeln ('ptop [-v] [-i indent] [-b bufsize ][-c optsfile][-l linesize] infile outfile');
  29. Writeln (' converts infile to outfile.');
  30. Writeln (' -c : read options from optsfile');
  31. Writeln (' -i : Set number of indent spaces.');
  32. Writeln (' -l : Set maximum output linesize.');
  33. Writeln (' -b : Use buffers of size bufsize');
  34. Writeln (' -v : be verbose');
  35. writeln ('ptop -g ofile');
  36. writeln (' generate default options file');
  37. Writeln ('ptop -h : This help');
  38. halt(0);
  39. end;
  40. Procedure Genopts;
  41. Var S : PBufStream;
  42. begin
  43. S:=New(PBufStream,Init(ConfigFile,stCreate,255));
  44. GeneratecfgFile(S);
  45. S^.Close;
  46. S^.Done;
  47. end;
  48. Procedure ProcessOpts;
  49. Var c : char;
  50. begin
  51. { Set defaults }
  52. Infilename:='';
  53. OutFileName:='';
  54. ConfigFile:='';
  55. TheIndent:=2;
  56. TheBufSize:=255;
  57. TheLineSize:=MaxLineSize;
  58. BeVerbose:=False;
  59. Repeat
  60. c:=getopt('i:c:g:b:hv');
  61. case c of
  62. 'i' : begin
  63. TheIndent:=StrToInt(OptArg);
  64. If TheIndent=0 then TheIndent:=2;
  65. end;
  66. 'b' : begin
  67. TheBufSize:=StrToInt(OptArg);
  68. If TheBufSize=0 then TheBufSize:=255;
  69. end;
  70. 'c' : ConfigFile:=OptArg;
  71. 'l' : begin
  72. TheLineSize:=StrToInt(OptArg);
  73. If TheLineSIze=0 Then TheLineSize:=MaxLineSize;
  74. end;
  75. 'g' : begin
  76. ConfigFIle:=OptArg;
  77. GenOpts;
  78. halt(0);
  79. end;
  80. 'h' : usage;
  81. 'v' : BeVerbose:=True;
  82. else
  83. end;
  84. until c=endofoptions;
  85. If optind<=paramcount then
  86. begin
  87. InFileName:=paramstr(OptInd);
  88. Inc(optind);
  89. If OptInd<=paramcount then
  90. OutFilename:=Paramstr(OptInd);
  91. end;
  92. end; { Of ProcessOpts }
  93. Var DiagS : PMemoryStream;
  94. InS,OutS,cfgS : PBufSTream;
  95. PPrinter : TPrettyPrinter;
  96. P : Pchar;
  97. i : longint;
  98. begin
  99. ProcessOpts;
  100. If (Length(InfileName)=0) or (Length(OutFileName)=0) Then
  101. Usage;
  102. Ins:=New(PBufStream,Init(InFileName,StopenRead,TheBufSize));
  103. OutS:=New(PBufStream,Init(OutFileName,StCreate,TheBufSize));
  104. If BeVerbose then
  105. diagS:=New(PMemoryStream,Init(1000,255))
  106. else
  107. DiagS:=Nil;
  108. If ConfigFile<>'' then
  109. CfgS:=New(PBufStream,Init(ConfigFile,StOpenRead,TheBufSize))
  110. else
  111. CfgS:=Nil;
  112. PPrinter.Create;
  113. PPrinter.Indent:=TheIndent;
  114. PPrinter.LineSize:=TheLineSize;
  115. PPrinter.Ins:=Ins;
  116. PPrinter.outS:=OutS;
  117. PPrinter.cfgS:=CfgS;
  118. PPrinter.DiagS:=DiagS;
  119. PPrinter.PrettyPrint;
  120. If Assigned(DiagS) then
  121. begin
  122. I:=DiagS^.GetSize;
  123. DiagS^.Seek(0);
  124. getmem (P,I+1);
  125. DiagS^.Read(P[0],I);
  126. P[I]:=#0;
  127. Writeln (stderr,P);
  128. Flush(stderr);
  129. DiagS^.Done;
  130. end;
  131. If Assigned(CfgS) then
  132. CfgS^.Done;
  133. Ins^.Done;
  134. OutS^.Done;
  135. end.
  136. {
  137. $Log$
  138. Revision 1.2 1999-07-08 21:17:10 michael
  139. + Made output linesize variable
  140. Revision 1.1 1999/05/12 16:11:39 peter
  141. * moved
  142. Revision 1.3 1999/03/25 16:52:29 michael
  143. + Implemented Delphi keywords and delphi comments
  144. Revision 1.2 1999/03/23 13:47:47 michael
  145. Added GPL and log
  146. }