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