Quote
> I'm getting a string via a socket (currently using the TTimeDay component,
I
> believe). That's getting put into a string format. What I need to do is to
be
> able to parse that string into its component parts and adjust as
necessary. The
> format of the string, ferinstance is:
> /mnt/point|553245552|4020335|/mnt/point2|52355029|23552211 ...
> The first part is the unix mount point, the second is total disk space,
and the
> third is the available disk space. I need to know if there's a way of
breaking
> this up and massaging them. Once I get each bit into the appropriate
variable,
> I can then do what I need to do to extract the info my boss wants.
I wrote a unit a while back to handle the parsing of strings. Here you go.
:) Example is below the unit.
-----
unit Tokenizer;
interface
function StrToken( var S: String; Sep: Char ): String;
function TokenCount( const S: String; Sep: Char ): LongInt;
function Substr( sStr: String; iStart, iCount: LongInt) : String;
implementation
uses
SysUtils;
function TokenCount( const S: String; Sep: Char ): LongInt;
var
sTmp : String;
begin
Result := 0;
sTmp := S; { Make a copy, as StrToken() expects a var argument }
while StrToken( sTmp, Sep ) <> '' do
Inc( Result );
end;
function Substr( sStr: String; iStart, iCount: LongInt) : String;
begin
Result := '';
{ Is the starting position past the end of the string? If so,
handle the same way as Clipper - return a null string }
if (iStart > Length(sStr)) then
exit;
{ If zero iCount passed, they want the rest of string from iStart. If
a negative number is passed, we'll default to zero. }
if (iCount < 1) then
iCount := Length(sStr);
Result := Copy( sStr, iStart, iCount );
end;
function StrToken( var S: String; Sep: Char ): String;
var
iPos : Word;
begin
iPos := Pos( Sep, S );
if ( iPos <> 0 ) then
begin
Result := SubStr( S, 1, iPos - 1 );
S := SubStr( S, iPos + 1, 0 );
end
else
begin
Result := S;
S := '';
end;
end;
---
Beware! StrToken does modify the original string. So.. Given your example
above, we will write a function called ParseString..
var
cMnt :String;
nTotalDiskSpace,
nFreeDiskSpace :Integer;
procedure ParseString(cOriginalString :String);
var
cTmp :String;
begin
{
Assumed to work given the string:
/mnt/point|553245552|4020335|/mnt/point2|52355029|23552211
cTmp := cOriginalString;
cMnt := StrToken(cTmp, '|');
nTotalDiskSpace := StrToInt(StrToken(cTmp, '|'));
nFreeDiskSpace := StrToInt(StrToken(cTmp, '|'));
end;
I hope this helps!
-Luke Croteau