home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / STRTOK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  2.6 KB  |  89 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - strtok.c
  3.  *
  4.  * function(s)
  5.  *        strtok - searches one string for tokens, which are
  6.  *                 separated by delimiters defined in a second string
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*
  10.  *      C/C++ Run Time Library - Version 5.0
  11.  *
  12.  *      Copyright (c) 1987, 1992 by Borland International
  13.  *      All Rights Reserved.
  14.  *
  15.  */
  16.  
  17.  
  18. #include <string.h>
  19. #include <RtlData.h>
  20.  
  21. #if !defined( _RTLDLL )
  22. static  char *Ss;
  23. #endif
  24.  
  25. /*---------------------------------------------------------------------*
  26.  
  27. Name            strtok - searches one string for tokens, which are
  28.                          separated by delimiters defined in a second string
  29.  
  30. Usage           char *strtok(char *str1, const char *str2);
  31.  
  32. Prototype in    string.h
  33.  
  34. Description     strtok considers the string str1 to consist of a sequence of
  35.                 zero or more text tokens, separated by spans of one or more
  36.                 characters from the separator string str2.
  37.  
  38.                 The first call to strtok returns a pointer to the first
  39.                 character of the first token in str1 and writes a null character
  40.                 into str1 immediately following the returned token. Subsequent
  41.                 calls with NULL for the first argument will work through the
  42.                 string str1 in this way until no tokens remain.
  43.  
  44.                 The separator string, str2, may be different from call to
  45.                 call.
  46.  
  47. Return value    pointer to the scanned token.  When no tokens remain in str1,
  48.                 strtok returns a NULL pointer.
  49.  
  50. *---------------------------------------------------------------------*/
  51. #if defined(__FARFUNCS__)
  52. #include <_farfunc.h>
  53. #endif
  54.  
  55. char * _CType _FARFUNC strtok(char *s1, const char *s2)
  56. {
  57.     register const char *sp;
  58.     char *tok;
  59.     _QRTLDataBlock;
  60.  
  61.     if (s1) _QRTLInstanceData(Ss) = (char *)s1;
  62.  
  63.     /* First skip separators */
  64.  
  65.     while (*_QRTLInstanceData(Ss))
  66.     {
  67.         for (sp = s2; *sp; sp++)
  68.             if (*sp == *_QRTLInstanceData(Ss))
  69.                 break;
  70.         if (*sp == 0)
  71.             break;
  72.         _QRTLInstanceData(Ss)++;
  73.     }
  74.     if (*_QRTLInstanceData(Ss) == 0)
  75.         return (0);
  76.     tok = _QRTLInstanceData(Ss);
  77.     while (*_QRTLInstanceData(Ss))
  78.     {
  79.         for (sp = s2; *sp; sp++)
  80.             if (*sp == *_QRTLInstanceData(Ss))
  81.             {
  82.                 *_QRTLInstanceData(Ss)++ = 0;
  83.                 return (tok);
  84.             }
  85.         _QRTLInstanceData(Ss)++;
  86.     }
  87.     return (tok);
  88. }
  89.