Skip to content

Commit f8b5f61

Browse files
extinguishxiaoxiang781216
authored andcommitted
libs/libc/wchar: add fputwc implementation
Signed-off-by: guoshichao <[email protected]>
1 parent d386df4 commit f8b5f61

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed

include/wchar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ int fwscanf(FILE *, FAR const wchar_t *, ...);
145145
wint_t fgetwc(FILE *);
146146
FAR wchar_t *fgetws(wchar_t *, int, FILE *);
147147
wint_t fputwc(wchar_t, FILE *);
148+
wint_t fputwc_unlocked(wchar_t, FAR FILE *);
148149
int fputws(FAR const wchar_t *, FILE *);
149150
int fwide(FILE *, int);
150151
wint_t getwc(FILE *);

libs/libc/stdio/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ if(CONFIG_FILE_STREAM)
100100
lib_setvbuf.c
101101
lib_libstream.c
102102
lib_libfilelock.c
103-
lib_libgetstreams.c)
103+
lib_libgetstreams.c
104+
lib_fputwc.c)
104105
endif()
105106

106107
target_sources(c PRIVATE ${SRCS})

libs/libc/stdio/Make.defs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ CSRCS += lib_fputs.c lib_ungetc.c lib_fprintf.c lib_vfprintf.c
4747
CSRCS += lib_feof.c lib_ferror.c lib_rewind.c lib_clearerr.c
4848
CSRCS += lib_scanf.c lib_vscanf.c lib_fscanf.c lib_vfscanf.c lib_tmpfile.c
4949
CSRCS += lib_setbuf.c lib_setvbuf.c lib_libstream.c lib_libfilelock.c
50-
CSRCS += lib_libgetstreams.c lib_setbuffer.c
50+
CSRCS += lib_libgetstreams.c lib_setbuffer.c lib_fputwc.c
5151
endif
5252

5353
# Add the stdio directory to the build

libs/libc/stdio/lib_fputwc.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/****************************************************************************
2+
* libs/libc/stdio/lib_fputwc.c
3+
*
4+
* Copyright © 2005-2014 Rich Felker, et al.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining
7+
* a copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to
12+
* the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*
25+
****************************************************************************/
26+
27+
/****************************************************************************
28+
* Included Files
29+
****************************************************************************/
30+
31+
#include <wchar.h>
32+
#include <limits.h>
33+
#include <ctype.h>
34+
#include <nuttx/fs/fs.h>
35+
36+
#include "libc.h"
37+
38+
#ifdef CONFIG_FILE_STREAM
39+
40+
/****************************************************************************
41+
* Public Functions
42+
****************************************************************************/
43+
44+
/****************************************************************************
45+
* Name: fputwc_unlocked
46+
*
47+
* Description:
48+
* Write wide character to stream without lock the stream
49+
*
50+
* Input Parameters:
51+
* c - the wide character to write
52+
* f - the FILE object that identifies an output stream
53+
*
54+
* Returned Value:
55+
* Return the wide character that written in to the stream on success,
56+
* return WEOF on fail write to the stream
57+
*
58+
****************************************************************************/
59+
60+
wint_t fputwc_unlocked(wchar_t c, FAR FILE *f)
61+
{
62+
char mbc[MB_LEN_MAX];
63+
int l;
64+
65+
if (isascii(c))
66+
{
67+
c = putc(c, f);
68+
}
69+
else
70+
{
71+
l = wctomb(mbc, c);
72+
if (l < 0 || lib_fwrite_unlocked(mbc, l, f) < l)
73+
{
74+
c = WEOF;
75+
}
76+
}
77+
78+
return c;
79+
}
80+
81+
/****************************************************************************
82+
* Name: fputwc
83+
*
84+
* Description:
85+
* Write wide character to stream
86+
*
87+
* Input Parameters:
88+
* c - the wide character to write
89+
* f - the FILE object that identifies an output stream
90+
*
91+
* Returned Value:
92+
* Return the wide character that written in to the stream on success,
93+
* return WEOF on fail write to the stream
94+
*
95+
****************************************************************************/
96+
97+
wint_t fputwc(wchar_t c, FAR FILE *f)
98+
{
99+
flockfile(f);
100+
c = fputwc_unlocked(c, f);
101+
funlockfile(f);
102+
return c;
103+
}
104+
105+
#endif /* CONFIG_FILE_STREAM */

0 commit comments

Comments
 (0)