Board index » delphi » --> Rank Newbie Question <--

--> Rank Newbie Question <--

Greetings one and all.

I've been tasked with creating a program that sends information
from a given machine to a client on a windows box. I was given
a copy of Delphi 5 to do this with.

Now I'm a Unix based C and C++ programmer by trade and have
little experience with Windows in general and Delphi in
particular.

But I like what I see. Maybe this Windows stuff isn't so bad after all.

Nah.  [g]

Anyway, I'm getting info over a socket and am using one of the
network objects (TDayTime or something like that, as I recall.
It's part of the stock distribution in Delphi 5). Point that
object at a different port and have the Unix machine spit the
information back. That part works dandy. Heck, it even looks
cool with the font I'm using.

The question is:

Where can I find info on how to parse this? I know what each of
the individual fields means (I should, anyway, since I wrote the
daemon on the Unix side), it's just writing a parser, converting
fields from strings to long integers and back again, and the like.

Any recommendations for online resources?

Thanks, and if this is not the proper newsgroup for this, a quick
pointer and a shove in the right direction would be greatly appreciated!

--
2,036,571,428,571.4286:

   Amount of prohibited airspace that the president of the United States
carries around with him, wherever he goes (in cubic feet).

 

Re:--> Rank Newbie Question <--


Quote
"Roger Lee" <r...@thranduil.org> wrote in message

news:slrn94hj5j.bcd.raj@thranduil.org...

Quote

> The question is:

> Where can I find info on how to parse this? I know what each of
> the individual fields means (I should, anyway, since I wrote the
> daemon on the Unix side), it's just writing a parser, converting
> fields from strings to long integers and back again, and the like.

> Any recommendations for online resources?

> Thanks, and if this is not the proper newsgroup for this, a quick
> pointer and a shove in the right direction would be greatly appreciated!

Don't know where to refer you for online help. My first step would be
to check out the help on string manipulation and conversion functions.
Look for IntToStr, StrToInt etc.

Charles Appel
Bona Na Croin

Re:--> Rank Newbie Question <--


Quote
On Thu, 28 Dec 2000 19:41:44 -0500, Charles Appel <charlesap...@mindspring.com> wrote:
>"Roger Lee" <r...@thranduil.org> wrote in message
>news:slrn94hj5j.bcd.raj@thranduil.org...

[I sez:]

Quote
>> Thanks, and if this is not the proper newsgroup for this, a quick
>> pointer and a shove in the right direction would be greatly appreciated!

>Don't know where to refer you for online help. My first step would be
>to check out the help on string manipulation and conversion functions.
>Look for IntToStr, StrToInt etc.

OK, that's definitely a good start. But let me tell you what I'm doing and what
I'm trying to do with it:

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.

Thanks!
   Roger

--
2,036,571,428,571.4286:

   Amount of prohibited airspace that the president of the United States
carries around with him, wherever he goes (in cubic feet).

Re:--> Rank Newbie Question <--


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

Quote
}

  cTmp := cOriginalString;
  cMnt := StrToken(cTmp, '|');
  nTotalDiskSpace := StrToInt(StrToken(cTmp, '|'));
  nFreeDiskSpace := StrToInt(StrToken(cTmp, '|'));
end;

I hope this helps!

-Luke Croteau

Re:--> Rank Newbie Question <--


In article <slrn94pgml.8ui....@thranduil.org>, r...@thranduil.org (Roger Lee)
writes:

Quote
>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.

If (as it seems from your example) the elements are delimited (in your example
by a "|") then Delphi's TStringList will help you.

var
  SL : TStringList;
  Str :string;
i : integer;
begin
  Str = '/mnt/point|553245552|4020335|/mnt/point2|52355029|23552211' ;  // your
input string
  SL := TStringList.Create;
  for i := 1 to Length(Str) do
    if Str[i] = '|' then
      Str[i] := #13;  // change the delimiter to a carriage return
  SL.Text := Str;
// now your string has been split into separate strings inSL

ShowMessage('Mount Point : ' + SL.Strings[0] + #13 +
                 'Total Disk Space : ' + SL.Strings[1] . . . . etc etc  );

SL.Free;  // don't forget to free the object's resources as you've created it

As to presentation as is, TStringGrid would enable you to show a grid of the
values (headings on the left in the first column, values in the second column).
If you used that, then you could dump the values you've got into the second
column with . . .

  StringGrid1.Cols[1] := SL;

Alan Lloyd
alangll...@aol.com

Re:--> Rank Newbie Question <--


In article <20001231023321.19648.00004...@nso-fp.aol.com>,
  alangll...@aol.com (AlanGLLoyd) wrote:

Quote
> In article <slrn94pgml.8ui....@thranduil.org>, r...@thranduil.org
(Roger Lee)
> writes:

> >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.

> If (as it seems from your example) the elements are delimited (in
your example
> by a "|") then Delphi's TStringList will help you.

You Could also try StringReplace, just like below

Ronald Broeders

var
  SL : TStringList;
begin
  SL := TStringList.Create;
  SL.Text := StringReplace
('/mnt/point|553245552|4020335|/mnt/point2|52355029|23552211','|',#13,
[rfReplaceAll]) ;  // your input string
// now your string has been split into separate strings inSL

ShowMessage('Mount Point : ' + SL.Strings[0] + #13 +
                 'Total Disk Space : ' + SL.Strings[1] . . . . etc
etc  );

SL.Free;  // don't forget to free the object's resources as you've
created it

Quote

> As to presentation as is, TStringGrid would enable you to show a grid
of the
> values (headings on the left in the first column, values in the
second column).
> If you used that, then you could dump the values you've got into the
second
> column with . . .

>   StringGrid1.Cols[1] := SL;

> Alan Lloyd
> alangll...@aol.com

Sent via Deja.com
http://www.deja.com/

Other Threads