From: Jean Privat Date: Sat, 13 Dec 2014 07:49:28 +0000 (-0500) Subject: Merge: Newstreams X-Git-Tag: v0.7~55 X-Git-Url: http://nitlanguage.org Merge: Newstreams Small refactoring of Streams/Files: - Added a simple error management system - Removed FDStreams, reified under FStream Next PR to come: Proposition for renaming of the classes of the stream hierarchy (as requested in #466) Pull-Request: #932 Reviewed-by: Alexis Laferrière Reviewed-by: Alexandre Terrasa Reviewed-by: Jean Privat --- 5589fb8bdbf5f01cc87cd513f2008c4923ca08f5 diff --cc lib/standard/exec.nit index 2495800,941c2ce..79f9bfd --- a/lib/standard/exec.nit +++ b/lib/standard/exec.nit @@@ -91,9 -92,7 +91,9 @@@ en class IProcess super Process super IStream + + # File Descriptor used for the input. - var stream_in: FDIStream is noinit + var stream_in: IFStream is noinit redef fun close do stream_in.close diff --cc lib/standard/file.nit index 88fb3c8,369d991..a7c48db --- a/lib/standard/file.nit +++ b/lib/standard/file.nit @@@ -143,9 -210,25 +211,26 @@@ class OFStrea end end + redef class Int + # Creates a file stream from a file descriptor `fd` using the file access `mode`. + # + # NOTE: The `mode` specified must be compatible with the one used in the file descriptor. + private fun fd_to_stream(mode: NativeString): NativeFile is extern "file_int_fdtostream" + end + + # Constant for read-only file streams + private fun read_only: NativeString do return "r".to_cstring + + # Constant for write-only file streams + # + # If a stream is opened on a file with this method, + # it will wipe the previous file if any. + # Else, it will create the file. + private fun wipe_write: NativeString do return "w".to_cstring + ############################################################################### +# Standard input stream. class Stdin super IFStream diff --cc lib/standard/stream.nit index 4139e45,f928fac..933523a --- a/lib/standard/stream.nit +++ b/lib/standard/stream.nit @@@ -48,32 -61,9 +61,33 @@@ abstract class IStrea end # Read a string until the end of the line. + # + # The line terminator '\n', if any, is preserved in each line. + # Use the method `Text::chomp` to safely remove it. + # + # ~~~ + # var txt = "Hello\n\nWorld\n" + # var i = new StringIStream(txt) + # assert i.read_line == "Hello\n" + # assert i.read_line == "\n" + # assert i.read_line == "World\n" + # assert i.eof + # ~~~ + # + # If `\n` is not present at the end of the result, it means that + # a non-eol terminated last line was returned. + # + # ~~~ + # var i2 = new StringIStream("hello") + # assert not i2.eof + # assert i2.read_line == "hello" + # assert i2.eof + # ~~~ + # + # NOTE: Only LINE FEED (`\n`) is considered to delimit the end of lines. fun read_line: String do + if last_error != null then return "" assert not eof var s = new FlatBuffer append_line_to(s) @@@ -115,10 -83,9 +130,11 @@@ end # Read a string until the end of the line and append it to `s`. + # + # SEE: `read_line` for details. fun append_line_to(s: Buffer) do + if last_error != null then return loop var x = read_char if x == -1 then @@@ -293,8 -263,7 +312,8 @@@ abstract class BufferedIStrea end end +# An Input/Output Stream - interface IOStream + abstract class IOStream super IStream super OStream end