ilusm.dev

strm

Stream reader/writer records - create a reader from read(n) and read_all() functions, create a writer from a write(s) function, check reader/writer interface, read n bytes, read all, write, stdin/stdout helpers, pipe reader to writer with configurable chunk size, and a close stub. Exposes a strm namespace object.

Load with: use strm

What this module does

strm defines the same reader/writer interface as strio but under slightly different function names and with a module-object (strm.new, strm.rd, etc.). Use this module when you need a strm.* namespace or when integrating with code that calls the strm interface by name.

Quick example

use strm

src = "hello world"
pos = 0
r = strm.new(
    \(n) txt.sub(src, pos, pos + n),
    \() txt.sub(src, pos))

w = strm.writer(\(s) prn("wrote: " + s))

strm.pipe(r, w, 5)  # "wrote: hello" "wrote:  worl" "wrote: d"

# Stdin/stdout
i = strm.stdin()
o = strm.stdout()
strm.pipe(i, o, nil)

Functions

Construction

ststr(rf, af) / strm.new(rf, af)

Creates a reader from rf(n) (read n bytes) and af() (read all). Both must be functions.

sstrw(wf) / strm.writer(wf)

Creates a writer from wf(s). Must be a function.

ststd() / strm.stdin()

Returns a stdin reader backed by host syscalls.

strou() / strm.stdout()

Returns a stdout writer backed by host syscalls.

Interface checks

stisr(r) / strm.isr(r)

Returns tru if r has both read and read_all function fields.

stisw(w) / strm.isw(w)

Returns tru if w has a write function field.

Reading, writing and piping

strd(r, n) / strm.rd(r, n)

Reads up to n bytes from reader r.

strda(r) / strm.rda(r)

Reads all content from reader r.

stwr(w, s) / strm.wr(w, s)

Writes string s to writer w.

sstrp(r, w, chunk_sz) / strm.pipe(r, w, n)

Pipes content from r to w in chunks of n bytes (default 4096). Returns total bytes written.

stcl(s) / strm.cls(s)

Close stub - returns nil. Satisfies close interface without effect.

Notes

  • Requires txt, trl, and eru.