0 Members and 1 Guest are viewing this topic.
{ usage: parse_cmd(cmd: string; limit: byte) : array of string; cmd: the string with the command to parse (ex: /cmd arg1 arg2 "arg3 stillArg3") limit: number of arguments to return (the command entry (usually /xxx) is not included in the count). So if you want command plus two arguments, pass the number 2, not 3. -1 means to return as many arguments as given. var parsed := array of string; { simple split; -1 as limit parameter means no limit to how many parameters will be returned } { [ '/login', 'myname', 'mypass' ] } parsed := parse_cmd('/login myname mypass', -1); { quotes around text makes it a single argument } { [ '/login', 'my name', 'mypass' ] } parsed := parse_cmd('/login "my name" mypass', -1); { multiple quoted strings allowed } { [ '/login', 'my name', 'my pass' ] } parsed := parse_cmd('/login "my name" "my pass"', -1); { to get a double quote, you need to be within quotes and then double the quote } { [ '/login', 'myname', 'pass"with"quotes' ] } parsed := parse_cmd('/login myname "pass""with""quotes"', -1); { adding a limit parameter returns exactly that number of command arguments } { [ '/login', 'myname', 'my pass with spaces' ] } parsed := parse_cmd('/login myname my pass with spaces', 2); { about as complicated as it gets } { [ '/quote', '"To be, or not to be".', 'That was his question' ] } parsed := parse_cmd('/quote """To be, or not to be""." "That was his question"', -1); { if we ask for more arguments than are given in command, we get empty args back } { [ '/login', 'user', 'pass', '', '', '', '' ] } parsed := parse_cmd('/login user pass', 6);}function parse_cmd (cmd: string; limit: shortint): array of string; var args: array of string; step, arg, cur, next: string; len: byte; begin limit := limit + 1; {offsets cmd} step := 'new'; while Length(cmd) > 0 do begin cur := cmd[1]; Delete(cmd, 1, 1); if Length(cmd) > 0 then next := cmd[1] else next := ''; if step = 'new' then begin if cur = ' ' then continue else if cur = '"' then step := 'quoted' else begin arg := arg + cur; step := 'unquoted'; end; end else if step = 'quoted' then begin if cur = '"' then begin if next = '"' then step := 'quotedquote' else begin SetArrayLength(args, len + 1); args[len] := arg; len := len + 1; arg := ''; step := 'new'; end; end else arg := arg + cur; end else if step = 'quotedquote' then begin arg := arg + cur; step := 'quoted'; end else if step = 'unquoted' then begin if cur = '"' then begin SetArrayLength(args, len + 1); args[len] := arg; len := len + 1; arg := ''; step := 'new'; cmd := '"' + cmd; end else if (cur = ' ') and ( (limit <> len + 1) or (StrPos('"', cmd) > 0) ) then begin SetArrayLength(args, len + 1); args[len] := arg; len := len + 1; arg := ''; step := 'new'; end else arg := arg + cur; end; end; if (arg <> '') or (step = 'quoted') then begin SetArrayLength(args, len + 1); args[len] := arg; len := len + 1; end; while len < limit do begin SetArrayLength(args, len + 1); args[len] := ''; len := len + 1; end; if len = 0 then begin SetArrayLength(args, 1); args[0] := ''; end; Result := args; end;
Can't you just use GetPiece and split at spaces or commas?