Racket has the nice read-bytes-async!
function, which I believe exists in every other programming language in the world. It reads what it can from an input stream, without blocking, into a buffer, returning the number of bytes written.
Said function seems like an absolutely essential function for efficiently implementing, say, the Unix cat
tool, yet Chicken Scheme seems to lack any such function. Of course, I can use (read-byte)
and (write-byte)
, but that is slow and eats up all my CPU.
Even (copy-port)
seems to not have any such implementation. Instead, before the stream is closed, the data is copied buffer-by-buffer only when the buffers fill. This means that (copy-port (current-input-port) (current-output-port))
does not behave like cat
at all.
Am I just suffering from a terrible blind spot in reading the documentation, or does Chicken shockingly actually lack such a function? So cat
can't even be written efficiently in Chicken?
I fixed my problem. The posix
library has the file-read
function that does what I want, albeit on a file descriptor. Fortunately, ports in Chicken are just thin wrappers around file descriptors; there is a port to file descriptor converter in the posix
library as well.
Interestingly, these functions work on Windows as well. posix
seems to not be limited to POSIX systems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With