1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
{$mode objfpc}
{$H+}
Program PtoP;
{
This file is part of the Free Pascal run time library.
Copyright (c) 1999-2002 by Michael Van Canneyt, member of
the Free Pascal development team
Pascal pretty print program
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
Uses SysUtils,Classes,PtoPu,CustApp, bufstream;
ResourceString
Version = 'Version 1.2';
ATitle = '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;
Procedure TPToP.DoVerbose(Sender : TObject; Const Msg : String);
begin
Writeln(StdErr,Msg);
end;
Procedure TPToP.Usage(ECode : Word);
begin
Writeln ('ptop : Usage : ');
Writeln ('ptop [-v] [-i indent] [-b bufsize ][-c optsfile][-l linesize] infile outfile');
Writeln (' converts infile to outfile.');
Writeln (' -c : read options from optsfile');
Writeln (' -i : Set number of indent spaces.');
Writeln (' -l : Set maximum output linesize.');
Writeln (' -b : Use buffers of size bufsize');
Writeln (' -v : be verbose');
writeln ('ptop -g ofile');
writeln (' generate default options file');
Writeln ('ptop -h : This help');
halt(Ecode);
end;
Procedure TPToP.Genopts;
Var S : TFileStream;
begin
S:=TFileStream.Create(ConfigFile,fmCreate);
Try
GeneratecfgFile(S);
Finally
S.Free;
end;
end;
Procedure TPToP.ProcessOpts;
Var
S : String;
begin
{ Set defaults }
Infilename:='';
OutFileName:='';
ConfigFile:='';
TheIndent:=2;
TheBufSize:=255;
TheLineSize:=DefLineSize;
BeVerbose:=False;
S:=CheckOptions('icglbhv','');
If (S<>'') then
begin
Writeln(stderr,S);
Usage(1);
end;
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; { Of ProcessOpts }
Procedure TPToP.DoRun;
Var
F,InS,OutS,cfgS : TSTream;
PPrinter : TPrettyPrinter;
begin
ProcessOpts;
if BeVerbose then
begin
writeln(Title+' '+Version);
writeln(Copyright);
Writeln;
end;
If (Length(InfileName)=0) or (Length(OutFileName)=0) Then
begin
Writeln(stderr,SErrNoInputOutput);
Usage(1);
end;
Ins:=TMemoryStream.Create;
try
F:=TFileStream.Create(InFileName,fmOpenRead);
Try
Ins.CopyFrom(F,0);
Ins.Position:=0;
Finally
F.Free;
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
Title:= ATitle;
StopOnException:=True;
Initialize;
Run;
Finally
Free;
end;
end.
|