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