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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
{
FPCRes - Free Pascal Resource Converter
Part of the Free Pascal distribution
Copyright (C) 2008 by Giulio Bernardi
Handles the parsing of parameters
See the file COPYING, 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.
}
unit paramparser;
{$MODE OBJFPC} {$H+}
interface
uses
Classes, SysUtils, target;
type
EParametersException = class(Exception);
EOutputFileAlreadySetException = class(EParametersException);
EUnknownParameterException = class(EParametersException);
EArgumentMissingException = class(EParametersException);
EUnknownObjFormatException = class(EParametersException);
EUnknownMachineException = class(EParametersException);
ECannotReadConfFile = class(EParametersException);
type
{ TParameters }
TParameters = class
private
fHelp : boolean;
fVersion : boolean;
fVerbose : boolean;
fInputFiles : TStringList;
fOutputFile : string;
fTarget : TResTarget;
procedure ParseInputFiles(aList : TStringList; var index : integer; const parname : string);
procedure ParseOutputFile(aList : TStringList; var index : integer; const parname : string);
procedure ParseOutputFormat(aList : TStringList; var index : integer; const parname : string);
procedure ParseArchitecture(aList : TStringList; var index : integer; const parname : string);
procedure ParseConfigFile(aList : TStringList; var index : integer; const parname : string);
function DoOptionalArgument(aList : TStringList; const i : integer) : string;
function DoMandatoryArgument(aList : TStringList; const i : integer) : string;
function IsParameter(const s : string) : boolean;
function ParamsToStrList : TStringList;
protected
public
constructor Create;
destructor Destroy; override;
procedure Parse;
property Help : boolean read fHelp;
property Version : boolean read fVersion;
property Verbose : boolean read fVerbose;
property InputFiles : TStringList read fInputFiles;
property OutputFile : string read fOutputFile write fOutputFile;
property Target : TResTarget read fTarget;
end;
implementation
uses
msghandler;
type
{ TConfFileParser }
TConfFileParser = class
private
fConfFile : TStringList;
fParList : TStringList;
fInsPos : integer;
procedure ParseLine(idx : integer);
function GetParameter(pc : pchar; var i : integer) : string;
function GetString(pc : pchar; var i : integer) : string;
protected
public
constructor Create(aFileName : string; aParList : TStringList; aInsPos : integer);
procedure Parse;
destructor Destroy; override;
end;
{ TConfFileParser }
procedure TConfFileParser.ParseLine(idx: integer);
var pc : pchar;
tmp : string;
i : integer;
begin
pc:=pchar(fConfFile[idx]);
i:=0;
while pc[i]<>#0 do
begin
case pc[i] of
' ',#9,#13,#10 : inc(i);
'#' : break
else
begin
tmp:=GetParameter(pc,i);
if tmp<>'' then
begin
fParList.Insert(fInsPos,tmp);
inc(fInsPos);
end;
end;
end;
end;
end;
function TConfFileParser.GetParameter(pc : pchar; var i : integer): string;
begin
Result:='';
while pc[i]<>#0 do
begin
case pc[i] of
' ',#9,#13,#10 : exit;
'#' : exit;
'"' : Result:=Result+GetString(pc,i);
else
Result:=Result+pc[i];
end;
inc(i);
end;
end;
function TConfFileParser.GetString(pc: pchar; var i: integer): string;
begin
Result:='';
inc(i);
while pc[i]<>#0 do
begin
if pc[i] = '"' then
exit
else
Result:=Result+pc[i];
inc(i);
end;
dec(i);
end;
constructor TConfFileParser.Create(aFileName: string; aParList: TStringList; aInsPos : integer);
begin
fInsPos:=aInsPos+1;
fConfFile:=TStringList.Create;
fParList:=aParList;
try
fConfFile.LoadFromFile(aFileName);
except
raise ECannotReadConfFile.Create(aFileName);
end;
end;
procedure TConfFileParser.Parse;
var i : integer;
begin
for i:=0 to fConfFile.Count-1 do
ParseLine(i);
end;
destructor TConfFileParser.Destroy;
begin
fConfFile.Free;
end;
{ TParameters }
//for compatibility allow -i <inputfiles>
procedure TParameters.ParseInputFiles(aList: TStringList; var index: integer;
const parname : string);
var tmp : string;
begin
tmp:=DoMandatoryArgument(aList,index+1);
if tmp='' then
raise EArgumentMissingException.Create(parname);
while tmp<>'' do
begin
inc(index);
fInputFiles.Add(tmp);
tmp:=DoOptionalArgument(aList,index+1);
end;
end;
procedure TParameters.ParseOutputFile(aList: TStringList; var index: integer;
const parname : string);
begin
if fOutputFile<>'' then
raise EOutputFileAlreadySetException.Create('');
inc(index);
fOutputFile:=DoMandatoryArgument(aList,index);
if fOutputFile='' then
raise EArgumentMissingException.Create(parname);
end;
procedure TParameters.ParseOutputFormat(aList: TStringList; var index: integer;
const parname: string);
var tmp : string;
aFormat : TObjFormat;
begin
inc(index);
tmp:=DoMandatoryArgument(aList,index);
if tmp='' then
raise EArgumentMissingException.Create(parname);
for aFormat:=low(TObjFormat) to high(TObjFormat) do
begin
if ObjFormats[aFormat].name=tmp then
begin
fTarget.objformat:=aFormat;
exit;
end;
end;
raise EUnknownObjFormatException.Create(tmp);
end;
procedure TParameters.ParseArchitecture(aList: TStringList; var index: integer;
const parname: string);
var tmp : string;
aMachine : TMachineType;
begin
inc(index);
tmp:=DoMandatoryArgument(aList,index);
if tmp='' then
raise EArgumentMissingException.Create(parname);
for aMachine:=low(TMachineType) to high(TMachineType) do
begin
if Machines[aMachine].name=tmp then
begin
fTarget.machine:=aMachine;
exit;
end;
end;
raise EUnknownMachineException.Create(tmp);
end;
procedure TParameters.ParseConfigFile(aList: TStringList; var index: integer;
const parname : string);
var tmp : string;
cp : TConfFileParser;
begin
tmp:=copy(parname,2,length(parname)-1);
if tmp='' then
raise EArgumentMissingException.Create(parname);
cp:=TConfFileParser.Create(tmp,aList,index);
try
cp.Parse;
finally
cp.Free;
end;
end;
function TParameters.DoOptionalArgument(aList: TStringList; const i: integer
): string;
begin
Result:='';
if aList.Count>i then
begin
if not IsParameter(aList[i]) then
Result:=aList[i];
end;
end;
function TParameters.DoMandatoryArgument(aList: TStringList; const i: integer
): string;
begin
Result:='';
if aList.count>i then
Result:=aList[i];
end;
function TParameters.IsParameter(const s: string): boolean;
begin
Result:=false;
if length(s)<=1 then exit;
if copy(s,1,1)='-' then Result:=true;
end;
function TParameters.ParamsToStrList: TStringList;
var i : integer;
begin
Result:=TStringList.Create;
try
for i:=1 to ParamCount do
Result.Add(ParamStr(i));
except
Result.Free;
raise;
end;
end;
procedure TParameters.Parse;
var fList : TStringList;
tmp : string;
i : integer;
begin
fList:=ParamsToStrList;
try
i:=0;
while i<fList.Count do
begin
tmp:=fList[i];
Messages.DoVerbose(Format('parsing parameter ''%s''',[tmp]));
if IsParameter(tmp) then
begin
if ((tmp='--help') or (tmp='-h') or (tmp='-?')) then
fHelp:=true
else if ((tmp='--version') or (tmp='-V')) then
fVersion:=true
else if ((tmp='--verbose') or (tmp='-v')) then
fVerbose:=true
else if ((tmp='-i') or (tmp='--input')) then
ParseInputFiles(fList,i,tmp)
else if ((tmp='-o') or (tmp='--output')) then
ParseOutputFile(fList,i,tmp)
else if (tmp='-of') then
ParseOutputFormat(fList,i,tmp)
else if ((tmp='-a') or (tmp='--arch')) then
ParseArchitecture(fList,i,tmp)
else
raise EUnknownParameterException.Create(tmp);
end
else
if copy(tmp,1,1)='@' then
ParseConfigFile(fList,i,tmp)
else
fInputFiles.Add(tmp); //assume it is an input file
inc(i);
end;
finally
fList.Free;
end;
end;
constructor TParameters.Create;
begin
fHelp:=false;
fVersion:=false;
fVerbose:=false;
fInputFiles:=TStringList.Create;
fOutputFile:='';
fTarget.machine:=mtnone;
fTarget.objformat:=ofnone;
end;
destructor TParameters.Destroy;
begin
fInputFiles.Free;
end;
end.
|