00001 /*--------------------------------------------------------------------------*/ 00002 /* */ 00003 /* File: STRTOK_R.C */ 00004 /* Created: 20-September-2002 */ 00005 /* Author: Peter Scandrett */ 00006 /* Description: Module to provide a reentrant version of the 'C' */ 00007 /* function STRTOK. */ 00008 /* */ 00009 /*--------------------------------------------------------------------------*/ 00010 /* */ 00011 /* Copyright (C) 2002 by ALSTOM Australia Limited. All rights reserved. */ 00012 /* */ 00013 /* Redistribution and use in source and binary forms, with or without */ 00014 /* modification, are permitted provided that the following conditions */ 00015 /* are met: */ 00016 /* 1. Redistributions of source code must retain the above copyright */ 00017 /* notice and this list of conditions. */ 00018 /* 2. Neither the name of ALSTOM Australia Limited nor the names of its */ 00019 /* contributors may be used to endorse or promote products derived */ 00020 /* from this software. */ 00021 /* */ 00022 /* THIS SOFTWARE IS PROVIDED BY ALSTOM AUSTRALIA LIMITED AND CONTRIBUTORS */ 00023 /* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */ 00024 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */ 00025 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ALSTOM */ 00026 /* AUSTRALIA LIMITED OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */ 00027 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, */ 00028 /* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS */ 00029 /* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED */ 00030 /* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */ 00031 /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF */ 00032 /* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH */ 00033 /* DAMAGE. */ 00034 /* */ 00035 /*--------------------------------------------------------------------------*/ 00036 00037 /* 00038 * $Log: strtok_r.c,v $ 00039 * Revision 1.6 2006/10/08 16:48:08 haraldkipp 00040 * Documentation fixed 00041 * 00042 * Revision 1.5 2004/07/27 19:28:51 drsung 00043 * Implementation of strtok_r adjusted to the POSIX 1c standard. 00044 * 00045 * Revision 1.4 2003/12/16 22:34:41 drsung 00046 * Portability issues 00047 * 00048 */ 00049 00050 #include <stdlib.h> 00051 #include <string.h> 00052 00053 #include "strtok_r.h" 00054 00067 00068 /*--------------------------------------------------------------------------*/ 00069 00070 static char *end_tok(char **pp_str, CONST char *p_delim, char *p_sep) 00071 { 00072 register const char *sp; 00073 char *p_tok; 00074 char *p_ch; 00075 00076 /* Use a local pointer. */ 00077 p_ch = *pp_str; 00078 00079 /* Scan for next deliminator. */ 00080 p_tok = p_ch; 00081 while (*p_ch != 0) { 00082 for (sp = p_delim; *sp != 0; sp++) { 00083 if (*sp == *p_ch) { 00084 if (p_sep != NULL) { 00085 /* Save terminator. */ 00086 *p_sep = *p_ch; 00087 } 00088 *p_ch++ = 0; 00089 *pp_str = p_ch; 00090 return p_tok; 00091 } 00092 } 00093 p_ch++; 00094 } 00095 00096 /* At end of string, so exit, but return last token. */ 00097 *pp_str = p_ch; 00098 return p_tok; 00099 } 00100 00101 /*--------------------------------------------------------------------------*/ 00102 00109 char *strsep_rs(char **pp_str, CONST char *p_delim, char *p_sep) 00110 { 00111 char *p_ch; 00112 00113 /* Assume terminator was end of string. */ 00114 if (p_sep != NULL) 00115 *p_sep = 0; 00116 00117 /* Check not passed a NULL. */ 00118 if (pp_str == NULL) 00119 return NULL; 00120 00121 /* Use a local pointer. */ 00122 p_ch = *pp_str; 00123 if (p_ch == NULL) 00124 return NULL; 00125 00126 if (*p_ch == 0) 00127 return NULL; 00128 00129 /* Check a valid delimiter string. */ 00130 if ((p_delim == NULL) || (*p_delim == 0)) { 00131 *pp_str = NULL; 00132 return p_ch; 00133 } 00134 /* Scan for next deliminator. */ 00135 return end_tok(pp_str, p_delim, p_sep); 00136 } 00137 00138 /*--------------------------------------------------------------------------*/ 00139 00157 char *strsep_r(char **pp_str, CONST char *p_delim) 00158 { 00159 return strsep_rs(pp_str, p_delim, NULL); 00160 } 00161 00162 /*--------------------------------------------------------------------------*/ 00177 #ifdef __IMAGECRAFT__ 00178 /* Parse S into tokens separated by characters in DELIM. 00179 If S is NULL, the saved pointer in SAVE_PTR is used as 00180 the next starting point. For example: 00181 char s[] = "-abc-=-def"; 00182 char *sp; 00183 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def" 00184 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL 00185 x = strtok_r(NULL, "=", &sp); // x = NULL 00186 // s = "abc\\0-def\\0" 00187 */ 00188 char *strtok_r(char *s, CONST char *delim, char **save_ptr) 00189 { 00190 char *token; 00191 00192 if (s == NULL) 00193 s = *save_ptr; 00194 00195 /* Scan leading delimiters. */ 00196 s += strspn(s, delim); 00197 if (*s == '\0') 00198 return NULL; 00199 00200 /* Find the end of the token. */ 00201 token = s; 00202 s = strpbrk(token, delim); 00203 if (s == NULL) 00204 /* This token finishes the string. */ 00205 *save_ptr = strchr(token, '\0'); 00206 else { 00207 /* Terminate the token and make *SAVE_PTR point past it. */ 00208 *s = '\0'; 00209 *save_ptr = s + 1; 00210 } 00211 return token; 00212 } 00213 #endif /*__IMAGECRAFT__ */ 00214 00217 /*-------------------------- end of file STRTOK_R.C ------------------------*/