Mercurial > nand-ecoscentric
comparison packages/language/c/libc/current/src/string/strtok.cxx @ 0:3111d98ba7b3 ecos-v1_1-release
Initial commit of eCos version 1.1
| author | jlarmour |
|---|---|
| date | Tue, 11 May 1999 11:16:07 +0000 |
| parents | |
| children | 443894e2e912 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:3111d98ba7b3 |
|---|---|
| 1 //=========================================================================== | |
| 2 // | |
| 3 // strtok.cxx | |
| 4 // | |
| 5 // ANSI standard strtok() routine | |
| 6 // | |
| 7 //=========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
| 9 // | |
| 10 // ------------------------------------------- | |
| 11 // The contents of this file are subject to the Cygnus eCos Public License | |
| 12 // Version 1.0 (the "License"); you may not use this file except in | |
| 13 // compliance with the License. You may obtain a copy of the License at | |
| 14 // http://sourceware.cygnus.com/ecos | |
| 15 // | |
| 16 // Software distributed under the License is distributed on an "AS IS" | |
| 17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | |
| 18 // License for the specific language governing rights and limitations under | |
| 19 // the License. | |
| 20 // | |
| 21 // The Original Code is eCos - Embedded Cygnus Operating System, released | |
| 22 // September 30, 1998. | |
| 23 // | |
| 24 // The Initial Developer of the Original Code is Cygnus. Portions created | |
| 25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. | |
| 26 // ------------------------------------------- | |
| 27 // | |
| 28 //####COPYRIGHTEND#### | |
| 29 //=========================================================================== | |
| 30 //#####DESCRIPTIONBEGIN#### | |
| 31 // | |
| 32 // Author(s): jlarmour | |
| 33 // Contributors: jlarmour@cygnus.co.uk | |
| 34 // Date: 1998-02-13 | |
| 35 // Purpose: | |
| 36 // Description: | |
| 37 // Usage: | |
| 38 // | |
| 39 //####DESCRIPTIONEND#### | |
| 40 // | |
| 41 //=========================================================================== | |
| 42 // | |
| 43 // This code is based on original code with the following copyright: | |
| 44 // | |
| 45 /* | |
| 46 * Copyright (c) 1988 Regents of the University of California. | |
| 47 * All rights reserved. | |
| 48 * | |
| 49 * Redistribution and use in source and binary forms, with or without | |
| 50 * modification, are permitted provided that the following conditions | |
| 51 * are met: | |
| 52 * 1. Redistributions of source code must retain the above copyright | |
| 53 * notice, this list of conditions and the following disclaimer. | |
| 54 * 2. Redistributions in binary form must reproduce the above copyright | |
| 55 * notice, this list of conditions and the following disclaimer in the | |
| 56 * documentation and/or other materials provided with the distribution. | |
| 57 * 3. All advertising materials mentioning features or use of this software | |
| 58 * must display the following acknowledgement: | |
| 59 * This product includes software developed by the University of | |
| 60 * California, Berkeley and its contributors. | |
| 61 * 4. Neither the name of the University nor the names of its contributors | |
| 62 * may be used to endorse or promote products derived from this software | |
| 63 * without specific prior written permission. | |
| 64 * | |
| 65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 75 * SUCH DAMAGE. | |
| 76 */ | |
| 77 | |
| 78 | |
| 79 // CONFIGURATION | |
| 80 | |
| 81 #include <pkgconf/libc.h> // Configuration header | |
| 82 | |
| 83 // Include the C library? And do we want strtok() at all? | |
| 84 #if defined(CYGPKG_LIBC) && defined(CYGFUN_LIBC_strtok) | |
| 85 | |
| 86 // INCLUDES | |
| 87 | |
| 88 #include <cyg/infra/cyg_type.h> // Common type definitions | |
| 89 #include <cyg/infra/cyg_trac.h> // Tracing support | |
| 90 #include <cyg/infra/cyg_ass.h> // Assertion support | |
| 91 #include <string.h> // Header for this file | |
| 92 #include <stddef.h> // Compiler definitions such as size_t, NULL etc. | |
| 93 #include "clibincl/stringsupp.hxx" // Useful string function support and | |
| 94 // prototypes | |
| 95 #include "clibincl/clibdata.hxx" // C library internal data | |
| 96 | |
| 97 // EXPORTED SYMBOLS | |
| 98 | |
| 99 externC char * | |
| 100 strtok( char *s1, const char *s2 ) CYGPRI_LIBC_WEAK_ALIAS("_strtok"); | |
| 101 | |
| 102 externC char * | |
| 103 strtok_r( char *s1, const char *s2, char **lasts ) \ | |
| 104 CYGPRI_LIBC_WEAK_ALIAS("_strtok_r"); | |
| 105 | |
| 106 // TRACE | |
| 107 | |
| 108 #if defined(CYGDBG_USE_TRACING) && defined(CYGNUM_LIBC_STRTOK_TRACE_LEVEL) | |
| 109 static int strtok_trace = CYGNUM_LIBC_STRTOK_TRACE_LEVEL; | |
| 110 # define TL1 (0 < strtok_trace) | |
| 111 #else | |
| 112 # define TL1 (0) | |
| 113 #endif | |
| 114 | |
| 115 | |
| 116 // FUNCTIONS | |
| 117 | |
| 118 char * | |
| 119 _strtok( char *s1, const char *s2 ) | |
| 120 { | |
| 121 char **lasts; | |
| 122 char *retval; | |
| 123 | |
| 124 CYG_REPORT_FUNCNAMETYPE( "_strtok", "returning %08x" ); | |
| 125 CYG_REPORT_FUNCARG2( "s1=%08x, s2=%08x", s1, s2 ); | |
| 126 | |
| 127 if (s1 != NULL) | |
| 128 CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" ); | |
| 129 CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" ); | |
| 130 | |
| 131 CYGPRI_LIBC_INTERNAL_DATA_ALLOC_CHECK_PREAMBLE; | |
| 132 | |
| 133 lasts = CYGPRI_LIBC_INTERNAL_DATA.get_strtok_last_p(); | |
| 134 | |
| 135 CYG_TRACE2( TL1, "Retrieved strtok_last address %08x containing %s", | |
| 136 lasts, *lasts ); | |
| 137 | |
| 138 retval = _strtok_r( s1, s2, lasts ); | |
| 139 | |
| 140 CYG_REPORT_RETVAL( retval ); | |
| 141 | |
| 142 return retval; | |
| 143 } // _strtok() | |
| 144 | |
| 145 char * | |
| 146 _strtok_r( char *s1, const char *s2, char **lasts ) | |
| 147 { | |
| 148 char *spanp; | |
| 149 int c, sc; | |
| 150 char *tok; | |
| 151 | |
| 152 CYG_REPORT_FUNCNAMETYPE( "_strtok_r", "returning %08x" ); | |
| 153 CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, lasts=%08x", s1, s2, lasts ); | |
| 154 | |
| 155 if (s1 != NULL) | |
| 156 CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" ); | |
| 157 CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" ); | |
| 158 CYG_CHECK_DATA_PTR( lasts, "lasts is not a valid pointer!" ); | |
| 159 | |
| 160 | |
| 161 if (s1 == NULL && (s1 = *lasts) == NULL) | |
| 162 { | |
| 163 CYG_REPORT_RETVAL( NULL ); | |
| 164 return NULL; | |
| 165 } // if | |
| 166 | |
| 167 // | |
| 168 // Skip (span) leading delimiters (s += strspn(s, delim), sort of). | |
| 169 // | |
| 170 cont: | |
| 171 c = *s1++; | |
| 172 for (spanp = (char *)s2; (sc = *spanp++) != 0;) { | |
| 173 if (c == sc) | |
| 174 goto cont; | |
| 175 } // for | |
| 176 | |
| 177 if (c == 0) { // no non-delimiter characters | |
| 178 *lasts = NULL; | |
| 179 | |
| 180 CYG_REPORT_RETVAL( NULL ); | |
| 181 return NULL; | |
| 182 } // if | |
| 183 tok = s1 - 1; | |
| 184 | |
| 185 // | |
| 186 // Scan token (scan for delimiters: s += strcspn(s, delim), sort of). | |
| 187 // Note that delim must have one NUL; we stop if we see that, too. | |
| 188 // | |
| 189 for (;;) { | |
| 190 c = *s1++; | |
| 191 spanp = (char *)s2; | |
| 192 do { | |
| 193 if ((sc = *spanp++) == c) { | |
| 194 if (c == 0) | |
| 195 s1 = NULL; | |
| 196 else | |
| 197 s1[-1] = 0; | |
| 198 *lasts = s1; | |
| 199 | |
| 200 CYG_REPORT_RETVAL( tok ); | |
| 201 | |
| 202 return (tok); | |
| 203 } // if | |
| 204 } while (sc != 0); | |
| 205 } // for | |
| 206 // NOTREACHED | |
| 207 } // _strtok_r() | |
| 208 | |
| 209 #endif // if defined(CYGPKG_LIBC) && defined(CYGFUN_LIBC_strtok) | |
| 210 | |
| 211 // EOF strtok.cxx |
