Merge: Newstreams
authorJean Privat <jean@pryen.org>
Sat, 13 Dec 2014 07:49:28 +0000 (02:49 -0500)
committerJean Privat <jean@pryen.org>
Sat, 13 Dec 2014 07:49:28 +0000 (02:49 -0500)
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 <alexis.laf@xymus.net>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
Reviewed-by: Jean Privat <jean@pryen.org>

1  2 
lib/pnacl.nit
lib/standard/exec.nit
lib/standard/file.nit
lib/standard/stream.nit
src/interpreter/naive_interpreter.nit

diff --cc lib/pnacl.nit
Simple merge
@@@ -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
  
@@@ -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
  
@@@ -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)
        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
Simple merge