blob: a6462837899b688ca614dfca4e2d6f258dfa3a58 [file] [log] [blame]
Guido van Rossumbe0e9421993-12-24 10:32:00 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumbe0e9421993-12-24 10:32:00 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossumb6775db1994-08-01 11:34:53 +000025#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
Guido van Rossum7f7f2741995-02-10 17:01:56 +000029/* 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 Rossum5c2306c1995-01-17 16:31:21 +000037#include "rename2.h"
38
Guido van Rossumb6775db1994-08-01 11:34:53 +000039/* strtol and strtoul, renamed to avoid conflicts */
40
Guido van Rossumbe0e9421993-12-24 10:32:00 +000041/*
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
58unsigned long
Guido van Rossumb6775db1994-08-01 11:34:53 +000059mystrtoul(str, ptr, base)
Guido van Rossumbe0e9421993-12-24 10:32:00 +000060register char * str;
61char ** ptr;
62int 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 Rossum7f7f2741995-02-10 17:01:56 +000081 while (*str && isspace(Py_CHARMASK(*str)))
Guido van Rossumbe0e9421993-12-24 10:32:00 +000082 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 Rossum7f7f2741995-02-10 17:01:56 +0000110 while (c = Py_CHARMASK(*str))
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000111 {
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 Rossumb6775db1994-08-01 11:34:53 +0000127#ifndef MPW
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000128 if ((result - c) / base != temp) /* overflow */
129 ovf = 1;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000130#endif
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000131 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
145long
Guido van Rossumb6775db1994-08-01 11:34:53 +0000146mystrtol(str, ptr, base)
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000147char * str;
148char ** ptr;
149int base;
150{
151 long result;
152 char sign;
153
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000154 while (*str && isspace(Py_CHARMASK(*str)))
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000155 str++;
156
157 sign = *str;
158 if (sign == '+' || sign == '-')
159 str++;
160
Guido van Rossumb6775db1994-08-01 11:34:53 +0000161 result = (long) mystrtoul(str, ptr, base);
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000162
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}