lib: implement `close` in StringOStream
[nit.git] / lib / standard / stream.nit
index 507ba8d..a94601c 100644 (file)
 # Input and output streams of characters
 module stream
 
-import string
+intrude import ropes
 
 in "C" `{
        #include <unistd.h>
        #include <poll.h>
        #include <errno.h>
        #include <string.h>
+       #include <signal.h>
 `}
 
 # Abstract stream class
@@ -136,6 +137,29 @@ redef class Text
        redef fun write_to(stream) do stream.write(self)
 end
 
+redef class RopeNode
+       super Streamable
+end
+
+redef class Leaf
+
+       redef fun write_to(s) do s.write(str)
+end
+
+redef class Concat
+
+       redef fun write_to(s)
+       do
+               if left != null then left.write_to(s)
+               if right != null then right.write_to(s)
+       end
+end
+
+redef class RopeString
+
+       redef fun write_to(s) do root.write_to(s)
+end
+
 # Input streams with a buffer
 abstract class BufferedIStream
        super IStream
@@ -395,6 +419,13 @@ class StringOStream
 
        private var content = new Array[String]
        redef fun to_s do return content.to_s
-       redef fun is_writable do return true
-       redef fun write(str) do content.add(str.to_s)
+       redef fun is_writable do return not closed
+       redef fun write(str)
+       do
+               assert not closed
+               content.add(str.to_s)
+       end
+
+       protected var closed = false
+       redef fun close do closed = true
 end