mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-09-04 20:11:38 +02:00
* Replaced with new version based on classes
git-svn-id: trunk@5904 -
This commit is contained in:
parent
13335d84d8
commit
d444058d0e
251
utils/ptop.pp
251
utils/ptop.pp
@ -1,4 +1,5 @@
|
|||||||
|
{$mode objfpc}
|
||||||
|
{$H+}
|
||||||
Program PtoP;
|
Program PtoP;
|
||||||
{
|
{
|
||||||
This file is part of the Free Pascal run time library.
|
This file is part of the Free Pascal run time library.
|
||||||
@ -16,31 +17,41 @@ Program PtoP;
|
|||||||
|
|
||||||
**********************************************************************}
|
**********************************************************************}
|
||||||
|
|
||||||
Uses PtoPu,Objects,getopts;
|
|
||||||
|
|
||||||
const
|
Uses SysUtils,Classes,PtoPu,CustApp, bufstream;
|
||||||
Version = 'Version 1.1';
|
|
||||||
Title = 'DelPascal';
|
ResourceString
|
||||||
Copyright = 'Copyright (c) 1999-2002 by the Free Pascal Development Team';
|
Version = 'Version 1.2';
|
||||||
|
Title = 'PToP';
|
||||||
|
Copyright = 'Copyright (c) 1999-2005 by the Free Pascal Development Team';
|
||||||
|
SErrNoInputOutput = 'No input and output file given';
|
||||||
|
|
||||||
|
Type
|
||||||
|
TPToP = Class(TCustomApplication)
|
||||||
|
Private
|
||||||
|
Infilename,
|
||||||
|
OutFileName,
|
||||||
|
ConfigFile : String;
|
||||||
|
BeVerbose : Boolean;
|
||||||
|
TheIndent,
|
||||||
|
TheBufSize,
|
||||||
|
TheLineSize : Integer;
|
||||||
|
Procedure Usage(ECode : Word);
|
||||||
|
Procedure GenOpts;
|
||||||
|
Procedure ProcessOpts;
|
||||||
|
Procedure DoVerbose(Sender : TObject; Const Msg : String);
|
||||||
|
Public
|
||||||
|
Procedure DoRun; override;
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
Var
|
Procedure TPToP.DoVerbose(Sender : TObject; Const Msg : String);
|
||||||
Infilename,OutFileName,ConfigFile : String;
|
|
||||||
BeVerbose : Boolean;
|
|
||||||
TheIndent,TheBufSize,TheLineSize : Integer;
|
|
||||||
|
|
||||||
Function StrToInt(Const S : String) : Integer;
|
|
||||||
|
|
||||||
Var Code : integer;
|
|
||||||
Int : integer;
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
Val(S,int,Code);
|
Writeln(StdErr,Msg);
|
||||||
StrToInt := int;
|
|
||||||
If Code<>0 then StrToInt:=0;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Procedure Usage;
|
Procedure TPToP.Usage(ECode : Word);
|
||||||
|
|
||||||
begin
|
begin
|
||||||
Writeln ('ptop : Usage : ');
|
Writeln ('ptop : Usage : ');
|
||||||
@ -54,26 +65,26 @@ begin
|
|||||||
writeln ('ptop -g ofile');
|
writeln ('ptop -g ofile');
|
||||||
writeln (' generate default options file');
|
writeln (' generate default options file');
|
||||||
Writeln ('ptop -h : This help');
|
Writeln ('ptop -h : This help');
|
||||||
halt(0);
|
halt(Ecode);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Procedure Genopts;
|
Procedure TPToP.Genopts;
|
||||||
|
|
||||||
Var S : PBufStream;
|
Var S : TFileStream;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
S:=New(PBufStream,Init(ConfigFile,stCreate,255));
|
S:=TFileStream.Create(ConfigFile,fmCreate);
|
||||||
GeneratecfgFile(S);
|
Try
|
||||||
{$ifndef tp}
|
GeneratecfgFile(S);
|
||||||
S^.Close;
|
Finally
|
||||||
{$endif}
|
S.Free;
|
||||||
S^.Done;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Procedure ProcessOpts;
|
Procedure TPToP.ProcessOpts;
|
||||||
|
|
||||||
Var c : char;
|
|
||||||
|
|
||||||
|
Var
|
||||||
|
S : String;
|
||||||
begin
|
begin
|
||||||
{ Set defaults }
|
{ Set defaults }
|
||||||
Infilename:='';
|
Infilename:='';
|
||||||
@ -81,113 +92,103 @@ begin
|
|||||||
ConfigFile:='';
|
ConfigFile:='';
|
||||||
TheIndent:=2;
|
TheIndent:=2;
|
||||||
TheBufSize:=255;
|
TheBufSize:=255;
|
||||||
TheLineSize:=MaxLineSize;
|
TheLineSize:=DefLineSize;
|
||||||
BeVerbose:=False;
|
BeVerbose:=False;
|
||||||
Repeat
|
S:=CheckOptions('icglbhv','');
|
||||||
c:=getopt('i:c:g:l:b:hv');
|
If (S<>'') then
|
||||||
case c of
|
|
||||||
'i' : begin
|
|
||||||
TheIndent:=StrToInt(OptArg);
|
|
||||||
If TheIndent=0 then TheIndent:=2;
|
|
||||||
end;
|
|
||||||
'b' : begin
|
|
||||||
TheBufSize:=StrToInt(OptArg);
|
|
||||||
If TheBufSize=0 then TheBufSize:=255;
|
|
||||||
end;
|
|
||||||
'c' : ConfigFile:=OptArg;
|
|
||||||
'l' : begin
|
|
||||||
TheLineSize:=StrToInt(OptArg);
|
|
||||||
If TheLineSIze=0 Then TheLineSize:=MaxLineSize;
|
|
||||||
end;
|
|
||||||
'g' : begin
|
|
||||||
ConfigFIle:=OptArg;
|
|
||||||
GenOpts;
|
|
||||||
halt(0);
|
|
||||||
end;
|
|
||||||
'h' : usage;
|
|
||||||
'v' : BeVerbose:=True;
|
|
||||||
else
|
|
||||||
end;
|
|
||||||
until c=endofoptions;
|
|
||||||
If optind<=paramcount then
|
|
||||||
begin
|
begin
|
||||||
InFileName:=paramstr(OptInd);
|
Writeln(stderr,S);
|
||||||
Inc(optind);
|
Usage(1);
|
||||||
If OptInd<=paramcount then
|
end;
|
||||||
OutFilename:=Paramstr(OptInd);
|
if HasOption('h') then
|
||||||
|
usage(0);
|
||||||
|
TheIndent:=StrToIntDef(GetOptionValue('i',''),2);
|
||||||
|
TheBufSize:=StrToIntDef(GetOptionValue('b',''),255);
|
||||||
|
TheLineSize:=StrToIntDef(GetOptionValue('l',''),DefLineSize);
|
||||||
|
If HasOption('g') then
|
||||||
|
begin
|
||||||
|
ConfigFile:=GetOptionValue('g','');
|
||||||
|
GenOpts;
|
||||||
|
halt(0);
|
||||||
|
end;
|
||||||
|
ConfigFile:=GetOptionValue('c','');
|
||||||
|
BeVerbose:=HasOption('v');
|
||||||
|
If (ParamCount>1) then
|
||||||
|
begin
|
||||||
|
InFileName:=paramstr(ParamCount-1);
|
||||||
|
OutFilename:=Paramstr(ParamCount);
|
||||||
end;
|
end;
|
||||||
end; { Of ProcessOpts }
|
end; { Of ProcessOpts }
|
||||||
|
|
||||||
Var DiagS : PMemoryStream;
|
Procedure TPToP.DoRun;
|
||||||
InS,OutS,cfgS : PBufSTream;
|
|
||||||
PPrinter : TPrettyPrinter;
|
|
||||||
P : Pchar;
|
|
||||||
i : longint;
|
|
||||||
|
|
||||||
|
|
||||||
Procedure StreamErrorProcedure(Var S: TStream);{$ifndef fpc}FAR;{$endif}
|
|
||||||
Begin
|
|
||||||
If S.Status = StError then
|
|
||||||
WriteLn('ERROR: General Access failure. Halting');
|
|
||||||
If S.Status = StInitError then
|
|
||||||
WriteLn('ERROR: Cannot Init Stream. Halting. ');
|
|
||||||
If S.Status = StReadError then
|
|
||||||
WriteLn('ERROR: Read beyond end of Stream. Halting');
|
|
||||||
If S.Status = StWriteError then
|
|
||||||
WriteLn('ERROR: Cannot expand Stream. Halting');
|
|
||||||
If S.Status = StGetError then
|
|
||||||
WriteLn('ERROR: Get of Unregistered type. Halting');
|
|
||||||
If S.Status = StPutError then
|
|
||||||
WriteLn('ERROR: Put of Unregistered type. Halting');
|
|
||||||
end;
|
|
||||||
|
|
||||||
|
Var
|
||||||
|
F,InS,OutS,cfgS : TSTream;
|
||||||
|
PPrinter : TPrettyPrinter;
|
||||||
|
P : String;
|
||||||
|
i : longint;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
StreamError:=@StreamErrorProcedure;
|
|
||||||
ProcessOpts;
|
ProcessOpts;
|
||||||
if BeVerbose then
|
if BeVerbose then
|
||||||
begin
|
begin
|
||||||
writeln(Title+' '+Version);
|
writeln(Title+' '+Version);
|
||||||
writeln(Copyright);
|
writeln(Copyright);
|
||||||
Writeln;
|
Writeln;
|
||||||
end;
|
end;
|
||||||
If (Length(InfileName)=0) or (Length(OutFileName)=0) Then
|
If (Length(InfileName)=0) or (Length(OutFileName)=0) Then
|
||||||
Usage;
|
|
||||||
Ins:=New(PBufStream,Init(InFileName,StopenRead,TheBufSize));
|
|
||||||
OutS:=New(PBufStream,Init(OutFileName,StCreate,TheBufSize));
|
|
||||||
If BeVerbose then
|
|
||||||
diagS:=New(PMemoryStream,Init(1000,255))
|
|
||||||
else
|
|
||||||
DiagS:=Nil;
|
|
||||||
If ConfigFile<>'' then
|
|
||||||
CfgS:=New(PBufStream,Init(ConfigFile,StOpenRead,TheBufSize))
|
|
||||||
else
|
|
||||||
CfgS:=Nil;
|
|
||||||
PPrinter.Create;
|
|
||||||
PPrinter.Indent:=TheIndent;
|
|
||||||
PPrinter.LineSize:=TheLineSize;
|
|
||||||
PPrinter.Ins:=Ins;
|
|
||||||
PPrinter.outS:=OutS;
|
|
||||||
PPrinter.cfgS:=CfgS;
|
|
||||||
PPrinter.DiagS:=DiagS;
|
|
||||||
PPrinter.PrettyPrint;
|
|
||||||
If Assigned(DiagS) then
|
|
||||||
begin
|
begin
|
||||||
I:=DiagS^.GetSize;
|
Writeln(stderr,SErrNoInputOutput);
|
||||||
DiagS^.Seek(0);
|
Usage(1);
|
||||||
getmem (P,I+1);
|
end;
|
||||||
DiagS^.Read(P[0],I);
|
Ins:=TMemoryStream.Create;
|
||||||
P[I]:=#0;
|
try
|
||||||
{$ifndef tp}
|
F:=TFileStream.Create(InFileName,fmOpenRead);
|
||||||
Writeln (stderr,P);
|
Try
|
||||||
Flush(stderr);
|
Ins.CopyFrom(F,0);
|
||||||
{$else}
|
Ins.Position:=0;
|
||||||
Writeln (P);
|
Finally
|
||||||
{$endif}
|
F.Free;
|
||||||
DiagS^.Done;
|
end;
|
||||||
|
OutS:=TwriteBufStream.Create(TFileStream.Create(OutFileName,fmCreate));
|
||||||
|
Try
|
||||||
|
If ConfigFile<>'' then
|
||||||
|
CfgS:=TFileStream.Create(ConfigFile,fmOpenRead)
|
||||||
|
else
|
||||||
|
CfgS:=Nil;
|
||||||
|
try
|
||||||
|
PPrinter:=TPrettyPrinter.Create;
|
||||||
|
Try
|
||||||
|
PPrinter.Indent:=TheIndent;
|
||||||
|
PPrinter.LineSize:=TheLineSize;
|
||||||
|
PPrinter.Source:=Ins;
|
||||||
|
PPrinter.Dest:=OutS;
|
||||||
|
PPrinter.Config:=CfgS;
|
||||||
|
If BeVerbose then
|
||||||
|
PPrinter.OnVerbose:=@DoVerbose;
|
||||||
|
PPrinter.PrettyPrint;
|
||||||
|
Finally
|
||||||
|
FreeAndNil(PPrinter);
|
||||||
|
end;
|
||||||
|
Finally
|
||||||
|
FreeAndNil(CfgS);
|
||||||
|
end;
|
||||||
|
Finally
|
||||||
|
FreeAndNil(OutS);
|
||||||
|
end;
|
||||||
|
Finally
|
||||||
|
FreeAndNil(Ins);
|
||||||
|
end;
|
||||||
|
Terminate;
|
||||||
|
end;
|
||||||
|
|
||||||
|
begin
|
||||||
|
With TPToP.Create(Nil) do
|
||||||
|
Try
|
||||||
|
StopOnException:=True;
|
||||||
|
Initialize;
|
||||||
|
Run;
|
||||||
|
Finally
|
||||||
|
Free;
|
||||||
end;
|
end;
|
||||||
If Assigned(CfgS) then
|
|
||||||
CfgS^.Done;
|
|
||||||
Ins^.Done;
|
|
||||||
OutS^.Done;
|
|
||||||
end.
|
end.
|
||||||
|
752
utils/ptopu.pp
752
utils/ptopu.pp
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user