Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.1 KiB

  1. /***
  2. *wcstok.c - tokenize a wide-character string with given delimiters
  3. *
  4. * Copyright (c) 1989-2001, 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. *Revision History:
  11. * 09-09-91 ETC Created from strtok.c.
  12. * 08-17-92 KRS Activate multithread support.
  13. * 02-17-93 GJF Changed for new _getptd().
  14. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  15. * 09-27-93 CFW Remove Cruiser support.
  16. * 09-29-93 GJF Replaced MTHREAD with _MT.
  17. * 02-07-94 CFW POSIXify.
  18. * 09-06-94 CFW Replace MTHREAD with _MT (again).
  19. *
  20. *******************************************************************************/
  21. #ifndef _POSIX_
  22. #include <cruntime.h>
  23. #include <string.h>
  24. #ifdef _MT
  25. #include <mtdll.h>
  26. #endif
  27. /***
  28. *wchar_t *wcstok(string, control) - tokenize string with delimiter in control
  29. * (wide-characters)
  30. *
  31. *Purpose:
  32. * wcstok considers the string to consist of a sequence of zero or more
  33. * text tokens separated by spans of one or more control chars. the first
  34. * call, with string specified, returns a pointer to the first wchar_t of
  35. * the first token, and will write a null wchar_t into string immediately
  36. * following the returned token. subsequent calls with zero for the first
  37. * argument (string) will work thru the string until no tokens remain. the
  38. * control string may be different from call to call. when no tokens remain
  39. * in string a NULL pointer is returned. remember the control chars with a
  40. * bit map, one bit per wchar_t. the null wchar_t is always a control char
  41. * (wide-characters).
  42. *
  43. *Entry:
  44. * wchar_t *string - wchar_t string to tokenize, or NULL to get next token
  45. * wchar_t *control - wchar_t string of characters to use as delimiters
  46. *
  47. *Exit:
  48. * returns pointer to first token in string, or if string
  49. * was NULL, to next token
  50. * returns NULL when no more tokens remain.
  51. *
  52. *Uses:
  53. *
  54. *Exceptions:
  55. *
  56. *******************************************************************************/
  57. wchar_t * __cdecl wcstok (
  58. wchar_t * string,
  59. const wchar_t * control
  60. )
  61. {
  62. wchar_t *token;
  63. const wchar_t *ctl;
  64. #ifdef _MT
  65. _ptiddata ptd = _getptd();
  66. #else
  67. static wchar_t *nextoken;
  68. #endif
  69. /* If string==NULL, continue with previous string */
  70. if (!string)
  71. #ifdef _MT
  72. string = ptd->_wtoken;
  73. #else
  74. string = nextoken;
  75. #endif
  76. /* Find beginning of token (skip over leading delimiters). Note that
  77. * there is no token iff this loop sets string to point to the terminal
  78. * null (*string == '\0') */
  79. while (*string) {
  80. for (ctl=control; *ctl && *ctl != *string; ctl++)
  81. ;
  82. if (!*ctl) break;
  83. string++;
  84. }
  85. token = string;
  86. /* Find the end of the token. If it is not the end of the string,
  87. * put a null there. */
  88. for ( ; *string ; string++ ) {
  89. for (ctl=control; *ctl && *ctl != *string; ctl++)
  90. ;
  91. if (*ctl) {
  92. *string++ = '\0';
  93. break;
  94. }
  95. }
  96. /* Update nextoken (or the corresponding field in the per-thread data
  97. * structure */
  98. #ifdef _MT
  99. ptd->_wtoken = string;
  100. #else
  101. nextoken = string;
  102. #endif
  103. /* Determine if a token has been found. */
  104. if ( token == string )
  105. return NULL;
  106. else
  107. return token;
  108. }
  109. #endif /* _POSIX_ */