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