Blame


1 5b302645 2023-12-21 op // This is free and unencumbered software released into the public domain.
2 5b302645 2023-12-21 op //
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
6 5b302645 2023-12-21 op // means.
7 5b302645 2023-12-21 op //
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.
15 5b302645 2023-12-21 op //
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.
23 5b302645 2023-12-21 op
24 5b302645 2023-12-21 op use io;
25 5b302645 2023-12-21 op
26 5b302645 2023-12-21 op def chunksize = 1024;
27 5b302645 2023-12-21 op
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,
31 5b302645 2023-12-21 op buf: []u8,
32 5b302645 2023-12-21 op length: size,
33 5b302645 2023-12-21 op };
34 5b302645 2023-12-21 op
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,
38 5b302645 2023-12-21 op ...
39 5b302645 2023-12-21 op };
40 5b302645 2023-12-21 op
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,
44 5b302645 2023-12-21 op dst = s,
45 5b302645 2023-12-21 op ...
46 5b302645 2023-12-21 op };
47 5b302645 2023-12-21 op };
48 5b302645 2023-12-21 op
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;
53 5b302645 2023-12-21 op };
54 5b302645 2023-12-21 op
55 5b302645 2023-12-21 op if (cap == len(w.buf))
56 5b302645 2023-12-21 op return;
57 5b302645 2023-12-21 op
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;
60 5b302645 2023-12-21 op free(w.buf);
61 5b302645 2023-12-21 op w.buf = newbuf;
62 5b302645 2023-12-21 op };
63 5b302645 2023-12-21 op
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;
66 5b302645 2023-12-21 op
67 5b302645 2023-12-21 op dynstream_resize(w, len(buf));
68 5b302645 2023-12-21 op
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);
71 5b302645 2023-12-21 op
72 5b302645 2023-12-21 op return len(buf);
73 5b302645 2023-12-21 op };
74 5b302645 2023-12-21 op
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;
77 5b302645 2023-12-21 op
78 5b302645 2023-12-21 op free(w.buf);
79 5b302645 2023-12-21 op };
80 5b302645 2023-12-21 op
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])?;
83 5b302645 2023-12-21 op
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;
87 5b302645 2023-12-21 op };