File: strtol.c

package info (click to toggle)
qemu 1%3A10.1.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 415,888 kB
  • sloc: ansic: 4,763,605; pascal: 115,173; python: 105,698; asm: 68,689; sh: 53,146; makefile: 27,519; perl: 18,863; cpp: 11,443; xml: 3,629; objc: 2,877; yacc: 2,505; php: 1,299; tcl: 1,296; lex: 1,110; sql: 71; awk: 43; sed: 35; javascript: 7
file content (115 lines) | stat: -rw-r--r-- 2,337 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/******************************************************************************
 * Copyright (c) 2004, 2008 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

#include <stdlib.h>

long int strtol(const char *S, char **PTR,int BASE)
{
	long rval = 0;
	short int negative = 0;
	short int digit;
	// *PTR is S, unless PTR is NULL, in which case i override it with my own ptr
	char* ptr;
	if (PTR == 0)
	{
		//override
		PTR = &ptr;
	}
	// i use PTR to advance through the string
	*PTR = (char *) S;
	//check if BASE is ok
	if ((BASE < 0) || BASE > 36)
	{
		return 0;
	}
	// ignore white space at beginning of S
	while ((**PTR == ' ')
			|| (**PTR == '\t')
			|| (**PTR == '\n')
			|| (**PTR == '\r')
			)
	{
		(*PTR)++;
	}
	// check if S starts with "-" in which case the return value is negative
	if (**PTR == '-')
	{
		negative = 1;
		(*PTR)++;
	}
	// if BASE is 0... determine the base from the first chars...
	if (BASE == 0)
	{
		// if S starts with "0x", BASE = 16, else 10
		if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
		{
			BASE = 16;
			(*PTR)++;
			(*PTR)++;
		}
		else
		{
			BASE = 10;
		}
	}
	if (BASE == 16)
	{
		// S may start with "0x"
		if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
		{
			(*PTR)++;
			(*PTR)++;
		}
	}
	//until end of string
	while (**PTR)
	{
		if (((**PTR) >= '0') && ((**PTR) <= '9'))
		{
			//digit (0..9)
			digit = **PTR - '0';
		}
		else if (((**PTR) >= 'a') && ((**PTR) <='z'))
		{
			//alphanumeric digit lowercase(a (10) .. z (35) )
			digit = (**PTR - 'a') + 10;
		}
		else if (((**PTR) >= 'A') && ((**PTR) <='Z'))
		{
			//alphanumeric digit uppercase(a (10) .. z (35) )
			digit = (**PTR - 'A') + 10;
		}
		else
		{
			//end of parseable number reached...
			break;
		}
		if (digit < BASE)
		{
			rval = (rval * BASE) + digit;
		}
		else
		{
			//digit found, but its too big for current base
			//end of parseable number reached...
			break;
		}
		//next...
		(*PTR)++;
	}
	if (negative)
	{
		return rval * -1;
	}
	//else
	return rval;
}