84b167ab4e7d47310f54b7d7fe60faab4ea1c079
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
11 # Input and output streams of characters
24 # Abstract stream class
30 # Abstract input streams
33 # Read a character. Return its ASCII value, -1 on EOF or timeout
34 fun read_char
: Int is abstract
36 # Read at most i bytes
37 fun read
(i
: Int): String
39 var s
= new FlatBuffer.with_capacity
(i
)
40 while i
> 0 and not eof
do
50 # Read a string until the end of the line.
54 var s
= new FlatBuffer
59 # Read all the stream until the eof.
62 var s
= new FlatBuffer
65 if c
>= 0 then s
.add
(c
.ascii
)
70 # Read a string until the end of the line and append it to `s`.
71 fun append_line_to
(s
: Buffer)
80 if c
== '\n' then return
85 # Is there something to read.
86 # This function returns 'false' if there is something to read.
87 fun eof
: Bool is abstract
90 # IStream capable of declaring if readable without blocking
91 interface PollableIStream
94 # Is there something to read? (without blocking)
95 fun poll_in
: Bool is abstract
99 # Abstract output stream
103 fun write
(s
: Text) is abstract
105 # Can the stream be used to write
106 fun is_writable
: Bool is abstract
109 # Things that can be efficienlty writen to a OStream
111 # The point of this interface it to allow is instance to be efficenty
112 # writen into a OStream without having to allocate a big String object
114 # ready-to-save documents usually provide this interface.
116 # Write itself to a `stream`
117 # The specific logic it let to the concrete subclasses
118 fun write_to
(stream
: OStream) is abstract
120 # Like `write_to` but return a new String (may be quite large)
122 # This funtionnality is anectodical, since the point
123 # of streamable object to to be efficienlty written to a
124 # stream without having to allocate and concatenate strings
125 fun write_to_string
: String
127 var stream
= new StringOStream
135 redef fun write_to
(stream
) do stream
.write
(self)
138 # Input streams with a buffer
139 abstract class BufferedIStream
144 if _buffer_pos
>= _buffer
.length
then
147 if _buffer_pos
>= _buffer
.length
then
150 var c
= _buffer
.chars
[_buffer_pos
]
157 if _buffer
.length
== _buffer_pos
then
164 if _buffer_pos
+ i
>= _buffer
.length
then
165 var from
= _buffer_pos
166 _buffer_pos
= _buffer
.length
167 return _buffer
.substring_from
(from
).to_s
170 return _buffer
.substring
(_buffer_pos
- i
, i
).to_s
175 var s
= new FlatBuffer
178 var k
= _buffer
.length
189 redef fun append_line_to
(s
)
192 # First phase: look for a '\n'
194 while i
< _buffer
.length
and _buffer
.chars
[i
] != '\n' do i
+= 1
196 # if there is something to append
197 if i
> _buffer_pos
then
198 # Enlarge the string (if needed)
199 s
.enlarge
(s
.length
+ i
- _buffer_pos
)
201 # Copy from the buffer to the string
204 s
.add
(_buffer
.chars
[j
])
209 if i
< _buffer
.length
then
210 # so \n is in _buffer[i]
211 _buffer_pos
= i
+ 1 # skip \n
225 redef fun eof
do return _buffer_pos
>= _buffer
.length
and end_reached
228 private var buffer
: nullable FlatBuffer = null
230 # The current position in the buffer
231 private var buffer_pos
: Int = 0
234 protected fun fill_buffer
is abstract
236 # Is the last fill_buffer reach the end
237 protected fun end_reached
: Bool is abstract
239 # Allocate a `_buffer` for a given `capacity`.
240 protected fun prepare_buffer
(capacity
: Int)
242 _buffer
= new FlatBuffer.with_capacity
(capacity
)
243 _buffer_pos
= 0 # need to read
252 ##############################################################"
254 abstract class FDStream
259 redef fun close
do native_close
(fd
)
261 private fun native_close
(i
: Int): Int is extern "stream_FDStream_FDStream_native_close_1"
262 private fun native_read_char
(i
: Int): Int is extern "stream_FDStream_FDStream_native_read_char_1"
263 private fun native_read
(i
: Int, buf
: NativeString, len
: Int): Int is extern "stream_FDStream_FDStream_native_read_3"
264 private fun native_write
(i
: Int, buf
: NativeString, len
: Int): Int is extern "stream_FDStream_FDStream_native_write_3"
265 private fun native_write_char
(i
: Int, c
: Char): Int is extern "stream_FDStream_FDStream_native_write_char_2"
271 redef var eof
: Bool = false
275 var nb
= native_read_char
(fd
)
276 if nb
== -1 then eof
= true
284 redef var is_writable
= true
288 var nb
= native_write
(fd
, s
.to_cstring
, s
.length
)
289 if nb
< s
.length
then is_writable
= false
299 redef interface Object
300 # returns first available stream to read or write to
301 # return null on interruption (possibly a signal)
302 protected fun poll
( streams
: Sequence[FDStream] ) : nullable FDStream
304 var in_fds
= new Array[Int]
305 var out_fds
= new Array[Int]
306 var fd_to_stream
= new HashMap[Int,FDStream]
309 if s
isa FDIStream then in_fds
.add
( fd
)
310 if s
isa FDOStream then out_fds
.add
( fd
)
315 var polled_fd
= intern_poll
( in_fds
, out_fds
)
317 if polled_fd
== null then
320 return fd_to_stream
[polled_fd
]
324 private fun intern_poll
(in_fds
: Array[Int], out_fds
: Array[Int]) : nullable Int is extern import Array[Int].length
, Array[Int].[], Int.as(nullable Int) `{
325 int in_len, out_len, total_len;
326 struct pollfd *c_fds;
329 int first_polled_fd = -1;
332 in_len = Array_of_Int_length( in_fds );
333 out_len = Array_of_Int_length( out_fds );
334 total_len = in_len + out_len;
335 c_fds = malloc( sizeof(struct pollfd) * total_len );
338 for ( i=0; i<in_len; i ++ ) {
340 fd = Array_of_Int__index( in_fds, i );
343 c_fds[i].events = POLLIN;
347 for ( i=0; i<out_len; i ++ ) {
349 fd = Array_of_Int__index( out_fds, i );
352 c_fds[i].events = POLLOUT;
355 /* poll all fds, unlimited timeout */
356 result = poll( c_fds, total_len, -1 );
359 /* analyse results */
360 for ( i=0; i<total_len; i++ )
361 if ( c_fds[i].revents & c_fds[i].events || /* awaited event */
362 c_fds[i].revents & POLLHUP ) /* closed */
364 first_polled_fd = c_fds[i].fd;
368 return Int_as_nullable( first_polled_fd );
370 else if ( result < 0 )
371 fprintf( stderr, "Error in Stream:poll: %s\n", strerror( errno ) );
377 # Stream to a String.
379 # Mainly used for compatibility with OStream type and tests.
383 private var content
= new Array[String]
384 redef fun to_s
do return content
.to_s
385 redef fun is_writable
do return not closed
389 content
.add
(str
.to_s
)
392 protected var closed
= false
393 redef fun close
do closed
= true
396 # Stream from a String.
398 # Mainly used for compatibility with IStream type and tests.
402 # The string to read from.
405 # The current position in the string.
406 private var cursor
: Int = 0
408 redef fun read_char
do
409 if cursor
< source
.length
then
410 var c
= source
[cursor
].ascii
423 redef fun eof
do return cursor
>= source
.length