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

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