source: trunk/libs/newlib/src/newlib/libc/stdlib/wcstold.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 3.9 KB
Line 
1/*
2(C) Copyright IBM Corp. 2009
3
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
9* Redistributions of source code must retain the above copyright notice,
10this list of conditions and the following disclaimer.
11* Redistributions in binary form must reproduce the above copyright
12notice, this list of conditions and the following disclaimer in the
13documentation and/or other materials provided with the distribution.
14* Neither the name of IBM nor the names of its contributors may be
15used to endorse or promote products derived from this software without
16specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28POSSIBILITY OF SUCH DAMAGE.
29*/
30
31#define _GNU_SOURCE
32#include <stdlib.h>
33#include <string.h>
34#include <wchar.h>
35#include <wctype.h>
36#include <locale.h>
37#include "local.h"
38#include "../locale/setlocale.h"
39
40long double
41wcstold_l (const wchar_t *__restrict nptr, wchar_t **__restrict endptr,
42           locale_t loc)
43{
44#ifdef _LDBL_EQ_DBL
45/* On platforms where long double is as wide as double.  */
46  return wcstod_l(nptr, endptr, loc);
47
48#else /* This is a duplicate of the code in wcstod.c, but converted to long double.  */
49
50  static const mbstate_t initial;
51  mbstate_t mbs;
52  long double val;
53  char *buf, *end;
54  const wchar_t *wcp;
55  size_t len;
56
57  while (iswspace (*nptr))
58    nptr++;
59
60  /* Convert the supplied numeric wide char string to multibyte.  */
61  wcp = nptr;
62  mbs = initial;
63  if ((len = _wcsnrtombs_l (_REENT, NULL, &wcp, (size_t) -1, 0, &mbs, loc))
64      == (size_t) -1)
65    {
66      if (endptr != NULL)
67        *endptr = (wchar_t *) nptr;
68      return 0.0L;
69    }
70
71  if ((buf = malloc (len + 1)) == NULL)
72    return 0.0L;
73
74  mbs = initial;
75  _wcsnrtombs_l (_REENT, buf, &wcp, (size_t) -1, len + 1, &mbs, loc);
76
77  val = strtold_l (buf, &end, loc);
78
79  /* We only know where the number ended in the _multibyte_
80     representation of the string. If the caller wants to know
81     where it ended, count multibyte characters to find the
82     corresponding position in the wide char string.  */
83
84  if (endptr != NULL)
85    {
86      /* The only valid multibyte char in a float converted by
87         strtold/wcstold is the radix char.  What we do here is,
88         figure out if the radix char was in the valid leading
89         float sequence in the incoming string.  If so, the
90         multibyte float string is strlen (radix char) - 1 bytes
91         longer than the incoming wide char string has characters.
92         To fix endptr, reposition end as if the radix char was
93         just one byte long.  The resulting difference (end - buf)
94         is then equivalent to the number of valid wide characters
95         in the input string.  */
96      len = strlen (__localeconv_l (loc)->decimal_point);
97      if (len > 1)
98        {
99          char *d = strstr (buf, __localeconv_l (loc)->decimal_point);
100
101          if (d && d < end)
102            end -= len - 1;
103        }
104
105      *endptr = (wchar_t *) nptr + (end - buf);
106    }
107
108  free (buf);
109
110  return val;
111#endif /* _LDBL_EQ_DBL */
112}
113
114long double
115wcstold (const wchar_t *__restrict nptr, wchar_t **__restrict endptr)
116{
117#ifdef _LDBL_EQ_DBL
118/* On platforms where long double is as wide as double.  */
119  return wcstod_l(nptr, endptr, __get_current_locale ());
120#else
121  return wcstold_l(nptr, endptr, __get_current_locale ());
122#endif
123}
Note: See TracBrowser for help on using the repository browser.