2020-11-15 21:54:47 +01:00
|
|
|
module io
|
|
|
|
|
|
|
|
// BufferedReader provides a buffered interface for a reader
|
2022-06-19 16:42:22 +02:00
|
|
|
pub struct BufferedReader {
|
2020-11-15 21:54:47 +01:00
|
|
|
mut:
|
2021-02-16 21:53:46 +01:00
|
|
|
reader Reader
|
2022-04-15 14:35:35 +02:00
|
|
|
buf []u8
|
2021-02-16 21:53:46 +01:00
|
|
|
offset int // current offset in the buffer
|
|
|
|
len int
|
|
|
|
fails int // how many times fill_buffer has read 0 bytes in a row
|
|
|
|
mfails int // maximum fails, after which we can assume that the stream has ended
|
|
|
|
pub mut:
|
|
|
|
end_of_stream bool // whether we reached the end of the upstream reader
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// BufferedReaderConfig are options that can be given to a reader
|
|
|
|
pub struct BufferedReaderConfig {
|
2021-02-16 21:53:46 +01:00
|
|
|
reader Reader
|
|
|
|
cap int = 128 * 1024 // large for fast reading of big(ish) files
|
|
|
|
retries int = 2 // how many times to retry before assuming the stream ended
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// new_buffered_reader creates new BufferedReader
|
|
|
|
pub fn new_buffered_reader(o BufferedReaderConfig) &BufferedReader {
|
2021-03-04 07:57:30 +01:00
|
|
|
if o.cap <= 0 {
|
|
|
|
panic('new_buffered_reader should be called with a positive `cap`')
|
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
// create
|
|
|
|
r := &BufferedReader{
|
|
|
|
reader: o.reader
|
2022-04-15 14:35:35 +02:00
|
|
|
buf: []u8{len: o.cap, cap: o.cap}
|
2020-11-15 21:54:47 +01:00
|
|
|
offset: 0
|
2021-02-16 21:53:46 +01:00
|
|
|
mfails: o.retries
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// read fufills the Reader interface
|
2022-04-15 14:35:35 +02:00
|
|
|
pub fn (mut r BufferedReader) read(mut buf []u8) ?int {
|
2021-03-04 07:57:30 +01:00
|
|
|
if r.end_of_stream {
|
|
|
|
return none
|
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
// read data out of the buffer if we dont have any
|
2020-12-16 18:22:26 +01:00
|
|
|
if r.needs_fill() {
|
|
|
|
if !r.fill_buffer() {
|
|
|
|
// end of stream
|
|
|
|
return none
|
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
2022-03-09 19:26:00 +01:00
|
|
|
read := copy(mut buf, r.buf[r.offset..r.len])
|
2021-03-04 07:57:30 +01:00
|
|
|
if read == 0 {
|
|
|
|
return none
|
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
r.offset += read
|
|
|
|
return read
|
|
|
|
}
|
|
|
|
|
2021-03-15 21:25:19 +01:00
|
|
|
pub fn (mut r BufferedReader) free() {
|
|
|
|
unsafe {
|
|
|
|
r.buf.free()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:22:26 +01:00
|
|
|
// fill_buffer attempts to refill the internal buffer
|
|
|
|
// and returns whether it got any data
|
|
|
|
fn (mut r BufferedReader) fill_buffer() bool {
|
|
|
|
if r.end_of_stream {
|
|
|
|
// we know we have already reached the end of stream
|
|
|
|
// so return early
|
2021-01-07 20:21:47 +01:00
|
|
|
return true
|
2020-12-16 18:22:26 +01:00
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
r.offset = 0
|
2020-12-16 18:22:26 +01:00
|
|
|
r.len = 0
|
|
|
|
r.len = r.reader.read(mut r.buf) or {
|
|
|
|
// end of stream was reached
|
|
|
|
r.end_of_stream = true
|
|
|
|
return false
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
2021-02-16 21:53:46 +01:00
|
|
|
if r.len == 0 {
|
|
|
|
r.fails++
|
|
|
|
} else {
|
|
|
|
r.fails = 0
|
|
|
|
}
|
|
|
|
if r.fails >= r.mfails {
|
|
|
|
// When reading 0 bytes several times in a row, assume the stream has ended.
|
|
|
|
// This prevents infinite loops ¯\_(ツ)_/¯ ...
|
|
|
|
r.end_of_stream = true
|
|
|
|
return false
|
|
|
|
}
|
2020-12-16 18:22:26 +01:00
|
|
|
// we got some data
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// needs_fill returns whether the buffer needs refilling
|
|
|
|
fn (r BufferedReader) needs_fill() bool {
|
2021-03-04 07:57:30 +01:00
|
|
|
return r.offset >= r.len
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
|
|
|
|
2020-12-16 18:22:26 +01:00
|
|
|
// end_of_stream returns whether the end of the stream was reached
|
|
|
|
pub fn (r BufferedReader) end_of_stream() bool {
|
|
|
|
return r.end_of_stream
|
|
|
|
}
|
|
|
|
|
|
|
|
// read_line attempts to read a line from the buffered reader
|
|
|
|
// it will read until it finds a new line character (\n) or
|
|
|
|
// the end of stream
|
2020-11-15 21:54:47 +01:00
|
|
|
pub fn (mut r BufferedReader) read_line() ?string {
|
2020-12-16 18:22:26 +01:00
|
|
|
if r.end_of_stream {
|
|
|
|
return none
|
|
|
|
}
|
2022-04-15 14:35:35 +02:00
|
|
|
mut line := []u8{}
|
2020-11-15 21:54:47 +01:00
|
|
|
for {
|
2020-12-16 18:22:26 +01:00
|
|
|
if r.needs_fill() {
|
2020-11-15 21:54:47 +01:00
|
|
|
// go fetch some new data
|
2020-12-16 18:22:26 +01:00
|
|
|
if !r.fill_buffer() {
|
|
|
|
// We are at the end of the stream
|
|
|
|
if line.len == 0 {
|
|
|
|
// we had nothing so return nothing
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
return line.bytestr()
|
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
|
|
|
// try and find a newline character
|
|
|
|
mut i := r.offset
|
|
|
|
for ; i < r.len; i++ {
|
|
|
|
c := r.buf[i]
|
|
|
|
if c == `\n` {
|
|
|
|
// great, we hit something
|
|
|
|
// do some checking for whether we hit \r\n or just \n
|
2020-12-16 18:22:26 +01:00
|
|
|
if i != 0 && r.buf[i - 1] == `\r` {
|
|
|
|
x := i - 1
|
2020-11-15 21:54:47 +01:00
|
|
|
line << r.buf[r.offset..x]
|
|
|
|
} else {
|
|
|
|
line << r.buf[r.offset..i]
|
|
|
|
}
|
|
|
|
r.offset = i + 1
|
|
|
|
return line.bytestr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
line << r.buf[r.offset..i]
|
|
|
|
r.offset = i
|
|
|
|
}
|
2021-01-12 11:43:55 +01:00
|
|
|
return none
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|