A sys_iostream_t is a small, fixed set of primitives - peek, read, write, seek, close - implemented differently per backend.
More...
A sys_iostream_t is a small, fixed set of primitives - peek, read, write, seek, close - implemented differently per backend.
Instances come from a static pool (SYS_IOSTREAM_CAPACITY), never the heap, and are constructed by a source-specific function rather than directly; sys_string_read() (sys/string.h) is the only backend today, wrapping an existing string with no copy of its bytes. sys/rune.h's tokenizer and sys/scanner.h's scanner are both built entirely on this interface, so a future backend (a file, say) would work with them unchanged.
Example - read a stream in two passes by seeking back to the start:
◆ sys_iostream_t
◆ sys_iostream_close()
Release a stream back to the pool.
- Parameters
-
| s | The stream to close, or NULL (a no-op). |
Releases any resources the stream's source holds (nothing, for a string-backed stream) and frees its pool slot for reuse.
◆ sys_iostream_peek()
Look at the next byte without consuming it.
- Parameters
-
- Returns
- The next unread byte, as an unsigned value 0-255, or SYS_IOSTREAM_EOF if the stream is at its end. Calling this repeatedly without an intervening sys_iostream_read() returns the same byte every time.
◆ sys_iostream_read()
Read bytes from a stream.
- Parameters
-
| s | The stream to read from. |
| buf | Destination buffer. |
| n | Maximum number of bytes to read. |
- Returns
- The number of bytes actually read into buf, which may be less than n. 0 means the stream is at its end - it is not an error.
◆ sys_iostream_seek()
| ptrdiff_t sys_iostream_seek |
( |
sys_iostream_t * |
s, |
|
|
ptrdiff_t |
offset, |
|
|
bool |
abs |
|
) |
| |
Move a stream's read/write position.
- Parameters
-
| s | The stream to seek. |
| offset | Byte offset - absolute from the start when abs is true (must be >= 0), otherwise relative to the current position (negative moves backward). |
| abs | true for an absolute seek, false for relative. |
- Returns
- The resulting absolute position (>= 0) on success, or -1 if the seek would go out of bounds - the position is left unchanged in that case.
◆ sys_iostream_write()
| size_t sys_iostream_write |
( |
sys_iostream_t * |
s, |
|
|
const char * |
buf, |
|
|
size_t |
n |
|
) |
| |
Write bytes to a stream.
- Parameters
-
| s | The stream to write to. |
| buf | Source buffer. |
| n | Number of bytes to write. |
- Returns
- The number of bytes actually written, which may be less than n (for example if the destination is full). 0 means nothing could be written - for a read-only stream (such as one from sys_string_read()), this is always the case.