25f530ea232f34a4c0f2a92ca398ee15ee7e9a2d
[reactos.git] / lib / msvcrt / wstring / wcstok.c
1 #include <msvcrt/string.h>
2 #include <msvcrt/internal/tls.h>
3
4 wchar_t** _wlasttoken(); /* wlasttok.c */
5
6 /*
7  * @implemented
8  */
9 wchar_t *wcstok(wchar_t *s, const wchar_t *ct)
10 {
11         const wchar_t *spanp;
12         int c, sc;
13         wchar_t *tok;
14 #if 1
15         wchar_t ** wlasttoken = _wlasttoken();
16 #else
17         PTHREADDATA ThreadData = GetThreadData();
18         wchar_t ** wlasttoken = &ThreadData->wlasttoken;
19 #endif
20
21         if (s == NULL && (s = *wlasttoken) == NULL)
22                 return (NULL);
23
24         /*
25          * Skip (span) leading ctiters (s += strspn(s, ct), sort of).
26          */
27         cont:
28         c = *s;
29         s++;
30         for (spanp = ct; (sc = *spanp) != 0;spanp++) {
31                 if (c == sc)
32                         goto cont;
33         }
34
35         if (c == 0) {                   /* no non-ctiter characters */
36                 *wlasttoken = NULL;
37                 return (NULL);
38         }
39         tok = s - 2;
40
41         /*
42          * Scan token (scan for ctiters: s += strcspn(s, ct), sort of).
43          * Note that ct must have one NUL; we stop if we see that, too.
44          */
45         for (;;) {
46                 c = *s;
47                 s+=2;
48                 spanp = ct;
49                 do {
50                         if ((sc = *spanp) == c) {
51                                 if (c == 0)
52                                         s = NULL;
53                                 else
54                                         s[-1] = 0;
55                                 *wlasttoken = s;
56                                 return (tok);
57                         }
58                         spanp+=2;
59                 } while (sc != 0);
60         }
61         /* NOTREACHED */
62 }