Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 25 | #ifdef HAVE_CONFIG_H |
| 26 | #include "config.h" |
| 27 | #endif |
| 28 | |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame^] | 29 | /* Convert a possibly signed character to a nonnegative int */ |
| 30 | /* XXX This assumes characters are 8 bits wide */ |
| 31 | #ifdef __CHAR_UNSIGNED__ |
| 32 | #define Py_CHARMASK(c) (c) |
| 33 | #else |
| 34 | #define Py_CHARMASK(c) ((c) & 0xff) |
| 35 | #endif |
| 36 | |
Guido van Rossum | 5c2306c | 1995-01-17 16:31:21 +0000 | [diff] [blame] | 37 | #include "rename2.h" |
| 38 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 39 | /* strtol and strtoul, renamed to avoid conflicts */ |
| 40 | |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 41 | /* |
| 42 | ** strtoul |
| 43 | ** This is a general purpose routine for converting |
| 44 | ** an ascii string to an integer in an arbitrary base. |
| 45 | ** Leading white space is ignored. If 'base' is zero |
| 46 | ** it looks for a leading 0, 0x or 0X to tell which |
| 47 | ** base. If these are absent it defaults to 10. |
| 48 | ** Base must be 0 or between 2 and 36 (inclusive). |
| 49 | ** If 'ptr' is non-NULL it will contain a pointer to |
| 50 | ** the end of the scan. |
| 51 | ** Errors due to bad pointers will probably result in |
| 52 | ** exceptions - we don't check for them. |
| 53 | */ |
| 54 | |
| 55 | #include <ctype.h> |
| 56 | #include <errno.h> |
| 57 | |
| 58 | unsigned long |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 59 | mystrtoul(str, ptr, base) |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 60 | register char * str; |
| 61 | char ** ptr; |
| 62 | int base; |
| 63 | { |
| 64 | register unsigned long result; /* return value of the function */ |
| 65 | register int c; /* current input character */ |
| 66 | register unsigned long temp; /* used in overflow testing */ |
| 67 | int ovf; /* true if overflow occurred */ |
| 68 | |
| 69 | result = 0; |
| 70 | ovf = 0; |
| 71 | |
| 72 | /* catch silly bases */ |
| 73 | if (base != 0 && (base < 2 || base > 36)) |
| 74 | { |
| 75 | if (ptr) |
| 76 | *ptr = str; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* skip leading white space */ |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame^] | 81 | while (*str && isspace(Py_CHARMASK(*str))) |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 82 | str++; |
| 83 | |
| 84 | /* check for leading 0 or 0x for auto-base or base 16 */ |
| 85 | switch (base) |
| 86 | { |
| 87 | case 0: /* look for leading 0, 0x or 0X */ |
| 88 | if (*str == '0') |
| 89 | { |
| 90 | str++; |
| 91 | if (*str == 'x' || *str == 'X') |
| 92 | { |
| 93 | str++; |
| 94 | base = 16; |
| 95 | } |
| 96 | else |
| 97 | base = 8; |
| 98 | } |
| 99 | else |
| 100 | base = 10; |
| 101 | break; |
| 102 | |
| 103 | case 16: /* skip leading 0x or 0X */ |
| 104 | if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X')) |
| 105 | str += 2; |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | /* do the conversion */ |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame^] | 110 | while (c = Py_CHARMASK(*str)) |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 111 | { |
| 112 | if (isdigit(c) && c - '0' < base) |
| 113 | c -= '0'; |
| 114 | else |
| 115 | { |
| 116 | if (isupper(c)) |
| 117 | c = tolower(c); |
| 118 | if (c >= 'a' && c <= 'z') |
| 119 | c -= 'a' - 10; |
| 120 | else /* non-"digit" character */ |
| 121 | break; |
| 122 | if (c >= base) /* non-"digit" character */ |
| 123 | break; |
| 124 | } |
| 125 | temp = result; |
| 126 | result = result * base + c; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 127 | #ifndef MPW |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 128 | if ((result - c) / base != temp) /* overflow */ |
| 129 | ovf = 1; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 130 | #endif |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 131 | str++; |
| 132 | } |
| 133 | |
| 134 | /* set pointer to point to the last character scanned */ |
| 135 | if (ptr) |
| 136 | *ptr = str; |
| 137 | if (ovf) |
| 138 | { |
| 139 | result = ~0; |
| 140 | errno = ERANGE; |
| 141 | } |
| 142 | return result; |
| 143 | } |
| 144 | |
| 145 | long |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 146 | mystrtol(str, ptr, base) |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 147 | char * str; |
| 148 | char ** ptr; |
| 149 | int base; |
| 150 | { |
| 151 | long result; |
| 152 | char sign; |
| 153 | |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame^] | 154 | while (*str && isspace(Py_CHARMASK(*str))) |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 155 | str++; |
| 156 | |
| 157 | sign = *str; |
| 158 | if (sign == '+' || sign == '-') |
| 159 | str++; |
| 160 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 161 | result = (long) mystrtoul(str, ptr, base); |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 162 | |
| 163 | /* Signal overflow if the result appears negative, |
| 164 | except for the largest negative integer */ |
| 165 | if (result < 0 && !(sign == '-' && result == -result)) { |
| 166 | errno = ERANGE; |
| 167 | result = 0x7fffffff; |
| 168 | } |
| 169 | |
| 170 | if (sign == '-') |
| 171 | result = -result; |
| 172 | |
| 173 | return result; |
| 174 | } |