From 594dcf57e29f791729e262c84c031dbea1b95747 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Mon, 27 Jul 2015 13:01:14 -0400 Subject: [PATCH] lib/exec: intro `ProcessDuplex::write_and_read` MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/standard/exec.nit | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/standard/exec.nit b/lib/standard/exec.nit index 517edf3..3e0d97a 100644 --- a/lib/standard/exec.nit +++ b/lib/standard/exec.nit @@ -272,9 +272,49 @@ class ProcessDuplex redef fun pipeflags do return 3 - redef fun execute + redef fun execute do super + + # Write `input` to process and return its output + # + # Writing and reading are processed line by line, + # reading only when something is available. + # + # ~~~ + # var proc = new ProcessDuplex("tr", "[:lower:]", "[:upper:]") + # assert proc.write_and_read(""" + # Alice + # Bob + # """) == """ + # ALICE + # BOB + # """ + # ~~~ + fun write_and_read(input: Text): String do - super + var read = new Buffer #new Array[String] + + # Main loop, read and write line by line + var prev = 0 + for delimiter in input.search_all('\n') do + write input.substring(prev, delimiter.after-prev) + prev = delimiter.after + + while stream_in.poll_in do + read.append stream_in.read_line + end + end + + # Write the last line + write input.substring_from(prev) + stream_out.close + + # Read the rest, may be everything for some programs + read.append stream_in.read_all + stream_in.close + + # Clean up + wait + return read.to_s end end -- 1.7.9.5