The source of the PtoP program is conveniently split in two files: one is a unit containing an object that does the actual beautifying of the source, the other is a shell built around this object so it can be used from the command line. This design makes it possible to include the object in a program (e.g. an IDE) and use its features to format code.
The object resides in the PtoPU unit, and is declared as follows
TPrettyPrinter=Object(TObject) Indent : Integer; { How many characters to indent ? } InS : PStream; OutS : PStream; DiagS : PStream; CfgS : PStream; Constructor Create; Function PrettyPrint : Boolean; end;
Using this object is very simple. The procedure is as follows:
So, a minimal procedure would be:
Procedure CleanUpCode; var Ins,OutS : PBufStream; PPRinter : TPrettyPrinter; begin Ins:=New(PBufStream,Init('ugly.pp',StopenRead,TheBufSize)); OutS:=New(PBufStream,Init('beauty.pp',StCreate,TheBufSize)); PPrinter.Create; PPrinter.Ins:=Ins; PPrinter.outS:=OutS; PPrinter.PrettyPrint; end;
Using memory streams allows very fast formatting of code, and is particularly suitable for editors.