nitlanguage
/
nit.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
lib: add standard/error.nit
[nit.git]
/
lib
/
standard
/
stream.nit
diff --git
a/lib/standard/stream.nit
b/lib/standard/stream.nit
index
932919b
..
d14e82f
100644
(file)
--- a/
lib/standard/stream.nit
+++ b/
lib/standard/stream.nit
@@
-13,13
+13,14
@@
# Input and output streams of characters
module stream
# 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>
in "C" `{
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include <string.h>
+ #include <signal.h>
`}
# Abstract stream class
`}
# Abstract stream class
@@
-88,6
+89,15
@@
interface IStream
fun eof: Bool is abstract
end
fun eof: Bool is abstract
end
+# IStream capable of declaring if readable without blocking
+interface PollableIStream
+ super IStream
+
+ # Is there something to read? (without blocking)
+ fun poll_in: Bool is abstract
+
+end
+
# Abstract output stream
interface OStream
super IOS
# Abstract output stream
interface OStream
super IOS
@@
-127,6
+137,29
@@
redef class Text
redef fun write_to(stream) do stream.write(self)
end
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
# Input streams with a buffer
abstract class BufferedIStream
super IStream
@@
-146,24
+179,20
@@
abstract class BufferedIStream
redef fun read(i)
do
redef fun read(i)
do
- var s = new FlatBuffer.with_capacity(i)
- var j = _buffer_pos
- var k = _buffer.length
- while i > 0 do
- if j >= k then
+ if _buffer.length == _buffer_pos then
+ if not eof then
fill_buffer
fill_buffer
- if eof then return s.to_s
- j = _buffer_pos
- k = _buffer.length
- end
- while j < k and i > 0 do
- s.add(_buffer.chars[j])
- j += 1
- i -= 1
+ return read(i)
end
end
+ return ""
end
end
- _buffer_pos = j
- return s.to_s
+ if _buffer_pos + i >= _buffer.length then
+ var from = _buffer_pos
+ _buffer_pos = _buffer.length
+ return _buffer.substring_from(from).to_s
+ end
+ _buffer_pos += i
+ return _buffer.substring(_buffer_pos - i, i).to_s
end
redef fun read_all
end
redef fun read_all
@@
-173,7
+202,7
@@
abstract class BufferedIStream
var j = _buffer_pos
var k = _buffer.length
while j < k do
var j = _buffer_pos
var k = _buffer.length
while j < k do
- s.add(_buffer.chars[j])
+ s.add(_buffer[j])
j += 1
end
_buffer_pos = j
j += 1
end
_buffer_pos = j
@@
-390,6
+419,13
@@
class StringOStream
private var content = new Array[String]
redef fun to_s do return content.to_s
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
end