wcstok


wcstok

Divides a wide string into tokens

 #include <wchar.h> wchar_t *wcstok ( wchar_t * restrict s1 , const wchar_t * restrict s2 ,                  wchar_t ** restrict ptr  ); 

The wcstok( ) function isolates tokens in the wide string addressed by s1 that are delimited by any of the wide characters contained in the string addressed by s2. The tokens are identified one at a time by successive calls to wcstok( ). On calls after the first, the s1 argument is a null pointer. The third argument is a pointer to a wchar_t pointer; wcstok( ) stores caller-specific information at the location addressed by this pointer for use on successive calls in the same sequence.

On the first call, wcstok( ) searches in s1 for the first character that does not match any character in s2, similarly to the wcsspn( ) function. The first such wide character found is considered to be the beginning of a token. Then wcstok( ) searches further for the first wide character that does match any of the wide characters in s2or the null wide character that terminates the string, whichever comes firstsimilarly to the wcscspn( ) function. The first such wide character found is considered to be the delimiter that ends the token. wcstok( ) then replaces this ending delimiter with L'\0', and returns a pointer to the beginning of the token (or a null pointer if no token was found), after storing a value in the location addressed by the ptr argument for use in subsequent wcstok( ) calls.

On each subsequent call with a null pointer as the s1 argument and the same value as before for the ptr argument, wcstok( ) behaves similarly, but starts the search at the wide character that follows the previous delimiter. You can specify a different set of delimiters in the s2 argument on each call. The locations that wcstok( ) reads from and writes to using its restricted pointer arguments must not overlap on any given call.

Example

 wchar_t *mnemonic, *arg1, *arg2, *comment, *ptr; wchar_t line[ ] = L"    mul eax,[ebp+4]    ; Multiply by y\n"; // First word between spaces or tabs mnemonic = wcstok( line, L" \t", &ptr ); arg1 = wcstok( NULL, L",", &ptr );   // From there to the comma is arg1.                                      // Trim off any spaces later. arg2 = wcstok( NULL, L";\n", &ptr ); // From there to the semicolon is arg2. // To line or page end is comment:   comment = wcstok( NULL, L"\n\r\v\f", &ptr ); wprintf( L"Command:      %ls\n"          L"1st argument: %ls\n"          L"2nd argument: %ls\n"          L"Comment:      %ls\n\n",          mnemonic, arg1, arg2, comment ); 

This code produces the following output:

 Command:      mul 1st argument: eax 2nd argument: [ebp+4] Comment:       Multiply by y 

See Also

wcsspn( ), wcscspn( ), wcsstr( ), wcspbrk( ), and strtok( )



C(c) In a Nutshell
C in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596006977
EAN: 2147483647
Year: 2006
Pages: 473

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net