1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
13 # Input and output streams of characters
25 # Abstract stream class
31 # Abstract input streams
34 # Read a character. Return its ASCII value, -1 on EOF or timeout
35 fun read_char
: Int is abstract
37 # Read at most i bytes
38 fun read
(i
: Int): String
40 var s
= new FlatBuffer.with_capacity
(i
)
41 while i
> 0 and not eof
do
51 # Read a string until the end of the line.
55 var s
= new FlatBuffer
60 # Read all the stream until the eof.
63 var s
= new FlatBuffer
66 if c
>= 0 then s
.add
(c
.ascii
)
71 # Read a string until the end of the line and append it to `s`.
72 fun append_line_to
(s
: Buffer)
81 if c
== '\n' then return
86 # Is there something to read.
87 # This function returns 'false' if there is something to read.
88 fun eof
: Bool is abstract
91 # IStream capable of declaring if readable without blocking
92 interface PollableIStream
95 # Is there something to read? (without blocking)
96 fun poll_in
: Bool is abstract
100 # Abstract output stream
104 fun write
(s
: Text) is abstract
106 # Can the stream be used to write
107 fun is_writable
: Bool is abstract
110 # Things that can be efficienlty writen to a OStream
112 # The point of this interface it to allow is instance to be efficenty
113 # writen into a OStream without having to allocate a big String object
115 # ready-to-save documents usually provide this interface.
117 # Write itself to a `stream`
118 # The specific logic it let to the concrete subclasses
119 fun write_to
(stream
: OStream) is abstract
121 # Like `write_to` but return a new String (may be quite large)
123 # This funtionnality is anectodical, since the point
124 # of streamable object to to be efficienlty written to a
125 # stream without having to allocate and concatenate strings
126 fun write_to_string
: String
128 var stream
= new StringOStream
136 redef fun write_to
(stream
) do stream
.write
(self)
145 redef fun write_to
(s
) do s
.write
(str
)
150 redef fun write_to
(s
)
152 if left
!= null then left
.write_to
(s
)
153 if right
!= null then right
.write_to
(s
)
157 redef class RopeString
159 redef fun write_to
(s
) do root
.write_to
(s
)
162 # Input streams with a buffer
163 abstract class BufferedIStream
168 if _buffer_pos
>= _buffer
.length
then
171 if _buffer_pos
>= _buffer
.length
then
174 var c
= _buffer
.chars
[_buffer_pos
]
181 if _buffer
.length
== _buffer_pos
then
188 if _buffer_pos
+ i
>= _buffer
.length
then
189 var from
= _buffer_pos
190 _buffer_pos
= _buffer
.length
191 return _buffer
.substring_from
(from
).to_s
194 return _buffer
.substring
(_buffer_pos
- i
, i
).to_s
199 var s
= new FlatBuffer
202 var k
= _buffer
.length
204 s
.add
(_buffer
.chars
[j
])
213 redef fun append_line_to
(s
)
216 # First phase: look for a '\n'
218 while i
< _buffer
.length
and _buffer
.chars
[i
] != '\n' do i
+= 1
220 # if there is something to append
221 if i
> _buffer_pos
then
222 # Enlarge the string (if needed)
223 s
.enlarge
(s
.length
+ i
- _buffer_pos
)
225 # Copy from the buffer to the string
228 s
.add
(_buffer
.chars
[j
])
233 if i
< _buffer
.length
then
234 # so \n is in _buffer[i]
235 _buffer_pos
= i
+ 1 # skip \n
249 redef fun eof
do return _buffer_pos
>= _buffer
.length
and end_reached
252 var _buffer
: nullable FlatBuffer = null
254 # The current position in the buffer
255 var _buffer_pos
: Int = 0
258 protected fun fill_buffer
is abstract
260 # Is the last fill_buffer reach the end
261 protected fun end_reached
: Bool is abstract
263 # Allocate a `_buffer` for a given `capacity`.
264 protected fun prepare_buffer
(capacity
: Int)
266 _buffer
= new FlatBuffer.with_capacity
(capacity
)
267 _buffer_pos
= 0 # need to read
276 ##############################################################"
278 abstract class FDStream
283 redef fun close
do native_close
(fd
)
285 private fun native_close
(i
: Int): Int is extern "stream_FDStream_FDStream_native_close_1"
286 private fun native_read_char
(i
: Int): Int is extern "stream_FDStream_FDStream_native_read_char_1"
287 private fun native_read
(i
: Int, buf
: NativeString, len
: Int): Int is extern "stream_FDStream_FDStream_native_read_3"
288 private fun native_write
(i
: Int, buf
: NativeString, len
: Int): Int is extern "stream_FDStream_FDStream_native_write_3"
289 private fun native_write_char
(i
: Int, c
: Char): Int is extern "stream_FDStream_FDStream_native_write_char_2"
291 init(fd
: Int) do self.fd
= fd
297 redef var eof
: Bool = false
301 var nb
= native_read_char
(fd
)
302 if nb
== -1 then eof
= true
312 redef var is_writable
: Bool
316 var nb
= native_write
(fd
, s
.to_cstring
, s
.length
)
317 if nb
< s
.length
then is_writable
= false
337 redef interface Object
338 # returns first available stream to read or write to
339 # return null on interruption (possibly a signal)
340 protected fun poll
( streams
: Sequence[FDStream] ) : nullable FDStream
342 var in_fds
= new Array[Int]
343 var out_fds
= new Array[Int]
344 var fd_to_stream
= new HashMap[Int,FDStream]
347 if s
isa FDIStream then in_fds
.add
( fd
)
348 if s
isa FDOStream then out_fds
.add
( fd
)
353 var polled_fd
= intern_poll
( in_fds
, out_fds
)
355 if polled_fd
== null then
358 return fd_to_stream
[polled_fd
]
362 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) `{
363 int in_len, out_len, total_len;
364 struct pollfd *c_fds;
367 int first_polled_fd = -1;
370 in_len = Array_of_Int_length( in_fds );
371 out_len = Array_of_Int_length( out_fds );
372 total_len = in_len + out_len;
373 c_fds = malloc( sizeof(struct pollfd) * total_len );
376 for ( i=0; i<in_len; i ++ ) {
378 fd = Array_of_Int__index( in_fds, i );
381 c_fds[i].events = POLLIN;
385 for ( i=0; i<out_len; i ++ ) {
387 fd = Array_of_Int__index( out_fds, i );
390 c_fds[i].events = POLLOUT;
393 /* poll all fds, unlimited timeout */
394 result = poll( c_fds, total_len, -1 );
397 /* analyse results */
398 for ( i=0; i<total_len; i++ )
399 if ( c_fds[i].revents & c_fds[i].events || /* awaited event */
400 c_fds[i].revents & POLLHUP ) /* closed */
402 first_polled_fd = c_fds[i].fd;
406 return Int_as_nullable( first_polled_fd );
408 else if ( result < 0 )
409 fprintf( stderr, "Error in Stream:poll: %s\n", strerror( errno ) );
415 # Stream to a String. Mainly used for compatibility with OStream type and tests.
419 private var content
= new Array[String]
420 redef fun to_s
do return content
.to_s
421 redef fun is_writable
do return true
422 redef fun write
(str
) do content
.add
(str
.to_s
)