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.

152 lines
4.1 KiB

  1. /***
  2. *mbstok.c - Break string into tokens (MBCS)
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Break string into tokens (MBCS)
  8. *
  9. *Revision History:
  10. * 11-19-92 KRS Ported from 16-bit sources.
  11. * 12-04-92 KRS Added MTHREAD support.
  12. * 02-17-93 GJF Changed for new _getptd().
  13. * 07-14-93 KRS Fix: all references should be to _mtoken, not _token.
  14. * 09-27-93 CFW Remove Cruiser support.
  15. * 10-06-93 GJF Replaced _CRTAPI1 with __cdecl, MTHREAD with _MT.
  16. * 04-15-93 CFW Add _MB_CP_LOCK.
  17. * 05-09-94 CFW Optimize for SBCS.
  18. * 05-19-94 CFW Enable non-Win32.
  19. * 09-11-97 GJF Replaced __mbcodepage == 0 with _ISNOTMBCP.
  20. * 04-21-98 GJF Revised multithread support based on threadmbcinfo
  21. * structs
  22. *
  23. *******************************************************************************/
  24. #ifdef _MBCS
  25. #include <mtdll.h>
  26. #include <cruntime.h>
  27. #include <string.h>
  28. #include <mbdata.h>
  29. #include <mbctype.h>
  30. #include <mbstring.h>
  31. #include <stddef.h>
  32. /***
  33. * _mbstok - Break string into tokens (MBCS)
  34. *
  35. *Purpose:
  36. * strtok considers the string to consist of a sequence of zero or more
  37. * text tokens separated by spans of one or more control chars. the first
  38. * call, with string specified, returns a pointer to the first char of the
  39. * first token, and will write a null char into string immediately
  40. * following the returned token. subsequent calls with zero for the first
  41. * argument (string) will work thru the string until no tokens remain. the
  42. * control string may be different from call to call. when no tokens remain
  43. * in string a NULL pointer is returned. remember the control chars with a
  44. * bit map, one bit per ascii char. the null char is always a control char.
  45. *
  46. * MBCS chars supported correctly.
  47. *
  48. *Entry:
  49. * char *string = string to break into tokens.
  50. * char *sepset = set of characters to use as seperators
  51. *
  52. *Exit:
  53. * returns pointer to token, or NULL if no more tokens
  54. *
  55. *Exceptions:
  56. *
  57. *******************************************************************************/
  58. unsigned char * __cdecl _mbstok(
  59. unsigned char * string,
  60. const unsigned char * sepset
  61. )
  62. {
  63. unsigned char *nextsep;
  64. #ifdef _MT
  65. _ptiddata ptd = _getptd();
  66. unsigned char *nextoken;
  67. pthreadmbcinfo ptmbci;
  68. if ( (ptmbci = ptd->ptmbcinfo) != __ptmbcinfo )
  69. ptmbci = __updatetmbcinfo();
  70. if ( _ISNOTMBCP_MT(ptmbci) )
  71. #else /* _MT */
  72. static unsigned char *nextoken;
  73. if ( _ISNOTMBCP )
  74. #endif /* _MT */
  75. return strtok(string, sepset);
  76. /* init start of scan */
  77. if (string)
  78. nextoken = string;
  79. else
  80. /* If string==NULL, continue with previous string */
  81. {
  82. #ifdef _MT
  83. nextoken = ptd->_mtoken;
  84. #endif /* _MT */
  85. if (!nextoken)
  86. return NULL;
  87. }
  88. /* skip over lead seperators */
  89. #ifdef _MT
  90. if ( (string = __mbsspnp_mt(ptmbci, nextoken, sepset)) == NULL )
  91. #else
  92. if ( (string = _mbsspnp(nextoken, sepset)) == NULL )
  93. #endif
  94. return(NULL);
  95. /* test for end of string */
  96. if ( (*string == '\0') ||
  97. #ifdef _MT
  98. ((__ismbblead_mt(ptmbci, *string)) && (string[1] == '\0')) )
  99. #else
  100. ((_ismbblead(*string)) && (string[1] == '\0')) )
  101. #endif
  102. return(NULL);
  103. /* find next seperator */
  104. #ifdef _MT
  105. nextsep = __mbspbrk_mt(ptmbci, string, sepset);
  106. #else
  107. nextsep = _mbspbrk(string, sepset);
  108. #endif
  109. if ((nextsep == NULL) || (*nextsep == '\0'))
  110. nextoken = NULL;
  111. else {
  112. #ifdef _MT
  113. if ( __ismbblead_mt(ptmbci, *nextsep) )
  114. #else
  115. if ( _ismbblead(*nextsep) )
  116. #endif
  117. *nextsep++ = '\0';
  118. *nextsep = '\0';
  119. nextoken = ++nextsep;
  120. }
  121. #ifdef _MT
  122. /* Update the corresponding field in the per-thread data * structure */
  123. ptd->_mtoken = nextoken;
  124. #endif /* _MT */
  125. return(string);
  126. }
  127. #endif /* _MBCS */