Files
c3c/lib/std/io/stream/teereader.c3
konimarti f3304acc93 Add io stream primitives (#1626)
* io: implement MultiReader struct

Implement a MultiReader (InStream) which sequentially read from the
provided readers (InStreams). Return IoError.EOF when all of the readers
are read.

* io: implement MultiWriter struct

Implement a MultiWriter (OutStream). The MultiWriter duplicates its
writes to all the provided writers (OutStream).

* io: implement TeeReader struct

Implement a TeeReader (InStream) which reads from a wrapped reader
(InStream) and writes data to the provided writer (OutStream).
2024-11-15 23:18:29 +01:00

43 lines
948 B
Plaintext

module std::io;
struct TeeReader (InStream)
{
InStream r;
OutStream w;
}
<* Returns a reader that implements InStream and that will write any data read
from the wrapped reader r to the writer w. There is no internal buffering.
@param [&inout] r "Stream r to read from."
@param [&inout] w "Stream w to write to what it reads from r."
*>
macro TeeReader tee_reader(InStream r, OutStream w) => { r, w };
<*
@param [&inout] self
@param [&inout] r "Stream r to read from."
@param [&inout] w "Stream w to write to what it reads from r."
*>
fn TeeReader* TeeReader.init(&self, InStream r, OutStream w)
{
*self = tee_reader(r, w);
return self;
}
fn usz! TeeReader.read(&self, char[] bytes) @dynamic
{
usz nr, nw;
nr = self.r.read(bytes)!;
nw = self.w.write(bytes[:nr])!;
if (nr != nw) return IoError.GENERAL_ERROR?;
return nr;
}
fn char! TeeReader.read_byte(&self) @dynamic
{
char[1] data;
self.read(data[..])!;
return data[0];
}