~sircmpwn/hare-xml

1524aef6cffedb3c91dba4b6015a9012be7fe67d — Alexey Yerin 8 months ago 10e3c6b
bufio: use caller allocation

fixed() and dynamic() return a memstream, and buffered() returns
bufstream. This also removes the need for static_buffered() hack.

Similarly to strio, bufio::finish() is removed as the streams do not
allocate themselves.

Necessary update is adding '&' to uses of bufio streams.

Signed-off-by: Alexey Yerin <yyp@disroot.org>
2 files changed, 7 insertions(+), 5 deletions(-)

M parser.ha
M types.ha
M parser.ha => parser.ha +5 -4
@@ 22,15 22,16 @@ export fn parse(in: io::handle) (*parser | error) = {
	// stack is so that we have a consistent address for the bufio buffer.
	// This is kind of lame, maybe we can avoid that.
	let par = alloc(parser {
		in = in,
		close = false,
		namebuf = strio::dynamic(),
		entbuf = strio::dynamic(),
		textbuf = strio::dynamic(),
		...
	});
	if (!bufio::isbuffered(in)) {
		par.in = bufio::buffered(par.in, par.buf[..], []);
	if (bufio::isbuffered(in)) {
		par.in = in as *io::stream: *bufio::bufstream;
	} else {
		par.in = alloc(bufio::buffered(par.in, par.buf[..], []));
		par.close = true;
	};
	prolog(par)?;


@@ 41,7 42,7 @@ export fn parse(in: io::handle) (*parser | error) = {
// underlying I/O handle.
export fn parser_free(par: *parser) void = {
	if (par.close) {
		io::close(par.in);
		free(par.in);
	};
	io::close(&par.namebuf);
	io::close(&par.entbuf);

M types.ha => types.ha +2 -1
@@ 1,10 1,11 @@
use bufio;
use encoding::utf8;
use io;
use os;
use strio;

export type parser = struct {
	in: io::handle,
	in: *bufio::bufstream,
	buf: [os::BUFSIZ]u8,
	close: bool,
	state: state,