Debian Codegraph @

bufio/stream.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use encoding::utf8;
5 use errors;
6 use io;
7
8 const vtable_r: io::vtable = io::vtable {
9 closer = &close_buffered,
10 reader = &read,
11 ...
12 };
13
14 const vtable_w: io::vtable = io::vtable {
15 closer = &close_buffered,
16 writer = &write,
17 ...
18 };
19
20 const vtable_rw: io::vtable = io::vtable {
21 closer = &close_buffered,
22 reader = &read,
23 writer = &write,
24 ...
25 };
26
27 export type stream = struct {
28 vtable: io::stream,
29 source: io::handle,
30 rbuffer: []u8,
31 wbuffer: []u8,
32 rpos: size,
33 ravail: size,
34 wavail: size,
35 flush: []u8,
36 flags: flag,
37 };
38
39 // Flags to tune the behavior of [[bufio::stream]].
40 export type flag = enum uint {
41 NONE = 0,
42 // If set, the underling [[io::handle]] for a [[bufio::stream]] is
43 // closed when [[io::close]] is called on the [[bufio::stream]] object.
44 MANAGED_HANDLE = 1 << 0,
45 // If set, the read buffer for a [[bufio::stream]] is freed when
46 // [[io::close]] is called on the [[bufio::stream]] object.
47 MANAGED_RDBUF = 1 << 1,
48 // If set, the write buffer for a [[bufio::stream]] is freed when
49 // [[io::close]] is called on the [[bufio::stream]] object.
50 MANAGED_WRBUF = 1 << 2,
51 // The [[io::handle]] and the read and write buffers are owned by the
52 // [[bufio::stream]] object and will be disposed of (closed or freed
53 // respectively) when closing the [[bufio::stream]].
54 MANAGED = MANAGED_HANDLE | MANAGED_RDBUF | MANAGED_WRBUF,
55 };
56
57 // Creates a stream which buffers reads and writes for the underlying stream.
58 // This is generally used to improve performance of small reads/writes for
59 // sources where I/O operations are costly, such as if they invoke a syscall or
60 // take place over the network.
61 //
62 // The caller should supply one or both of a read and write buffer as a slice of
63 // the desired buffer, or empty slices if read or write functionality is
64 // disabled. The same buffer may not be used for both reads and writes.
65 //
66 // let rbuf: [os::BUFSZ]u8 = [0...];
67 // let wbuf: [os::BUFSZ]u8 = [0...];
68 // let buffered = bufio::init(source, rbuf, wbuf);
69 export fn init(
70 src: io::handle,
71 rbuf: []u8,
72 wbuf: []u8,
73 flags: flag = flag::NONE,
74 ) stream = {
75 static let flush_default = ['\n': u8];
76
77 let vtable: nullable *io::vtable = null;
78 if (len(rbuf) != 0 && len(wbuf) != 0) {
79 assert(rbuf: *[*]u8 != wbuf: *[*]u8,
80 "Cannot use same buffer for reads and writes");
81 vtable = &vtable_rw;
82 } else if (len(rbuf) != 0) {
83 vtable = &vtable_r;
84 } else if (len(wbuf) != 0) {
85 vtable = &vtable_w;
86 };
87
88 const vtable = match (vtable) {
89 case let vt: *io::vtable =>
90 yield vt;
91 case null =>
92 abort("Must provide at least one buffer");
93 };
94
95 return stream {
96 vtable = vtable,
97 source = src,
98 rbuffer = rbuf,
99 wbuffer = wbuf,
100 flush = flush_default,
101 rpos = len(rbuf), // necessary for unread() before read()
102 flags = flags,
103 ...
104 };
105 };
106
107 // Flushes pending writes to the underlying stream.
108 export fn flush(s: io::handle) (void | io::error) = {
109 let s = match (s) {
110 case let st: *io::stream =>
111 if (st.writer != &write) {
112 return errors::unsupported;
113 };
114 yield st: *stream;
115 case =>
116 return errors::unsupported;
117 };
118 if (s.wavail == 0) {
119 return;
120 };
121 io::writeall(s.source, s.wbuffer[..s.wavail])?;
122 s.wavail = 0;
123 return;
124 };
125
126 // Sets the list of bytes which will cause the stream to flush when written. By
127 // default, the stream will flush when a newline (\n) is written.
128 export fn setflush(s: io::handle, b: []u8) void = {
129 let s = match (s) {
130 case let st: *io::stream =>
131 if (st.writer != &write) {
132 abort("Attempted to set flush bytes on unbuffered stream");
133 };
134 yield st: *stream;
135 case =>
136 abort("Attempted to set flush bytes on unbuffered stream");
137 };
138 s.flush = b;
139 };
140
141 // "Unreads" a slice of bytes, such that the next call to "read" will return
142 // these bytes before reading any new data from the underlying source. The
143 // unread data must fit into the read buffer's available space. The amount of
144 // data which can be unread before the user makes any reads from a buffered
145 // stream is equal to the length of the read buffer, and otherwise it is equal
146 // to the length of the return value of the last call to [[io::read]] using this
147 // buffered stream. Attempting to unread more data than can fit into the read
148 // buffer will abort the program.
149 export fn unread(s: io::handle, buf: []u8) void = {
150 match (s) {
151 case let st: *io::stream =>
152 if (st.reader == &read) {
153 stream_unread(s: *stream, buf);
154 } else if (st.reader == &scan_read) {
155 scan_unread(s: *scanner, buf);
156 } else {
157 abort("Attempted unread on unbuffered stream");
158 };
159 case =>
160 abort("Attempted unread on unbuffered stream");
161 };
162 };
163
164 fn stream_unread(s: *stream, buf: []u8) void = {
165 assert(s.rpos >= len(buf),
166 "Attempted to unread more data than buffer has available");
167 s.rbuffer[s.rpos - len(buf)..s.rpos] = buf;
168 s.rpos -= len(buf);
169 s.ravail += len(buf);
170 };
171
172 // Unreads a rune; see [[unread]].
173 export fn unreadrune(s: io::handle, rn: rune) void = {
174 const buf = utf8::encoderune(rn);
175 unread(s, buf);
176 };
177
178 // Returns true if an [[io::handle]] is a [[stream]].
179 export fn isbuffered(in: io::handle) bool = {
180 match (in) {
181 case io::file =>
182 return false;
183 case let st: *io::stream =>
184 return st.reader == &read || st.writer == &write;
185 };
186 };
187
188 fn close_buffered(s: *io::stream) (void | io::error) = {
189 const s = s: *stream;
190 assert(s.vtable.closer == &close_buffered);
191 if (s.vtable.writer != null) {
192 flush(s: *stream)?;
193 };
194 if (s.flags & flag::MANAGED_HANDLE != 0) {
195 io::close(s.source)?;
196 };
197 if (s.flags & flag::MANAGED_RDBUF != 0) {
198 free(s.rbuffer);
199 };
200 if (s.flags & flag::MANAGED_WRBUF != 0) {
201 free(s.wbuffer);
202 };
203 };
204
205 fn read(s: *io::stream, buf: []u8) (size | io::EOF | io::error) = {
206 assert(s.reader == &read);
207 let s = s: *stream;
208
209 if (s.ravail < len(buf) && s.ravail < len(s.rbuffer)) {
210 s.rbuffer[..s.ravail] = s.rbuffer[s.rpos..s.rpos + s.ravail];
211 s.rpos = 0;
212 match (io::read(s.source, s.rbuffer[s.ravail..])) {
213 case let err: io::error =>
214 return err;
215 case io::EOF =>
216 if (s.ravail == 0) {
217 return io::EOF;
218 };
219 case let z: size =>
220 s.ravail += z;
221 };
222 };
223
224 const n = if (len(buf) < s.ravail) len(buf) else s.ravail;
225 buf[..n] = s.rbuffer[s.rpos..s.rpos + n];
226 s.rpos += n;
227 s.ravail -= n;
228 return n;
229 };
230
231 fn write(s: *io::stream, buf: const []u8) (size | io::error) = {
232 assert(s.writer == &write);
233 let s = s: *stream;
234 let buf = buf;
235
236 let doflush = false;
237 if (len(s.flush) != 0) {
238 for :search (let i = 0z; i < len(buf); i += 1) {
239 for (let j = 0z; j < len(s.flush); j += 1) {
240 if (buf[i] == s.flush[j]) {
241 doflush = true;
242 break :search;
243 };
244 };
245 };
246 };
247
248 let z = 0z;
249 for (len(buf) > 0) {
250 let avail = len(s.wbuffer) - s.wavail;
251 if (avail == 0) {
252 flush(s)?;
253 avail = len(s.wbuffer);
254 };
255
256 const n = if (avail < len(buf)) avail else len(buf);
257 s.wbuffer[s.wavail..s.wavail + n] = buf[..n];
258 buf = buf[n..];
259 s.wavail += n;
260 z += n;
261 };
262
263 if (doflush) {
264 flush(s)?;
265 };
266
267 return z;
268 };