| 1 |
148 |
jeremybenn |
/*
|
| 2 |
|
|
FUNCTION
|
| 3 |
|
|
<<wcstok>>---get next token from a string
|
| 4 |
|
|
|
| 5 |
|
|
INDEX
|
| 6 |
|
|
wcstok
|
| 7 |
|
|
|
| 8 |
|
|
|
| 9 |
|
|
ANSI_SYNOPSIS
|
| 10 |
|
|
#include <wchar.h>
|
| 11 |
|
|
wchar_t *wcstok(wchar_t *<[source]>, const wchar_t *<[delimiters]>,
|
| 12 |
|
|
wchar_t **<[lasts]>)
|
| 13 |
|
|
|
| 14 |
|
|
TRAD_SYNOPSIS
|
| 15 |
|
|
#include <wchar.h>
|
| 16 |
|
|
wchar_t *wcstok(<[source]>, <[delimiters]>, <[lasts]>)
|
| 17 |
|
|
wchar_t *<[source]>;
|
| 18 |
|
|
wchar_t *<[delimiters]>;
|
| 19 |
|
|
wchar_t **<[lasts]>;
|
| 20 |
|
|
|
| 21 |
|
|
DESCRIPTION
|
| 22 |
|
|
The <<wcstok>> function is the wide-character equivalent of the
|
| 23 |
|
|
<<strtok_r>> function (which in turn is the same as the <<strtok>>
|
| 24 |
|
|
function with an added argument to make it thread-safe).
|
| 25 |
|
|
|
| 26 |
|
|
The <<wcstok>> function is used to isolate (one at a time)
|
| 27 |
|
|
sequential tokens in a null-terminated wide-character string,
|
| 28 |
|
|
<<*<[source]>>>. A token is defined as a substring not containing
|
| 29 |
|
|
any wide-characters from <<*<[delimiters]>>>.
|
| 30 |
|
|
|
| 31 |
|
|
The first time that <<wcstok>> is called, <<*<[source]>>> should
|
| 32 |
|
|
be specified with the wide-character string to be searched, and
|
| 33 |
|
|
<<*<[lasts]>>>--but not <<lasts>>, which must be non-NULL--may be
|
| 34 |
|
|
random; subsequent calls, wishing to obtain further tokens from
|
| 35 |
|
|
the same string, should pass a null pointer for <<*<[source]>>>
|
| 36 |
|
|
instead but must supply <<*<[lasts]>>> unchanged from the last
|
| 37 |
|
|
call. The separator wide-character string, <<*<[delimiters]>>>,
|
| 38 |
|
|
must be supplied each time and may change between calls.
|
| 39 |
|
|
A pointer to placeholder <<*<[lasts]>>> must be supplied by
|
| 40 |
|
|
the caller, and is set each time as needed to save the state
|
| 41 |
|
|
by <<wcstok>>. Every call to <<wcstok>> with <<*<[source]>>>
|
| 42 |
|
|
== <<NULL>> must pass the value of <<*<[lasts]>>> as last set
|
| 43 |
|
|
by <<wcstok>>.
|
| 44 |
|
|
|
| 45 |
|
|
The <<wcstok>> function returns a pointer to the beginning of each
|
| 46 |
|
|
subsequent token in the string, after replacing the separator
|
| 47 |
|
|
wide-character itself with a null wide-character. When no more tokens
|
| 48 |
|
|
remain, a null pointer is returned.
|
| 49 |
|
|
|
| 50 |
|
|
RETURNS
|
| 51 |
|
|
<<wcstok>> returns a pointer to the first wide character of a token, or
|
| 52 |
|
|
<<NULL>> if there is no token.
|
| 53 |
|
|
|
| 54 |
|
|
NOTES
|
| 55 |
|
|
<<wcstok>> is thread-safe (unlike <<strtok>>, but like <<strtok_r>>).
|
| 56 |
|
|
<<wcstok>> writes into the string being searched.
|
| 57 |
|
|
|
| 58 |
|
|
PORTABILITY
|
| 59 |
|
|
<<wcstok>> is C99 and POSIX.1-2001.
|
| 60 |
|
|
|
| 61 |
|
|
<<wcstok>> requires no supporting OS subroutines.
|
| 62 |
|
|
|
| 63 |
|
|
QUICKREF
|
| 64 |
|
|
strtok ansi pure
|
| 65 |
|
|
*/
|
| 66 |
|
|
/* wcstok for Newlib created by adapting strtok_r, 2008. */
|
| 67 |
|
|
/*
|
| 68 |
|
|
* Copyright (c) 1988 Regents of the University of California.
|
| 69 |
|
|
* All rights reserved.
|
| 70 |
|
|
*
|
| 71 |
|
|
* Redistribution and use in source and binary forms, with or without
|
| 72 |
|
|
* modification, are permitted provided that the following conditions
|
| 73 |
|
|
* are met:
|
| 74 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
| 75 |
|
|
* notice, this list of conditions and the following disclaimer.
|
| 76 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
| 77 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
| 78 |
|
|
* documentation and/or other materials provided with the distribution.
|
| 79 |
|
|
* 3. Neither the name of the University nor the names of its contributors
|
| 80 |
|
|
* may be used to endorse or promote products derived from this software
|
| 81 |
|
|
* without specific prior written permission.
|
| 82 |
|
|
*
|
| 83 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
| 84 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 85 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
| 86 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
| 87 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 88 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
| 89 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
| 90 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
| 91 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
| 92 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
| 93 |
|
|
* SUCH DAMAGE.
|
| 94 |
|
|
*/
|
| 95 |
|
|
|
| 96 |
|
|
#include <wchar.h>
|
| 97 |
|
|
|
| 98 |
|
|
wchar_t *
|
| 99 |
|
|
_DEFUN (wcstok, (s, delim, lasts),
|
| 100 |
|
|
register wchar_t *s _AND
|
| 101 |
|
|
register const wchar_t *delim _AND
|
| 102 |
|
|
wchar_t **lasts)
|
| 103 |
|
|
{
|
| 104 |
|
|
register const wchar_t *spanp;
|
| 105 |
|
|
register int c, sc;
|
| 106 |
|
|
wchar_t *tok;
|
| 107 |
|
|
|
| 108 |
|
|
|
| 109 |
|
|
if (s == NULL && (s = *lasts) == NULL)
|
| 110 |
|
|
return (NULL);
|
| 111 |
|
|
|
| 112 |
|
|
/*
|
| 113 |
|
|
* Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
|
| 114 |
|
|
*/
|
| 115 |
|
|
cont:
|
| 116 |
|
|
c = *s++;
|
| 117 |
|
|
for (spanp = delim; (sc = *spanp++) != L'\0';) {
|
| 118 |
|
|
if (c == sc) goto cont;
|
| 119 |
|
|
}
|
| 120 |
|
|
|
| 121 |
|
|
if (c == L'\0') { /* no non-delimiter characters */
|
| 122 |
|
|
*lasts = NULL;
|
| 123 |
|
|
return (NULL);
|
| 124 |
|
|
}
|
| 125 |
|
|
tok = s - 1;
|
| 126 |
|
|
|
| 127 |
|
|
/*
|
| 128 |
|
|
* Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
|
| 129 |
|
|
* Note that delim must have one NUL; we stop if we see that, too.
|
| 130 |
|
|
*/
|
| 131 |
|
|
for (;;) {
|
| 132 |
|
|
c = *s++;
|
| 133 |
|
|
spanp = delim;
|
| 134 |
|
|
do {
|
| 135 |
|
|
if ((sc = *spanp++) == c) {
|
| 136 |
|
|
if (c == L'\0')
|
| 137 |
|
|
s = NULL;
|
| 138 |
|
|
else
|
| 139 |
|
|
s[-1] = L'\0';
|
| 140 |
|
|
*lasts = s;
|
| 141 |
|
|
return (tok);
|
| 142 |
|
|
}
|
| 143 |
|
|
} while (sc != L'\0');
|
| 144 |
|
|
}
|
| 145 |
|
|
/* NOTREACHED */
|
| 146 |
|
|
}
|
| 147 |
|
|
|
| 148 |
|
|
/* The remainder of this file can serve as a regression test. Compile
|
| 149 |
|
|
* with -D_REGRESSION_TEST. */
|
| 150 |
|
|
#if defined(_REGRESSION_TEST) /* [Test code: example from C99 standard */
|
| 151 |
|
|
#include <stdio.h>
|
| 152 |
|
|
#include <wchar.h>
|
| 153 |
|
|
|
| 154 |
|
|
/* example from C99 standard with minor additions to be a test */
|
| 155 |
|
|
int
|
| 156 |
|
|
main(void)
|
| 157 |
|
|
{
|
| 158 |
|
|
int errs=0;
|
| 159 |
|
|
static wchar_t str1[] = L"?a???b,,,#c";
|
| 160 |
|
|
static wchar_t str2[] = L"\t \t";
|
| 161 |
|
|
wchar_t *t, *ptr1, *ptr2;
|
| 162 |
|
|
|
| 163 |
|
|
t = wcstok(str1, L"?", &ptr1); // t points to the token L"a"
|
| 164 |
|
|
if(wcscmp(t,L"a")) errs++;
|
| 165 |
|
|
t = wcstok(NULL, L",", &ptr1); // t points to the token L"??b"
|
| 166 |
|
|
if(wcscmp(t,L"??b")) errs++;
|
| 167 |
|
|
t = wcstok(str2, L" \t", &ptr2); // t is a null pointer
|
| 168 |
|
|
if(t != NULL) errs++;
|
| 169 |
|
|
t = wcstok(NULL, L"#,", &ptr1); // t points to the token L"c"
|
| 170 |
|
|
if(wcscmp(t,L"c")) errs++;
|
| 171 |
|
|
t = wcstok(NULL, L"?", &ptr1); // t is a null pointer
|
| 172 |
|
|
if(t != NULL) errs++;
|
| 173 |
|
|
|
| 174 |
|
|
printf("wcstok() test ");
|
| 175 |
|
|
if(errs) printf("FAILED %d test cases", errs);
|
| 176 |
|
|
else printf("passed");
|
| 177 |
|
|
printf(".\n");
|
| 178 |
|
|
|
| 179 |
|
|
return(errs);
|
| 180 |
|
|
}
|
| 181 |
|
|
#endif /* defined(_REGRESSION_TEST) ] */
|