1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:28:12 +00:00

LibC: Implement {f,}putwc()

This commit is contained in:
Ali Mohammad Pur 2021-12-20 16:08:03 +03:30 committed by Ali Mohammad Pur
parent a4e8a09188
commit cb90856756
4 changed files with 40 additions and 1 deletions

View file

@ -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;

View file

@ -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)

View file

@ -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

View file

@ -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);
}
}