home *** CD-ROM | disk | FTP | other *** search
/ GCW Games & More & Wacky Windows Companion / gcw.iso / win / util / mygroups / cstr.pas < prev    next >
Pascal/Delphi Source File  |  1994-05-26  |  2KB  |  81 lines

  1. Unit CStr;
  2. {Various procedures for handling C style null terminated strings}
  3. Interface
  4.  
  5. Function StrTok(S,Delim:PChar):PChar;
  6. Function StrDelta(S1,S2:PChar):Word;
  7. Function StrVal(S:PChar; N:Word):Word;
  8.  
  9. Implementation
  10.  
  11. Uses WinProcs, Strings;
  12.  
  13. Const S1:PChar = Nil;
  14.  
  15. Function StrTok(S,Delim:PChar):PChar;
  16. {Implement the C function StrTok which breaks a string into a series
  17.  of token delimited substrings. This function modifies the source string.
  18.  Only one string can be scanned at a time.
  19.  Returns a pointer to the next substring or NIL if no more
  20.  
  21.  Input:  S - String to scan
  22.          Delim - Set of delimiters}
  23.  
  24. Var Len,I:Word;
  25.  
  26.     Begin
  27.    If S <> Nil then S1:=S;
  28.    Len:=StrLen(Delim);
  29.    StrTok:=S1;
  30.    If (S1 <> Nil) and (Delim <> Nil) and
  31.       (Len > 0) and (StrLen(S1) > 0) then
  32.       While S1[0] <> #00 do
  33.           Begin
  34.          For I:=0 to Len-1 do
  35.              If S1[0] = Delim[I] then
  36.                 Begin
  37.                S1[0]:=#00;
  38.                Inc(S1);
  39.                Exit;
  40.                End;
  41.          Inc(S1);
  42.          End;
  43.    End;
  44.  
  45. Function StrDelta(S1,S2:PChar):Word;
  46. {Computes the number of characters between two pointers to the same string.
  47.  Returns the difference between the two pointers.
  48.  
  49.  Input:  S1 - First pointer
  50.          S2 - Second pointer}
  51.  
  52.    Var L1:LongInt absolute S1;
  53.        L2:LongInt absolute S2;
  54.  
  55.    Begin
  56.    If HiWord(L1) <> HiWord(L2) then
  57.       StrDelta:=0       {Must point to same selector}
  58.    else
  59.       StrDelta:=Abs(LoWord(L2)-LoWord(L1));
  60.    End;
  61.  
  62. Function StrVal(S:PChar; N:Word):Word;
  63. {Similar to the Val function. Converts a sequence of numeric characters
  64.  into a Word variable. No error checking is performed.
  65.  Returns the value of the string.
  66.  
  67.  Input:  S - String to be converted
  68.          N - Number of characters to be converted (max 10)}
  69.  
  70. Var I,Result:Word;
  71.     Buf:Array [0..10] of Char;
  72.  
  73.    Begin
  74.    If N >= Sizeof(Buf) then N:=Sizeof(Buf)-1;
  75.    StrLCopy(Buf,S,N);
  76.    Val(Buf,I,Result);
  77.    StrVal:=I;
  78.    End;
  79.  
  80. Begin
  81. End.