Merge: Basename fix
[nit.git] / lib / filter_stream.nit
index bcd01be..f1537e4 100644 (file)
@@ -1,36 +1,32 @@
 # This file is part of NIT ( http://www.nitlanguage.org ).
 #
 # Copyright 2006 Floréal Morandat <morandat@lirmm.fr>
+# Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 #
 # This file is free software, which comes along with NIT.  This software is
 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
-# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A 
+# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A
 # PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
 # is kept unaltered, and a notification of the changes is added.
 # You  are  allowed  to  redistribute it and sell it, alone or is a part of
 # another product.
 
-class FilterIStream
-special IStream
+class FilterReader
+       super Reader
        # Filter readed elements
-       readable var _stream: IStream 
+       var stream: nullable Reader = null
 
        redef fun eof: Bool
        do
                assert stream != null
                return stream.eof
        end
-
-       private fun stream=(i: IStream)
-       do
-               _stream = i
-       end
 end
 
-class FilterOStream
-special OStream
+class FilterWriter
+       super Writer
        # Filter outputed elements
-       readable var _stream: OStream 
+       var stream: nullable Writer = null
 
        # Can the stream be used to write
        redef fun is_writable: Bool
@@ -38,16 +34,11 @@ special OStream
                assert stream != null
                return stream.is_writable
        end
-
-       private fun stream=(i: OStream)
-       do
-               _stream = i
-       end
 end
 
 class StreamCat
-special FilterIStream
-       var _streams: Iterator[IStream]
+       super FilterReader
+       private var streams: Iterator[Reader]
 
        redef fun eof: Bool
        do
@@ -62,17 +53,19 @@ special FilterIStream
                end
        end
 
-       redef fun stream: IStream
+       redef fun stream: nullable Reader
        do
-               if _stream == null and _streams.is_ok then
-                       stream = _streams.item
-                       assert _stream != null
+               var res = super
+               if res == null and _streams.is_ok then
+                       res = _streams.item
+                       stream = res
+                       assert stream != null
                        _streams.next
                end
-               return _stream
+               return res
        end
 
-       redef fun read_char: Int
+       redef fun read_char
        do
                assert not eof
                return stream.read_char
@@ -86,19 +79,19 @@ special FilterIStream
                end
        end
 
-       init with_streams(streams: Array[IStream])
+       init with_streams(streams: Array[Reader])
        do
                _streams = streams.iterator
        end
-       init(streams: IStream ...)
-       do
+
+       init(streams: Reader ...) is old_style_init do
                _streams = streams.iterator
        end
 end
 
 class StreamDemux
-special FilterOStream
-       var _streams: Array[OStream]
+       super FilterWriter
+       private var streams: Array[Writer]
 
        redef fun is_writable: Bool
        do
@@ -115,7 +108,7 @@ special FilterOStream
                end
        end
 
-       redef fun write(s: String)
+       redef fun write(s: Text)
        do
                for i in _streams
                do
@@ -126,13 +119,21 @@ special FilterOStream
                end
        end
 
-       init with_streams(streams: Array[OStream])
+       redef fun close
+       do
+               for i in _streams
+               do
+                       stream = i
+                       stream.close
+               end
+       end
+
+       init with_streams(streams: Array[Writer])
        do
                _streams = streams
        end
 
-       init(streams: OStream ...)
-       do
+       init(streams: Writer ...) is old_style_init do
                _streams = streams
        end
 end