os/ossrv/genericopenlibs/cstdlib/LCHAR/STRTOK.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LCHAR/STRTOK.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,162 @@
     1.4 +/*
     1.5 +* FUNCTION
     1.6 +* <<strtok>>---get next token from a string
     1.7 +* INDEX
     1.8 +* strtok
     1.9 +* INDEX
    1.10 +* strtok_r
    1.11 +* ANSI_SYNOPSIS
    1.12 +* #include <string.h>
    1.13 +* char *strtok(char *<[source]>, const char *<[delimiters]>)
    1.14 +* char *strtok_r(char *<[source]>, const char *<[delimiters]>,
    1.15 +* char **<[lasts]>)
    1.16 +* TRAD_SYNOPSIS
    1.17 +* #include <string.h>
    1.18 +* char *strtok(<[source]>, <[delimiters]>)
    1.19 +* char *<[source]>;
    1.20 +* char *<[delimiters]>;
    1.21 +* char *strtok_r(<[source]>, <[delimiters]>, <[lasts]>)
    1.22 +* char *<[source]>;
    1.23 +* char *<[delimiters]>;
    1.24 +* char **<[lasts]>;
    1.25 +* A series of calls to <<strtok>> break the string starting at
    1.26 +* <<*<[source]>>> into a sequence of tokens.  The tokens are
    1.27 +* delimited from one another by characters from the string at
    1.28 +* <<*<[delimiters]>>>, at the outset.
    1.29 +* The first call to <<strtok>> normally has a string address as
    1.30 +* the first argument; subsequent calls can use <<NULL>> as the
    1.31 +* first argument, to continue searching the same string.  You
    1.32 +* can continue searching a single string with different
    1.33 +* delimiters by using a different delimiter string on each call.
    1.34 +* <<strtok>> begins by searching for any character not in the
    1.35 +* <[delimiters]> string: the first such character is the
    1.36 +* beginning of a token (and its address will be the result of
    1.37 +* the <<strtok>> call). <<strtok>> then continues searching
    1.38 +* until it finds another delimiter character; it replaces that
    1.39 +* character by <<NULL>> and returns.  (If <<strtok>> comes to
    1.40 +* the end of the <<*<[source]>>> string without finding any more
    1.41 +* delimiters, the entire remainder of the string is treated as
    1.42 +* the next token).
    1.43 +* <<strtok>> starts its search at <<*<[source]>>>, unless you
    1.44 +* pass <<NULL>> as the first argument;  if <[source]> is
    1.45 +* <<NULL>>, <<strtok>> continues searching from the end of the
    1.46 +* last search. Exploiting the <<NULL>> first argument leads to
    1.47 +* non-reentrant code. You can easily circumvent this problem by
    1.48 +* saving the last delimiter address in your application, and
    1.49 +* always using it to pass a non-null <[source]> argument.
    1.50 +* RETURNS
    1.51 +* <<strtok>> returns a pointer to the next token, or <<NULL>> if
    1.52 +* no more tokens can be found.
    1.53 +* PORTABILITY
    1.54 +* <<strtok>> is ANSI C.
    1.55 +* <<strtok>> requires no supporting OS subroutines.
    1.56 +* QUICKREF
    1.57 +* strtok ansi impure
    1.58 +* 
    1.59 +*
    1.60 +*/
    1.61 +
    1.62 +
    1.63 +
    1.64 +#include <sys/reent.h>
    1.65 +#include <string.h>
    1.66 +
    1.67 +/**
    1.68 +Sequentially truncate string if delimiter is found.
    1.69 +If string is not NULL, the function scans string for the first occurrence of 
    1.70 +any character included in delimiters. If it is found, the function overwrites 
    1.71 +the delimiter in string by a null-character and returns a pointer to the token,
    1.72 +i.e. the part of the scanned string previous to the delimiter.
    1.73 +After a first call to strtok, the function may be called with NULL as string parameter,
    1.74 +and it will follow by where the last call to strtok found a delimiter.
    1.75 +@return A pointer to the last token found in string.
    1.76 +NULL is returned when there are no more tokens to be found.
    1.77 +@param s Null-terminated string to scan. 
    1.78 +@param delim Null-terminated string containing the separators.
    1.79 +*/
    1.80 +EXPORT_C char *
    1.81 +strtok (register char *s, register const char *delim)
    1.82 +{
    1.83 +    return strtok_r (s, delim, &(_REENT->_scanpoint));
    1.84 +}
    1.85 +
    1.86 +/*
    1.87 + * Copyright (c) 1988 Regents of the University of California.
    1.88 + * All rights reserved.
    1.89 + *
    1.90 + * Redistribution and use in source and binary forms, with or without
    1.91 + * modification, are permitted provided that the following conditions
    1.92 + * are met:
    1.93 + * 1. Redistributions of source code must retain the above copyright
    1.94 + *    notice, this list of conditions and the following disclaimer.
    1.95 + * 2. Redistributions in binary form must reproduce the above copyright
    1.96 + *    notice, this list of conditions and the following disclaimer in the
    1.97 + *    documentation and/or other materials provided with the distribution.
    1.98 + * 3. All advertising materials mentioning features or use of this software
    1.99 + *    must display the following acknowledgement:
   1.100 + *	This product includes software developed by the University of
   1.101 + *	California, Berkeley and its contributors.
   1.102 + * 4. Neither the name of the University nor the names of its contributors
   1.103 + *    may be used to endorse or promote products derived from this software
   1.104 + *    without specific prior written permission.
   1.105 + *
   1.106 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   1.107 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   1.108 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   1.109 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   1.110 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   1.111 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   1.112 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   1.113 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   1.114 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   1.115 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   1.116 + * SUCH DAMAGE.
   1.117 + */
   1.118 +
   1.119 +EXPORT_C char *
   1.120 +strtok_r (register char *s, register const char *delim, char **lasts)
   1.121 +{
   1.122 +	register char *spanp;
   1.123 +	register int c, sc;
   1.124 +	char *tok;
   1.125 +
   1.126 +
   1.127 +	if (s == NULL && (s = *lasts) == NULL)
   1.128 +		return (NULL);
   1.129 +
   1.130 +	/*
   1.131 +	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
   1.132 +	 */
   1.133 +cont:
   1.134 +	c = *s++;
   1.135 +	for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
   1.136 +		if (c == sc)
   1.137 +			goto cont;
   1.138 +	}
   1.139 +
   1.140 +	if (c == 0) {		/* no non-delimiter characters */
   1.141 +		*lasts = NULL;
   1.142 +		return (NULL);
   1.143 +	}
   1.144 +	tok = s - 1;
   1.145 +
   1.146 +	/*
   1.147 +	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
   1.148 +	 * Note that delim must have one NUL; we stop if we see that, too.
   1.149 +	 */
   1.150 +	for (;;) {
   1.151 +		c = *s++;
   1.152 +		spanp = (char *)delim;
   1.153 +		do {
   1.154 +			if ((sc = *spanp++) == c) {
   1.155 +				if (c == 0)
   1.156 +					s = NULL;
   1.157 +				else
   1.158 +					s[-1] = 0;
   1.159 +				*lasts = s;
   1.160 +				return (tok);
   1.161 +			}
   1.162 +		} while (sc != 0);
   1.163 +	}
   1.164 +	/* NOTREACHED */
   1.165 +}