tcstok_s.inl 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // Copyright (c) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  4. //
  5. /***
  6. *tcstok_s.inl - general implementation of _tcstok_s
  7. *
  8. *
  9. *Purpose:
  10. * This file contains the general algorithm for strtok_s and its variants.
  11. *
  12. ****/
  13. _FUNC_PROLOGUE
  14. _CHAR * __cdecl _FUNC_NAME(_CHAR *_String, const _CHAR *_Control, _CHAR **_Context)
  15. {
  16. _CHAR *token;
  17. const _CHAR *ctl;
  18. /* validation section */
  19. _VALIDATE_POINTER_ERROR_RETURN(_Context, EINVAL, NULL);
  20. _VALIDATE_POINTER_ERROR_RETURN(_Control, EINVAL, NULL);
  21. _VALIDATE_CONDITION_ERROR_RETURN(_String != NULL || *_Context != NULL, EINVAL, NULL);
  22. /* If string==NULL, continue with previous string */
  23. if (!_String)
  24. {
  25. _String = *_Context;
  26. }
  27. /* Find beginning of token (skip over leading delimiters). Note that
  28. * there is no token iff this loop sets string to point to the terminal null. */
  29. for ( ; *_String != 0 ; _String++)
  30. {
  31. for (ctl = _Control; *ctl != 0 && *ctl != *_String; ctl++)
  32. ;
  33. if (*ctl == 0)
  34. {
  35. break;
  36. }
  37. }
  38. token = _String;
  39. /* Find the end of the token. If it is not the end of the string,
  40. * put a null there. */
  41. for ( ; *_String != 0 ; _String++)
  42. {
  43. for (ctl = _Control; *ctl != 0 && *ctl != *_String; ctl++)
  44. ;
  45. if (*ctl != 0)
  46. {
  47. *_String++ = 0;
  48. break;
  49. }
  50. }
  51. /* Update the context */
  52. *_Context = _String;
  53. /* Determine if a token has been found. */
  54. if (token == _String)
  55. {
  56. return NULL;
  57. }
  58. else
  59. {
  60. return token;
  61. }
  62. }