#! @file writer.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains `writer`, which holds what it is given and writes it to any stream #! in larger pieces, and the standard output's and standard error's writers. import system; module std::io; # Bytes a writer holds before it writes them. const writer_bytes: u64 = 16384; # When a writer writes what it holds, besides when it fills, is flushed, or closes. enum writer_flushing: u8 { when_full, #! Any writer `writer_of` makes. at_lines, #! The standard output's: once it is given a line break. at_writes, #! The standard error's: after every write. } #! Writes any stream through a buffer, and is closed where its scope ends, #! writing what it still holds first. #! #! A writer is made by `writer_of`, which owns or borrows what it writes as it #! is given it: `writer_of(move(f))` owns the file `f` and closes it, while #! `writer_of(&mut f)` and `writer_of(&mut text)` borrow what they write to and #! close nothing of it. It writes what it holds only once its buffer fills, it #! is flushed, or it closes, which saves asking the system to write each piece. #! #! The standard output is written through `io::output()`, and the standard error #! through `io::error_output()`, which every part of the program shares, as #! `print` does. The standard output's writer writes what it holds once it is #! given a line break, and the standard error's after every write. #! #! Neither shared writer is closed when the program ends, so text given to #! `io::output()` after its last line break is written only once it is flushed, #! or once `print` writes, since `print` flushes every time. Reading the #! standard input flushes it too, so a prompt shows before the program waits for #! an answer. #! @tparam S The stream it writes, held by value where it owns it, or as `&mut` #! where it borrows it. pub struct writer : owning, writable, flushable where S: writable { pub var _sink: S; #! The stream written, which std's tests read. var _open: bool; #! Whether it writes, which one moved from, closed, or never made does not. var _buffer: *mut u8; #! Null until the first write. var _length: u64; #! How much of the buffer is held, yet to be written. var _flush: writer_flushing; var _error: maybe; } # The standard output and error, as `output()` and `error_output()` lend them. var standard_output: writer; var standard_error: writer; #! Lends the writer of the standard output, which every part of the program #! shares, and which writes what it holds once it is given a line break. #! @returns The writer, which is never closed by the program ending. pub def output(): &mut writer { standard_output._sink._handle = system::fd::stdout; standard_output._open = true; standard_output._flush = writer_flushing::at_lines; return &mut standard_output; } #! Lends the writer of the standard error, which every part of the program #! shares, and which writes what it is given at once. #! @returns The writer, which is never closed by the program ending. pub def error_output(): &mut writer { standard_error._sink._handle = system::fd::stderr; standard_error._open = true; standard_error._flush = writer_flushing::at_writes; return &mut standard_error; } #! Makes a writer of a stream, which owns what it is given: a stream given by #! value, as `move(f)` or a temporary, is the writer's, and closed where it #! closes, while one given by reference, as `&mut f`, is only borrowed. #! @tparam S The stream to write, as it is given. #! @param sink The stream to write, from where it is. #! @returns The writer. pub def writer_of(sink: S): writer where S: writable { return writer:: { _sink = move(sink), _open = true }; } #! Writes what the writer holds, gives back its buffer, and closes what it #! writes where it owns it. Closing the writer of the standard output or error #! leaves it open, and `output()` or `error_output()` lends it again. A writer #! that is closed, moved from, or never made writes nothing, failing with #! `io_error::invalid`. #! @param this The writer to close. #! @remarks Closing cannot say whether what the writer held was written, and #! keeps the failure in `error`, so a program that needs to know #! flushes first. pub def writer::close(&mut this): void { _ = this.flush(); raw_release(this._buffer); this._buffer = null; this._length = 0; if !this._open { return; } this._open = false; close(&mut this._sink); } #! Why the last time the writer wrote what it held failed, where it did. #! @param this The writer to ask. #! @returns The failure, or `nothing` where the last write did not fail. pub val writer::error: maybe { get => this._error; } #! Gives the writer bytes to write, which it holds until it writes them. #! @param this The writer to give them to. #! @param data The bytes to write, which may be a literal, a view of one, or #! the text a `string` lends. #! @returns Why what the writer held could not be written, where writing it to #! make room, or because a line ended, failed. pub def writer::write(&mut this, data: span): result { if !this._open { return io_error::invalid; } if data.length == 0 { return; } if this._length + data.length > writer_bytes { this.pass() else (e) { return e; }; # What is too large for the buffer to be worth passing through goes straight # to what the writer writes. if data.length >= writer_bytes { return this.wrote(this._sink.write(data)); } } if this._buffer == null { this._buffer = raw_allocate(writer_bytes); } memcpy(this._buffer + this._length, data.data, data.length); this._length += data.length; if this._flush == writer_flushing::at_writes { return this.pass(); } if this._flush == writer_flushing::at_lines && holds_line_break(data) { return this.pass(); } return; } #! Gives the writer text, then a line break, to write. #! @param this The writer to give them to. #! @param text The text to write before the line break. #! @returns Why what the writer held could not be written, where it could not. pub def writer::write_line(&mut this, text: strview): result { this.write(text) else (e) { return e; }; return this.write("\n"); } #! Writes everything the writer holds, then flushes what it writes to, where #! that holds bytes back too, so writers stacked on one another flush all the #! way down. #! @param this The writer to flush. #! @returns Why it could not all be written, where it could not, in which case #! what was held is let go of, rather than written again by the next #! flush. pub def writer::flush(&mut this): result { this.pass() else (e) { return e; }; if const S is flushable { if this._open { return this.wrote(this._sink.flush()); } } return; } # Writes everything the writer holds to what it writes, without flushing that. def writer::pass(&mut this): result { this._error = nothing{}; if this._length == 0 { return; } val count := this._length; this._length = 0; return this.wrote(this._sink.write(span::::over(this._buffer as *u8, count))); } # Keeps the failure of a write, where it failed, and gives it back. def writer::wrote(&mut this, outcome: result): result { if outcome is error(why) { this._error = why; } return outcome; } # Whether some bytes hold a line break. def holds_line_break(data: span): bool { for byte in data { if byte == '\n' { return true; } } return false; }