2020-11-15 21:54:47 +01:00
|
|
|
module io
|
|
|
|
|
|
|
|
// Reader represents a stream of data that can be read
|
|
|
|
pub interface Reader {
|
|
|
|
// read reads up to buf.len bytes and places
|
|
|
|
// them into buf.
|
2020-12-16 18:22:26 +01:00
|
|
|
// A type that implements this should return
|
2020-11-15 21:54:47 +01:00
|
|
|
// `none` on end of stream (EOF) instead of just returning 0
|
|
|
|
read(mut buf []byte) ?int
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:22:26 +01:00
|
|
|
// make_reader is a temp that converts a type to a reader
|
2020-11-15 21:54:47 +01:00
|
|
|
// (e.g. for use in struct initialisation)
|
2020-12-16 18:22:26 +01:00
|
|
|
// (this shouldnt need to be a thing but until coercion gets made better
|
|
|
|
// it is required)
|
2021-05-13 09:26:10 +02:00
|
|
|
[deprecated: 'use just `x` instead of `io.make_reader(x)`. Interfaces are now checked against all types.']
|
|
|
|
[deprecated_after: '2021-05-27']
|
2020-11-15 21:54:47 +01:00
|
|
|
pub fn make_reader(r Reader) Reader {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
read_all_len = 10 * 1024
|
|
|
|
read_all_grow_len = 1024
|
|
|
|
)
|
|
|
|
|
2020-12-16 18:22:26 +01:00
|
|
|
// ReadAllConfig allows options to be passed for the behaviour
|
|
|
|
// of read_all
|
|
|
|
pub struct ReadAllConfig {
|
2021-03-24 11:39:09 +01:00
|
|
|
reader Reader
|
2020-12-16 18:22:26 +01:00
|
|
|
read_to_end_of_stream bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// read_all reads all bytes from a reader until either a 0 length read
|
|
|
|
// or if read_to_end_of_stream is true then the end of the stream (`none`)
|
|
|
|
pub fn read_all(config ReadAllConfig) ?[]byte {
|
|
|
|
r := config.reader
|
|
|
|
read_till_eof := config.read_to_end_of_stream
|
|
|
|
|
2021-03-24 11:39:09 +01:00
|
|
|
mut b := []byte{len: io.read_all_len}
|
2020-12-16 18:22:26 +01:00
|
|
|
mut read := 0
|
|
|
|
for {
|
2021-03-24 11:39:09 +01:00
|
|
|
new_read := r.read(mut b[read..]) or { break }
|
2020-12-16 18:22:26 +01:00
|
|
|
read += new_read
|
|
|
|
if !read_till_eof && read == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if b.len == read {
|
2021-03-24 11:39:09 +01:00
|
|
|
unsafe { b.grow_len(io.read_all_grow_len) }
|
2020-12-16 18:22:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return b[..read]
|
|
|
|
}
|
|
|
|
|
|
|
|
// read_any reads any available bytes from a reader
|
|
|
|
// (until the reader returns a read of 0 length)
|
|
|
|
pub fn read_any(r Reader) ?[]byte {
|
2021-03-24 11:39:09 +01:00
|
|
|
mut b := []byte{len: io.read_all_len}
|
2020-11-15 21:54:47 +01:00
|
|
|
mut read := 0
|
|
|
|
for {
|
2021-03-24 11:39:09 +01:00
|
|
|
new_read := r.read(mut b[read..]) or { break }
|
2020-11-15 21:54:47 +01:00
|
|
|
read += new_read
|
|
|
|
if new_read == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if b.len == read {
|
2021-03-24 11:39:09 +01:00
|
|
|
unsafe { b.grow_len(io.read_all_grow_len) }
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return b[..read]
|
|
|
|
}
|
|
|
|
|
|
|
|
// RandomReader represents a stream of data that can be read from at a random location
|
|
|
|
interface RandomReader {
|
2021-03-26 07:51:55 +01:00
|
|
|
read_from(pos u64, mut buf []byte) ?int
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|