1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: strtoofft.c,v 1.5 2004/02/19 08:12:13 bagder Exp $
22 ***************************************************************************/
25 #include "strtoofft.h"
27 #ifdef NEED_CURL_STRTOLL
32 static int get_char(char c, int base);
35 * Emulated version of the strtoll function. This extracts a long long
36 * value from the given input string and returns it.
39 curlx_strtoll(const char *nptr, char **endptr, int base)
48 /* Skip leading whitespace. */
50 while (isspace((int)end[0])) {
54 /* Handle the sign, if any. */
59 else if (end[0] == '+') {
62 else if (end[0] == '\0') {
63 /* We had nothing but perhaps some whitespace -- there was no number. */
70 /* Handle special beginnings, if present and allowed. */
71 if (end[0] == '0' && end[1] == 'x') {
72 if (base == 16 || base == 0) {
77 else if (end[0] == '0') {
78 if (base == 8 || base == 0) {
84 /* Matching strtol, if the base is 0 and it doesn't look like
85 * the number is octal or hex, we assume it's base 10.
91 /* Loop handling digits. */
94 for (i = get_char(end[0], base);
96 end++, i = get_char(end[0], base)) {
97 newval = base * value + i;
99 /* We've overflowed. */
115 value = 0x8000000000000000L;
117 value = 0x7FFFFFFFFFFFFFFFL;
129 * Returns the value of c in the given base, or -1 if c cannot
130 * be interpreted properly in that base (i.e., is out of range,
133 * @param c the character to interpret according to base
134 * @param base the base in which to interpret c
136 * @return the value of c in base, or -1 if c isn't in range
138 static int get_char(char c, int base)
141 if (c <= '9' && c >= '0') {
144 else if (c <= 'Z' && c >= 'A') {
145 value = c - 'A' + 10;
147 else if (c <= 'z' && c >= 'a') {
148 value = c - 'a' + 10;
157 #endif /* Only present if we need strtoll, but don't have it. */