1 5b302645 2023-12-21 op // This is free and unencumbered software released into the public domain.
3 5b302645 2023-12-21 op // Anyone is free to copy, modify, publish, use, compile, sell, or
4 5b302645 2023-12-21 op // distribute this software, either in source code form or as a compiled
5 5b302645 2023-12-21 op // binary, for any purpose, commercial or non-commercial, and by any
8 5b302645 2023-12-21 op // In jurisdictions that recognize copyright laws, the author or authors
9 5b302645 2023-12-21 op // of this software dedicate any and all copyright interest in the
10 5b302645 2023-12-21 op // software to the public domain. We make this dedication for the benefit
11 5b302645 2023-12-21 op // of the public at large and to the detriment of our heirs and
12 5b302645 2023-12-21 op // successors. We intend this dedication to be an overt act of
13 5b302645 2023-12-21 op // relinquishment in perpetuity of all present and future rights to this
14 5b302645 2023-12-21 op // software under copyright law.
16 5b302645 2023-12-21 op // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 5b302645 2023-12-21 op // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 5b302645 2023-12-21 op // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 5b302645 2023-12-21 op // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 5b302645 2023-12-21 op // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 5b302645 2023-12-21 op // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 5b302645 2023-12-21 op // OTHER DEALINGS IN THE SOFTWARE.
26 5b302645 2023-12-21 op def chunksize = 1024;
28 5b302645 2023-12-21 op export type dynstream = struct {
29 5b302645 2023-12-21 op vtable: io::stream,
30 5b302645 2023-12-21 op dst: io::handle,
35 5b302645 2023-12-21 op const dynstream_vtable: io::vtable = io::vtable {
36 5b302645 2023-12-21 op writer = &dynstream_write,
37 5b302645 2023-12-21 op closer = &dynstream_close,
41 5b302645 2023-12-21 op fn new_dynstream(s: io::handle) dynstream = {
42 5b302645 2023-12-21 op return dynstream {
43 5b302645 2023-12-21 op vtable = &dynstream_vtable,
49 5b302645 2023-12-21 op fn dynstream_resize(w: *dynstream, n: size) void = {
50 5b302645 2023-12-21 op let cap = len(w.buf);
51 5b302645 2023-12-21 op for (!(w.length + n < cap)) {
52 5b302645 2023-12-21 op cap += chunksize;
55 5b302645 2023-12-21 op if (cap == len(w.buf))
58 5b302645 2023-12-21 op let newbuf: []u8 = alloc([0...], cap);
59 5b302645 2023-12-21 op newbuf[0..len(w.buf)] = w.buf;
61 5b302645 2023-12-21 op w.buf = newbuf;
64 5b302645 2023-12-21 op fn dynstream_write(s: *io::stream, buf: const []u8) (size | io::error) = {
65 5b302645 2023-12-21 op let w = s: *dynstream;
67 5b302645 2023-12-21 op dynstream_resize(w, len(buf));
69 5b302645 2023-12-21 op w.buf[w.length..(w.length+len(buf))] = buf;
70 5b302645 2023-12-21 op w.length += len(buf);
72 5b302645 2023-12-21 op return len(buf);
75 5b302645 2023-12-21 op fn dynstream_close(s: *io::stream) (void | io::error) = {
76 5b302645 2023-12-21 op let w = s: *dynstream;
81 5b302645 2023-12-21 op fn dynstream_flush(w: *dynstream) (void | io::error) = {
82 5b302645 2023-12-21 op let wrote = io::write(w.dst, w.buf[0..w.length])?;
84 5b302645 2023-12-21 op let n = w.length - wrote;
85 5b302645 2023-12-21 op w.buf[0..n] = w.buf[wrote..w.length];
86 5b302645 2023-12-21 op w.length -= wrote;