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.

93 lines
2.9 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 <string.h>
  15. /***
  16. *char *StrTokEx(pstring, control) - tokenize string with delimiter in control
  17. *
  18. *Purpose:
  19. * StrTokEx considers the string to consist of a sequence of zero or more
  20. * text tokens separated by spans of one or more control chars. the first
  21. * call, with string specified, returns a pointer to the first char of the
  22. * first token, and will write a null char into pstring immediately
  23. * following the returned token. when no tokens remain
  24. * in pstring a NULL pointer is returned. remember the control chars with a
  25. * bit map, one bit per ascii char. the null char is always a control char.
  26. *
  27. *Entry:
  28. * char **pstring - ptr to ptr to string to tokenize
  29. * char *control - string of characters to use as delimiters
  30. *
  31. *Exit:
  32. * returns pointer to first token in string,
  33. * returns NULL when no more tokens remain.
  34. * pstring points to the beginning of the next token.
  35. *
  36. *WARNING!!!
  37. * upon exit, the first delimiter in the input string will be replaced with '\0'
  38. *
  39. *******************************************************************************/
  40. char * __cdecl StrTokEx (char ** pstring, const char * control)
  41. {
  42. /*unsigned*/ char *str;
  43. const /*unsigned*/ char *ctrl = control;
  44. unsigned char map[32];
  45. int count;
  46. char *tokenstr;
  47. if(*pstring == NULL)
  48. return NULL;
  49. /* Clear control map */
  50. for (count = 0; count < 32; count++)
  51. map[count] = 0;
  52. /* Set bits in delimiter table */
  53. do
  54. {
  55. map[*ctrl >> 3] |= (1 << (*ctrl & 7));
  56. } while (*ctrl++);
  57. /* Initialize str. */
  58. str = *pstring;
  59. /* Find beginning of token (skip over leading delimiters). Note that
  60. * there is no token if this loop sets str to point to the terminal
  61. * null (*str == '\0') */
  62. while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
  63. str++;
  64. tokenstr = str;
  65. /* Find the end of the token. If it is not the end of the string,
  66. * put a null there. */
  67. for ( ; *str ; str++ )
  68. {
  69. if ( map[*str >> 3] & (1 << (*str & 7)) )
  70. {
  71. *str++ = '\0';
  72. break;
  73. }
  74. }
  75. /* string now points to beginning of next token */
  76. *pstring = str;
  77. /* Determine if a token has been found. */
  78. if ( tokenstr == str )
  79. return NULL;
  80. else
  81. return tokenstr;
  82. }