Skip to content

Commit 5bd8e56

Browse files
extinguishxiaoxiang781216
authored andcommitted
libs/libc/wchar: add fputws implementation
Signed-off-by: guoshichao <[email protected]>
1 parent d67090c commit 5bd8e56

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

include/wchar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ FAR wchar_t *fgetws(wchar_t *, int, FILE *);
147147
wint_t fputwc(wchar_t, FILE *);
148148
wint_t fputwc_unlocked(wchar_t, FAR FILE *);
149149
int fputws(FAR const wchar_t *, FILE *);
150+
int fputws_unlocked(FAR const wchar_t *, FAR FILE *);
150151
int fwide(FILE *, int);
151152
wint_t getwc(FILE *);
152153
wint_t getwchar(void);

libs/libc/stdio/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ if(CONFIG_FILE_STREAM)
103103
lib_libfilelock.c
104104
lib_libgetstreams.c
105105
lib_fputwc.c
106-
lib_putwc.c)
106+
lib_putwc.c
107+
lib_fputws.c)
107108
endif()
108109

109110
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 lib_fputwc.c lib_putwc.c
50+
CSRCS += lib_libgetstreams.c lib_setbuffer.c lib_fputwc.c lib_putwc.c lib_fputws.c
5151
endif
5252

5353
# Add the stdio directory to the build

libs/libc/stdio/lib_fputws.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/****************************************************************************
2+
* libs/libc/stdio/lib_fputws.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 <stdio.h>
32+
#include <wchar.h>
33+
34+
#include "libc.h"
35+
36+
#ifdef CONFIG_FILE_STREAM
37+
38+
/****************************************************************************
39+
* Public Functions
40+
****************************************************************************/
41+
42+
/****************************************************************************
43+
* Name: fputws_unlocked
44+
*
45+
* Description:
46+
* Write wide string to stream without lock the stream
47+
*
48+
* Input Parameters:
49+
* ws - the wide string to write to the stream
50+
* f - the FILE object that identifies an output stream
51+
*
52+
* Returned Value:
53+
* Return the wide character that written in to the stream on success,
54+
* return WEOF on fail write to the stream
55+
*
56+
****************************************************************************/
57+
58+
int fputws_unlocked(FAR const wchar_t *ws, FAR FILE *f)
59+
{
60+
char buf[BUFSIZ];
61+
size_t l = 0;
62+
63+
while (ws &&
64+
(l = wcsrtombs(buf, &ws, sizeof(buf), NULL)) + 1 > 1)
65+
{
66+
if (lib_fwrite_unlocked(buf, l, f) < l)
67+
{
68+
funlockfile(f);
69+
return -1;
70+
}
71+
}
72+
73+
return l;
74+
}
75+
76+
/****************************************************************************
77+
* Name: fputws
78+
*
79+
* Description:
80+
* Write wide string to stream
81+
*
82+
* Input Parameters:
83+
* ws - the wide string to write to the stream
84+
* f - the FILE object that identifies an output stream
85+
*
86+
* Returned Value:
87+
* Return the wide character that written in to the stream on success,
88+
* return WEOF on fail write to the stream
89+
*
90+
****************************************************************************/
91+
92+
int fputws(FAR const wchar_t *ws, FAR FILE *f)
93+
{
94+
int l;
95+
flockfile(f);
96+
l = fputws_unlocked(ws, f);
97+
funlockfile(f);
98+
return l;
99+
}
100+
101+
#endif /* CONFIG_FILE_STREAM */

0 commit comments

Comments
 (0)