diff options
author | Mark Kettenis <kettenis@cvs.openbsd.org> | 2009-01-13 18:18:32 +0000 |
---|---|---|
committer | Mark Kettenis <kettenis@cvs.openbsd.org> | 2009-01-13 18:18:32 +0000 |
commit | 29ad94053136b52b4d06a5e9d5acd8f81c8ac857 (patch) | |
tree | ddf7c3dbe7611dc40294f10e0389934ca6650ab0 /lib | |
parent | 93ae206d29a685964b804442570adc0c66d857cb (diff) |
Add wcstof(3) and wcstold(3).
ok miod@
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/locale/Makefile.inc | 5 | ||||
-rw-r--r-- | lib/libc/locale/_wcstod.h | 128 | ||||
-rw-r--r-- | lib/libc/locale/wcstod.c | 123 | ||||
-rw-r--r-- | lib/libc/locale/wcstof.c | 13 | ||||
-rw-r--r-- | lib/libc/locale/wcstold.c | 13 |
5 files changed, 162 insertions, 120 deletions
diff --git a/lib/libc/locale/Makefile.inc b/lib/libc/locale/Makefile.inc index 7436049c410..c433947d987 100644 --- a/lib/libc/locale/Makefile.inc +++ b/lib/libc/locale/Makefile.inc @@ -1,11 +1,12 @@ -# $OpenBSD: Makefile.inc,v 1.12 2009/01/13 18:13:51 kettenis Exp $ +# $OpenBSD: Makefile.inc,v 1.13 2009/01/13 18:18:31 kettenis Exp $ # locale sources .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/locale ${LIBCSRCDIR}/locale SRCS+= _def_messages.c _def_monetary.c _def_numeric.c _def_time.c \ localeconv.c nl_langinfo.c setlocale.c iswctype.c mbrtowc_sb.c \ - multibyte_sb.c __mb_cur_max.c wcstod.c \ + multibyte_sb.c __mb_cur_max.c \ + wcstof.c wcstod.c wcstold.c \ wcstol.c wcstoul.c wcstoll.c wcstoull.c wcstoimax.c wcstoumax.c \ setrunelocale.c runeglue.c rune.c runetable.c ___runetype_mb.c \ _wctrans.c diff --git a/lib/libc/locale/_wcstod.h b/lib/libc/locale/_wcstod.h new file mode 100644 index 00000000000..70aa40d239d --- /dev/null +++ b/lib/libc/locale/_wcstod.h @@ -0,0 +1,128 @@ +/* $OpenBSD: _wcstod.h,v 1.1 2009/01/13 18:18:31 kettenis Exp $ */ +/* $NetBSD: wcstod.c,v 1.4 2001/10/28 12:08:43 yamt Exp $ */ + +/*- + * Copyright (c)1999, 2000, 2001 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstod.c,v 1.2 2001/09/27 16:23:57 yamt Exp $ + */ + +/* + * function template for wcstof, wcstod and wcstold. + * + * parameters: + * FUNCNAME : function name + * float_type : return type + * STRTOD_FUNC : conversion function + */ + +float_type +FUNCNAME(const wchar_t *nptr, wchar_t **endptr) +{ + const wchar_t *src; + size_t size; + const wchar_t *start; + + /* + * check length of string and call strtod + */ + src = nptr; + + /* skip space first */ + while (iswspace(*src)) { + src++; + } + + /* get length of string */ + start = src; + if (*src && wcschr(L"+-", *src)) + src++; + size = wcsspn(src, L"0123456789"); + src += size; + if (*src == L'.') {/* XXX use localeconv */ + src++; + size = wcsspn(src, L"0123456789"); + src += size; + } + if (*src && wcschr(L"Ee", *src)) { + src++; + if (*src && wcschr(L"+-", *src)) + src++; + size = wcsspn(src, L"0123456789"); + src += size; + } + size = src - start; + + /* + * convert to a char-string and pass it to strtod. + * + * since all mb chars used to represent a double-constant + * are in the portable character set, we can assume + * that they are 1-byte chars. + */ + if (size) + { + mbstate_t st; + char *buf; + char *end; + const wchar_t *s; + size_t size_converted; + float_type result; + + buf = malloc(size + 1); + if (!buf) { + /* error */ + errno = ENOMEM; /* XXX */ + return 0; + } + + s = start; + memset(&st, 0, sizeof(st)); + size_converted = wcsrtombs(buf, &s, size, &st); + if (size != size_converted) { + /* XXX should not happen */ + free(buf); + errno = EILSEQ; + return 0; + } + + buf[size] = 0; + result = STRTOD_FUNC(buf, &end); + + free(buf); + + if (endptr) + /* LINTED bad interface */ + *endptr = (wchar_t*)start + (end - buf); + + return result; + } + + if (endptr) + /* LINTED bad interface */ + *endptr = (wchar_t*)start; + + return 0; +} diff --git a/lib/libc/locale/wcstod.c b/lib/libc/locale/wcstod.c index 22467f0eb68..957d0a1492a 100644 --- a/lib/libc/locale/wcstod.c +++ b/lib/libc/locale/wcstod.c @@ -1,126 +1,13 @@ -/* $OpenBSD: wcstod.c,v 1.2 2008/10/05 19:52:34 kettenis Exp $ */ -/* $NetBSD: wcstod.c,v 1.4 2001/10/28 12:08:43 yamt Exp $ */ +/* $OpenBSD: wcstod.c,v 1.3 2009/01/13 18:18:31 kettenis Exp $ */ -/*- - * Copyright (c)1999, 2000, 2001 Citrus Project, - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstod.c,v 1.2 2001/09/27 16:23:57 yamt Exp $ - */ - -#include <sys/cdefs.h> #include <errno.h> #include <stdlib.h> #include <string.h> #include <wchar.h> #include <wctype.h> -double -wcstod(const wchar_t *nptr, wchar_t **endptr) -{ - const wchar_t *src; - size_t size; - const wchar_t *start; - - /* - * check length of string and call strtod - */ - src = nptr; - - /* skip space first */ - while (iswspace(*src)) { - src++; - } - - /* get length of string */ - start = src; - if (*src && wcschr(L"+-", *src)) - src++; - size = wcsspn(src, L"0123456789"); - src += size; - if (*src == L'.') {/* XXX use localeconv */ - src++; - size = wcsspn(src, L"0123456789"); - src += size; - } - if (*src && wcschr(L"Ee", *src)) { - src++; - if (*src && wcschr(L"+-", *src)) - src++; - size = wcsspn(src, L"0123456789"); - src += size; - } - size = src - start; - - /* - * convert to a char-string and pass it to strtod. - * - * since all mb chars used to represent a double-constant - * are in the portable character set, we can assume - * that they are 1-byte chars. - */ - if (size) - { - mbstate_t st; - char *buf; - char *end; - const wchar_t *s; - size_t size_converted; - double result; - - buf = malloc(size + 1); - if (!buf) { - /* error */ - errno = ENOMEM; /* XXX */ - return 0; - } - - s = start; - memset(&st, 0, sizeof(st)); - size_converted = wcsrtombs(buf, &s, size, &st); - if (size != size_converted) { - /* XXX should not happen */ - free(buf); - errno = EILSEQ; - return 0; - } - - buf[size] = 0; - result = strtod(buf, &end); - - free(buf); - - if (endptr) - /* LINTED bad interface */ - *endptr = (wchar_t*)start + (end - buf); - - return result; - } - - if (endptr) - /* LINTED bad interface */ - *endptr = (wchar_t*)start; +#define FUNCNAME wcstod +typedef double float_type; +#define STRTOD_FUNC strtod - return 0; -} +#include "_wcstod.h" diff --git a/lib/libc/locale/wcstof.c b/lib/libc/locale/wcstof.c new file mode 100644 index 00000000000..40d76c791de --- /dev/null +++ b/lib/libc/locale/wcstof.c @@ -0,0 +1,13 @@ +/* $OpenBSD: wcstof.c,v 1.1 2009/01/13 18:18:31 kettenis Exp $ */ + +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> +#include <wctype.h> + +#define FUNCNAME wcstof +typedef float float_type; +#define STRTOD_FUNC strtof + +#include "_wcstod.h" diff --git a/lib/libc/locale/wcstold.c b/lib/libc/locale/wcstold.c new file mode 100644 index 00000000000..a6425422e9a --- /dev/null +++ b/lib/libc/locale/wcstold.c @@ -0,0 +1,13 @@ +/* $OpenBSD: wcstold.c,v 1.1 2009/01/13 18:18:31 kettenis Exp $ */ + +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> +#include <wctype.h> + +#define FUNCNAME wcstold +typedef long double float_type; +#define STRTOD_FUNC strtold + +#include "_wcstod.h" |