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

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