Added binary and unicode file i/o support to msvcrt.
[reactos.git] / reactos / lib / crtdll / mbstring / mbstok.c
1 #include <msvcrt/mbstring.h>
2
3 unsigned char * _mbstok(unsigned char *s, unsigned char *delim)
4 {
5 const char *spanp;
6 int c, sc;
7 char *tok;
8 static char *last;
9
10
11 if (s == NULL && (s = last) == NULL)
12 return (NULL);
13
14 /*
15 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
16 */
17 cont:
18 c = *s;
19 s = _mbsinc(s);
20
21 for (spanp = delim; (sc = *spanp) != 0; spanp = _mbsinc(spanp)) {
22 if (c == sc)
23 goto cont;
24 }
25
26 if (c == 0) { /* no non-delimiter characters */
27 last = NULL;
28 return (NULL);
29 }
30 tok = s - 1;
31
32 /*
33 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
34 * Note that delim must have one NUL; we stop if we see that, too.
35 */
36 for (;;) {
37 c = *s;
38 s = _mbsinc(s);
39 spanp = delim;
40 do {
41 if ((sc = *spanp) == c) {
42 if (c == 0)
43 s = NULL;
44 else
45 s[-1] = 0;
46 last = s;
47 return (tok);
48 }
49 spanp = _mbsinc(spanp);
50 } while (sc != 0);
51 }
52 /* NOTREACHED */
53 }