You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/06/02 13:13:50 UTC

[incubator-nuttx] 03/04: libc: Implement wcsrtombs, wcsnrtombs and mbsnrtowcs

This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 7cbcbcde51052fc854c6feac0a96558cb70613fa
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Tue Jun 2 12:22:26 2020 +0800

    libc: Implement wcsrtombs, wcsnrtombs and mbsnrtowcs
    
    and update the related stuff in libs/libc/libc.csv
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
    Change-Id: Id695a7f07bf18a7b4e526297f9131c75ddb79d30
---
 include/wchar.h                  |  2 +-
 libs/libc/libc.csv               |  6 ++++-
 libs/libc/stdlib/lib_mbtowc.c    |  4 ++--
 libs/libc/wchar/Make.defs        |  2 +-
 libs/libc/wchar/lib_mbrlen.c     |  4 ++--
 libs/libc/wchar/lib_mbsnrtowcs.c | 20 +++++++++++++++-
 libs/libc/wchar/lib_mbsrtowcs.c  |  3 ++-
 libs/libc/wchar/lib_wcsnrtombs.c | 49 +++++++++++++++++++++++++++++++++++++---
 libs/libc/wchar/lib_wcsrtombs.c  | 46 +++++++++++++++++++++++++++++++++++++
 9 files changed, 124 insertions(+), 12 deletions(-)

diff --git a/include/wchar.h b/include/wchar.h
index 6b7ba0d..d5719b2 100644
--- a/include/wchar.h
+++ b/include/wchar.h
@@ -168,7 +168,7 @@ size_t            mbrtowc(FAR wchar_t *, FAR const char *, size_t,
                       FAR mbstate_t *);
 size_t            mbsnrtowcs(FAR wchar_t *, FAR const char **, size_t,
                       size_t, FAR mbstate_t *);
-size_t            mbsrtowcs(wchar_t *, FAR const char **, size_t,
+size_t            mbsrtowcs(FAR wchar_t *, FAR const char **, size_t,
                       FAR mbstate_t *);
 wint_t            putwc(wchar_t, FILE *);
 wint_t            putwchar(wchar_t);
diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv
index ce2b716..f6c6cf5 100644
--- a/libs/libc/libc.csv
+++ b/libs/libc/libc.csv
@@ -17,6 +17,7 @@
 "b16sin","fixedmath.h","","b16_t","b16_t"
 "b16sqr","fixedmath.h","!defined(CONFIG_HAVE_LONG_LONG)","b16_t","b16_t"
 "basename","libgen.h","","FAR char *","FAR char *"
+"btowc","wchar.h","defined(CONFIG_LIBC_WCHAR)","wint_t","int"
 "cfgetspeed","termios.h","defined(CONFIG_SERIAL_TERMIOS)","speed_t","FAR const struct termios *"
 "cfsetspeed","termios.h","defined(CONFIG_SERIAL_TERMIOS)","int","FAR struct termios *","speed_t"
 "chdir","unistd.h","!defined(CONFIG_DISABLE_ENVIRON)","int","FAR const char *"
@@ -85,9 +86,11 @@
 "llabs","stdlib.h","defined(CONFIG_HAVE_LONG_LONG)","long long int","long long int"
 "malloc","stdlib.h","","FAR void *","size_t"
 "match","nuttx/lib/regex.h","","int","FAR const char *","FAR const char *"
+"mbrlen","wchar.h","defined(CONFIG_LIBC_WCHAR)","size_t","FAR const char *","size_t","FAR mbstate_t *"
 "mbrtowc","wchar.h","defined(CONFIG_LIBC_WCHAR)","size_t","FAR wchar_t *","FAR const char *","size_t","FAR mbstate_t *"
 "mbsnrtowcs","wchar.h","defined(CONFIG_LIBC_WCHAR)","size_t","FAR wchar_t *","FAR const char **","size_t","size_t","FAR mbstate_t *"
-"mbtowc","stdlib.h","defined(CONFIG_LIBC_WCHAR)","int","FAR wchar_t *","FAR const wchar_t *","size_t"
+"mbsrtowcs","wchar.h","defined(CONFIG_LIBC_WCHAR)","size_t","FAR wchar_t *","FAR const char **","size_t","FAR mbstate_t *"
+"mbtowc","stdlib.h","defined(CONFIG_LIBC_WCHAR)","int","FAR wchar_t *","FAR const char *","size_t"
 "memccpy","string.h","","FAR void *","FAR void *","FAR const void *","int","size_t"
 "memchr","string.h","","FAR void *","FAR const void *","int","size_t"
 "memcmp","string.h","","int","FAR const void *","FAR const void *","size_t"
@@ -221,6 +224,7 @@
 "wcslcpy","wchar.h","defined(CONFIG_LIBC_WCHAR)","size_t","FAR wchar_t *","FAR const wchar_t *","size_t"
 "wcslen","wchar.h","defined(CONFIG_LIBC_WCHAR)","size_t","FAR const wchar_t *"
 "wcsnrtombs","wchar.h","defined(CONFIG_LIBC_WCHAR)","size_t","FAR char *","FAR const wchar_t **","size_t","size_t","FAR mbstate_t *"
+"wcsrtombs","wchar.h","defined(CONFIG_LIBC_WCHAR)","size_t","FAR char *","FAR const wchar_t **","size_t","FAR mbstate_t *"
 "wcstod","wchar.h","defined(CONFIG_LIBC_WCHAR)","double","FAR const wchar_t *","FAR wchar_t **"
 "wcstof","wchar.h","defined(CONFIG_LIBC_WCHAR)","float","FAR const wchar_t *","FAR wchar_t **"
 "wcstol","wchar.h","defined(CONFIG_LIBC_WCHAR)","long int","FAR const wchar_t *","FAR wchar_t **","int"
diff --git a/libs/libc/stdlib/lib_mbtowc.c b/libs/libc/stdlib/lib_mbtowc.c
index 159af2a..abe0f8d 100644
--- a/libs/libc/stdlib/lib_mbtowc.c
+++ b/libs/libc/stdlib/lib_mbtowc.c
@@ -50,7 +50,7 @@
  *
  ****************************************************************************/
 
-int mbtowc(FAR wchar_t * pwc, FAR const char *s, size_t n)
+int mbtowc(FAR wchar_t *pwc, FAR const char *s, size_t n)
 {
   if (s == NULL)
     {
@@ -64,7 +64,7 @@ int mbtowc(FAR wchar_t * pwc, FAR const char *s, size_t n)
 
   if (pwc)
     {
-      *pwc = (wchar_t) * s;
+      *pwc = (wchar_t)*s;
     }
 
   return (*s != '\0');
diff --git a/libs/libc/wchar/Make.defs b/libs/libc/wchar/Make.defs
index f975ca2..f9a80fc 100644
--- a/libs/libc/wchar/Make.defs
+++ b/libs/libc/wchar/Make.defs
@@ -43,7 +43,7 @@ CSRCS += lib_wcslcpy.c lib_wcsxfrm.c lib_wcrtomb.c lib_wcsftime.c
 CSRCS += lib_wcscoll.c lib_wcstol.c lib_wcstoll.c lib_wcstoul.c
 CSRCS += lib_wcstoull.c lib_wcstold.c lib_wcstof.c lib_wcstod.c
 CSRCS += lib_swprintf.c lib_mbsnrtowcs.c lib_wcsnrtombs.c
-CSRCS += lib_mbrlen.c lib_mbsrtowcs.c
+CSRCS += lib_mbrlen.c lib_mbsrtowcs.c lib_wcsrtombs.c
 
 # Add the wchar directory to the build
 
diff --git a/libs/libc/wchar/lib_mbrlen.c b/libs/libc/wchar/lib_mbrlen.c
index 44e077f..fd6b897 100644
--- a/libs/libc/wchar/lib_mbrlen.c
+++ b/libs/libc/wchar/lib_mbrlen.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * libs/libc/wchar/lib_mbrtowc.c
+ * libs/libc/wchar/lib_mbrlen.c
  *
  *   Copyright (c) 2002-2004 Tim J. Robbins.
  *   All rights reserved.
@@ -60,7 +60,7 @@
  *
  ****************************************************************************/
 
-size_t mbrlen(const char *s, size_t n, mbstate_t *ps)
+size_t mbrlen(FAR const char *s, size_t n, FAR mbstate_t *ps)
 {
   return mbrtowc(NULL, s, n, ps);
 }
diff --git a/libs/libc/wchar/lib_mbsnrtowcs.c b/libs/libc/wchar/lib_mbsnrtowcs.c
index 6df037a..4bd5ecf 100644
--- a/libs/libc/wchar/lib_mbsnrtowcs.c
+++ b/libs/libc/wchar/lib_mbsnrtowcs.c
@@ -84,7 +84,25 @@
 size_t mbsnrtowcs(FAR wchar_t *dst, FAR const char **src, size_t nms,
                   size_t len, FAR mbstate_t *ps)
 {
-  return len;
+  size_t i;
+
+  if (dst == NULL)
+    {
+      return strnlen(*src, nms);
+    }
+
+  for (i = 0; i < nms && i < len; i++)
+    {
+      dst[i] = (wchar_t)(*src)[i];
+      if (dst[i] == L'\0')
+        {
+          *src = NULL;
+          return i;
+        }
+    }
+
+  *src += i;
+  return i;
 }
 
 #endif /* CONFIG_LIBC_WCHAR */
diff --git a/libs/libc/wchar/lib_mbsrtowcs.c b/libs/libc/wchar/lib_mbsrtowcs.c
index 7cabfdd..e80d53a 100644
--- a/libs/libc/wchar/lib_mbsrtowcs.c
+++ b/libs/libc/wchar/lib_mbsrtowcs.c
@@ -61,7 +61,8 @@
  *
  ****************************************************************************/
 
-size_t mbsrtowcs(wchar_t *dst, const char **src, size_t len, mbstate_t *ps)
+size_t mbsrtowcs(FAR wchar_t *dst, FAR const char **src,
+                 size_t len, FAR mbstate_t *ps)
 {
   return mbsnrtowcs(dst, src, SIZE_MAX, len, ps);
 }
diff --git a/libs/libc/wchar/lib_wcsnrtombs.c b/libs/libc/wchar/lib_wcsnrtombs.c
index 0464af0..9143f1c 100644
--- a/libs/libc/wchar/lib_wcsnrtombs.c
+++ b/libs/libc/wchar/lib_wcsnrtombs.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * libs/libc/stdio/lib_asprintf.c
+ * libs/libc/wchar/lib_wcsnrtombs.c
  *
  *   Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <gn...@nuttx.org>
@@ -85,9 +85,52 @@
  ****************************************************************************/
 
 size_t wcsnrtombs(FAR char *dst, FAR const wchar_t **src, size_t nwc,
-                  size_t len, mbstate_t *ps)
+                  size_t len, FAR mbstate_t *ps)
 {
-  return len;
+  size_t i;
+
+  if (dst == NULL)
+    {
+      for (i = 0; i < nwc; i++)
+        {
+          wchar_t wc = (*src)[i];
+
+          if (wc < 0 || wc > 0xff)
+            {
+              set_errno(EILSEQ);
+              return -1;
+            }
+
+          if (wc == L'\0')
+            {
+              return i;
+            }
+        }
+
+      return i;
+    }
+
+  for (i = 0; i < nwc && i < len; i++)
+    {
+      wchar_t wc = (*src)[i];
+
+      if (wc < 0 || wc > 0xff)
+        {
+          *src += i;
+          set_errno(EILSEQ);
+          return -1;
+        }
+
+      dst[i] = wc;
+      if (wc == L'\0')
+        {
+          *src = NULL;
+          return i;
+        }
+    }
+
+  *src += i;
+  return i;
 }
 
 #endif /* CONFIG_LIBC_WCHAR */
diff --git a/libs/libc/wchar/lib_wcsrtombs.c b/libs/libc/wchar/lib_wcsrtombs.c
new file mode 100644
index 0000000..dc6b651
--- /dev/null
+++ b/libs/libc/wchar/lib_wcsrtombs.c
@@ -0,0 +1,46 @@
+/****************************************************************************
+ * libs/libc/wchar/lib_wcsrtombs.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <wchar.h>
+
+#ifdef CONFIG_LIBC_WCHAR
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: wcsrtombs
+ *
+ * Description:
+ *   Convert a wide-characterto a  multibyte string string
+ ****************************************************************************/
+
+size_t wcsrtombs(FAR char *dst, FAR const wchar_t **src,
+                 size_t len, FAR mbstate_t *ps)
+{
+  return wcsnrtombs(dst, src, SIZE_MAX, len, ps);
+}
+
+#endif /* CONFIG_LIBC_WCHAR */