Mercurial > flash_v2
annotate packages/language/c/libc/current/src/stdlib/strtol.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 // strtol.cxx | |
| 4 // | |
| 5 // ANSI standard string to long int conversion function defined in | |
| 6 // section 7.10.1.5 of the standard | |
| 7 // | |
| 8 //=========================================================================== | |
| 9 //####COPYRIGHTBEGIN#### | |
|
64
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 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
46
diff
changeset
|
12 // 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
|
13 // 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
|
14 // 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
|
15 // http://sourceware.cygnus.com/ecos |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
46
diff
changeset
|
16 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
46
diff
changeset
|
17 // 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
|
18 // 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
|
19 // 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
|
20 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
46
diff
changeset
|
21 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
46
diff
changeset
|
22 // 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
|
23 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
46
diff
changeset
|
24 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
46
diff
changeset
|
25 // 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
|
26 // 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
|
27 // 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
|
28 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
46
diff
changeset
|
29 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
46
diff
changeset
|
30 // |
| 0 | 31 //####COPYRIGHTEND#### |
| 32 //=========================================================================== | |
| 33 //#####DESCRIPTIONBEGIN#### | |
| 34 // | |
| 35 // Author(s): jlarmour | |
| 2 | 36 // Contributors: jlarmour |
| 0 | 37 // Date: 1998-02-13 |
| 38 // Purpose: | |
| 39 // Description: | |
| 40 // Usage: | |
| 41 // | |
| 42 //####DESCRIPTIONEND#### | |
| 43 // | |
| 44 //=========================================================================== | |
| 45 // | |
| 46 // This code is based on original code with the following copyright: | |
| 47 // | |
| 48 /*- | |
| 49 * Copyright (c) 1990 The Regents of the University of California. | |
| 50 * All rights reserved. | |
| 51 * | |
| 52 * Redistribution and use in source and binary forms, with or without | |
| 53 * modification, are permitted provided that the following conditions | |
| 54 * are met: | |
| 55 * 1. Redistributions of source code must retain the above copyright | |
| 56 * notice, this list of conditions and the following disclaimer. | |
| 57 * 2. Redistributions in binary form must reproduce the above copyright | |
| 58 * notice, this list of conditions and the following disclaimer in the | |
| 59 * 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
|
60 * 3. Neither the name of the University nor the names of its contributors |
| 0 | 61 * may be used to endorse or promote products derived from this software |
| 62 * without specific prior written permission. | |
| 63 * | |
| 64 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 67 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 74 * SUCH DAMAGE. | |
| 75 */ | |
| 76 | |
| 77 | |
| 78 // CONFIGURATION | |
| 79 | |
| 80 #include <pkgconf/libc.h> // Configuration header | |
| 81 | |
| 82 // Include the C library? | |
| 83 #ifdef CYGPKG_LIBC | |
| 84 | |
| 85 // INCLUDES | |
| 86 | |
| 87 #include <cyg/infra/cyg_type.h> // Common type definitions and support | |
| 88 #include <cyg/infra/cyg_trac.h> // Tracing support | |
| 89 #include <cyg/infra/cyg_ass.h> // Assertion support | |
| 90 #include <limits.h> // Definition of LONG_MIN and LONG_MAX | |
| 91 #include <ctype.h> // Definition of many ctype functions | |
| 92 #include <errno.h> // Error code definitions | |
| 93 #include <stdlib.h> // Header for all stdlib functions | |
| 94 // (like this one) | |
| 95 #include "clibincl/stdlibsupp.hxx" // Support for stdlib functions | |
| 96 | |
| 97 | |
| 98 // EXPORTED SYMBOLS | |
| 99 | |
| 100 externC long | |
| 101 strtol( const char *nptr, char **endptr, int base ) \ | |
| 102 CYGPRI_LIBC_WEAK_ALIAS("_strtol"); | |
| 103 | |
| 104 // FUNCTIONS | |
| 105 | |
| 106 // | |
| 107 // Convert a string to a long integer. | |
| 108 // | |
| 109 // Ignores `locale' stuff. Assumes that the upper and lower case | |
| 110 // alphabets and digits are each contiguous. | |
| 111 // | |
| 112 | |
| 113 long | |
| 114 _strtol( const char *nptr, char **endptr, int base ) | |
| 115 { | |
| 116 const char *s = nptr; | |
| 117 unsigned long acc; | |
| 118 int c; | |
| 119 unsigned long cutoff; | |
| 120 int neg = 0, any, cutlim; | |
| 121 | |
| 122 CYG_REPORT_FUNCNAMETYPE( "_strtol", "returning long %d" ); | |
| 123 CYG_REPORT_FUNCARG3( "nptr=%08x, endptr=%08x, base=%d", | |
| 124 nptr, endptr, base ); | |
| 125 CYG_CHECK_DATA_PTR( nptr, "nptr is not a valid pointer!" ); | |
| 126 | |
| 127 if (endptr != NULL) | |
| 128 CYG_CHECK_DATA_PTR( endptr, "endptr is not a valid pointer!" ); | |
| 129 | |
| 130 // | |
| 131 // Skip white space and pick up leading +/- sign if any. | |
| 132 // If base is 0, allow 0x for hex and 0 for octal, else | |
| 133 // assume decimal; if base is already 16, allow 0x. | |
| 134 // | |
| 135 | |
| 136 do { | |
| 137 c = *s++; | |
| 138 } while (isspace(c)); | |
| 139 if (c == '-') { | |
| 140 neg = 1; | |
| 141 c = *s++; | |
| 142 } else if (c == '+') | |
| 143 c = *s++; | |
| 144 if ((base == 0 || base == 16) && | |
| 145 c == '0' && (*s == 'x' || *s == 'X')) { | |
| 146 c = s[1]; | |
| 147 s += 2; | |
| 148 base = 16; | |
| 149 } | |
| 150 if (base == 0) | |
| 151 base = c == '0' ? 8 : 10; | |
| 152 | |
| 153 // | |
| 154 // Compute the cutoff value between legal numbers and illegal | |
| 155 // numbers. That is the largest legal value, divided by the | |
| 156 // base. An input number that is greater than this value, if | |
| 157 // followed by a legal input character, is too big. One that | |
| 158 // is equal to this value may be valid or not; the limit | |
| 159 // between valid and invalid numbers is then based on the last | |
| 160 // digit. For instance, if the range for longs is | |
| 161 // [-2147483648..2147483647] and the input base is 10, | |
| 162 // cutoff will be set to 214748364 and cutlim to either | |
| 163 // 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated | |
| 164 // a value > 214748364, or equal but the next digit is > 7 (or 8), | |
| 165 // the number is too big, and we will return a range error. | |
| 166 // | |
| 167 // Set any if any `digits' consumed; make it negative to indicate | |
| 168 // overflow. | |
| 169 // | |
| 170 | |
| 171 cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; | |
| 172 cutlim = cutoff % (unsigned long)base; | |
| 173 cutoff /= (unsigned long)base; | |
| 174 for (acc = 0, any = 0;; c = *s++) { | |
| 175 if (isdigit(c)) | |
| 176 c -= '0'; | |
| 177 else if (isalpha(c)) | |
| 178 c -= isupper(c) ? 'A' - 10 : 'a' - 10; | |
| 179 else | |
| 180 break; | |
| 181 if (c >= base) | |
| 182 break; | |
| 183 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | |
| 184 any = -1; | |
| 185 else { | |
| 186 any = 1; | |
| 187 acc *= base; | |
| 188 acc += c; | |
| 189 } | |
| 190 } | |
| 191 if (any < 0) { | |
| 192 acc = neg ? LONG_MIN : LONG_MAX; | |
| 193 errno = ERANGE; | |
| 194 } else if (neg) | |
| 195 acc = -acc; | |
| 196 if (endptr != 0) | |
| 197 *endptr = (char *) (any ? s - 1 : nptr); | |
| 198 | |
| 199 CYG_REPORT_RETVAL ( acc ); | |
| 200 | |
| 201 return acc; | |
| 202 } // _strtol() | |
| 203 | |
| 204 #endif // ifdef CYGPKG_LIBC | |
| 205 | |
| 206 // EOF strtol.cxx |
