Mercurial > flash_v2
annotate packages/language/c/libc/current/src/string/strtok.cxx @ 46:797268ecc331 ecos-sw-1999-10-19
Merge from eCos master repository on 1999-10-19-18:55:31-BST
| author | jlarmour |
|---|---|
| date | Tue, 19 Oct 1999 19:19:52 +0000 |
| parents | 443894e2e912 |
| children | c38311975d4f |
| rev | line source |
|---|---|
| 0 | 1 //=========================================================================== |
| 2 // | |
| 3 // strtok.cxx | |
| 4 // | |
| 2 | 5 // ISO standard strtok() routine |
| 0 | 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 | |
| 2 | 25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
| 0 | 26 // ------------------------------------------- |
| 27 // | |
| 28 //####COPYRIGHTEND#### | |
| 29 //=========================================================================== | |
| 30 //#####DESCRIPTIONBEGIN#### | |
| 31 // | |
| 2 | 32 // Author(s): jlarmour |
| 33 // Contributors: jlarmour | |
| 34 // Date: 1999-01-19 | |
| 35 // Purpose: Provide ISO C strtok() and POSIX strtok_r() routines | |
| 0 | 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. | |
|
46
797268ecc331
Merge from eCos master repository on 1999-10-19-18:55:31-BST
jlarmour
parents:
2
diff
changeset
|
57 * 3. Neither the name of the University nor the names of its contributors |
| 0 | 58 * may be used to endorse or promote products derived from this software |
| 59 * without specific prior written permission. | |
| 60 * | |
| 61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 71 * SUCH DAMAGE. | |
| 72 */ | |
| 73 | |
| 74 | |
| 75 // CONFIGURATION | |
| 76 | |
| 77 #include <pkgconf/libc.h> // Configuration header | |
| 78 | |
| 79 // INCLUDES | |
| 80 | |
| 81 #include <cyg/infra/cyg_type.h> // Common type definitions | |
| 82 #include <cyg/infra/cyg_trac.h> // Tracing support | |
| 83 #include <cyg/infra/cyg_ass.h> // Assertion support | |
| 84 #include <string.h> // Header for this file | |
| 85 #include <stddef.h> // Compiler definitions such as size_t, NULL etc. | |
| 86 #include "clibincl/stringsupp.hxx" // Useful string function support and | |
| 87 // prototypes | |
| 2 | 88 |
| 89 #ifdef CYGSEM_LIBC_PER_THREAD_STRTOK | |
| 90 # include <pkgconf/kernel.h> // kernel configuration | |
| 91 # include <cyg/kernel/thread.hxx> // per-thread data | |
| 92 # include <cyg/kernel/thread.inl> // per-thread data | |
| 93 # include <cyg/kernel/mutex.hxx> // mutexes | |
| 94 #endif | |
| 0 | 95 |
| 96 // EXPORTED SYMBOLS | |
| 97 | |
| 98 externC char * | |
| 2 | 99 strtok( char *s1, const char *s2 ) CYGBLD_ATTRIB_WEAK_ALIAS(_strtok); |
| 0 | 100 |
| 101 externC char * | |
| 102 strtok_r( char *s1, const char *s2, char **lasts ) \ | |
| 2 | 103 CYGBLD_ATTRIB_WEAK_ALIAS(_strtok_r); |
| 0 | 104 |
| 105 // TRACE | |
| 106 | |
| 107 #if defined(CYGDBG_USE_TRACING) && defined(CYGNUM_LIBC_STRTOK_TRACE_LEVEL) | |
| 108 static int strtok_trace = CYGNUM_LIBC_STRTOK_TRACE_LEVEL; | |
| 109 # define TL1 (0 < strtok_trace) | |
| 110 #else | |
| 111 # define TL1 (0) | |
| 112 #endif | |
| 113 | |
| 2 | 114 // STATICS |
| 115 | |
| 116 #ifdef CYGSEM_LIBC_PER_THREAD_STRTOK | |
| 117 static cyg_ucount32 strtok_data_index=CYGNUM_KERNEL_THREADS_DATA_MAX; | |
| 118 static Cyg_Mutex strtok_data_mutex CYG_INIT_PRIORITY(LIBC); | |
| 119 #else | |
| 120 static char *cyg_libc_strtok_last; | |
| 121 #endif | |
| 0 | 122 |
| 123 // FUNCTIONS | |
| 124 | |
| 125 char * | |
| 126 _strtok( char *s1, const char *s2 ) | |
| 127 { | |
| 128 char **lasts; | |
| 129 char *retval; | |
| 130 | |
| 131 CYG_REPORT_FUNCNAMETYPE( "_strtok", "returning %08x" ); | |
| 132 CYG_REPORT_FUNCARG2( "s1=%08x, s2=%08x", s1, s2 ); | |
| 133 | |
| 134 if (s1 != NULL) | |
| 135 CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" ); | |
| 136 CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" ); | |
| 137 | |
| 2 | 138 #ifdef CYGSEM_LIBC_PER_THREAD_STRTOK |
| 139 Cyg_Thread *self = Cyg_Thread::self(); | |
| 140 | |
| 141 // Get a per-thread data slot if we haven't got one already | |
| 142 // Do a simple test before locking and retrying test, as this is a | |
| 143 // rare situation | |
| 144 if (CYGNUM_KERNEL_THREADS_DATA_MAX==strtok_data_index) { | |
| 145 strtok_data_mutex.lock(); | |
| 146 if (CYGNUM_KERNEL_THREADS_DATA_MAX==strtok_data_index) { | |
| 0 | 147 |
| 2 | 148 // the kernel just throws an assert if this doesn't work |
| 149 // FIXME: Should use real CDL to pre-allocate a slot at compile | |
| 150 // time to ensure there are enough slots | |
| 151 strtok_data_index = self->new_data_index(); | |
| 152 | |
| 153 } | |
| 154 strtok_data_mutex.unlock(); | |
| 155 } // if | |
| 156 | |
| 157 // we have a valid index now | |
| 158 | |
| 159 lasts = (char **)self->get_data_ptr(strtok_data_index); | |
| 160 #else | |
| 161 lasts = &cyg_libc_strtok_last; | |
| 162 #endif | |
| 0 | 163 |
| 164 CYG_TRACE2( TL1, "Retrieved strtok_last address %08x containing %s", | |
| 165 lasts, *lasts ); | |
| 166 | |
| 167 retval = _strtok_r( s1, s2, lasts ); | |
| 168 | |
| 169 CYG_REPORT_RETVAL( retval ); | |
| 170 | |
| 171 return retval; | |
| 172 } // _strtok() | |
| 173 | |
| 174 char * | |
| 175 _strtok_r( char *s1, const char *s2, char **lasts ) | |
| 176 { | |
| 177 char *spanp; | |
| 178 int c, sc; | |
| 179 char *tok; | |
| 180 | |
| 181 CYG_REPORT_FUNCNAMETYPE( "_strtok_r", "returning %08x" ); | |
| 182 CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, lasts=%08x", s1, s2, lasts ); | |
| 183 | |
| 184 if (s1 != NULL) | |
| 185 CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" ); | |
| 186 CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" ); | |
| 187 CYG_CHECK_DATA_PTR( lasts, "lasts is not a valid pointer!" ); | |
| 188 | |
| 189 | |
| 190 if (s1 == NULL && (s1 = *lasts) == NULL) | |
| 191 { | |
| 192 CYG_REPORT_RETVAL( NULL ); | |
| 193 return NULL; | |
| 194 } // if | |
| 195 | |
| 196 // | |
| 197 // Skip (span) leading delimiters (s += strspn(s, delim), sort of). | |
| 198 // | |
| 199 cont: | |
| 200 c = *s1++; | |
| 201 for (spanp = (char *)s2; (sc = *spanp++) != 0;) { | |
| 202 if (c == sc) | |
| 203 goto cont; | |
| 204 } // for | |
| 205 | |
| 206 if (c == 0) { // no non-delimiter characters | |
| 207 *lasts = NULL; | |
| 208 | |
| 209 CYG_REPORT_RETVAL( NULL ); | |
| 210 return NULL; | |
| 211 } // if | |
| 212 tok = s1 - 1; | |
| 213 | |
| 214 // | |
| 215 // Scan token (scan for delimiters: s += strcspn(s, delim), sort of). | |
| 216 // Note that delim must have one NUL; we stop if we see that, too. | |
| 217 // | |
| 218 for (;;) { | |
| 219 c = *s1++; | |
| 220 spanp = (char *)s2; | |
| 221 do { | |
| 222 if ((sc = *spanp++) == c) { | |
| 223 if (c == 0) | |
| 224 s1 = NULL; | |
| 225 else | |
| 226 s1[-1] = 0; | |
| 227 *lasts = s1; | |
| 228 | |
| 229 CYG_REPORT_RETVAL( tok ); | |
| 230 | |
| 231 return (tok); | |
| 232 } // if | |
| 233 } while (sc != 0); | |
| 234 } // for | |
| 235 // NOTREACHED | |
| 236 } // _strtok_r() | |
| 237 | |
| 238 // EOF strtok.cxx |
