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 */