blob: 4181ea1f3ddfecba47081dd388f640fbf1e9ff04 [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 Rossum5e416441996-07-31 17:52:04 +000029#ifdef WITH_THREAD
30#define _REENTRANT
31#endif
32
Guido van Rossum7f7f2741995-02-10 17:01:56 +000033/* Convert a possibly signed character to a nonnegative int */
34/* XXX This assumes characters are 8 bits wide */
35#ifdef __CHAR_UNSIGNED__
36#define Py_CHARMASK(c) (c)
37#else
38#define Py_CHARMASK(c) ((c) & 0xff)
39#endif
40
Guido van Rossum5c2306c1995-01-17 16:31:21 +000041#include "rename2.h"
42
Guido van Rossumb6775db1994-08-01 11:34:53 +000043/* strtol and strtoul, renamed to avoid conflicts */
44
Guido van Rossumbe0e9421993-12-24 10:32:00 +000045/*
46** strtoul
47** This is a general purpose routine for converting
48** an ascii string to an integer in an arbitrary base.
49** Leading white space is ignored. If 'base' is zero
50** it looks for a leading 0, 0x or 0X to tell which
51** base. If these are absent it defaults to 10.
52** Base must be 0 or between 2 and 36 (inclusive).
53** If 'ptr' is non-NULL it will contain a pointer to
54** the end of the scan.
55** Errors due to bad pointers will probably result in
56** exceptions - we don't check for them.
57*/
58
59#include <ctype.h>
60#include <errno.h>
61
62unsigned long
Guido van Rossumb6775db1994-08-01 11:34:53 +000063mystrtoul(str, ptr, base)
Guido van Rossumbe0e9421993-12-24 10:32:00 +000064register char * str;
65char ** ptr;
66int base;
67{
68 register unsigned long result; /* return value of the function */
69 register int c; /* current input character */
70 register unsigned long temp; /* used in overflow testing */
71 int ovf; /* true if overflow occurred */
72
73 result = 0;
74 ovf = 0;
75
76/* catch silly bases */
77 if (base != 0 && (base < 2 || base > 36))
78 {
79 if (ptr)
80 *ptr = str;
81 return 0;
82 }
83
84/* skip leading white space */
Guido van Rossum7f7f2741995-02-10 17:01:56 +000085 while (*str && isspace(Py_CHARMASK(*str)))
Guido van Rossumbe0e9421993-12-24 10:32:00 +000086 str++;
87
88/* check for leading 0 or 0x for auto-base or base 16 */
89 switch (base)
90 {
91 case 0: /* look for leading 0, 0x or 0X */
92 if (*str == '0')
93 {
94 str++;
95 if (*str == 'x' || *str == 'X')
96 {
97 str++;
98 base = 16;
99 }
100 else
101 base = 8;
102 }
103 else
104 base = 10;
105 break;
106
107 case 16: /* skip leading 0x or 0X */
108 if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
109 str += 2;
110 break;
111 }
112
113/* do the conversion */
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000114 while (c = Py_CHARMASK(*str))
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000115 {
116 if (isdigit(c) && c - '0' < base)
117 c -= '0';
118 else
119 {
120 if (isupper(c))
121 c = tolower(c);
122 if (c >= 'a' && c <= 'z')
123 c -= 'a' - 10;
124 else /* non-"digit" character */
125 break;
126 if (c >= base) /* non-"digit" character */
127 break;
128 }
129 temp = result;
130 result = result * base + c;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000131#ifndef MPW
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000132 if ((result - c) / base != temp) /* overflow */
133 ovf = 1;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000134#endif
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000135 str++;
136 }
137
138/* set pointer to point to the last character scanned */
139 if (ptr)
140 *ptr = str;
141 if (ovf)
142 {
143 result = ~0;
144 errno = ERANGE;
145 }
146 return result;
147}
148
149long
Guido van Rossumb6775db1994-08-01 11:34:53 +0000150mystrtol(str, ptr, base)
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000151char * str;
152char ** ptr;
153int base;
154{
155 long result;
156 char sign;
157
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000158 while (*str && isspace(Py_CHARMASK(*str)))
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000159 str++;
160
161 sign = *str;
162 if (sign == '+' || sign == '-')
163 str++;
164
Guido van Rossumb6775db1994-08-01 11:34:53 +0000165 result = (long) mystrtoul(str, ptr, base);
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000166
167 /* Signal overflow if the result appears negative,
168 except for the largest negative integer */
169 if (result < 0 && !(sign == '-' && result == -result)) {
170 errno = ERANGE;
171 result = 0x7fffffff;
172 }
173
174 if (sign == '-')
175 result = -result;
176
177 return result;
178}