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