aboutsummaryrefslogtreecommitdiff
path: root/core/bufio/read_writer.odin
blob: 84769b9a69db36ddfaf3a191c930a35ca6c35f09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package bufio

import "core:io"

// Read_Writer stores pointers to a Reader and a Writer
Read_Writer :: struct {
	r: ^Reader,
	w: ^Writer,
}


read_writer_init :: proc(rw: ^Read_Writer, r: ^Reader, w: ^Writer) {
	rw.r, rw.w = r, w
}

read_writer_to_stream :: proc(rw: ^Read_Writer) -> (s: io.Stream) {
	s.procedure = _read_writer_procedure
	s.data = rw
	return
}

@(private)
_read_writer_procedure := proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
	rw := (^Read_Writer)(stream_data)
	n_int: int
	#partial switch mode {
	case .Flush:
		err = writer_flush(rw.w)
		return
	case .Read:
		n_int, err = reader_read(rw.r, p)
		n = i64(n_int)
		return
	case .Write:
		n_int, err = writer_write(rw.w, p)
		n = i64(n_int)
		return
	case .Query:
		return io.query_utility({.Flush, .Read, .Write, .Query})
	}
	return 0, .Unsupported
}