Add user friendly file IO functions
developedby opened this issue · 0 comments
developedby commented
#573 adds the primitive file IO functions for Bend, building on the IO functions that HVM exposes.
However, for the common use case they are a bit hard to use.
So we'd like more usuer-friendly functions. I propose these, but we could have others:
IO/Fs/read_file: (path: String) -> (List U24)
, reads a file in its entirety and returns a list of bytes.IO/Fs/read_until_end: (file: U24) -> (List U24)
, takes a file descriptor as returned byIO/Fs/open
and reads it until EOF.IO/Fs/read_line: (file: U24) -> (List U24)
, takes a file descriptor and reads a line until\n
is found.IO/Fs/write_file: (path: String) -> (data: List U24) -> *
, replaces the entire content of a file with some contents (or writes a new file).
Since reads have a maximum length imposed by hvm, the read functions should read in chunks and then stitch them together.
read_line
can read a large chunk, check for \n, if found split the string, and then move back the tail length using seek
. I think this will be a bit faster than reading char by char.
I think these functions should operate with lists of bytes, so they'll work with both binary file and encoded text files.