const
Max = 50;
type
Tab= array[1..Max] of string;
procedure ClearTab(var T: Tab);
var
i: Integer;
begin
for i := 1 to Max do T[i] := '';
end;
procedure Split(S: String; var T: Tab; var Count: Integer); //or : ; out Count); if that syntax is allowed for you
{
Preconditions:
Comma (',') is used as separator
S is not allowed to end in a comma
Postconditions:
Count is set to the number of strings found
T[1]..T[Count] hold the respective strings
T[Count+1]..T[50] are empty strings
}
var
p: Integer;
begin
ClearTab(T);
Count := 1;
while S <> '' do
begin
p := Pos(',', S);
if p = 0 then
begin
T[Count] := S;
Break;
end;
T[Count] := copy(S,1,p-1);
Delete(S, 1, p);
Inc(Count);
end;
end;
function TryExtractNumberAndTypeHtmlFromTag(Tag: String; var Number: Integer; var typeHtml: String): Boolean;
{
Precondition:
Tag is in the form of "<for SomeID='SomeValue' type='comma,separated,values'/>"
SomeId cannot be the string 'type'
SomeID='SomeValue' is the first Key/Value pair inside the tag.
None of the comma-separated values itself can contain a single quote as part of the value
There will be no space before or after the equals sign after "SomeID" an d"type"
PostCondition:
The function returns True if both a number and a value for typeHtml are foud, otherwise it returns False
The Number is set to 'SomeValue'
The typeHtml is set to 'comma,separated,values'
}
var
p, ErrorCode: Integer;
NumberStr: String;
begin
Result := False;
typeHtml := '';
Number := 0;
if Length(Tag) < Length('<type=''''/>') then Exit;
if (Copy(Tag, 1,5) <> '<for ') and (Tag[Length(Tag)] <> '>') and (Tag[Length(Tag)-1] <> '/') then Exit;
Delete(Tag,1,5);
p := pos('=''', Tag); //also find the first single quote atfer the equals sign
if p = 0 then Exit;
Inc(p, Length('='''));
Delete(Tag, 1, p-1);
p := Pos('''', Tag);
if p = 0 then Exit;
NumberStr := Copy(Tag, 1, p-1);
Val(NumberStr, Number, ErrorCode);
if ErrorCode <> 0 then Exit;
p := Pos('type=''', Tag); //also find the first single quote atfer the equals sign
if p = 0 then Exit;
Inc(p, Length('type='''));
typeHtml := Copy(Tag, p, MaxInt);
p := Pos('''', typeHtml);
if p = 0 then Exit;
Delete(typeHtml, p, MaxInt);
Result := True;
end;
function TryExtractNumberAndTypeHtmlFromTagSimple(Tag: String; var Number: Integer; var typeHtml: String): Boolean;
{
Precondition:
Tag must be in the form of "<for it='SomeIntegerValue' type='comma,separated,values'/>"
None of the comma-separated values itself can contain a single quote as part of the value
There will be no space before or after any of the equals signs
PostCondition:
The function returns True if both a number and a value for typeHtml are foud, otherwise it returns False
The Number is set to 'SomeValue'
The typeHtml is set to 'comma,separated,values'
}
var
p, ErrorCode: Integer;
NumberStr: String;
const
ForLen = 9; //<for it='
TypeLen = 6; //type='
begin
Result := False;
typeHtml := '';
Number := 0;
p := Pos('<for it=''', Tag);
if p = 0 then Exit;
Delete(Tag,1,p + ForLen - 1);
p := Pos('''', Tag);
if p = 0 then Exit;
NumberStr := Copy(Tag, 1, p-1);
Val(NumberStr, Number, ErrorCode);
if ErrorCode <> 0 then Exit;
p := Pos('type=''', Tag); //also find the first single quote atfer the equals sign
if p = 0 then Exit;
typeHtml := Copy(Tag, p + TypeLen, MaxInt);
p := Pos('''', typeHtml);
if p = 0 then Exit;
Delete(typeHtml, p, MaxInt);
Result := True;
end;
procedure AskTags(var Tags: Tab; var NrOfTags: Integer);
var
Tag: String;
begin
ClearTab(Tags); //Clear the array
NrOfTags := 0;
repeat
write('Tag # ',NrOfTags+1,': (empty line quits input): ');
readln(Tag);
if Tag <> '' then
begin
Inc(NrOfTags);
Tags[NrOfTags] := Tag;
end;
until (Tag = '') or (NrOfTags = Max);
end;
var
typeHtml: String;
Tag: string = '<for it=''123'' type=''password,button,text'' hidden=''true''/>';
tType, Tags: tab;
i, j, NrOfTags, Count, Number: Integer;
begin
AskTags(Tags, NrOfTags);
for i := 1 to NrOfTags do
begin
if not TryExtractNumberAndTypeHtmlFromTagSimple(Tags[i], Number, typeHtml) then
begin
writeln('Cannot extract Number and typeHtml from "',Tags[i],'"');
Continue; //skip rest of the loop code, go to the next value of i
end;
writeln('Number = "',Number,'"');
writeln('typeHtml = "',typeHtml,'"');
Split(typeHtml, tType, Count);
writeln('Count = ',Count);
//write the seperate strings we found for this typeHtml
for j := 1 to Count do writeln(' "',tType[j],'"');
end;
end.