Nut/OS  4.10.3
API Reference
strtok_r.c
Go to the documentation of this file.
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$
00039  * Revision 1.7  2008/02/15 17:13:16  haraldkipp
00040  * Use configurable constant attribute.
00041  *
00042  * Revision 1.6  2006/10/08 16:48:08  haraldkipp
00043  * Documentation fixed
00044  *
00045  * Revision 1.5  2004/07/27 19:28:51  drsung
00046  * Implementation of strtok_r adjusted to the POSIX 1c standard.
00047  *
00048  * Revision 1.4  2003/12/16 22:34:41  drsung
00049  * Portability issues
00050  *
00051  */
00052 
00053 #include <stdlib.h>
00054 #include <string.h>
00055 
00056 #include "strtok_r.h"
00057 
00070 
00071 /*--------------------------------------------------------------------------*/
00072 
00073 static char *end_tok(char **pp_str, CONST char *p_delim, char *p_sep)
00074 {
00075     register CONST char *sp;
00076     char *p_tok;
00077     char *p_ch;
00078 
00079     /*  Use a local pointer. */
00080     p_ch = *pp_str;
00081 
00082     /*  Scan for next deliminator. */
00083     p_tok = p_ch;
00084     while (*p_ch != 0) {
00085         for (sp = p_delim; *sp != 0; sp++) {
00086             if (*sp == *p_ch) {
00087                 if (p_sep != NULL) {
00088                     /*  Save terminator. */
00089                     *p_sep = *p_ch;
00090                 }
00091                 *p_ch++ = 0;
00092                 *pp_str = p_ch;
00093                 return p_tok;
00094             }
00095         }
00096         p_ch++;
00097     }
00098 
00099     /*  At end of string, so exit, but return last token. */
00100     *pp_str = p_ch;
00101     return p_tok;
00102 }
00103 
00104 /*--------------------------------------------------------------------------*/
00105 
00112 char *strsep_rs(char **pp_str, CONST char *p_delim, char *p_sep)
00113 {
00114     char *p_ch;
00115 
00116     /*  Assume terminator was end of string. */
00117     if (p_sep != NULL)
00118         *p_sep = 0;
00119 
00120     /*  Check not passed a NULL. */
00121     if (pp_str == NULL)
00122         return NULL;
00123 
00124     /*  Use a local pointer. */
00125     p_ch = *pp_str;
00126     if (p_ch == NULL)
00127         return NULL;
00128 
00129     if (*p_ch == 0)
00130         return NULL;
00131 
00132     /*  Check a valid delimiter string. */
00133     if ((p_delim == NULL) || (*p_delim == 0)) {
00134         *pp_str = NULL;
00135         return p_ch;
00136     }
00137     /*  Scan for next deliminator. */
00138     return end_tok(pp_str, p_delim, p_sep);
00139 }
00140 
00141 /*--------------------------------------------------------------------------*/
00142 
00160 char *strsep_r(char **pp_str, CONST char *p_delim)
00161 {
00162     return strsep_rs(pp_str, p_delim, NULL);
00163 }
00164 
00165 /*--------------------------------------------------------------------------*/
00180 #ifdef __IMAGECRAFT__
00181 /* Parse S into tokens separated by characters in DELIM.
00182    If S is NULL, the saved pointer in SAVE_PTR is used as
00183    the next starting point.  For example:
00184         char s[] = "-abc-=-def";
00185         char *sp;
00186         x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
00187         x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
00188         x = strtok_r(NULL, "=", &sp);   // x = NULL
00189                 // s = "abc\\0-def\\0"
00190 */
00191 char *strtok_r(char *s, CONST char *delim, char **save_ptr)
00192 {
00193     char *token;
00194 
00195     if (s == NULL)
00196         s = *save_ptr;
00197 
00198     /* Scan leading delimiters.  */
00199     s += strspn(s, delim);
00200     if (*s == '\0')
00201         return NULL;
00202 
00203     /* Find the end of the token.  */
00204     token = s;
00205     s = strpbrk(token, delim);
00206     if (s == NULL)
00207         /* This token finishes the string.  */
00208         *save_ptr = strchr(token, '\0');
00209     else {
00210         /* Terminate the token and make *SAVE_PTR point past it.  */
00211         *s = '\0';
00212         *save_ptr = s + 1;
00213     }
00214     return token;
00215 }
00216 #endif /*__IMAGECRAFT__ */
00217 
00220 /*-------------------------- end of file STRTOK_R.C ------------------------*/