A: Here is a translation of the strtok function from the BC++ RTL:
function strtok(S, DivChars: PChar): PChar;
const NextS: PChar = nil;
var DivScan: PChar;
begin
Result:= nil;
if S <> nil then
NextS:= S;
if NextS = nil then Exit;
if DivChars = nil then Exit;
{ First skip separators }
while NextS^ <> #0 do begin
DivScan:= DivChars;
while DivScan^ <> #0 do begin
if DivScan^ = NextS^ then
Break; {NextS is a divider}
Inc(DivScan);
end;
if DivScan^ = #0 then Break;
{if NextS is not in DivChars, it is start of new token}
Inc(NextS);
end;
if NextS^ = #0 then Exit {no token
found before end of string, return nil}
Result:= NextS;
{Now look for end of token}
Inc(NextS);
while NextS^ <> #0 do begin
DivScan:= DIvChars;
while DivScan^ <> #0 do
if DivScan^ = NextS^ then
begin {found a divider, will become end of token}
NextS^:= #0;
{replace divider char with end of string}
Inc(NextS);
{point past end of string for next call}
Exit;
end;
Inc(DivScan);
end;
Inc(NextS);
{NextS^ not a divider, point to next char}
end;
{if we get here, we've reached the end of the string,
which will become the end of the
returned token}
end;
And here is a slightly more Pascal version of the same thing:
type TCharSet = set of char;
function strtok(S: PChar; const DivChars: TCharSet): PChar;
const NextS: PChar = nil;
begin
Result:= nil;
if S <> nil then
NextS:= S;
if NextS = nil then Exit;
if DivChars = [] then Exit;
{ First skip separators }
while NextS^ <> #0 do begin
if not (NextS^ in DivChars) then
Break;
{if NextS is not in DivChars, it is start of new token}
Inc(NextS);
end;
if NextS^ = #0 then Exit {no token
found before end of string, return nil}
Result:= NextS;
{Now look for end of token}
Inc(NextS);
while NextS^ <> #0 do begin
if NextS^ in DivChars then begin
{found a divider, will become end of token}
NextS^:= #0;
{replace divider char with end of string}
Inc(NextS);
{point past end of string for next call}
Exit;
end;
Inc(NextS);
{NextS^ not a divider, point to next char}
end;
{if we get here, we've reached the end of the string,
which will become the end of the
returned token}
end;