From d508a2e23073f9999abcefa18383290c2f5382ad Mon Sep 17 00:00:00 2001 From: guoshichao Date: Fri, 15 Dec 2023 12:01:26 +0800 Subject: [PATCH 1/3] libs/libc/wchar: add fgetwc implementation Signed-off-by: guoshichao --- include/wchar.h | 3 +- libs/libc/libc.csv | 2 + libs/libc/stdio/CMakeLists.txt | 3 +- libs/libc/stdio/Make.defs | 2 +- libs/libc/stdio/lib_fgetwc.c | 121 +++++++++++++++++++++++++++++++++ 5 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 libs/libc/stdio/lib_fgetwc.c diff --git a/include/wchar.h b/include/wchar.h index 45db66054197c..7d954287e7e78 100644 --- a/include/wchar.h +++ b/include/wchar.h @@ -142,7 +142,8 @@ extern "C" wint_t btowc(int); int fwprintf(FILE *, FAR const wchar_t *, ...); int fwscanf(FILE *, FAR const wchar_t *, ...); -wint_t fgetwc(FILE *); +wint_t fgetwc(FAR FILE *); +wint_t fgetwc_unlocked(FAR FILE *f); FAR wchar_t *fgetws(wchar_t *, int, FILE *); wint_t fputwc(wchar_t, FILE *); wint_t fputwc_unlocked(wchar_t, FAR FILE *); diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv index e19a5e3101b26..f05cc9f0c8ab3 100644 --- a/libs/libc/libc.csv +++ b/libs/libc/libc.csv @@ -64,6 +64,8 @@ "fgetc","stdio.h","defined(CONFIG_FILE_STREAM)","int","FAR FILE *" "fgetpos","stdio.h","defined(CONFIG_FILE_STREAM)","int","FAR FILE *","FAR fpos_t *" "fgets","stdio.h","defined(CONFIG_FILE_STREAM)","FAR char *","FAR char *","int","FAR FILE *" +"fgetwc","wchar.h","defined(CONFIG_FILE_STREAM)","wint_t","FAR FILE *" +"fgetwc_unlocked","wchar.h","defined(CONFIG_FILE_STREAM)","wint_t","FAR FILE *" "fileno","stdio.h","","int","FAR FILE *" "flockfile","stdio.h","!defined(CONFIG_FILE_STREAM)","void","FAR FILE *" "fnmatch","fnmatch.h","","int","FAR const char *","FAR const char *","int" diff --git a/libs/libc/stdio/CMakeLists.txt b/libs/libc/stdio/CMakeLists.txt index 4646e808c1501..6bb5234c7dadd 100644 --- a/libs/libc/stdio/CMakeLists.txt +++ b/libs/libc/stdio/CMakeLists.txt @@ -105,7 +105,8 @@ if(CONFIG_FILE_STREAM) lib_putwc.c lib_fputws.c lib_fopencookie.c - lib_fmemopen.c) + lib_fmemopen.c + lib_fgetwc.c) endif() target_sources(c PRIVATE ${SRCS}) diff --git a/libs/libc/stdio/Make.defs b/libs/libc/stdio/Make.defs index 0878d3a511235..e182ce17c20a0 100644 --- a/libs/libc/stdio/Make.defs +++ b/libs/libc/stdio/Make.defs @@ -48,7 +48,7 @@ CSRCS += lib_feof.c lib_ferror.c lib_rewind.c lib_clearerr.c CSRCS += lib_scanf.c lib_vscanf.c lib_fscanf.c lib_vfscanf.c lib_tmpfile.c CSRCS += lib_setbuf.c lib_setvbuf.c lib_libfilelock.c lib_libgetstreams.c CSRCS += lib_setbuffer.c lib_fputwc.c lib_putwc.c lib_fputws.c -CSRCS += lib_fopencookie.c lib_fmemopen.c lib_open_memstream.c +CSRCS += lib_fopencookie.c lib_fmemopen.c lib_open_memstream.c lib_fgetwc.c endif # Add the stdio directory to the build diff --git a/libs/libc/stdio/lib_fgetwc.c b/libs/libc/stdio/lib_fgetwc.c new file mode 100644 index 0000000000000..90b451a6c9b8d --- /dev/null +++ b/libs/libc/stdio/lib_fgetwc.c @@ -0,0 +1,121 @@ +/**************************************************************************** + * libs/libc/stdio/lib_fgetwc.c + * + * Copyright © 2005-2014 Rich Felker, et al. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +#ifdef CONFIG_FILE_STREAM + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: fgetwc_unlocked + * + * Description: + * Get wide character from stream without lock the stream + * + * Input Parameters: + * f - Pointer to a FILE object that identifies an input stream, during + * the read operaiton, the stream will not be locked + * + * Returned Value: + * Return the character read is returned, + * Return WEOF is the sequence of bytes that read cannot be interpreted as + * a valid wide characted, and sets the errno to EILSEQ + * + ****************************************************************************/ + +wint_t fgetwc_unlocked(FAR FILE *f) +{ + mbstate_t st; + wchar_t wc; + int c; + char b; + size_t l = -2; + + memset(&st, 0, sizeof(mbstate_t)); + + /* Convert character byte-by-byte */ + + while (l == -2) + { + b = c = getc_unlocked(f); + if (c < 0) + { + if (!mbsinit(&st)) + { + set_errno(EILSEQ); + } + + f->fs_flags |= __FS_FLAG_EOF; + return WEOF; + } + + l = mbrtowc(&wc, &b, 1, &st); + if (l == -1) + { + f->fs_flags |= __FS_FLAG_EOF; + return WEOF; + } + } + + return wc; +} + +/**************************************************************************** + * Name: fgetwc + * + * Description: + * Get wide character from stream + * + * Input Parameters: + * f - Pointer to a FILE object that identifies an input stream, the stream + * will be locked during the read operation + * + * Returned Value: + * Return the character read is returned, + * Return WEOF is the sequence of bytes that read cannot be interpreted as + * a valid wide characted, and sets the errno to EILSEQ + * + ****************************************************************************/ + +wint_t fgetwc(FAR FILE *f) +{ + wint_t c; + flockfile(f); + c = fgetwc_unlocked(f); + funlockfile(f); + return c; +} + +#endif /* CONFIG_FILE_STREAM */ From e4799e2951e0cef3b493ab878c8a2a529cc98a43 Mon Sep 17 00:00:00 2001 From: guoshichao Date: Fri, 15 Dec 2023 16:16:27 +0800 Subject: [PATCH 2/3] libs/libc/wchar: add getwc implementation Signed-off-by: guoshichao --- include/wchar.h | 2 +- libs/libc/libc.csv | 1 + libs/libc/stdio/CMakeLists.txt | 3 +- libs/libc/stdio/Make.defs | 1 + libs/libc/stdio/lib_getwc.c | 60 ++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 libs/libc/stdio/lib_getwc.c diff --git a/include/wchar.h b/include/wchar.h index 7d954287e7e78..ffe0a30239329 100644 --- a/include/wchar.h +++ b/include/wchar.h @@ -150,7 +150,7 @@ wint_t fputwc_unlocked(wchar_t, FAR FILE *); int fputws(FAR const wchar_t *, FILE *); int fputws_unlocked(FAR const wchar_t *, FAR FILE *); int fwide(FILE *, int); -wint_t getwc(FILE *); +wint_t getwc(FAR FILE *); wint_t getwchar(void); int mbsinit(FAR const mbstate_t *); size_t mbrlen(FAR const char *, size_t, FAR mbstate_t *); diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv index f05cc9f0c8ab3..1a8fedcc61bf9 100644 --- a/libs/libc/libc.csv +++ b/libs/libc/libc.csv @@ -109,6 +109,7 @@ "gets","stdio.h","defined(CONFIG_FILE_STREAM)","FAR char *","FAR char *" "gettext","libintl.h","defined(CONFIG_LIBC_LOCALE_GETTEXT)","FAR char *","FAR const char *" "gettimeofday","sys/time.h","","int","FAR struct timeval *","FAR struct timezone *" +"getwc","wchar.h","defined(CONFIG_FILE_STREAM)","wint_t","FAR FILE *" "gmtime","time.h","","FAR struct tm *","FAR const time_t *" "gmtime_r","time.h","","FAR struct tm *","FAR const time_t *","FAR struct tm *" "htonl","arpa/inet.h","","uint32_t","uint32_t" diff --git a/libs/libc/stdio/CMakeLists.txt b/libs/libc/stdio/CMakeLists.txt index 6bb5234c7dadd..1e85b03d41e87 100644 --- a/libs/libc/stdio/CMakeLists.txt +++ b/libs/libc/stdio/CMakeLists.txt @@ -106,7 +106,8 @@ if(CONFIG_FILE_STREAM) lib_fputws.c lib_fopencookie.c lib_fmemopen.c - lib_fgetwc.c) + lib_fgetwc.c + lib_getwc.c) endif() target_sources(c PRIVATE ${SRCS}) diff --git a/libs/libc/stdio/Make.defs b/libs/libc/stdio/Make.defs index e182ce17c20a0..0acddb12a9c5c 100644 --- a/libs/libc/stdio/Make.defs +++ b/libs/libc/stdio/Make.defs @@ -49,6 +49,7 @@ CSRCS += lib_scanf.c lib_vscanf.c lib_fscanf.c lib_vfscanf.c lib_tmpfile.c CSRCS += lib_setbuf.c lib_setvbuf.c lib_libfilelock.c lib_libgetstreams.c CSRCS += lib_setbuffer.c lib_fputwc.c lib_putwc.c lib_fputws.c CSRCS += lib_fopencookie.c lib_fmemopen.c lib_open_memstream.c lib_fgetwc.c +CSRCS += lib_getwc.c endif # Add the stdio directory to the build diff --git a/libs/libc/stdio/lib_getwc.c b/libs/libc/stdio/lib_getwc.c new file mode 100644 index 0000000000000..df8870f706b37 --- /dev/null +++ b/libs/libc/stdio/lib_getwc.c @@ -0,0 +1,60 @@ +/**************************************************************************** + * libs/libc/stdio/lib_getwc.c + * + * Copyright © 2005-2014 Rich Felker, et al. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#ifdef CONFIG_FILE_STREAM + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: getwc + * + * Description: + * Get wide character from stream + * + * Input Parameters: + * f - Pointer to a FILE object that identifies an input stream + * + * Returned Value: + * Return the character read is returned, + * Return WEOF is the sequence of bytes that read cannot be interpreted as + * a valid wide characted, and sets the errno to EILSEQ + * + ****************************************************************************/ + +wint_t getwc(FAR FILE *f) +{ + return fgetwc(f); +} + +#endif /* CONFIG_FILE_STREAM */ From f1141ad5deb3d87eb98fe0e8b7317b5e4741fb15 Mon Sep 17 00:00:00 2001 From: guoshichao Date: Fri, 15 Dec 2023 21:07:35 +0800 Subject: [PATCH 3/3] libs/libc/wchar: add ungetwc implementation Signed-off-by: guoshichao --- include/wchar.h | 3 +- libs/libc/libc.csv | 2 + libs/libc/stdio/CMakeLists.txt | 3 +- libs/libc/stdio/Make.defs | 2 +- libs/libc/stdio/lib_ungetwc.c | 125 +++++++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 libs/libc/stdio/lib_ungetwc.c diff --git a/include/wchar.h b/include/wchar.h index ffe0a30239329..38a439f1668e4 100644 --- a/include/wchar.h +++ b/include/wchar.h @@ -166,7 +166,8 @@ wint_t putwchar(wchar_t); wint_t putwchar_unlocked(wchar_t); int swprintf(FAR wchar_t *, size_t, FAR const wchar_t *, ...); int swscanf(FAR const wchar_t *, FAR const wchar_t *, ...); -wint_t ungetwc(wint_t, FILE *); +wint_t ungetwc(wint_t, FAR FILE *); +wint_t ungetwc_unlocked(wint_t, FAR FILE *); int vfwprintf(FILE *, FAR const wchar_t *, va_list); int vfwscanf(FILE *, FAR const wchar_t *, va_list); int vwprintf(FAR const wchar_t *, va_list); diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv index 1a8fedcc61bf9..c85ae93057dfa 100644 --- a/libs/libc/libc.csv +++ b/libs/libc/libc.csv @@ -332,6 +332,8 @@ "ub16sqr","fixedmath.h","!defined(CONFIG_HAVE_LONG_LONG)","ub16_t","ub16_t" "uname","sys/utsname.h","","int","FAR struct utsname *" "ungetc","stdio.h","defined(CONFIG_FILE_STREAM)","int","int","FAR FILE *" +"ungetwc","wchar.h","defined(CONFIG_FILE_STREAM)","wint_t","wint_t","FAR FILE *" +"ungetwc_unlocked","wchar.h","defined(CONFIG_FILE_STREAM)","wint_t","wint_t","FAR FILE *" "usleep","unistd.h","","int","useconds_t" "vasprintf","stdio.h","","int","FAR char **","FAR const IPTR char *","va_list" "versionsort","dirent.h","","int","FAR const struct dirent **","FAR const struct dirent **" diff --git a/libs/libc/stdio/CMakeLists.txt b/libs/libc/stdio/CMakeLists.txt index 1e85b03d41e87..36b16a1faa089 100644 --- a/libs/libc/stdio/CMakeLists.txt +++ b/libs/libc/stdio/CMakeLists.txt @@ -107,7 +107,8 @@ if(CONFIG_FILE_STREAM) lib_fopencookie.c lib_fmemopen.c lib_fgetwc.c - lib_getwc.c) + lib_getwc.c + lib_ungetwc.c) endif() target_sources(c PRIVATE ${SRCS}) diff --git a/libs/libc/stdio/Make.defs b/libs/libc/stdio/Make.defs index 0acddb12a9c5c..7ddd9bdd74e2f 100644 --- a/libs/libc/stdio/Make.defs +++ b/libs/libc/stdio/Make.defs @@ -49,7 +49,7 @@ CSRCS += lib_scanf.c lib_vscanf.c lib_fscanf.c lib_vfscanf.c lib_tmpfile.c CSRCS += lib_setbuf.c lib_setvbuf.c lib_libfilelock.c lib_libgetstreams.c CSRCS += lib_setbuffer.c lib_fputwc.c lib_putwc.c lib_fputws.c CSRCS += lib_fopencookie.c lib_fmemopen.c lib_open_memstream.c lib_fgetwc.c -CSRCS += lib_getwc.c +CSRCS += lib_getwc.c lib_ungetwc.c endif # Add the stdio directory to the build diff --git a/libs/libc/stdio/lib_ungetwc.c b/libs/libc/stdio/lib_ungetwc.c new file mode 100644 index 0000000000000..d9f81c6d17ec4 --- /dev/null +++ b/libs/libc/stdio/lib_ungetwc.c @@ -0,0 +1,125 @@ +/**************************************************************************** + * libs/libc/stdio/lib_ungetwc.c + * + * Copyright © 2005-2014 Rich Felker, et al. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include + +#ifdef CONFIG_FILE_STREAM + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ungetwc_unlocked + * + * Description: + * Put a wide character back to the stream without lock the stream + * + * Input Parameters: + * wc - the wide character to be put back + * f - the file stream to put the wide character back to + * + * Returned Value: + * Return the wide character that put back to the stream on success, + * return WEOF on failed, and keep the given stream remains unchanged. + * + ****************************************************************************/ + +wint_t ungetwc_unlocked(wint_t wc, FAR FILE *f) +{ + char mbc[MB_LEN_MAX]; + int l = 1; + + /* Stream must be open for read access */ + + if ((f->fs_oflags & O_RDOK) == 0) + { + return WEOF; + } + + /* Try conversion early so we can fail without locking if invalid */ + + if ((l = wctomb(mbc, wc)) < 0) + { + return WEOF; + } + +#if CONFIG_NUNGET_CHARS > 0 + if (f->fs_nungotten + l <= CONFIG_NUNGET_CHARS) + { + memcpy(f->fs_ungotten + f->fs_nungotten, mbc, l); + f->fs_nungotten += l; + return wc; + } + else +#endif + { + return WEOF; + } +} + +/**************************************************************************** + * Name: ungetwc + * + * Description: + * Put a wide character back to the stream + * + * Input Parameters: + * wc - the wide character that need to put back to the stream + * f - pointer to a FILE object that identifies an input stream + * + * Returned Value: + * Return the wide character that put back to the stream on success, + * return WEOF on failed, and keep the given stream remains unchanged. + * + ****************************************************************************/ + +wint_t ungetwc(wint_t wc, FAR FILE *f) +{ + wint_t ret; + + /* Verify that a non-NULL stream was provided and wc is not WEOF */ + + if (!f || wc == WEOF) + { + return WEOF; + } + + flockfile(f); + ret = ungetwc_unlocked(wc, f); + funlockfile(f); + return ret; +} + +#endif /* CONFIG_FILE_STREAM */