From 5354bc1dc753715bd88bef6fce04ae8d01b3fcbe Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Mon, 17 Oct 2016 16:02:08 -0600 Subject: [PATCH] Add wcslen, wmemchr, wmemcmp, wmemcpy and wmemset to NuttX --- libc/Kconfig | 6 +++ libc/Makefile | 1 + libc/README.txt | 1 + libc/wchar/Make.defs | 43 ++++++++++++++++++++ libc/wchar/lib_wcslen.c | 80 +++++++++++++++++++++++++++++++++++++ libc/wchar/lib_wmemchr.c | 84 +++++++++++++++++++++++++++++++++++++++ libc/wchar/lib_wmemcmp.c | 85 ++++++++++++++++++++++++++++++++++++++++ libc/wchar/lib_wmemcpy.c | 72 ++++++++++++++++++++++++++++++++++ libc/wchar/lib_wmemset.c | 81 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 453 insertions(+) create mode 100644 libc/wchar/Make.defs create mode 100644 libc/wchar/lib_wcslen.c create mode 100644 libc/wchar/lib_wmemchr.c create mode 100644 libc/wchar/lib_wmemcmp.c create mode 100644 libc/wchar/lib_wmemcpy.c create mode 100644 libc/wchar/lib_wmemset.c diff --git a/libc/Kconfig b/libc/Kconfig index 4a2fa5ed49..6df979b0ce 100644 --- a/libc/Kconfig +++ b/libc/Kconfig @@ -104,6 +104,12 @@ config LIBC_IOCTL_VARIADIC this should just result in a garbage value for arg. But you may discover cases where something worse happens! +config LIBC_WCHAR + bool "Enable wide-characters (Unicode) support" + default n + ---help--- + By default, wide-characters support is disabled. + config LIB_RAND_ORDER int "Order of the random number generate" default 1 diff --git a/libc/Makefile b/libc/Makefile index b903fe4737..4df221514c 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -87,6 +87,7 @@ include termios/Make.defs include time/Make.defs include tls/Make.defs include unistd/Make.defs +include wchar/Make.defs include wqueue/Make.defs # REVISIT: Backslash causes problems in $(COBJS) target diff --git a/libc/README.txt b/libc/README.txt index c964749e28..2e4f902212 100644 --- a/libc/README.txt +++ b/libc/README.txt @@ -44,6 +44,7 @@ we have: string - string.h time - time.h unistd - unistd.h + wchar - wchar.h Most of these are "standard" header files; some are not: hex2bin.h and fixemath.h are non-standard. diff --git a/libc/wchar/Make.defs b/libc/wchar/Make.defs new file mode 100644 index 0000000000..5cf6997007 --- /dev/null +++ b/libc/wchar/Make.defs @@ -0,0 +1,43 @@ +############################################################################ +# libc/wchar/Make.defs +# +# Copyright (C) 2011-2012, 2014, 2016 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# 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. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 +# COPYRIGHT OWNER 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. +# +############################################################################ + +# Add the internal C files to the build + +CSRCS += lib_wcslen.c lib_wmemchr.c lib_wmemcmp.c lib_wmemcpy.c lib_wmemset.c + +# Add the wchar directory to the build + +DEPPATH += --dep-path wchar +VPATH += :wchar diff --git a/libc/wchar/lib_wcslen.c b/libc/wchar/lib_wcslen.c new file mode 100644 index 0000000000..be99ccded6 --- /dev/null +++ b/libc/wchar/lib_wcslen.c @@ -0,0 +1,80 @@ + +/**************************************************************************** + * libc/wchar/lib_wcslen.c + * + * Copyright (c)1999 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 Id: wcslen.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wcslen + * + * Description: + * The wcslen() function is the wide-character equivalent of the strlen() + * function. It determines the length of the wide-character string pointed + * to by s, excluding the terminating null wide character (L'\0'). + * + ****************************************************************************/ + +#ifdef CONFIG_LIBC_WCHAR +size_t wcslen(FAR const wchar_t * s) +{ + const wchar_t *p; + + p = s; + while (*p) + { + p++; + } + + return p - s; +} +#endif diff --git a/libc/wchar/lib_wmemchr.c b/libc/wchar/lib_wmemchr.c new file mode 100644 index 0000000000..696de1abf9 --- /dev/null +++ b/libc/wchar/lib_wmemchr.c @@ -0,0 +1,84 @@ + +/**************************************************************************** + * libc/wchar/lib_wmemchr.c + * + * Copyright (c)1999 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 Id: wmemchr.c,v 1.2 2000/12/20 14:08:31 itojun Exp + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wmemchr + * + * Description: + * The wmemchr() function is the wide-character equivalent of the memchr() + * function. It searches the n wide characters starting at s for the first + * occurrence of the wide character c. + * + ****************************************************************************/ + +#ifdef CONFIG_LIBC_WCHAR +FAR wchar_t *wmemchr(FAR wchar_t * s, wchar_t c, size_t n) +{ + size_t i; + + for (i = 0; i < n; i++) + { + if (*s == c) + { + /* LINTED const castaway */ + return (wchar_t *) s; + } + s++; + } + + return NULL; +} +#endif diff --git a/libc/wchar/lib_wmemcmp.c b/libc/wchar/lib_wmemcmp.c new file mode 100644 index 0000000000..223c3eefc8 --- /dev/null +++ b/libc/wchar/lib_wmemcmp.c @@ -0,0 +1,85 @@ + +/**************************************************************************** + * libc/wchar/lib_wmemcmp.c + * + * Copyright (c)1999 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 Id: wmemcmp.c,v 1.2 2000/12/20 14:08:31 itojun Exp + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wmemcmp + * + * Description: + * The wmemcmp() function is the wide-character equivalent of the memcmp() + * function. It compares the n wide-characters starting at s1 and the n + * wide-characters starting at s2. + * + ****************************************************************************/ + +#ifdef CONFIG_LIBC_WCHAR +int wmemcmp(FAR const wchar_t * s1, FAR const wchar_t * s2, size_t n) +{ + size_t i; + + for (i = 0; i < n; i++) + { + if (*s1 != *s2) + { + /* wchar might be unsigned */ + return *s1 > *s2 ? 1 : -1; + } + s1++; + s2++; + } + + return 0; +} +#endif diff --git a/libc/wchar/lib_wmemcpy.c b/libc/wchar/lib_wmemcpy.c new file mode 100644 index 0000000000..e5e6b926e7 --- /dev/null +++ b/libc/wchar/lib_wmemcpy.c @@ -0,0 +1,72 @@ + +/**************************************************************************** + * libc/wchar/lib_wmemcpy.c + * + * Copyright (c)1999 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 Id: wmemcpy.c,v 1.2 2000/12/20 14:08:31 itojun Exp + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wmemcpy + * + * Description: + * This function is the wide-character equivalent of the memcpy function. + * It copies n wide characters from the array starting at src to the array + * starting at dest. The arrays may not overlap. + * + ****************************************************************************/ + +#ifdef CONFIG_LIBC_WCHAR +wchar_t *wmemcpy(FAR wchar_t * d, FAR wchar_t * s, size_t n) +{ + return (wchar_t *) memcpy(d, s, n * sizeof(wchar_t)); +} +#endif diff --git a/libc/wchar/lib_wmemset.c b/libc/wchar/lib_wmemset.c new file mode 100644 index 0000000000..0deba00f4c --- /dev/null +++ b/libc/wchar/lib_wmemset.c @@ -0,0 +1,81 @@ + +/**************************************************************************** + * libc/wchar/lib_wmemset.c + * + * Copyright (c)1999 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 Id: wmemset.c,v 1.2 2000/12/20 14:08:31 itojun Exp + */ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wmemset + * + * Description: + * The wmemset() function is the wide-character equivalent of the memset() + * function. It fills the array of n wide-characters starting at s with n + * copies of the wide character c. + * + ****************************************************************************/ + +#ifdef CONFIG_LIBC_WCHAR +FAR wchar_t *wmemset(FAR wchar_t * s, wchar_t c, size_t n) +{ + size_t i; + wchar_t *p; + + p = (wchar_t *) s; + for (i = 0; i < n; i++) + { + *p = c; + p++; + } + + return s; +} +#endif