1 /* $OpenBSD: _wcstod.h,v 1.1 2009/01/13 18:18:31 kettenis Exp $ */ 2 /* $NetBSD: wcstod.c,v 1.4 2001/10/28 12:08:43 yamt Exp $ */ 3 4 /*- 5 * Copyright (c)1999, 2000, 2001 Citrus Project, 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstod.c,v 1.2 2001/09/27 16:23:57 yamt Exp $ 30 */ 31 32 /* 33 * function template for wcstof, wcstod and wcstold. 34 * 35 * parameters: 36 * FUNCNAME : function name 37 * float_type : return type 38 * STRTOD_FUNC : conversion function 39 */ 40 41 float_type 42 FUNCNAME(const wchar_t *nptr, wchar_t **endptr) 43 { 44 const wchar_t *src; 45 size_t size; 46 const wchar_t *start; 47 48 /* 49 * check length of string and call strtod 50 */ 51 src = nptr; 52 53 /* skip space first */ 54 while (iswspace(*src)) { 55 src++; 56 } 57 58 /* get length of string */ 59 start = src; 60 if (*src && wcschr(L"+-", *src)) 61 src++; 62 size = wcsspn(src, L"0123456789"); 63 src += size; 64 if (*src == L'.') {/* XXX use localeconv */ 65 src++; 66 size = wcsspn(src, L"0123456789"); 67 src += size; 68 } 69 if (*src && wcschr(L"Ee", *src)) { 70 src++; 71 if (*src && wcschr(L"+-", *src)) 72 src++; 73 size = wcsspn(src, L"0123456789"); 74 src += size; 75 } 76 size = src - start; 77 78 /* 79 * convert to a char-string and pass it to strtod. 80 * 81 * since all mb chars used to represent a double-constant 82 * are in the portable character set, we can assume 83 * that they are 1-byte chars. 84 */ 85 if (size) 86 { 87 mbstate_t st; 88 char *buf; 89 char *end; 90 const wchar_t *s; 91 size_t size_converted; 92 float_type result; 93 94 buf = malloc(size + 1); 95 if (!buf) { 96 /* error */ 97 errno = ENOMEM; /* XXX */ 98 return 0; 99 } 100 101 s = start; 102 memset(&st, 0, sizeof(st)); 103 size_converted = wcsrtombs(buf, &s, size, &st); 104 if (size != size_converted) { 105 /* XXX should not happen */ 106 free(buf); 107 errno = EILSEQ; 108 return 0; 109 } 110 111 buf[size] = 0; 112 result = STRTOD_FUNC(buf, &end); 113 114 free(buf); 115 116 if (endptr) 117 /* LINTED bad interface */ 118 *endptr = (wchar_t*)start + (end - buf); 119 120 return result; 121 } 122 123 if (endptr) 124 /* LINTED bad interface */ 125 *endptr = (wchar_t*)start; 126 127 return 0; 128 } 129