xref: /freebsd/lib/libc/locale/wcstold.c (revision 3c87aa1d3dc1d8dad3efad322852a8e1e76dee55)
1542bd65fSTim J. Robbins /*-
2542bd65fSTim J. Robbins  * Copyright (c) 2002, 2003 Tim J. Robbins
3542bd65fSTim J. Robbins  * All rights reserved.
4542bd65fSTim J. Robbins  *
5*3c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
6*3c87aa1dSDavid Chisnall  * All rights reserved.
7*3c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
8*3c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
9*3c87aa1dSDavid Chisnall  *
10542bd65fSTim J. Robbins  * Redistribution and use in source and binary forms, with or without
11542bd65fSTim J. Robbins  * modification, are permitted provided that the following conditions
12542bd65fSTim J. Robbins  * are met:
13542bd65fSTim J. Robbins  * 1. Redistributions of source code must retain the above copyright
14542bd65fSTim J. Robbins  *    notice, this list of conditions and the following disclaimer.
15542bd65fSTim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
16542bd65fSTim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
17542bd65fSTim J. Robbins  *    documentation and/or other materials provided with the distribution.
18542bd65fSTim J. Robbins  *
19542bd65fSTim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20542bd65fSTim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21542bd65fSTim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22542bd65fSTim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23542bd65fSTim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24542bd65fSTim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25542bd65fSTim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26542bd65fSTim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27542bd65fSTim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28542bd65fSTim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29542bd65fSTim J. Robbins  * SUCH DAMAGE.
30542bd65fSTim J. Robbins  */
31542bd65fSTim J. Robbins 
32542bd65fSTim J. Robbins #include <sys/cdefs.h>
33542bd65fSTim J. Robbins __FBSDID("$FreeBSD$");
34542bd65fSTim J. Robbins 
35542bd65fSTim J. Robbins #include <stdlib.h>
36542bd65fSTim J. Robbins #include <wchar.h>
37542bd65fSTim J. Robbins #include <wctype.h>
38*3c87aa1dSDavid Chisnall #include "xlocale_private.h"
39542bd65fSTim J. Robbins 
40542bd65fSTim J. Robbins /*
41542bd65fSTim J. Robbins  * See wcstod() for comments as to the logic used.
42542bd65fSTim J. Robbins  */
43542bd65fSTim J. Robbins long double
44*3c87aa1dSDavid Chisnall wcstold_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
45*3c87aa1dSDavid Chisnall 		locale_t locale)
46542bd65fSTim J. Robbins {
47dc763237STim J. Robbins 	static const mbstate_t initial;
48dc763237STim J. Robbins 	mbstate_t mbs;
49542bd65fSTim J. Robbins 	long double val;
5084d9142fSJacques Vidrine 	char *buf, *end;
51*3c87aa1dSDavid Chisnall 	const wchar_t *wcp = nptr;
5284d9142fSJacques Vidrine 	size_t len;
53*3c87aa1dSDavid Chisnall 	size_t spaces = 0;
54*3c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
55542bd65fSTim J. Robbins 
56*3c87aa1dSDavid Chisnall 	while (iswspace_l(*wcp, locale)) {
57*3c87aa1dSDavid Chisnall 		wcp++;
58*3c87aa1dSDavid Chisnall 		spaces++;
59*3c87aa1dSDavid Chisnall 	}
60542bd65fSTim J. Robbins 
61542bd65fSTim J. Robbins 	wcp = nptr;
62dc763237STim J. Robbins 	mbs = initial;
63*3c87aa1dSDavid Chisnall 	if ((len = wcsrtombs_l(NULL, &wcp, 0, &mbs, locale)) == (size_t)-1) {
64542bd65fSTim J. Robbins 		if (endptr != NULL)
65542bd65fSTim J. Robbins 			*endptr = (wchar_t *)nptr;
66542bd65fSTim J. Robbins 		return (0.0);
67542bd65fSTim J. Robbins 	}
68542bd65fSTim J. Robbins 	if ((buf = malloc(len + 1)) == NULL)
69542bd65fSTim J. Robbins 		return (0.0);
70dc763237STim J. Robbins 	mbs = initial;
71*3c87aa1dSDavid Chisnall 	wcsrtombs_l(buf, &wcp, len + 1, &mbs, locale);
72542bd65fSTim J. Robbins 
73*3c87aa1dSDavid Chisnall 	val = strtold_l(buf, &end, locale);
74542bd65fSTim J. Robbins 
75*3c87aa1dSDavid Chisnall 	if (endptr != NULL) {
76*3c87aa1dSDavid Chisnall 		/* XXX Assume each wide char is one byte. */
77542bd65fSTim J. Robbins 		*endptr = (wchar_t *)nptr + (end - buf);
78*3c87aa1dSDavid Chisnall 		if (buf != end)
79*3c87aa1dSDavid Chisnall 			*endptr += spaces;
80*3c87aa1dSDavid Chisnall 	}
81542bd65fSTim J. Robbins 
82542bd65fSTim J. Robbins 	free(buf);
83542bd65fSTim J. Robbins 
84542bd65fSTim J. Robbins 	return (val);
85542bd65fSTim J. Robbins }
86*3c87aa1dSDavid Chisnall long double
87*3c87aa1dSDavid Chisnall wcstold(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
88*3c87aa1dSDavid Chisnall {
89*3c87aa1dSDavid Chisnall 	return wcstold_l(nptr, endptr, __get_locale());
90*3c87aa1dSDavid Chisnall }
91