Home | History | Annotate | Line # | Download | only in libsa
strtok.cpp revision 1.1.2.2
      1  1.1.2.2  skrll /*	$NetBSD: strtok.cpp,v 1.1.2.2 2004/08/12 11:41:11 skrll Exp $	*/
      2  1.1.2.2  skrll 
      3  1.1.2.2  skrll /*
      4  1.1.2.2  skrll  * Copyright (c) 1988, 1993
      5  1.1.2.2  skrll  *	The Regents of the University of California.  All rights reserved.
      6  1.1.2.2  skrll  *
      7  1.1.2.2  skrll  * Redistribution and use in source and binary forms, with or without
      8  1.1.2.2  skrll  * modification, are permitted provided that the following conditions
      9  1.1.2.2  skrll  * are met:
     10  1.1.2.2  skrll  * 1. Redistributions of source code must retain the above copyright
     11  1.1.2.2  skrll  *    notice, this list of conditions and the following disclaimer.
     12  1.1.2.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1.2.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     14  1.1.2.2  skrll  *    documentation and/or other materials provided with the distribution.
     15  1.1.2.2  skrll  * 3. Neither the name of the University nor the names of its contributors
     16  1.1.2.2  skrll  *    may be used to endorse or promote products derived from this software
     17  1.1.2.2  skrll  *    without specific prior written permission.
     18  1.1.2.2  skrll  *
     19  1.1.2.2  skrll  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1.2.2  skrll  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1.2.2  skrll  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1.2.2  skrll  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1.2.2  skrll  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1.2.2  skrll  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1.2.2  skrll  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1.2.2  skrll  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1.2.2  skrll  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1.2.2  skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1.2.2  skrll  * SUCH DAMAGE.
     30  1.1.2.2  skrll  */
     31  1.1.2.2  skrll 
     32  1.1.2.2  skrll #include <sys/cdefs.h>
     33  1.1.2.2  skrll #include <sys/types.h>
     34  1.1.2.2  skrll #include <stdlib.h>
     35  1.1.2.2  skrll #include <libsa_string.h>
     36  1.1.2.2  skrll 
     37  1.1.2.2  skrll LIBSA_NAMESPACE_BEGIN
     38  1.1.2.2  skrll __BEGIN_DECLS
     39  1.1.2.2  skrll char *
     40  1.1.2.2  skrll strtok(char *s, const char *delim)
     41  1.1.2.2  skrll {
     42  1.1.2.2  skrll 	const char *spanp;
     43  1.1.2.2  skrll 	int c, sc;
     44  1.1.2.2  skrll 	char *tok;
     45  1.1.2.2  skrll 	static char *last;
     46  1.1.2.2  skrll 
     47  1.1.2.2  skrll 	if (s == NULL && (s = last) == NULL)
     48  1.1.2.2  skrll 		return (NULL);
     49  1.1.2.2  skrll 
     50  1.1.2.2  skrll 	/*
     51  1.1.2.2  skrll 	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
     52  1.1.2.2  skrll 	 */
     53  1.1.2.2  skrll cont:
     54  1.1.2.2  skrll 	c = *s++;
     55  1.1.2.2  skrll 	for (spanp = delim; (sc = *spanp++) != 0;) {
     56  1.1.2.2  skrll 		if (c == sc)
     57  1.1.2.2  skrll 			goto cont;
     58  1.1.2.2  skrll 	}
     59  1.1.2.2  skrll 
     60  1.1.2.2  skrll 	if (c == 0) {		/* no non-delimiter characters */
     61  1.1.2.2  skrll 		last = NULL;
     62  1.1.2.2  skrll 		return (NULL);
     63  1.1.2.2  skrll 	}
     64  1.1.2.2  skrll 	tok = s - 1;
     65  1.1.2.2  skrll 
     66  1.1.2.2  skrll 	/*
     67  1.1.2.2  skrll 	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
     68  1.1.2.2  skrll 	 * Note that delim must have one NUL; we stop if we see that, too.
     69  1.1.2.2  skrll 	 */
     70  1.1.2.2  skrll 	for (;;) {
     71  1.1.2.2  skrll 		c = *s++;
     72  1.1.2.2  skrll 		spanp = delim;
     73  1.1.2.2  skrll 		do {
     74  1.1.2.2  skrll 			if ((sc = *spanp++) == c) {
     75  1.1.2.2  skrll 				if (c == 0)
     76  1.1.2.2  skrll 					s = NULL;
     77  1.1.2.2  skrll 				else
     78  1.1.2.2  skrll 					s[-1] = 0;
     79  1.1.2.2  skrll 				last = s;
     80  1.1.2.2  skrll 				return (tok);
     81  1.1.2.2  skrll 			}
     82  1.1.2.2  skrll 		} while (sc != 0);
     83  1.1.2.2  skrll 	}
     84  1.1.2.2  skrll 	/* NOTREACHED */
     85  1.1.2.2  skrll }
     86  1.1.2.2  skrll __END_DECLS
     87  1.1.2.2  skrll LIBSA_NAMESPACE_END
     88