ReactOS 0.4.16-dev-927-g467dec4
wcstok_s.cpp
Go to the documentation of this file.
1//
2// wcstok_s.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines wcstok_s(), which tokenizes a string via repeated calls. See strtok()
7// for more details. This more secure function uses a caller-provided context
8// instead of the thread-local tokenization state.
9//
11#include <string.h>
12
13
14
15// This common implementation is used by both strtok() and strtok_s()
17 _Inout_opt_z_ wchar_t* string,
18 _In_z_ wchar_t const* control,
20 )
21{
22 // If string is null, set the iterator to the saved pointer (i.e., continue
23 // breaking tokens out of the string from the last strtok call):
24 wchar_t* string_it = string != nullptr
25 ? string
26 : *context;
27
28
29 // Find beginning of token (skip over leading delimiters). Note that
30 // there is no token iff this loop sets string to point to the terminal
31 // null (*string == '\0')
32 while (*string_it)
33 {
34 wchar_t const* control_it = control;
35 for (; *control_it && *control_it != *string_it; ++control_it)
36 {
37 }
38
39 if (!*control_it)
40 break;
41
42 ++string_it;
43 }
44
45 wchar_t* const token_first = string_it;
46
47 // Find the end of the token. If it is not the end of the string, put a
48 // null character there:
49 for (; *string_it; ++string_it)
50 {
51 wchar_t const* control_it = control;
52 for (; *control_it && *control_it != *string_it; ++control_it)
53 {
54 }
55
56 if (*control_it)
57 {
58 *string_it++ = '\0';
59 break;
60 }
61 }
62
63 // Update the saved pointer:
64 *context = string_it;
65
66 // Determine if a token has been found.
67 return string_it != token_first ? token_first : nullptr;
68}
69
70
71
72extern "C" wchar_t* __cdecl wcstok_s(
73 wchar_t* const string,
74 wchar_t const* const control,
75 wchar_t** const context
76 )
77{
80 _VALIDATE_CONDITION_ERROR_RETURN(string != nullptr || *context != nullptr, EINVAL, nullptr);
81
83}
#define EINVAL
Definition: acclib.h:90
#define __cdecl
Definition: accygwin.h:79
#define _VALIDATE_CONDITION_ERROR_RETURN(_Condition, _ErrorCode, _Ret)
#define _VALIDATE_POINTER_ERROR_RETURN(_Pointer, _ErrorCode, _Ret)
char string[160]
Definition: util.h:11
#define _Deref_prepost_opt_z_
Definition: ms_sal.h:1424
#define _Inout_
Definition: no_sal2.h:162
#define _In_z_
Definition: no_sal2.h:164
#define _Inout_opt_z_
Definition: no_sal2.h:220
Definition: http.c:7252
Definition: dialog.c:52
wchar_t *__cdecl __acrt_wcstok_s_novalidation(_Inout_opt_z_ wchar_t *string, _In_z_ wchar_t const *control, _Inout_ _Deref_prepost_opt_z_ wchar_t **context)
Definition: wcstok_s.cpp:16
wchar_t *__cdecl wcstok_s(wchar_t *const string, wchar_t const *const control, wchar_t **const context)
Definition: wcstok_s.cpp:72