Skip to content

Commit 3ffe7cb

Browse files
authored
Merge pull request #16 from corneliusdavid/main
fixed parameter validation for Delphi
2 parents 47c0d1c + caf5f8b commit 3ffe7cb

1 file changed

Lines changed: 121 additions & 94 deletions

File tree

generator/Delphi/src/generator.dpr

Lines changed: 121 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type
1818
private
1919
FGenerator: TGenerator;
2020
FParams: TStringList;
21+
function CheckShortParams(const AParam: char): Boolean;
22+
function CheckLongParams(const AParam: string): Boolean;
2123
protected
2224
function ParseConsoleParams: boolean;
2325
procedure Run;
@@ -58,6 +60,34 @@ begin
5860
end;
5961
end;
6062

63+
function TOneBRCGenerator.CheckLongParams(const AParam: string): Boolean;
64+
var
65+
J: Integer;
66+
begin
67+
for J := 0 to Pred(Length(cLongOptions)) do
68+
begin
69+
if (AParam = cLongOptions[J]) then
70+
begin
71+
Result := True;
72+
Break;
73+
end;
74+
end;
75+
end;
76+
77+
function TOneBRCGenerator.CheckShortParams(const AParam: char): Boolean;
78+
var
79+
J: Integer;
80+
begin
81+
for J := 0 to Pred(Length(cShortOptions)) do
82+
begin
83+
if (AParam = cShortOptions[J]) then
84+
begin
85+
Result := True;
86+
Break;
87+
end;
88+
end;
89+
end;
90+
6191
destructor TOneBRCGenerator.Destroy;
6292
begin
6393
if Assigned(FParams) then
@@ -69,8 +99,8 @@ function TOneBRCGenerator.ParseConsoleParams: boolean;
6999
var
70100
I, J, invalid, valid: Integer;
71101
tmpLineCount: String;
72-
paramflag: boolean;
73-
templist: TStringList;
102+
ParamOK: Boolean;
103+
SkipNext: Boolean;
74104
begin
75105
Result := false;
76106
// initialize the params list
@@ -98,121 +128,118 @@ begin
98128
// parsing
99129
// ************************************
100130
// check for invalid input
101-
templist := TStringList.Create;
102-
try
103-
templist.Text := FParams.Text;
104-
if templist.Count > 0 then
131+
if FParams.Count > 0 then
132+
begin
133+
SkipNext := False;
134+
for I := 0 to FParams.Count - 1 do
105135
begin
106-
for I := 0 to pred(Length(cShortOptions)) do
136+
if SkipNext then
107137
begin
108-
J := templist.IndexOfName(cShortOptions[I]);
109-
if J > -1 then
110-
templist.Delete(J);
138+
SkipNext := False;
139+
Continue;
111140
end;
112141

113-
for I := 0 to pred(Length(cLongOptions)) do
114-
begin
115-
J := templist.IndexOfName(cLongOptions[I]);
116-
if J > -1 then
117-
templist.Delete(J);
118-
end;
142+
if (Length(FParams[I]) = 1) or (FParams[I][2] = '=') then
143+
ParamOK := CheckShortParams(FParams[I][1])
144+
else
145+
ParamOK := CheckLongParams(Copy(FParams[I], 1, Pos('=', FParams[I]) - 1));
119146

120-
if templist.Count > 0 then
121-
begin
122-
WriteLn(Format(rsErrorMessage, [templist.Text]));
123-
Result := false;
124-
exit;
125-
end;
147+
// if we found a bad parameter, don't need to check the rest of them
148+
if not ParamOK then
149+
Break;
150+
end;
126151

127-
end
128-
else
152+
if not ParamOK then
129153
begin
154+
WriteLn(Format(rsErrorMessage, [FParams.CommaText]));
130155
Result := false;
131156
exit;
132157
end;
158+
end
159+
else
160+
begin
161+
Result := false;
162+
exit;
163+
end;
133164

134-
// ************************************
135-
// check for valid inputs
136-
// check help
137-
if (FParams.Find(cShortOptHelp, J) or FParams.Find(cLongOptHelp, J)) then
138-
begin
139-
WriteHelp;
140-
inc(invalid);
141-
end;
165+
// ************************************
166+
// check for valid inputs
167+
// check help
168+
if (FParams.Find(cShortOptHelp, J) or FParams.Find(cLongOptHelp, J)) then
169+
begin
170+
WriteHelp;
171+
inc(invalid);
172+
end;
142173

143-
// check version
144-
if (FParams.Find(cShortOptVersion, J) or FParams.Find(cLongOptVersion, J)) then
145-
begin
146-
WriteLn(Format(rsGeneratorVersion, [cVersion]));
147-
inc(invalid);
148-
end;
174+
// check version
175+
if (FParams.Find(cShortOptVersion, J) or FParams.Find(cLongOptVersion, J)) then
176+
begin
177+
WriteLn(Format(rsGeneratorVersion, [cVersion]));
178+
inc(invalid);
179+
end;
149180

150-
// check inputfilename
151-
J := -1;
152-
J := FParams.IndexOfName(cShortOptInput);
153-
if J = -1 then
154-
J := FParams.IndexOfName(cLongOptInput);
155-
if J = -1 then
156-
begin
157-
WriteLn(Format(rsErrorMessage, [rsMissingInputFlag]));
158-
inc(invalid);
159-
end
160-
else
161-
begin
162-
inputFilename := FParams.ValueFromIndex[J];
163-
inc(valid);
164-
end;
181+
// check inputfilename
182+
J := -1;
183+
J := FParams.IndexOfName(cShortOptInput);
184+
if J = -1 then
185+
J := FParams.IndexOfName(cLongOptInput);
186+
if J = -1 then
187+
begin
188+
WriteLn(Format(rsErrorMessage, [rsMissingInputFlag]));
189+
inc(invalid);
190+
end
191+
else
192+
begin
193+
inputFilename := FParams.ValueFromIndex[J];
194+
inc(valid);
195+
end;
196+
197+
// check outputfilename
198+
J := -1;
199+
J := FParams.IndexOfName(cShortOptOutput);
200+
if J = -1 then
201+
J := FParams.IndexOfName(cLongOptOutput);
202+
if J = -1 then
203+
begin
204+
WriteLn(Format(rsErrorMessage, [rsMissingOutputFlag]));
205+
inc(invalid);
206+
end
207+
else
208+
begin
209+
outputFilename := FParams.ValueFromIndex[J];
210+
inc(valid);
211+
end;
212+
213+
// check linecount
214+
J := -1;
215+
J := FParams.IndexOfName(cShortOptNumber);
216+
if J = -1 then
217+
J := FParams.IndexOfName(cLongOptNumber);
218+
if J = -1 then
219+
begin
220+
WriteLn(Format(rsErrorMessage, [rsMissingLineCountFlag]));
221+
inc(invalid);
222+
end
223+
else
224+
begin
225+
tmpLineCount := FParams.ValueFromIndex[J].Replace('_', '', [rfReplaceAll]);
165226

166-
// check outputfilename
167-
J := -1;
168-
J := FParams.IndexOfName(cShortOptOutput);
169-
if J = -1 then
170-
J := FParams.IndexOfName(cLongOptOutput);
171-
if J = -1 then
227+
if not TryStrToInt(tmpLineCount, lineCount) then
172228
begin
173-
WriteLn(Format(rsErrorMessage, [rsMissingOutputFlag]));
229+
WriteLn(Format(rsInvalidInteger, [tmpLineCount]));
174230
inc(invalid);
175-
end
176-
else
177-
begin
178-
outputFilename := FParams.ValueFromIndex[J];
179-
inc(valid);
180231
end;
181232

182-
// check linecount
183-
J := -1;
184-
J := FParams.IndexOfName(cShortOptNumber);
185-
if J = -1 then
186-
J := FParams.IndexOfName(cLongOptNumber);
187-
if J = -1 then
233+
if not(lineCount > 0) then
188234
begin
189-
WriteLn(Format(rsErrorMessage, [rsMissingLineCountFlag]));
235+
WriteLn(Format(rsErrorMessage, [rsInvalidLineNumber]));
190236
inc(invalid);
191-
end
192-
else
193-
begin
194-
tmpLineCount := FParams.ValueFromIndex[J].Replace('_', '', [rfReplaceAll]);
195-
196-
if not TryStrToInt(tmpLineCount, lineCount) then
197-
begin
198-
WriteLn(Format(rsInvalidInteger, [tmpLineCount]));
199-
inc(invalid);
200-
end;
201-
202-
if not(lineCount > 0) then
203-
begin
204-
WriteLn(Format(rsErrorMessage, [rsInvalidLineNumber]));
205-
inc(invalid);
206-
end;
207-
inc(valid);
208237
end;
209-
210-
// check if everything was provided
211-
Result := valid = 3;
212-
finally
213-
templist.Free;
238+
inc(valid);
214239
end;
215240

241+
// check if everything was provided
242+
Result := valid = 3;
216243
end;
217244

218245
var

0 commit comments

Comments
 (0)