home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / strtok / strtok.pas next >
Encoding:
Pascal/Delphi Source File  |  1995-12-22  |  2.0 KB  |  82 lines

  1. ========
  2. Newsgroups: comp.lang.pascal.delphi.misc
  3. Subject: Re: String Tokenizer for Delphi...
  4. From: Dave Cameron <davec@mstone.demon.co.uk>
  5. Date: Tue, 29 Aug 95 09:35:58 GMT
  6.  
  7. In article <40ss6q$gt5@allnews.infi.net>
  8.            RobinS@mailhost.richmond.infi.net "RobinS" writes:
  9.  
  10. > Anyone have code, component, or advice on how to achieve the strTok
  11. > functionality in Turbo Pascal?
  12.  
  13. No guarantees, but this seemed to do the job:-
  14.  
  15. ---------------------------Tear Here----------------------------------
  16. { Separate tokens in NULL-terminated ANSI string ---------------------- }
  17. { This is an untested version of the 'C' 'StrTok' function for the ---- }
  18. { Windows ANSI character set (should work with 2-byte character codes)  }
  19.  
  20. var
  21.   AnsiTokPtr: PChar;
  22.  
  23. function AnsiTok( Src: PChar; Delim: PChar ) : PChar;
  24. var
  25.   TokStart: PChar;
  26.   P: PChar;
  27.  
  28. function IsDelim( C: char ) : Bool;
  29. var
  30.   D: PChar;
  31.   res: Bool;
  32.  
  33. begin
  34.   D := Delim;
  35.   res := FALSE;
  36.   repeat
  37.   begin
  38.     if( C = D^ ) then
  39.       res := TRUE;
  40.     D := AnsiNext( D );
  41.   end;
  42.   until( ( D^ = NULL ) or res );
  43.   IsDelim := res;
  44. end;
  45.  
  46. begin
  47.   if( Src <> nil ) then
  48.     AnsiTokPtr := Src;
  49.  
  50. { Initialise running pointer }
  51.   P := AnsiTokPtr;
  52.  
  53. { Scan for start of token }
  54.   while( IsDelim( P^ ) ) do
  55.     P := AnsiNext( P );
  56.   if( P^ = NULL ) then
  57.     TokStart := nil
  58.   else
  59.     TokStart := P;
  60.  
  61. { Scan for end of token }
  62.   while( ( P^ <> NULL ) and not IsDelim( P^ ) ) do
  63.     P := AnsiNext( P );
  64.  
  65. { Update the token string pointer and terminate the token string }
  66.   AnsiTokPtr := AnsiNext( P );
  67.   P^ := NULL;
  68.  
  69. { Return pointer to token }
  70.   AnsiTok := TokStart;
  71.  
  72. end;
  73.  
  74.  
  75. ---------------------------Tear Here----------------------------------
  76.  
  77. -- 
  78. Dave Cameron
  79. ======================================================================
  80. Milestone Systems Ltd, Avonbridge, Scotland.  davec@mstone.demon.co.uk
  81. ======================================================================
  82.