1112177Stjr/*- 2112177Stjr * Copyright (c) 2002, 2003 Tim J. Robbins 3112177Stjr * All rights reserved. 4112177Stjr * 5227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation 6227753Stheraven * All rights reserved. 7227753Stheraven * Portions of this software were developed by David Chisnall 8227753Stheraven * under sponsorship from the FreeBSD Foundation. 9227753Stheraven * 10112177Stjr * Redistribution and use in source and binary forms, with or without 11112177Stjr * modification, are permitted provided that the following conditions 12112177Stjr * are met: 13112177Stjr * 1. Redistributions of source code must retain the above copyright 14112177Stjr * notice, this list of conditions and the following disclaimer. 15112177Stjr * 2. Redistributions in binary form must reproduce the above copyright 16112177Stjr * notice, this list of conditions and the following disclaimer in the 17112177Stjr * documentation and/or other materials provided with the distribution. 18112177Stjr * 19112177Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20112177Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21112177Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22112177Stjr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23112177Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24112177Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25112177Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26112177Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27112177Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28112177Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29112177Stjr * SUCH DAMAGE. 30112177Stjr */ 31112177Stjr 32112177Stjr#include <sys/cdefs.h> 33112177Stjr__FBSDID("$FreeBSD: releng/10.2/lib/libc/locale/wcstof.c 227753 2011-11-20 14:45:42Z theraven $"); 34112177Stjr 35112177Stjr#include <stdlib.h> 36112177Stjr#include <wchar.h> 37112177Stjr#include <wctype.h> 38227753Stheraven#include "xlocale_private.h" 39112177Stjr 40112177Stjr/* 41112177Stjr * See wcstod() for comments as to the logic used. 42112177Stjr */ 43112177Stjrfloat 44227753Stheravenwcstof_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr, 45227753Stheraven locale_t locale) 46112177Stjr{ 47127998Stjr static const mbstate_t initial; 48127998Stjr mbstate_t mbs; 49112177Stjr float val; 50121796Stjr char *buf, *end; 51112177Stjr const wchar_t *wcp; 52121796Stjr size_t len; 53227753Stheraven FIX_LOCALE(locale); 54112177Stjr 55227753Stheraven while (iswspace_l(*nptr, locale)) 56112177Stjr nptr++; 57112177Stjr 58112177Stjr wcp = nptr; 59127998Stjr mbs = initial; 60227753Stheraven if ((len = wcsrtombs_l(NULL, &wcp, 0, &mbs, locale)) == (size_t)-1) { 61112177Stjr if (endptr != NULL) 62112177Stjr *endptr = (wchar_t *)nptr; 63112177Stjr return (0.0); 64112177Stjr } 65112177Stjr if ((buf = malloc(len + 1)) == NULL) 66112177Stjr return (0.0); 67127998Stjr mbs = initial; 68227753Stheraven wcsrtombs_l(buf, &wcp, len + 1, &mbs, locale); 69112177Stjr 70227753Stheraven val = strtof_l(buf, &end, locale); 71112177Stjr 72121796Stjr if (endptr != NULL) 73112177Stjr *endptr = (wchar_t *)nptr + (end - buf); 74112177Stjr 75112177Stjr free(buf); 76112177Stjr 77112177Stjr return (val); 78112177Stjr} 79227753Stheravenfloat 80227753Stheravenwcstof(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr) 81227753Stheraven{ 82227753Stheraven return wcstof_l(nptr, endptr, __get_locale()); 83227753Stheraven} 84