Debian Codegraph @

pTk/strtoul.c

raw symbols SCIP
1 /*
2 * strtoul.c --
3 *
4 * Source code for the "strtoul" library procedure.
5 *
6 * Copyright (c) 1988 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * Permission is hereby granted, without written agreement and without
10 * license or royalty fees, to use, copy, modify, and distribute this
11 * software and its documentation for any purpose, provided that the
12 * above copyright notice and the following two paragraphs appear in
13 * all copies of this software.
14 *
15 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
16 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
17 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
18 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19 *
20 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 */
26
27 #ifndef lint
28 static char rcsid[] = "$Header: /home/auspex6/CVSROOT/tcl/compat/strtoul.c,v 1.1.1.1 1993/11/29 10:44:46 a904209 Exp $ SPRITE (Berkeley)";
29 #endif /* not lint */
30
31 #include <ctype.h>
32
33 /*
34 * The table below is used to convert from ASCII digits to a
35 * numerical equivalent. It maps from '0' through 'z' to integers
36 * (100 for non-digit characters).
37 */
38
39 static char cvtIn[] = {
40 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* '0' - '9' */
41 100, 100, 100, 100, 100, 100, 100, /* punctuation */
42 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* 'A' - 'Z' */
43 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
44 30, 31, 32, 33, 34, 35,
45 100, 100, 100, 100, 100, 100, /* punctuation */
46 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* 'a' - 'z' */
47 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
48 30, 31, 32, 33, 34, 35};
49
50 /*
51 *----------------------------------------------------------------------
52 *
53 * strtoul --
54 *
55 * Convert an ASCII string into an integer.
56 *
57 * Results:
58 * The return value is the integer equivalent of string. If endPtr
59 * is non-NULL, then *endPtr is filled in with the character
60 * after the last one that was part of the integer. If string
61 * doesn't contain a valid integer value, then zero is returned
62 * and *endPtr is set to string.
63 *
64 * Side effects:
65 * None.
66 *
67 *----------------------------------------------------------------------
68 */
69
70 unsigned long int
71 strtoul(string, endPtr, base)
72 char *string; /* String of ASCII digits, possibly
73 * preceded by white space. For bases
74 * greater than 10, either lower- or
75 * upper-case digits may be used.
76 */
77 char **endPtr; /* Where to store address of terminating
78 * character, or NULL. */
79 int base; /* Base for conversion. Must be less
80 * than 37. If 0, then the base is chosen
81 * from the leading characters of string:
82 * "0x" means hex, "0" means octal, anything
83 * else means decimal.
84 */
85 {
86 register char *p;
87 register unsigned long int result = 0;
88 register unsigned digit;
89 int anyDigits = 0;
90
91 /*
92 * Skip any leading blanks.
93 */
94
95 p = string;
96 while (isspace(*p)) {
97 p += 1;
98 }
99
100 /*
101 * If no base was provided, pick one from the leading characters
102 * of the string.
103 */
104
105 if (base == 0)
106 {
107 if (*p == '0') {
108 p += 1;
109 if (*p == 'x') {
110 p += 1;
111 base = 16;
112 } else {
113
114 /*
115 * Must set anyDigits here, otherwise "0" produces a
116 * "no digits" error.
117 */
118
119 anyDigits = 1;
120 base = 8;
121 }
122 }
123 else base = 10;
124 } else if (base == 16) {
125
126 /*
127 * Skip a leading "0x" from hex numbers.
128 */
129
130 if ((p[0] == '0') && (p[1] == 'x')) {
131 p += 2;
132 }
133 }
134
135 /*
136 * Sorry this code is so messy, but speed seems important. Do
137 * different things for base 8, 10, 16, and other.
138 */
139
140 if (base == 8) {
141 for ( ; ; p += 1) {
142 digit = *p - '0';
143 if (digit > 7) {
144 break;
145 }
146 result = (result << 3) + digit;
147 anyDigits = 1;
148 }
149 } else if (base == 10) {
150 for ( ; ; p += 1) {
151 digit = *p - '0';
152 if (digit > 9) {
153 break;
154 }
155 result = (10*result) + digit;
156 anyDigits = 1;
157 }
158 } else if (base == 16) {
159 for ( ; ; p += 1) {
160 digit = *p - '0';
161 if (digit > ('z' - '0')) {
162 break;
163 }
164 digit = cvtIn[digit];
165 if (digit > 15) {
166 break;
167 }
168 result = (result << 4) + digit;
169 anyDigits = 1;
170 }
171 } else {
172 for ( ; ; p += 1) {
173 digit = *p - '0';
174 if (digit > ('z' - '0')) {
175 break;
176 }
177 digit = cvtIn[digit];
178 if (digit >= base) {
179 break;
180 }
181 result = result*base + digit;
182 anyDigits = 1;
183 }
184 }
185
186 /*
187 * See if there were any digits at all.
188 */
189
190 if (!anyDigits) {
191 p = string;
192 }
193
194 if (endPtr != 0) {
195 *endPtr = p;
196 }
197
198 return result;
199 }