mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:37:37 +00:00
CIODevice: Add some basic output facilities: write() and printf().
Being able to do file.printf() is extremely comfy, I have to say. :^)
This commit is contained in:
parent
b719bf7fde
commit
7ce3b10568
2 changed files with 27 additions and 0 deletions
|
@ -2,6 +2,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <AK/printf.cpp>
|
||||||
|
|
||||||
CIODevice::CIODevice(CObject* parent)
|
CIODevice::CIODevice(CObject* parent)
|
||||||
: CObject(parent)
|
: CObject(parent)
|
||||||
|
@ -185,3 +186,25 @@ bool CIODevice::seek(signed_qword offset)
|
||||||
m_eof = false;
|
m_eof = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CIODevice::write(const byte* data, int size)
|
||||||
|
{
|
||||||
|
int rc = ::write(m_fd, data, size);
|
||||||
|
if (rc < 0) {
|
||||||
|
perror("CIODevice::write: write");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return rc == size;
|
||||||
|
}
|
||||||
|
|
||||||
|
int CIODevice::printf(const char* format, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, format);
|
||||||
|
// FIXME: We're not propagating write() failures to client here!
|
||||||
|
int ret = printf_internal([this] (char*&, char ch) {
|
||||||
|
write((const byte*)&ch, 1);
|
||||||
|
}, nullptr, format, ap);
|
||||||
|
va_end(ap);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
|
@ -30,6 +30,8 @@ public:
|
||||||
ByteBuffer read_line(int max_size);
|
ByteBuffer read_line(int max_size);
|
||||||
ByteBuffer read_all();
|
ByteBuffer read_all();
|
||||||
|
|
||||||
|
bool write(const byte*, int size);
|
||||||
|
|
||||||
// FIXME: I would like this to be const but currently it needs to call populate_read_buffer().
|
// FIXME: I would like this to be const but currently it needs to call populate_read_buffer().
|
||||||
bool can_read_line();
|
bool can_read_line();
|
||||||
|
|
||||||
|
@ -40,6 +42,8 @@ public:
|
||||||
virtual bool open(CIODevice::OpenMode) = 0;
|
virtual bool open(CIODevice::OpenMode) = 0;
|
||||||
virtual bool close();
|
virtual bool close();
|
||||||
|
|
||||||
|
int printf(const char*, ...);
|
||||||
|
|
||||||
virtual const char* class_name() const override { return "CIODevice"; }
|
virtual const char* class_name() const override { return "CIODevice"; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue