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 : 100 : long int strtol(const char *S, char **PTR,int BASE)
16 : : {
17 : 100 : long rval = 0;
18 : 100 : short int negative = 0;
19 : : short int digit;
20 : : // *PTR is S, unless PTR is NULL, in which case i override it with my own ptr
21 : : char* ptr;
22 : 100 : if (PTR == NULL)
23 : : {
24 : : //override
25 : 95 : PTR = &ptr;
26 : : }
27 : : // i use PTR to advance through the string
28 : 100 : *PTR = (char *) S;
29 : : //check if BASE is ok
30 : 100 : if ((BASE < 0) || BASE > 36)
31 : : {
32 : 10 : return 0;
33 : : }
34 : : // ignore white space at beginning of S
35 : 90 : while ((**PTR == ' ')
36 : 90 : || (**PTR == '\t')
37 : 90 : || (**PTR == '\n')
38 : 195 : || (**PTR == '\r')
39 : : )
40 : : {
41 : 15 : (*PTR)++;
42 : : }
43 : : // check if S starts with "-" in which case the return value is negative
44 : 90 : if (**PTR == '-')
45 : : {
46 : 20 : negative = 1;
47 : 20 : (*PTR)++;
48 : : }
49 : : // if BASE is 0... determine the base from the first chars...
50 : 90 : if (BASE == 0)
51 : : {
52 : : // if S starts with "0x", BASE = 16, else 10
53 : 10 : if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
54 : : {
55 : 10 : BASE = 16;
56 : : }
57 : : else
58 : : {
59 : 0 : BASE = 10;
60 : : }
61 : : }
62 : 90 : if (BASE == 16)
63 : : {
64 : : // S may start with "0x"
65 : 10 : if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
66 : : {
67 : 10 : (*PTR)++;
68 : 10 : (*PTR)++;
69 : : }
70 : : }
71 : : //until end of string
72 : 365 : while (**PTR)
73 : : {
74 : 310 : if (((**PTR) >= '0') && ((**PTR) <= '9'))
75 : : {
76 : : //digit (0..9)
77 : 270 : digit = **PTR - '0';
78 : : }
79 : 40 : else if (((**PTR) >= 'a') && ((**PTR) <='z'))
80 : : {
81 : : //alphanumeric digit lowercase(a (10) .. z (35) )
82 : 25 : digit = (**PTR - 'a') + 10;
83 : : }
84 : 15 : else if (((**PTR) >= 'A') && ((**PTR) <='Z'))
85 : : {
86 : : //alphanumeric digit uppercase(a (10) .. z (35) )
87 : 5 : digit = (**PTR - 'A') + 10;
88 : : }
89 : : else
90 : : {
91 : : //end of parseable number reached...
92 : : break;
93 : : }
94 : 300 : if (digit < BASE)
95 : : {
96 : 275 : rval = (rval * BASE) + digit;
97 : : }
98 : : else
99 : : {
100 : : //digit found, but its too big for current base
101 : : //end of parseable number reached...
102 : 25 : break;
103 : : }
104 : : //next...
105 : 275 : (*PTR)++;
106 : : }
107 : 90 : if (negative)
108 : : {
109 : 20 : return rval * -1;
110 : : }
111 : : //else
112 : 70 : return rval;
113 : : }
|