Add io::read_new_fully for reading to the end of a stream. Add io::wrap_bytes for reading bytes with io functions.

This commit is contained in:
Christoffer Lerno
2024-09-10 13:20:08 +02:00
parent 503032cbcf
commit 2092e2167e
3 changed files with 26 additions and 0 deletions

View File

@@ -401,3 +401,11 @@ fn File* stdin()
{
return &stdin_file;
}
/**
* Wrap bytes for reading using io functions.
**/
fn ByteReader wrap_bytes(char[] bytes)
{
return { bytes, 0 };
}

View File

@@ -77,6 +77,22 @@ macro usz! read_all(stream, char[] buffer)
return n;
}
/**
* @require @is_instream(stream)
*/
macro char[]! read_new_fully(stream, Allocator allocator = allocator::heap())
{
usz len = available(stream)!;
char* data = allocator::malloc_try(allocator, len)!;
defer catch allocator::free(allocator, data);
usz read = 0;
while (read < len)
{
read += stream.read(data[read:len - read])!;
}
return data[:len];
}
/**
* @require @is_outstream(stream)
*/