xref: /freebsd/lib/libc/locale/wcstold.c (revision 81f91de9f776f91e2eee6815d452c520f892ced3)
1542bd65fSTim J. Robbins /*-
2542bd65fSTim J. Robbins  * Copyright (c) 2002, 2003 Tim J. Robbins
3542bd65fSTim J. Robbins  * All rights reserved.
4542bd65fSTim J. Robbins  *
53c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
63c87aa1dSDavid Chisnall  * All rights reserved.
73c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
83c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
93c87aa1dSDavid 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>
383c87aa1dSDavid 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
443c87aa1dSDavid Chisnall wcstold_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
453c87aa1dSDavid 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*81f91de9SEric van Gyzen 	const wchar_t *wcp;
5284d9142fSJacques Vidrine 	size_t len;
53*81f91de9SEric van Gyzen 	size_t spaces;
543c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
55542bd65fSTim J. Robbins 
56*81f91de9SEric van Gyzen 	wcp = nptr;
57*81f91de9SEric van Gyzen 	spaces = 0;
583c87aa1dSDavid Chisnall 	while (iswspace_l(*wcp, locale)) {
593c87aa1dSDavid Chisnall 		wcp++;
603c87aa1dSDavid Chisnall 		spaces++;
613c87aa1dSDavid Chisnall 	}
62542bd65fSTim J. Robbins 
63dc763237STim J. Robbins 	mbs = initial;
643c87aa1dSDavid Chisnall 	if ((len = wcsrtombs_l(NULL, &wcp, 0, &mbs, locale)) == (size_t)-1) {
65542bd65fSTim J. Robbins 		if (endptr != NULL)
66542bd65fSTim J. Robbins 			*endptr = (wchar_t *)nptr;
67542bd65fSTim J. Robbins 		return (0.0);
68542bd65fSTim J. Robbins 	}
69*81f91de9SEric van Gyzen 	if ((buf = malloc(len + 1)) == NULL) {
70*81f91de9SEric van Gyzen 		if (endptr != NULL)
71*81f91de9SEric van Gyzen 			*endptr = (wchar_t *)nptr;
72542bd65fSTim J. Robbins 		return (0.0);
73*81f91de9SEric van Gyzen 	}
74dc763237STim J. Robbins 	mbs = initial;
753c87aa1dSDavid Chisnall 	wcsrtombs_l(buf, &wcp, len + 1, &mbs, locale);
76542bd65fSTim J. Robbins 
773c87aa1dSDavid Chisnall 	val = strtold_l(buf, &end, locale);
78542bd65fSTim J. Robbins 
793c87aa1dSDavid Chisnall 	if (endptr != NULL) {
80542bd65fSTim J. Robbins 		*endptr = (wchar_t *)nptr + (end - buf);
813c87aa1dSDavid Chisnall 		if (buf != end)
823c87aa1dSDavid Chisnall 			*endptr += spaces;
833c87aa1dSDavid Chisnall 	}
84542bd65fSTim J. Robbins 
85542bd65fSTim J. Robbins 	free(buf);
86542bd65fSTim J. Robbins 
87542bd65fSTim J. Robbins 	return (val);
88542bd65fSTim J. Robbins }
893c87aa1dSDavid Chisnall long double
903c87aa1dSDavid Chisnall wcstold(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
913c87aa1dSDavid Chisnall {
923c87aa1dSDavid Chisnall 	return wcstold_l(nptr, endptr, __get_locale());
933c87aa1dSDavid Chisnall }
94