Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

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