Branch data Line data Source code
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 : : #include <stdlib.h>
14 : :
15 : 80 : unsigned long int strtoul(const char *S, char **PTR,int BASE)
16 : : {
17 : 80 : unsigned long rval = 0;
18 : : short int digit;
19 : : // *PTR is S, unless PTR is NULL, in which case i override it with my own ptr
20 : : char* ptr;
21 : 80 : if (PTR == NULL)
22 : : {
23 : : //override
24 : 80 : PTR = &ptr;
25 : : }
26 : : // i use PTR to advance through the string
27 : 80 : *PTR = (char *) S;
28 : : //check if BASE is ok
29 : 80 : if ((BASE < 0) || BASE > 36)
30 : : {
31 : 10 : return 0;
32 : : }
33 : : // ignore white space at beginning of S
34 : 70 : while ((**PTR == ' ')
35 : 70 : || (**PTR == '\t')
36 : 70 : || (**PTR == '\n')
37 : 150 : || (**PTR == '\r')
38 : : )
39 : : {
40 : 10 : (*PTR)++;
41 : : }
42 : : // if BASE is 0... determine the base from the first chars...
43 : 70 : if (BASE == 0)
44 : : {
45 : : // if S starts with "0x", BASE = 16, else 10
46 : 15 : if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
47 : : {
48 : 10 : BASE = 16;
49 : : }
50 : : else
51 : : {
52 : 5 : BASE = 10;
53 : : }
54 : : }
55 : 70 : if (BASE == 16)
56 : : {
57 : : // S may start with "0x"
58 : 10 : if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
59 : : {
60 : 10 : (*PTR)++;
61 : 10 : (*PTR)++;
62 : : }
63 : : }
64 : : //until end of string
65 : 240 : while (**PTR)
66 : : {
67 : 200 : if (((**PTR) >= '0') && ((**PTR) <='9'))
68 : : {
69 : : //digit (0..9)
70 : 165 : digit = **PTR - '0';
71 : : }
72 : 35 : else if (((**PTR) >= 'a') && ((**PTR) <='z'))
73 : : {
74 : : //alphanumeric digit lowercase(a (10) .. z (35) )
75 : 20 : digit = (**PTR - 'a') + 10;
76 : : }
77 : 15 : else if (((**PTR) >= 'A') && ((**PTR) <='Z'))
78 : : {
79 : : //alphanumeric digit uppercase(a (10) .. z (35) )
80 : 5 : digit = (**PTR - 'A') + 10;
81 : : }
82 : : else
83 : : {
84 : : //end of parseable number reached...
85 : : break;
86 : : }
87 : 190 : if (digit < BASE)
88 : : {
89 : 170 : 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 : 20 : break;
96 : : }
97 : : //next...
98 : 170 : (*PTR)++;
99 : : }
100 : : //done
101 : 70 : return rval;
102 : : }
103 : :
|