Repository navigation
Expand file tree
/
Copy pathMainUnit.pas
More file actions
279 lines (241 loc) · 6.76 KB
/
Copy pathMainUnit.pas
File metadata and controls
279 lines (241 loc) · 6.76 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
{
This file is part of the MWA Software Pascal API Code Generator for OpenSSL .
Copyright © MWA Software 2024
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
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.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see <https://www.gnu.org/licenses/>.
}
unit MainUnit;
{$IFDEF FPC}
{$mode Delphi}
{$ENDIF}
{$IFDEF MSWINDOWS}
{$DEFINE WINDOWS}
{$ENDIF}
interface
uses
Classes , SysUtils, {$IFDEF FPC}CustApp,{$ENDIF} GenerateHeaderUnit,
GenerateSmartLoad , GenerateJITUnit;
const
DefaultOutputPrefix = 'OpenSSL_';
DefaultCopyFilePrefix = '';
type
{$if not declared(TCustomApplication)}
{$DEFINE LOCAL_TCUSTOMAPP}
TCustomApplication = class(TComponent)
private
FTitle: string;
protected
procedure DoRun; virtual; abstract;
public
function Exename: string;
procedure Run;
procedure Terminate;
property Title: string read FTitle write FTitle;
end;
{$IFEND}
{ OpenSSLAPICodeGenerator }
OpenSSLAPICodeGenerator = class(TCustomApplication)
private
procedure WriteLine(line: string);
protected
procedure DoRun; override;
public
constructor Create(TheOwner : TComponent); override;
destructor Destroy; override;
procedure WriteHelp; virtual;
end;
implementation
resourcestring
RSOBadOutputDir = 'Output Directory (%s) does not exist!';
RSOUnrecognisedOption = 'Unrecognised option: %s';
RSONoGeneratorSelected = 'Go Unit file generator selected';
RSOLastArgument = 'Last argument must either be a valid directory or file path';
RSOFileNotFound = 'File not found - %s';
RSONoFixUps = 'WARNING: no uses clause fix ups';
{$IFDEF LOCAL_TCUSTOMAPP}
function TCustomApplication.Exename: string;
begin
Result := ParamStr(0);
end;
procedure TCustomApplication.Run;
begin
try
DoRun;
except on E: Exception do
writeln(E.Message);
end;
end;
procedure TCustomApplication.Terminate;
begin
end;
{$ENDIF}
{ OpenSSLAPICodeGenerator }
procedure OpenSSLAPICodeGenerator.WriteLine(line : string);
begin
writeln(line);
end;
procedure OpenSSLAPICodeGenerator.DoRun;
var
SmartGen: boolean;
JitGen: boolean;
OutputDir: string;
Source: string;
SourceIsDir: boolean;
i: integer;
OutputPrefix: string;
CopyFilePrefix: string;
Generator: TGenerateAPIUnit;
CopyFiles: TStrings;
begin
writeln('OpenSSL Pascal API Header Generator');
writeln('Copyright © MWA Software 2024');
SmartGen := true;
JitGen := false;
OutputDir := GetCurrentDir;
Source := '';
SourceIsDir := false;
OutputPrefix := DefaultOutputPrefix;
CopyFilePrefix := DefaultCopyFilePrefix;
CopyFiles := TStringList.Create;
try
try
// quick check parameters
i := 1;
while i <= ParamCount do
begin
if ParamStr(i) = '-h' then
begin
WriteHelp;
Terminate;
Exit;
end;
if (ParamStr(i) = '-s') or (ParamStr(i) = '--smart') then
SmartGen := true
else
if (ParamStr(i) = '-j') or (ParamStr(i) = '--jit') then
begin
SmartGen := false;
JitGen := true;
end
else
if (ParamStr(i) = '-p') or (ParamStr(i) = '--prefix') then
begin
Inc(i);
OutputPrefix := ParamStr(i);
end
else
if (ParamStr(i) = '-P') or (ParamStr(i) = '--includeFilePrefix') then
begin
Inc(i);
CopyFilePrefix := ParamStr(i);
end
else
if (ParamStr(i) = '-o') or (ParamStr(i) = '--output') then
begin
Inc(i);
OutputDir := ExcludeTrailingPathDelimiter(ParamStr(i));
if not DirectoryExists(OutputDir) and
not CreateDir(OutputDir) then
raise Exception.CreateFmt(RSOBadOutputDir,[OutputDir]);
end
else
if (ParamStr(i) = '-a') or (ParamStr(i) = '--includeFile') then
begin
Inc(i);
if not DirectoryExists(ParamStr(i)) and not FileExists(ParamStr(i)) then
raise Exception.CreateFmt(RSOFileNotFound,[ParamStr(i)]);
CopyFiles.Add(ParamStr(i));
end
else
if i = ParamCount then
begin
if DirectoryExists(ParamStr(i)) then
begin
Source := ExcludeTrailingPathDelimiter(ParamStr(i));
SourceIsDir := true;
end
else
if FileExists(ParamStr(i)) then
Source := ParamStr(i)
else
raise Exception.CreateFmt(RSOLastArgument,[ParamStr(i)]);
end
else
raise Exception.CreateFmt(RSOUnrecognisedOption,[ParamStr(i)]);
Inc(i);
end;
if Source = '' then
begin
WriteHelp;
Terminate;
Exit;
end;
if SmartGen then
Generator := TGenerateSmartLoadUnit.Create
else
if JitGen then
Generator := TGenerateJITUnit.Create
else
raise Exception.Create(RSONoGeneratorSelected);
Generator.UnitPrefix := OutputPrefix;
Generator.CopyFilePrefix := CopyFilePrefix;
try
Generator.WriteLine := WriteLine;
if SourceIsDir then
begin
Generator.GenerateHeaderUnitFromDir(Source,OutputDir,CopyFiles);
end
else
begin
writeln(RSONoFixUps);
Generator.GenerateHeaderUnitFrom(Source,OutputDir);
end;
finally
Generator.Free;
end;
except on E: Exception do
writeln('Error: ', E.Message);
end;
finally
CopyFiles.Free;
end;
{$IFDEF DEBUG}
{$IFDEF WINDOWS}
write('Press Enter to Exit');
readln;
{$ENDIF}
{$ENDIF}
Terminate;
end;
constructor OpenSSLAPICodeGenerator.Create(TheOwner : TComponent);
begin
inherited Create(TheOwner);
{$IFDEF FPC}
StopOnException := True;
{$ENDIF}
end;
destructor OpenSSLAPICodeGenerator.Destroy;
begin
inherited Destroy;
end;
procedure OpenSSLAPICodeGenerator.WriteHelp;
begin
writeln('Usage: ' , ExtractFileName(ExeName) , ' [-h] [--smart] [--jit] [-p <output unit prefix] '+
'[-a <include file(s)][-P <include file prefix>][-o <output directory>] <file or directory>');
writeln('Options:');
writeln('-h Help: outputs this message');
writeln('-s|--smart Generate smart load strategy (default)');
writeln('-j|--jit Generator Just In Time Load strategy');
writeln('-p|--prefix Output unit name prefix');
writeln('-P|--includedFilePrefix Include File (unitname) Prefix (overrides unit name prefix');
writeln('-a|--includeFiles includefile(s) source dir');
writeln;
end;
end.