Leaked source code of windows server 2003
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.

90 lines
2.8 KiB

  1. /***
  2. *strtokex.c - tokenize a string with given delimiters
  3. *
  4. * Copyright (c) 1989-1993, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines strtok() - breaks string into series of token
  8. * via repeated calls.
  9. *
  10. *******************************************************************************/
  11. #if defined(unix)
  12. #define __cdecl
  13. #endif
  14. #include <windows.h>
  15. #include <string.h>
  16. /***
  17. *char *StrTokEx(pstring, control) - tokenize string with delimiter in control
  18. *
  19. *Purpose:
  20. * StrTokEx considers the string to consist of a sequence of zero or more
  21. * text tokens separated by spans of one or more control chars. the first
  22. * call, with string specified, returns a pointer to the first char of the
  23. * first token, and will write a null char into pstring immediately
  24. * following the returned token. when no tokens remain
  25. * in pstring a NULL pointer is returned. remember the control chars with a
  26. * bit map, one bit per ascii char. the null char is always a control char.
  27. *
  28. *Entry:
  29. * char **pstring - ptr to ptr to string to tokenize
  30. * char *control - string of characters to use as delimiters
  31. *
  32. *Exit:
  33. * returns pointer to first token in string,
  34. * returns NULL when no more tokens remain.
  35. * pstring points to the beginning of the next token.
  36. *
  37. *WARNING!!!
  38. * upon exit, the first delimiter in the input string will be replaced with '\0'
  39. *
  40. *******************************************************************************/
  41. char * __cdecl StrTokEx (char ** pstring, const char * control)
  42. {
  43. unsigned char *psz;
  44. const unsigned char *pszCtrl = (const unsigned char *)control;
  45. unsigned char map[32] = {0};
  46. LPSTR pszToken;
  47. if(*pstring == NULL)
  48. return NULL;
  49. /* Set bits in delimiter table */
  50. do
  51. {
  52. map[*pszCtrl >> 3] |= (1 << (*pszCtrl & 7));
  53. }
  54. while (*pszCtrl++);
  55. /* Initialize str. */
  56. psz = (unsigned char*)*pstring;
  57. /* Find beginning of token (skip over leading delimiters). Note that
  58. * there is no token if this loop sets str to point to the terminal
  59. * null (*str == '\0') */
  60. while ((map[*psz >> 3] & (1 << (*psz & 7))) && *psz)
  61. psz++;
  62. pszToken = (LPSTR)psz;
  63. /* Find the end of the token. If it is not the end of the string,
  64. * put a null there. */
  65. for (; *psz ; psz++)
  66. {
  67. if (map[*psz >> 3] & (1 << (*psz & 7)))
  68. {
  69. *psz++ = '\0';
  70. break;
  71. }
  72. }
  73. /* string now points to beginning of next token */
  74. *pstring = (LPSTR)psz;
  75. /* Determine if a token has been found. */
  76. if (pszToken == (LPSTR)psz)
  77. return NULL;
  78. else
  79. return pszToken;
  80. }