1 |
786 |
skrzyp |
//===========================================================================
|
2 |
|
|
//
|
3 |
|
|
// strtoull.cxx
|
4 |
|
|
//
|
5 |
|
|
// String to unsigned long long int conversion function
|
6 |
|
|
//
|
7 |
|
|
//===========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//===========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): jlarmour
|
43 |
|
|
// Contributors: Fredrik Hederstierna, converted file from long to long long.
|
44 |
|
|
// Date: 2000-04-30, 2004-02-19
|
45 |
|
|
// Purpose:
|
46 |
|
|
// Description:
|
47 |
|
|
// Usage:
|
48 |
|
|
//
|
49 |
|
|
//####DESCRIPTIONEND####
|
50 |
|
|
//
|
51 |
|
|
//===========================================================================
|
52 |
|
|
//
|
53 |
|
|
// This code is based on original code with the following copyright:
|
54 |
|
|
//
|
55 |
|
|
/*
|
56 |
|
|
* Copyright (c) 1990 Regents of the University of California.
|
57 |
|
|
* All rights reserved.
|
58 |
|
|
*
|
59 |
|
|
* Redistribution and use in source and binary forms, with or without
|
60 |
|
|
* modification, are permitted provided that the following conditions
|
61 |
|
|
* are met:
|
62 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
63 |
|
|
* notice, this list of conditions and the following disclaimer.
|
64 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
65 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
66 |
|
|
* documentation and/or other materials provided with the distribution.
|
67 |
|
|
* 3. Neither the name of the University nor the names of its contributors
|
68 |
|
|
* may be used to endorse or promote products derived from this software
|
69 |
|
|
* without specific prior written permission.
|
70 |
|
|
*
|
71 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
72 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
73 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
74 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
75 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
76 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
77 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
78 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
79 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
80 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
81 |
|
|
* SUCH DAMAGE.
|
82 |
|
|
*/
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
// CONFIGURATION
|
86 |
|
|
|
87 |
|
|
#include <pkgconf/libc_stdlib.h> // Configuration header
|
88 |
|
|
|
89 |
|
|
// INCLUDES
|
90 |
|
|
|
91 |
|
|
#include <cyg/infra/cyg_type.h> // Common type definitions and support
|
92 |
|
|
#include <cyg/infra/cyg_trac.h> // Tracing support
|
93 |
|
|
#include <cyg/infra/cyg_ass.h> // Assertion support
|
94 |
|
|
#include <limits.h> // Definition of ULONG_LONG_MAX
|
95 |
|
|
#include <ctype.h> // Definition of many ctype functions
|
96 |
|
|
#include <errno.h> // Error code definitions
|
97 |
|
|
#include <stdlib.h> // Header for all stdlib functions
|
98 |
|
|
// (like this one)
|
99 |
|
|
|
100 |
|
|
|
101 |
|
|
// FUNCTIONS
|
102 |
|
|
|
103 |
|
|
//
|
104 |
|
|
// Convert a string to an unsigned long long integer.
|
105 |
|
|
//
|
106 |
|
|
// Ignores `locale' stuff. Assumes that the upper and lower case
|
107 |
|
|
// alphabets and digits are each contiguous.
|
108 |
|
|
//
|
109 |
|
|
|
110 |
|
|
unsigned long long
|
111 |
|
|
strtoull( const char *nptr, char **endptr, int base )
|
112 |
|
|
{
|
113 |
|
|
const char *s = nptr;
|
114 |
|
|
unsigned long long acc;
|
115 |
|
|
int c;
|
116 |
|
|
unsigned long long cutoff;
|
117 |
|
|
int neg = 0, any, cutlim;
|
118 |
|
|
|
119 |
|
|
CYG_REPORT_FUNCNAMETYPE( "strtoull", "returning long long %lld" );
|
120 |
|
|
CYG_REPORT_FUNCARG3( "nptr=%08x, endptr=%08x, base=%d",
|
121 |
|
|
nptr, endptr, base );
|
122 |
|
|
CYG_CHECK_DATA_PTR( nptr, "nptr is not a valid pointer!" );
|
123 |
|
|
|
124 |
|
|
if (endptr != NULL)
|
125 |
|
|
CYG_CHECK_DATA_PTR( endptr, "endptr is not a valid pointer!" );
|
126 |
|
|
//
|
127 |
|
|
// See strtoll for comments as to the logic used.
|
128 |
|
|
//
|
129 |
|
|
do {
|
130 |
|
|
c = *s++;
|
131 |
|
|
} while (isspace(c));
|
132 |
|
|
if (c == '-') {
|
133 |
|
|
neg = 1;
|
134 |
|
|
c = *s++;
|
135 |
|
|
} else if (c == '+')
|
136 |
|
|
c = *s++;
|
137 |
|
|
if ((base == 0 || base == 16) &&
|
138 |
|
|
c == '0' && (*s == 'x' || *s == 'X')) {
|
139 |
|
|
c = s[1];
|
140 |
|
|
s += 2;
|
141 |
|
|
base = 16;
|
142 |
|
|
}
|
143 |
|
|
if (base == 0)
|
144 |
|
|
base = c == '0' ? 8 : 10;
|
145 |
|
|
cutoff = (unsigned long long)ULONG_LONG_MAX / (unsigned long long)base;
|
146 |
|
|
cutlim = (unsigned long long)ULONG_LONG_MAX % (unsigned long long)base;
|
147 |
|
|
for (acc = 0, any = 0;; c = *s++) {
|
148 |
|
|
if (isdigit(c))
|
149 |
|
|
c -= '0';
|
150 |
|
|
else if (isalpha(c))
|
151 |
|
|
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
|
152 |
|
|
else
|
153 |
|
|
break;
|
154 |
|
|
if (c >= base)
|
155 |
|
|
break;
|
156 |
|
|
if ((any < 0) || (acc > cutoff) || ((acc == cutoff) && (c > cutlim)))
|
157 |
|
|
any = -1;
|
158 |
|
|
else {
|
159 |
|
|
any = 1;
|
160 |
|
|
acc *= base;
|
161 |
|
|
acc += c;
|
162 |
|
|
}
|
163 |
|
|
}
|
164 |
|
|
if (any < 0) {
|
165 |
|
|
acc = ULONG_LONG_MAX;
|
166 |
|
|
errno = ERANGE;
|
167 |
|
|
} else if (neg)
|
168 |
|
|
acc = -acc;
|
169 |
|
|
if (endptr != 0)
|
170 |
|
|
*endptr = (char *) (any ? s - 1 : nptr);
|
171 |
|
|
|
172 |
|
|
CYG_REPORT_RETVAL( acc );
|
173 |
|
|
|
174 |
|
|
return acc;
|
175 |
|
|
} // strtoull()
|
176 |
|
|
|
177 |
|
|
// EOF strtoull.cxx
|