sl@0
|
1 |
/*
|
sl@0
|
2 |
* strtoll.c --
|
sl@0
|
3 |
*
|
sl@0
|
4 |
* Source code for the "strtoll" library procedure.
|
sl@0
|
5 |
*
|
sl@0
|
6 |
* Copyright (c) 1988 The Regents of the University of California.
|
sl@0
|
7 |
* Copyright (c) 1994 Sun Microsystems, Inc.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* See the file "license.terms" for information on usage and redistribution
|
sl@0
|
10 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* RCS: @(#) $Id: strtoll.c,v 1.5 2002/02/24 02:53:25 dgp Exp $
|
sl@0
|
13 |
*/
|
sl@0
|
14 |
|
sl@0
|
15 |
#include "tcl.h"
|
sl@0
|
16 |
#include "tclPort.h"
|
sl@0
|
17 |
#include <ctype.h>
|
sl@0
|
18 |
|
sl@0
|
19 |
#define TCL_WIDEINT_MAX (((Tcl_WideUInt)Tcl_LongAsWide(-1))>>1)
|
sl@0
|
20 |
|
sl@0
|
21 |
|
sl@0
|
22 |
/*
|
sl@0
|
23 |
*----------------------------------------------------------------------
|
sl@0
|
24 |
*
|
sl@0
|
25 |
* strtoll --
|
sl@0
|
26 |
*
|
sl@0
|
27 |
* Convert an ASCII string into an integer.
|
sl@0
|
28 |
*
|
sl@0
|
29 |
* Results:
|
sl@0
|
30 |
* The return value is the integer equivalent of string. If endPtr
|
sl@0
|
31 |
* is non-NULL, then *endPtr is filled in with the character
|
sl@0
|
32 |
* after the last one that was part of the integer. If string
|
sl@0
|
33 |
* doesn't contain a valid integer value, then zero is returned
|
sl@0
|
34 |
* and *endPtr is set to string.
|
sl@0
|
35 |
*
|
sl@0
|
36 |
* Side effects:
|
sl@0
|
37 |
* None.
|
sl@0
|
38 |
*
|
sl@0
|
39 |
*----------------------------------------------------------------------
|
sl@0
|
40 |
*/
|
sl@0
|
41 |
|
sl@0
|
42 |
#if TCL_WIDE_INT_IS_LONG
|
sl@0
|
43 |
long long
|
sl@0
|
44 |
#else
|
sl@0
|
45 |
Tcl_WideInt
|
sl@0
|
46 |
#endif
|
sl@0
|
47 |
strtoll(string, endPtr, base)
|
sl@0
|
48 |
CONST char *string; /* String of ASCII digits, possibly
|
sl@0
|
49 |
* preceded by white space. For bases
|
sl@0
|
50 |
* greater than 10, either lower- or
|
sl@0
|
51 |
* upper-case digits may be used.
|
sl@0
|
52 |
*/
|
sl@0
|
53 |
char **endPtr; /* Where to store address of terminating
|
sl@0
|
54 |
* character, or NULL. */
|
sl@0
|
55 |
int base; /* Base for conversion. Must be less
|
sl@0
|
56 |
* than 37. If 0, then the base is chosen
|
sl@0
|
57 |
* from the leading characters of string:
|
sl@0
|
58 |
* "0x" means hex, "0" means octal, anything
|
sl@0
|
59 |
* else means decimal.
|
sl@0
|
60 |
*/
|
sl@0
|
61 |
{
|
sl@0
|
62 |
register CONST char *p;
|
sl@0
|
63 |
Tcl_WideInt result = Tcl_LongAsWide(0);
|
sl@0
|
64 |
Tcl_WideUInt uwResult;
|
sl@0
|
65 |
|
sl@0
|
66 |
/*
|
sl@0
|
67 |
* Skip any leading blanks.
|
sl@0
|
68 |
*/
|
sl@0
|
69 |
|
sl@0
|
70 |
p = string;
|
sl@0
|
71 |
while (isspace(UCHAR(*p))) {
|
sl@0
|
72 |
p += 1;
|
sl@0
|
73 |
}
|
sl@0
|
74 |
|
sl@0
|
75 |
/*
|
sl@0
|
76 |
* Check for a sign.
|
sl@0
|
77 |
*/
|
sl@0
|
78 |
|
sl@0
|
79 |
errno = 0;
|
sl@0
|
80 |
if (*p == '-') {
|
sl@0
|
81 |
p += 1;
|
sl@0
|
82 |
uwResult = strtoull(p, endPtr, base);
|
sl@0
|
83 |
if (errno != ERANGE) {
|
sl@0
|
84 |
if (uwResult > TCL_WIDEINT_MAX+1) {
|
sl@0
|
85 |
errno = ERANGE;
|
sl@0
|
86 |
return Tcl_LongAsWide(-1);
|
sl@0
|
87 |
} else if (uwResult > TCL_WIDEINT_MAX) {
|
sl@0
|
88 |
return ~((Tcl_WideInt)TCL_WIDEINT_MAX);
|
sl@0
|
89 |
} else {
|
sl@0
|
90 |
result = -((Tcl_WideInt) uwResult);
|
sl@0
|
91 |
}
|
sl@0
|
92 |
}
|
sl@0
|
93 |
} else {
|
sl@0
|
94 |
if (*p == '+') {
|
sl@0
|
95 |
p += 1;
|
sl@0
|
96 |
}
|
sl@0
|
97 |
uwResult = strtoull(p, endPtr, base);
|
sl@0
|
98 |
if (errno != ERANGE) {
|
sl@0
|
99 |
if (uwResult > TCL_WIDEINT_MAX) {
|
sl@0
|
100 |
errno = ERANGE;
|
sl@0
|
101 |
return Tcl_LongAsWide(-1);
|
sl@0
|
102 |
} else {
|
sl@0
|
103 |
result = uwResult;
|
sl@0
|
104 |
}
|
sl@0
|
105 |
}
|
sl@0
|
106 |
}
|
sl@0
|
107 |
if ((result == 0) && (endPtr != 0) && (*endPtr == p)) {
|
sl@0
|
108 |
*endPtr = (char *) string;
|
sl@0
|
109 |
}
|
sl@0
|
110 |
return result;
|
sl@0
|
111 |
}
|