- Make the NDK compatible with the MSDDK again.
[reactos.git] / reactos / ntoskrnl / rtl / strtok.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/rtl/strtok.c
6 * PURPOSE: Unicode and thread safe implementation of strtok
7 *
8 * PROGRAMMERS: David Welch (welch@mcmail.com)
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <ntoskrnl.h>
14 #include <internal/debug.h>
15
16
17 /* FUNCTIONS *****************************************************************/
18
19 char* strtok(char *s, const char *delim)
20 {
21 const char *spanp;
22 int c, sc;
23 char *tok;
24 static char *last;
25
26 if (s == NULL && (s = last) == NULL)
27 return (NULL);
28
29 /*
30 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
31 */
32 cont:
33 c = *s++;
34 for (spanp = delim; (sc = *spanp++) != 0;) {
35 if (c == sc)
36 goto cont;
37 }
38
39 if (c == 0) { /* no non-delimiter characters */
40 last = NULL;
41 return (NULL);
42 }
43 tok = s - 1;
44
45 /*
46 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
47 * Note that delim must have one NUL; we stop if we see that, too.
48 */
49 for (;;) {
50 c = *s++;
51 spanp = delim;
52 do {
53 if ((sc = *spanp++) == c) {
54 if (c == 0)
55 s = NULL;
56 else
57 s[-1] = 0;
58 last = s;
59 return (tok);
60 }
61 } while (sc != 0);
62 }
63 /* NOTREACHED */
64 }
65
66 PWSTR RtlStrtok(PUNICODE_STRING _string, PWSTR _sep,
67 PWSTR* temp)
68 /*
69 * FUNCTION: Splits a string into tokens
70 * ARGUMENTS:
71 * string = string to operate on
72 * if NULL then continue with previous string
73 * sep = Token deliminators
74 * temp = Tempory storage provided by the caller
75 * ARGUMENTS: Returns the beginning of the next token
76 */
77 {
78 PWSTR string;
79 PWSTR sep;
80 PWSTR start;
81
82 if (_string!=NULL)
83 {
84 string = _string->Buffer;
85 }
86 else
87 {
88 string = *temp;
89 }
90
91 start = string;
92
93 while ((*string)!=0)
94 {
95 sep = _sep;
96 while ((*sep)!=0)
97 {
98 if ((*string)==(*sep))
99 {
100 *string=0;
101 *temp=string+1;
102 return(start);
103 }
104 sep++;
105 }
106 string++;
107 }
108 *temp=NULL;
109 return(start);
110 }