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