File: Add methods to read/write using buffers
This commit is contained in:
parent
51eedf2b16
commit
fe11b04832
@ -31,9 +31,12 @@ namespace os
|
||||
void set_close_on_exec();
|
||||
|
||||
Result<void> write(StringView str);
|
||||
Result<void> write(const Buffer& buf);
|
||||
|
||||
Result<String> read_line();
|
||||
|
||||
Result<void> read(Buffer& buf, usize size);
|
||||
|
||||
Result<int> getchar();
|
||||
|
||||
File(Badge<File>);
|
||||
|
@ -101,6 +101,13 @@ namespace os
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void> File::write(const Buffer& buf)
|
||||
{
|
||||
TRY(raw_write(buf.data(), buf.size()));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<String> File::read_line()
|
||||
{
|
||||
Vector<char> data;
|
||||
@ -124,6 +131,17 @@ namespace os
|
||||
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()
|
||||
{
|
||||
char value;
|
||||
|
Loading…
Reference in New Issue
Block a user