|
1449
|
1 /***********************************************************
|
|
|
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
|
|
|
3 Amsterdam, The Netherlands.
|
|
|
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
|
|
|
25 /*
|
|
|
26 ** strtoul
|
|
|
27 ** This is a general purpose routine for converting
|
|
|
28 ** an ascii string to an integer in an arbitrary base.
|
|
|
29 ** Leading white space is ignored. If 'base' is zero
|
|
|
30 ** it looks for a leading 0, 0x or 0X to tell which
|
|
|
31 ** base. If these are absent it defaults to 10.
|
|
|
32 ** Base must be 0 or between 2 and 36 (inclusive).
|
|
|
33 ** If 'ptr' is non-NULL it will contain a pointer to
|
|
|
34 ** the end of the scan.
|
|
|
35 ** Errors due to bad pointers will probably result in
|
|
|
36 ** exceptions - we don't check for them.
|
|
|
37 */
|
|
|
38
|
|
|
39 #include <ctype.h>
|
|
|
40 #include <errno.h>
|
|
|
41
|
|
|
42 unsigned long
|
|
|
43 strtoul(str, ptr, base)
|
|
|
44 register char * str;
|
|
|
45 char ** ptr;
|
|
|
46 int base;
|
|
|
47 {
|
|
|
48 register unsigned long result; /* return value of the function */
|
|
|
49 register int c; /* current input character */
|
|
|
50 register unsigned long temp; /* used in overflow testing */
|
|
|
51 int ovf; /* true if overflow occurred */
|
|
|
52
|
|
|
53 result = 0;
|
|
|
54 ovf = 0;
|
|
|
55
|
|
|
56 /* catch silly bases */
|
|
|
57 if (base != 0 && (base < 2 || base > 36))
|
|
|
58 {
|
|
|
59 if (ptr)
|
|
|
60 *ptr = str;
|
|
|
61 return 0;
|
|
|
62 }
|
|
|
63
|
|
|
64 /* skip leading white space */
|
|
|
65 while (*str && isspace(*str))
|
|
|
66 str++;
|
|
|
67
|
|
|
68 /* check for leading 0 or 0x for auto-base or base 16 */
|
|
|
69 switch (base)
|
|
|
70 {
|
|
|
71 case 0: /* look for leading 0, 0x or 0X */
|
|
|
72 if (*str == '0')
|
|
|
73 {
|
|
|
74 str++;
|
|
|
75 if (*str == 'x' || *str == 'X')
|
|
|
76 {
|
|
|
77 str++;
|
|
|
78 base = 16;
|
|
|
79 }
|
|
|
80 else
|
|
|
81 base = 8;
|
|
|
82 }
|
|
|
83 else
|
|
|
84 base = 10;
|
|
|
85 break;
|
|
|
86
|
|
|
87 case 16: /* skip leading 0x or 0X */
|
|
|
88 if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
|
|
|
89 str += 2;
|
|
|
90 break;
|
|
|
91 }
|
|
|
92
|
|
|
93 /* do the conversion */
|
|
|
94 while (c = *str)
|
|
|
95 {
|
|
|
96 if (isdigit(c) && c - '0' < base)
|
|
|
97 c -= '0';
|
|
|
98 else
|
|
|
99 {
|
|
|
100 if (isupper(c))
|
|
|
101 c = tolower(c);
|
|
|
102 if (c >= 'a' && c <= 'z')
|
|
|
103 c -= 'a' - 10;
|
|
|
104 else /* non-"digit" character */
|
|
|
105 break;
|
|
|
106 if (c >= base) /* non-"digit" character */
|
|
|
107 break;
|
|
|
108 }
|
|
|
109 temp = result;
|
|
|
110 result = result * base + c;
|
|
|
111 if ((result - c) / base != temp) /* overflow */
|
|
|
112 ovf = 1;
|
|
|
113 str++;
|
|
|
114 }
|
|
|
115
|
|
|
116 /* set pointer to point to the last character scanned */
|
|
|
117 if (ptr)
|
|
|
118 *ptr = str;
|
|
|
119 if (ovf)
|
|
|
120 {
|
|
|
121 result = ~0;
|
|
|
122 errno = ERANGE;
|
|
|
123 }
|
|
|
124 return result;
|
|
|
125 }
|
|
|
126
|
|
|
127 long
|
|
|
128 strtol(str, ptr, base)
|
|
|
129 char * str;
|
|
|
130 char ** ptr;
|
|
|
131 int base;
|
|
|
132 {
|
|
|
133 long result;
|
|
|
134 char sign;
|
|
|
135
|
|
|
136 while (*str && isspace(*str))
|
|
|
137 str++;
|
|
|
138
|
|
|
139 sign = *str;
|
|
|
140 if (sign == '+' || sign == '-')
|
|
|
141 str++;
|
|
|
142
|
|
|
143 result = (long) strtoul(str, ptr, base);
|
|
|
144
|
|
|
145 /* Signal overflow if the result appears negative,
|
|
|
146 except for the largest negative integer */
|
|
|
147 if (result < 0 && !(sign == '-' && result == -result)) {
|
|
|
148 errno = ERANGE;
|
|
|
149 result = 0x7fffffff;
|
|
|
150 }
|
|
|
151
|
|
|
152 if (sign == '-')
|
|
|
153 result = -result;
|
|
|
154
|
|
|
155 return result;
|
|
|
156 }
|