ENHANCED: open/4 to allow opening a stream by specifying stream(S).

This allows switching standard output to binary, using for example:

  ?- current_output(S0), open(stream(S0), write, S, [type(binary)]).

format/3 can then be used to write binary data to S. This is needed
for example when piping binary data to other programs.

This addresses #614, please read the discussion for more information.

The current implementation is very preliminary: Specifically, it works
by destructively modifiying the parameters of the underlying stream,
making it no longer usable in its original mode. Currently, if the
type of standard output is set to binary, then the toplevel no longer
works. Therefore, after writing binary output to standard output,
the program should either halt, or set the stream type back to text.
This commit is contained in:
Markus Triska
2021-11-04 18:28:05 +01:00
parent 67ef5fe8e6
commit afcd44deaa
3 changed files with 24 additions and 1 deletions

View File

@@ -3220,6 +3220,22 @@ impl MachineState {
self.bind(stream_var.as_var().unwrap(), stream);
}
&SystemClauseType::SetStreamOptions => {
let mut stream = self.get_stream_or_alias(
self[temp_v!(1)],
&indices.stream_aliases,
"open",
4,
)?;
let alias = self[temp_v!(2)];
let eof_action = self[temp_v!(3)];
let reposition = self[temp_v!(4)];
let stream_type = self[temp_v!(5)];
let options = self.to_stream_options(alias, eof_action, reposition, stream_type);
*stream.options_mut() = options;
}
&SystemClauseType::TruncateIfNoLiftedHeapGrowthDiff => {
self.truncate_if_no_lifted_heap_diff(|h| Addr::HeapCell(h))
}