mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:48:10 +00:00
LibC: Implement {f,}putwc()
This commit is contained in:
parent
a4e8a09188
commit
cb90856756
4 changed files with 40 additions and 1 deletions
|
@ -70,7 +70,7 @@ private:
|
|||
void realize(int fd);
|
||||
void drop();
|
||||
|
||||
bool may_use() const { return m_ungotten || m_mode != _IONBF; }
|
||||
bool may_use() const;
|
||||
bool is_not_empty() const { return m_ungotten || !m_empty; }
|
||||
size_t buffered_size() const;
|
||||
|
||||
|
|
|
@ -341,6 +341,11 @@ FILE::Buffer::~Buffer()
|
|||
free(m_data);
|
||||
}
|
||||
|
||||
bool FILE::Buffer::may_use() const
|
||||
{
|
||||
return m_ungotten || m_mode != _IONBF;
|
||||
}
|
||||
|
||||
void FILE::Buffer::realize(int fd)
|
||||
{
|
||||
if (m_mode == -1)
|
||||
|
|
|
@ -74,5 +74,8 @@ size_t wcsspn(const wchar_t* wcs, const wchar_t* accept);
|
|||
wint_t fgetwc(FILE* stream);
|
||||
wint_t getwc(FILE* stream);
|
||||
wint_t getwchar(void);
|
||||
wint_t fputwc(wchar_t wc, FILE* stream);
|
||||
wint_t putwc(wchar_t wc, FILE* stream);
|
||||
wint_t putwchar(wchar_t wc);
|
||||
|
||||
__END_DECLS
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/BitCast.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Types.h>
|
||||
#include <bits/stdio_file_implementation.h>
|
||||
|
@ -61,4 +62,34 @@ wint_t getwchar()
|
|||
{
|
||||
return getwc(stdin);
|
||||
}
|
||||
|
||||
wint_t fputwc(wchar_t wc, FILE* stream)
|
||||
{
|
||||
VERIFY(stream);
|
||||
// Negative wide chars are weird
|
||||
if constexpr (IsSigned<wchar_t>) {
|
||||
if (wc < 0) {
|
||||
errno = EILSEQ;
|
||||
return WEOF;
|
||||
}
|
||||
}
|
||||
StringBuilder sb;
|
||||
sb.append_code_point(static_cast<u32>(wc));
|
||||
auto bytes = sb.string_view().bytes();
|
||||
ScopedFileLock lock(stream);
|
||||
size_t nwritten = stream->write(bytes.data(), bytes.size());
|
||||
if (nwritten < bytes.size())
|
||||
return WEOF;
|
||||
return wc;
|
||||
}
|
||||
|
||||
wint_t putwc(wchar_t wc, FILE* stream)
|
||||
{
|
||||
return fputwc(wc, stream);
|
||||
}
|
||||
|
||||
wint_t putwchar(wchar_t wc)
|
||||
{
|
||||
return fputwc(wc, stdout);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue