OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [stdlib/] [wcstol.c] - Blame information for rev 179

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
/*
2
FUNCTION
3
   <<wcstol>>---wide string to long
4
 
5
INDEX
6
        wcstol
7
INDEX
8
        _wcstol_r
9
 
10
ANSI_SYNOPSIS
11
        #include <wchar.h>
12
        long wcstol(const wchar_t *<[s]>, wchar_t **<[ptr]>,int <[base]>);
13
 
14
        long _wcstol_r(void *<[reent]>,
15
                       const wchar_t *<[s]>, wchar_t **<[ptr]>,int <[base]>);
16
 
17
TRAD_SYNOPSIS
18
        #include <stdlib.h>
19
        long wcstol (<[s]>, <[ptr]>, <[base]>)
20
        wchar_t *<[s]>;
21
        wchar_t **<[ptr]>;
22
        int <[base]>;
23
 
24
        long _wcstol_r (<[reent]>, <[s]>, <[ptr]>, <[base]>)
25
        struct _reent *<[reent]>;
26
        wchar_t *<[s]>;
27
        wchar_t **<[ptr]>;
28
        int <[base]>;
29
 
30
DESCRIPTION
31
The function <<wcstol>> converts the wide string <<*<[s]>>> to
32
a <<long>>. First, it breaks down the string into three parts:
33
leading whitespace, which is ignored; a subject string consisting
34
of characters resembling an integer in the radix specified by <[base]>;
35
and a trailing portion consisting of zero or more unparseable characters,
36
and always including the terminating null character. Then, it attempts
37
to convert the subject string into a <<long>> and returns the
38
result.
39
 
40
If the value of <[base]> is 0, the subject string is expected to look
41
like a normal C integer constant: an optional sign, a possible `<<0x>>'
42
indicating a hexadecimal base, and a number. If <[base]> is between
43
2 and 36, the expected form of the subject is a sequence of letters
44
and digits representing an integer in the radix specified by <[base]>,
45
with an optional plus or minus sign. The letters <<a>>--<<z>> (or,
46
equivalently, <<A>>--<<Z>>) are used to signify values from 10 to 35;
47
only letters whose ascribed values are less than <[base]> are
48
permitted. If <[base]> is 16, a leading <<0x>> is permitted.
49
 
50
The subject sequence is the longest initial sequence of the input
51
string that has the expected form, starting with the first
52
non-whitespace character.  If the string is empty or consists entirely
53
of whitespace, or if the first non-whitespace character is not a
54
permissible letter or digit, the subject string is empty.
55
 
56
If the subject string is acceptable, and the value of <[base]> is zero,
57
<<wcstol>> attempts to determine the radix from the input string. A
58
string with a leading <<0x>> is treated as a hexadecimal value; a string with
59
a leading 0 and no <<x>> is treated as octal; all other strings are
60
treated as decimal. If <[base]> is between 2 and 36, it is used as the
61
conversion radix, as described above. If the subject string begins with
62
a minus sign, the value is negated. Finally, a pointer to the first
63
character past the converted subject string is stored in <[ptr]>, if
64
<[ptr]> is not <<NULL>>.
65
 
66
If the subject string is empty (or not in acceptable form), no conversion
67
is performed and the value of <[s]> is stored in <[ptr]> (if <[ptr]> is
68
not <<NULL>>).
69
 
70
The alternate function <<_wcstol_r>> is a reentrant version.  The
71
extra argument <[reent]> is a pointer to a reentrancy structure.
72
 
73
RETURNS
74
<<wcstol>> returns the converted value, if any. If no conversion was
75
made, 0 is returned.
76
 
77
<<wcstol>> returns <<LONG_MAX>> or <<LONG_MIN>> if the magnitude of
78
the converted value is too large, and sets <<errno>> to <<ERANGE>>.
79
 
80
PORTABILITY
81
<<wcstol>> is ANSI.
82
 
83
No supporting OS subroutines are required.
84
*/
85
 
86
/*-
87
 * Copyright (c) 1990 The Regents of the University of California.
88
 * All rights reserved.
89
 *
90
 * Redistribution and use in source and binary forms, with or without
91
 * modification, are permitted provided that the following conditions
92
 * are met:
93
 * 1. Redistributions of source code must retain the above copyright
94
 *    notice, this list of conditions and the following disclaimer.
95
 * 2. Redistributions in binary form must reproduce the above copyright
96
 *    notice, this list of conditions and the following disclaimer in the
97
 *    documentation and/or other materials provided with the distribution.
98
 * 3. All advertising materials mentioning features or use of this software
99
 *    must display the following acknowledgement:
100
 *      This product includes software developed by the University of
101
 *      California, Berkeley and its contributors.
102
 * 4. Neither the name of the University nor the names of its contributors
103
 *    may be used to endorse or promote products derived from this software
104
 *    without specific prior written permission.
105
 *
106
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
107
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
108
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
109
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
110
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
111
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
112
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
113
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
114
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
115
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
116
 * SUCH DAMAGE.
117
 */
118
 
119
 
120
#include <_ansi.h>
121
#include <limits.h>
122
#include <wctype.h>
123
#include <errno.h>
124
#include <wchar.h>
125
#include <reent.h>
126
 
127
/*
128
 * Convert a wide string to a long integer.
129
 *
130
 * Ignores `locale' stuff.  Assumes that the upper and lower case
131
 * alphabets and digits are each contiguous.
132
 */
133
long
134
_DEFUN (_wcstol_r, (rptr, nptr, endptr, base),
135
        struct _reent *rptr _AND
136
        _CONST wchar_t *nptr _AND
137
        wchar_t **endptr _AND
138
        int base)
139
{
140
        register const wchar_t *s = nptr;
141
        register unsigned long acc;
142
        register int c;
143
        register unsigned long cutoff;
144
        register int neg = 0, any, cutlim;
145
 
146
        /*
147
         * Skip white space and pick up leading +/- sign if any.
148
         * If base is 0, allow 0x for hex and 0 for octal, else
149
         * assume decimal; if base is already 16, allow 0x.
150
         */
151
        do {
152
                c = *s++;
153
        } while (iswspace(c));
154
        if (c == L'-') {
155
                neg = 1;
156
                c = *s++;
157
        } else if (c == L'+')
158
                c = *s++;
159
        if ((base == 0 || base == 16) &&
160
            c == L'0' && (*s == L'x' || *s == L'X')) {
161
                c = s[1];
162
                s += 2;
163
                base = 16;
164
        }
165
        if (base == 0)
166
                base = c == L'0' ? 8 : 10;
167
 
168
        /*
169
         * Compute the cutoff value between legal numbers and illegal
170
         * numbers.  That is the largest legal value, divided by the
171
         * base.  An input number that is greater than this value, if
172
         * followed by a legal input character, is too big.  One that
173
         * is equal to this value may be valid or not; the limit
174
         * between valid and invalid numbers is then based on the last
175
         * digit.  For instance, if the range for longs is
176
         * [-2147483648..2147483647] and the input base is 10,
177
         * cutoff will be set to 214748364 and cutlim to either
178
         * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
179
         * a value > 214748364, or equal but the next digit is > 7 (or 8),
180
         * the number is too big, and we will return a range error.
181
         *
182
         * Set any if any `digits' consumed; make it negative to indicate
183
         * overflow.
184
         */
185
        cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
186
        cutlim = cutoff % (unsigned long)base;
187
        cutoff /= (unsigned long)base;
188
        for (acc = 0, any = 0;; c = *s++) {
189
                if (iswdigit(c))
190
                        c -= L'0';
191
                else if (iswalpha(c))
192
                        c -= iswupper(c) ? L'A' - 10 : L'a' - 10;
193
                else
194
                        break;
195
                if (c >= base)
196
                        break;
197
               if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
198
                        any = -1;
199
                else {
200
                        any = 1;
201
                        acc *= base;
202
                        acc += c;
203
                }
204
        }
205
        if (any < 0) {
206
                acc = neg ? LONG_MIN : LONG_MAX;
207
                rptr->_errno = ERANGE;
208
        } else if (neg)
209
                acc = -acc;
210
        if (endptr != 0)
211
                *endptr = (wchar_t *) (any ? s - 1 : nptr);
212
        return (acc);
213
}
214
 
215
#ifndef _REENT_ONLY
216
 
217
long
218
_DEFUN (wcstol, (s, ptr, base),
219
        _CONST wchar_t *s _AND
220
        wchar_t **ptr _AND
221
        int base)
222
{
223
        return _wcstol_r (_REENT, s, ptr, base);
224
}
225
 
226
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.