File: Add methods to read/write using buffers

This commit is contained in:
apio 2023-04-18 16:41:17 +02:00
parent 51eedf2b16
commit fe11b04832
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 21 additions and 0 deletions

View File

@ -31,9 +31,12 @@ namespace os
void set_close_on_exec(); void set_close_on_exec();
Result<void> write(StringView str); Result<void> write(StringView str);
Result<void> write(const Buffer& buf);
Result<String> read_line(); Result<String> read_line();
Result<void> read(Buffer& buf, usize size);
Result<int> getchar(); Result<int> getchar();
File(Badge<File>); File(Badge<File>);

View File

@ -101,6 +101,13 @@ namespace os
return {}; return {};
} }
Result<void> File::write(const Buffer& buf)
{
TRY(raw_write(buf.data(), buf.size()));
return {};
}
Result<String> File::read_line() Result<String> File::read_line()
{ {
Vector<char> data; Vector<char> data;
@ -124,6 +131,17 @@ namespace os
return String::from_cstring(data.data()); return String::from_cstring(data.data());
} }
Result<void> File::read(Buffer& buf, usize size)
{
u8* slice = TRY(buf.slice(0, size));
usize nread = TRY(raw_read(slice, size));
TRY(buf.try_resize(nread));
return {};
}
Result<int> File::getchar() Result<int> File::getchar()
{ {
char value; char value;