|
|
1.1 root 1: /******************************************************************************
2: * Copyright (c) 2004, 2008 IBM Corporation
3: * All rights reserved.
4: * This program and the accompanying materials
5: * are made available under the terms of the BSD License
6: * which accompanies this distribution, and is available at
7: * http://www.opensource.org/licenses/bsd-license.php
8: *
9: * Contributors:
10: * IBM Corporation - initial implementation
11: *****************************************************************************/
12:
13: unsigned long int strtoul(const char *S, char **PTR,int BASE)
14: {
15: unsigned long rval = 0;
16: short int digit;
17: // *PTR is S, unless PTR is NULL, in which case i override it with my own ptr
18: char* ptr;
19: if (PTR == 0)
20: {
21: //override
22: PTR = &ptr;
23: }
24: // i use PTR to advance through the string
25: *PTR = (char *) S;
26: //check if BASE is ok
27: if ((BASE < 0) || BASE > 36)
28: {
29: return 0;
30: }
31: // ignore white space at beginning of S
32: while ((**PTR == ' ')
33: || (**PTR == '\t')
34: || (**PTR == '\n')
35: || (**PTR == '\r')
36: )
37: {
38: (*PTR)++;
39: }
40: // if BASE is 0... determine the base from the first chars...
41: if (BASE == 0)
42: {
43: // if S starts with "0x", BASE = 16, else 10
44: if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
45: {
46: BASE = 16;
47: (*PTR)++;
48: (*PTR)++;
49: }
50: else
51: {
52: BASE = 10;
53: }
54: }
55: if (BASE == 16)
56: {
57: // S may start with "0x"
58: if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
59: {
60: (*PTR)++;
61: (*PTR)++;
62: }
63: }
64: //until end of string
65: while (**PTR)
66: {
67: if (((**PTR) >= '0') && ((**PTR) <='9'))
68: {
69: //digit (0..9)
70: digit = **PTR - '0';
71: }
72: else if (((**PTR) >= 'a') && ((**PTR) <='z'))
73: {
74: //alphanumeric digit lowercase(a (10) .. z (35) )
75: digit = (**PTR - 'a') + 10;
76: }
77: else if (((**PTR) >= 'A') && ((**PTR) <='Z'))
78: {
79: //alphanumeric digit uppercase(a (10) .. z (35) )
80: digit = (**PTR - 'A') + 10;
81: }
82: else
83: {
84: //end of parseable number reached...
85: break;
86: }
87: if (digit < BASE)
88: {
89: rval = (rval * BASE) + digit;
90: }
91: else
92: {
93: //digit found, but its too big for current base
94: //end of parseable number reached...
95: break;
96: }
97: //next...
98: (*PTR)++;
99: }
100: //done
101: return rval;
102: }
103:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.