home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / strtok.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  4KB  |  115 lines

  1. /***
  2. *strtok.c - tokenize a string with given delimiters
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines strtok() - breaks string into series of token
  8. *       via repeated calls.
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <string.h>
  14. #ifdef _MT
  15. #include <mtdll.h>
  16. #endif  /* _MT */
  17.  
  18. /***
  19. *char *strtok(string, control) - tokenize string with delimiter in control
  20. *
  21. *Purpose:
  22. *       strtok considers the string to consist of a sequence of zero or more
  23. *       text tokens separated by spans of one or more control chars. the first
  24. *       call, with string specified, returns a pointer to the first char of the
  25. *       first token, and will write a null char into string immediately
  26. *       following the returned token. subsequent calls with zero for the first
  27. *       argument (string) will work thru the string until no tokens remain. the
  28. *       control string may be different from call to call. when no tokens remain
  29. *       in string a NULL pointer is returned. remember the control chars with a
  30. *       bit map, one bit per ascii char. the null char is always a control char.
  31. *
  32. *Entry:
  33. *       char *string - string to tokenize, or NULL to get next token
  34. *       char *control - string of characters to use as delimiters
  35. *
  36. *Exit:
  37. *       returns pointer to first token in string, or if string
  38. *       was NULL, to next token
  39. *       returns NULL when no more tokens remain.
  40. *
  41. *Uses:
  42. *
  43. *Exceptions:
  44. *
  45. *******************************************************************************/
  46.  
  47. char * __cdecl strtok (
  48.         char * string,
  49.         const char * control
  50.         )
  51. {
  52.         unsigned char *str;
  53.         const unsigned char *ctrl = control;
  54.  
  55.         unsigned char map[32];
  56.         int count;
  57.  
  58. #ifdef _MT
  59.         _ptiddata ptd = _getptd();
  60. #else  /* _MT */
  61.         static char *nextoken;
  62. #endif  /* _MT */
  63.  
  64.         /* Clear control map */
  65.         for (count = 0; count < 32; count++)
  66.                 map[count] = 0;
  67.  
  68.         /* Set bits in delimiter table */
  69.         do {
  70.                 map[*ctrl >> 3] |= (1 << (*ctrl & 7));
  71.         } while (*ctrl++);
  72.  
  73.         /* Initialize str. If string is NULL, set str to the saved
  74.          * pointer (i.e., continue breaking tokens out of the string
  75.          * from the last strtok call) */
  76.         if (string)
  77.                 str = string;
  78.         else
  79. #ifdef _MT
  80.                 str = ptd->_token;
  81. #else  /* _MT */
  82.                 str = nextoken;
  83. #endif  /* _MT */
  84.  
  85.         /* Find beginning of token (skip over leading delimiters). Note that
  86.          * there is no token iff this loop sets str to point to the terminal
  87.          * null (*str == '\0') */
  88.         while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
  89.                 str++;
  90.  
  91.         string = str;
  92.  
  93.         /* Find the end of the token. If it is not the end of the string,
  94.          * put a null there. */
  95.         for ( ; *str ; str++ )
  96.                 if ( map[*str >> 3] & (1 << (*str & 7)) ) {
  97.                         *str++ = '\0';
  98.                         break;
  99.                 }
  100.  
  101.         /* Update nextoken (or the corresponding field in the per-thread data
  102.          * structure */
  103. #ifdef _MT
  104.         ptd->_token = str;
  105. #else  /* _MT */
  106.         nextoken = str;
  107. #endif  /* _MT */
  108.  
  109.         /* Determine if a token has been found. */
  110.         if ( string == str )
  111.                 return NULL;
  112.         else
  113.                 return string;
  114. }
  115.