[CRT]
[reactos.git] / reactos / lib / sdk / crt / string / strtoi64.c
1 #include <precomp.h>
2
3
4 __int64
5 _strtoi64(const char *nptr, char **endptr, int base)
6 {
7 BOOL negative = FALSE;
8 __int64 ret = 0;
9
10 while(isspace(*nptr)) nptr++;
11
12 if(*nptr == '-') {
13 negative = TRUE;
14 nptr++;
15 } else if(*nptr == '+')
16 nptr++;
17
18 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
19 base = 16;
20 nptr += 2;
21 }
22
23 if(base == 0) {
24 if(*nptr=='0')
25 base = 8;
26 else
27 base = 10;
28 }
29
30 while(*nptr) {
31 char cur = tolower(*nptr);
32 int v;
33
34 if(isdigit(cur)) {
35 if(cur >= '0'+base)
36 break;
37 v = cur-'0';
38 } else {
39 if(cur<'a' || cur>='a'+base-10)
40 break;
41 v = cur-'a'+10;
42 }
43
44 if(negative)
45 v = -v;
46
47 nptr++;
48
49 if(!negative && (ret>_I64_MAX/base || ret*base>_I64_MAX-v)) {
50 ret = _I64_MAX;
51 *_errno() = ERANGE;
52 } else if(negative && (ret<_I64_MIN/base || ret*base<_I64_MIN-v)) {
53 ret = _I64_MIN;
54 *_errno() = ERANGE;
55 } else
56 ret = ret*base + v;
57 }
58
59 if(endptr)
60 *endptr = (char*)nptr;
61
62 return ret;
63 }
64
65
66 /* EOF */