]> pd.if.org Git - pdclib/blob - functions/string/strtok.c
69b45a73cb8aeb0753395907425beed9643ce13f
[pdclib] / functions / string / strtok.c
1 // ----------------------------------------------------------------------------
2 // $Id$
3 // ----------------------------------------------------------------------------
4 // Public Domain C Library - http://pdclib.sourceforge.net
5 // This code is Public Domain. Use, modify, and redistribute at will.
6 // ----------------------------------------------------------------------------
7
8 char * strtok( char * restrict src, const char * restrict seperators )
9 {
10     static char * store = NULL;
11     size_t token_length;
12
13     if ( src != NULL )
14     {
15         // new string
16         store = src;
17     }
18     if ( store == NULL )
19     {
20         // no old string, no new string, nothing to do
21         return NULL;
22     }
23     src += strspn( src, seperators ); // skipping leading seperators
24     if ( strlen( src ) == 0 )
25     {
26         // no more to parse
27         return ( store = NULL );
28     }
29     token_length = strcspn( src, seperators );
30     if ( src[ token_length ] == '\0' )
31     {
32         // parsed to end of string
33         store = NULL;
34         return src;
35     }
36     src[ token_length ] = '\0';
37     store = src + token_length + 1;
38     return src;
39 }