# This file is part of NIT ( http://www.nitlanguage.org ).
#
-# Copyright 2004-2008 Jean Privat <jean@pryen.org>
-#
-# This file is free software, which comes along with NIT. This software is
+# 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
-# PARTICULAR PURPOSE. You can modify it is you want, provided this header
+# 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
+# You are allowed to redistribute it and sell it, alone or is a part of
# another product.
# Input and output streams of characters
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
fill_buffer
end
return s.to_s
- end
+ end
redef fun append_line_to(s)
do
# Fill the buffer
protected fun fill_buffer is abstract
- # Is the last fill_buffer reach the end
+ # Is the last fill_buffer reach the end
protected fun end_reached: Bool is abstract
# Allocate a `_buffer` for a given `capacity`.
end
end
+# An Input/Output Stream
interface IOStream
super IStream
super OStream
##############################################################"
+# A File Descriptor Stream.
abstract class FDStream
super IOS
# File description
private fun native_read(i: Int, buf: NativeString, len: Int): Int is extern "stream_FDStream_FDStream_native_read_3"
private fun native_write(i: Int, buf: NativeString, len: Int): Int is extern "stream_FDStream_FDStream_native_write_3"
private fun native_write_char(i: Int, c: Char): Int is extern "stream_FDStream_FDStream_native_write_char_2"
-
- init(fd: Int) do self.fd = fd
end
+# An Input File Descriptor Stream.
class FDIStream
super FDStream
super IStream
if nb == -1 then eof = true
return nb
end
-
- init(fd: Int) do end
end
+# An Output File Descriptor Stream.
class FDOStream
super FDStream
super OStream
- redef var is_writable: Bool
+ redef var is_writable = true
redef fun write(s)
do
var nb = native_write(fd, s.to_cstring, s.length)
if nb < s.length then is_writable = false
end
-
- init(fd: Int)
- do
- is_writable = true
- end
end
+# An Input/Output File Descriptor Stream.
class FDIOStream
super FDIStream
super FDOStream
super IOStream
- init(fd: Int)
- do
- self.fd = fd
- is_writable = true
- end
end
redef interface Object
`}
end
-# Stream to a String. Mainly used for compatibility with OStream type and tests.
+# Stream to a String.
+#
+# Mainly used for compatibility with OStream type and tests.
class StringOStream
super OStream
content.add(str.to_s)
end
+ # Is the stream closed?
protected var closed = false
+
redef fun close do closed = true
end
+
+# Stream from a String.
+#
+# Mainly used for compatibility with IStream type and tests.
+class StringIStream
+ super IStream
+
+ # The string to read from.
+ var source: String
+
+ # The current position in the string.
+ private var cursor: Int = 0
+
+ redef fun read_char do
+ if cursor < source.length then
+ var c = source[cursor].ascii
+
+ cursor += 1
+ return c
+ else
+ return -1
+ end
+ end
+
+ redef fun close do
+ source = ""
+ end
+
+ redef fun eof do return cursor >= source.length
+end