2
0

ptop.pp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 : 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] 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 (' -b : Use buffers of size bufsize');
  33. Writeln (' -v : be verbose');
  34. writeln ('ptop -g ofile');
  35. writeln (' generate default options file');
  36. Writeln ('ptop -h : This help');
  37. halt(0);
  38. end;
  39. Procedure Genopts;
  40. Var S : PBufStream;
  41. begin
  42. S:=New(PBufStream,Init(ConfigFile,stCreate,255));
  43. GeneratecfgFile(S);
  44. S^.Close;
  45. S^.Done;
  46. end;
  47. Procedure ProcessOpts;
  48. Var c : char;
  49. begin
  50. { Set defaults }
  51. Infilename:='';
  52. OutFileName:='';
  53. ConfigFile:='';
  54. TheIndent:=2;
  55. TheBufSize:=255;
  56. BeVerbose:=False;
  57. Repeat
  58. c:=getopt('i:c:g:b:hv');
  59. case c of
  60. 'i' : begin
  61. TheIndent:=StrToInt(OptArg);
  62. If TheIndent=0 then TheIndent:=2;
  63. end;
  64. 'b' : begin
  65. TheBufSize:=StrToInt(OptArg);
  66. If TheBufSize=0 then TheBufSize:=255;
  67. end;
  68. 'c' : ConfigFile:=OptArg;
  69. 'g' : begin
  70. ConfigFIle:=OptArg;
  71. GenOpts;
  72. halt(0);
  73. end;
  74. 'h' : usage;
  75. 'v' : BeVerbose:=True;
  76. else
  77. end;
  78. until c=endofoptions;
  79. If optind<=paramcount then
  80. begin
  81. InFileName:=paramstr(OptInd);
  82. Inc(optind);
  83. If OptInd<=paramcount then
  84. OutFilename:=Paramstr(OptInd);
  85. end;
  86. end; { Of ProcessOpts }
  87. Var DiagS : PMemoryStream;
  88. InS,OutS,cfgS : PBufSTream;
  89. PPrinter : TPrettyPrinter;
  90. P : Pchar;
  91. i : longint;
  92. begin
  93. ProcessOpts;
  94. If (Length(InfileName)=0) or (Length(OutFileName)=0) Then
  95. Usage;
  96. Ins:=New(PBufStream,Init(InFileName,StopenRead,TheBufSize));
  97. OutS:=New(PBufStream,Init(OutFileName,StCreate,TheBufSize));
  98. If BeVerbose then
  99. diagS:=New(PMemoryStream,Init(1000,255))
  100. else
  101. DiagS:=Nil;
  102. If ConfigFile<>'' then
  103. CfgS:=New(PBufStream,Init(ConfigFile,StOpenRead,TheBufSize))
  104. else
  105. CfgS:=Nil;
  106. PPrinter.Create;
  107. PPrinter.Indent:=TheIndent;
  108. PPrinter.Ins:=Ins;
  109. PPrinter.outS:=OutS;
  110. PPrinter.cfgS:=CfgS;
  111. PPrinter.DiagS:=DiagS;
  112. PPrinter.PrettyPrint;
  113. If Assigned(DiagS) then
  114. begin
  115. I:=DiagS^.GetSize;
  116. DiagS^.Seek(0);
  117. getmem (P,I+1);
  118. DiagS^.Read(P[0],I);
  119. P[I]:=#0;
  120. Writeln (stderr,P);
  121. Flush(stderr);
  122. DiagS^.Done;
  123. end;
  124. If Assigned(CfgS) then
  125. CfgS^.Done;
  126. Ins^.Done;
  127. OutS^.Done;
  128. end.
  129. {
  130. $Log$
  131. Revision 1.1 1999-05-12 16:11:39 peter
  132. * moved
  133. Revision 1.3 1999/03/25 16:52:29 michael
  134. + Implemented Delphi keywords and delphi comments
  135. Revision 1.2 1999/03/23 13:47:47 michael
  136. Added GPL and log
  137. }