imapext-2007
diff src/ansilib/strtok.c @ 0:ada5e610ab86
imap-2007e
author | yuuji@gentei.org |
---|---|
date | Mon, 14 Sep 2009 15:17:45 +0900 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/ansilib/strtok.c Mon Sep 14 15:17:45 2009 +0900 1.3 @@ -0,0 +1,67 @@ 1.4 +/* ======================================================================== 1.5 + * Copyright 1988-2007 University of Washington 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * 1.14 + * ======================================================================== 1.15 + */ 1.16 + 1.17 +/* 1.18 + * Program: String return successive tokens 1.19 + * 1.20 + * Author: Mark Crispin 1.21 + * Networks and Distributed Computing 1.22 + * Computing & Communications 1.23 + * University of Washington 1.24 + * Administration Building, AG-44 1.25 + * Seattle, WA 98195 1.26 + * 1.27 + * Date: 11 May 1989 1.28 + * Last Edited: 30 January 2007 1.29 + */ 1.30 + 1.31 +/* Find token in string 1.32 + * Accepts: source pointer or NIL to use previous source 1.33 + * vector of token delimiters pointer 1.34 + * Returns: pointer to next token 1.35 + */ 1.36 + 1.37 +static char *state = NIL; /* string to locate tokens */ 1.38 + 1.39 +char *strtok (char *s,char *ct) 1.40 +{ 1.41 + return strtok_r (s,ct,&state);/* jacket into reentrant routine */ 1.42 +} 1.43 + 1.44 + 1.45 +/* Find token in string (reentrant) 1.46 + * Accepts: source pointer or NIL to use previous source 1.47 + * vector of token delimiters pointer 1.48 + * returned state pointer 1.49 + * Returns: pointer to next token 1.50 + */ 1.51 + 1.52 +char *strtok_r (char *s,char *ct,char **r) 1.53 +{ 1.54 + char *t,*ts; 1.55 + if (!s) s = *r; /* use previous token if none specified */ 1.56 + *r = NIL; /* default to no returned state */ 1.57 + if (!(s && *s)) return NIL; /* no tokens left */ 1.58 + /* find any leading delimiters */ 1.59 + do for (t = ct, ts = NIL; *t; t++) if (*t == *s) { 1.60 + if (*(ts = ++s)) break; /* yes, restart search if more in string */ 1.61 + return NIL; /* else no more tokens */ 1.62 + } while (ts); /* continue until no more leading delimiters */ 1.63 + /* can we find a new delimiter? */ 1.64 + for (ts = s; *ts; ts++) for (t = ct; *t; t++) if (*t == *ts) { 1.65 + *ts++ = '\0'; /* yes, tie off token at that point */ 1.66 + *r = ts; /* save subsequent tokens for future call */ 1.67 + return s; /* return our token */ 1.68 + } 1.69 + return s; /* return final token */ 1.70 +}