745064a1cda657468c170ab0183cf84b6b4422ce
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 Buffer.with_capacity
(i
)
41 while i
> 0 and not eof
do
51 # Read a string until the end of the line.
60 # Read all the stream until the eof.
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 # Abstract output stream
95 fun write
(s
: String) is abstract
97 # Can the stream be used to write
98 fun is_writable
: Bool is abstract
101 # Input streams with a buffer
102 abstract class BufferedIStream
107 if _buffer_pos
>= _buffer
.length
then
110 if _buffer_pos
>= _buffer
.length
then
113 var c
= _buffer
.chars
[_buffer_pos
]
120 var s
= new Buffer.with_capacity
(i
)
122 var k
= _buffer
.length
126 if eof
then return s
.to_s
130 while j
< k
and i
> 0 do
131 s
.add
(_buffer
.chars
[j
])
145 var k
= _buffer
.length
147 s
.add
(_buffer
.chars
[j
])
156 redef fun append_line_to
(s
)
159 # First phase: look for a '\n'
161 while i
< _buffer
.length
and _buffer
.chars
[i
] != '\n' do i
+= 1
163 # if there is something to append
164 if i
> _buffer_pos
then
165 # Enlarge the string (if needed)
166 s
.enlarge
(s
.length
+ i
- _buffer_pos
)
168 # Copy from the buffer to the string
171 s
.add
(_buffer
.chars
[j
])
176 if i
< _buffer
.length
then
177 # so \n is in _buffer[i]
178 _buffer_pos
= i
+ 1 # skip \n
192 redef fun eof
do return _buffer_pos
>= _buffer
.length
and end_reached
195 var _buffer
: nullable Buffer = null
197 # The current position in the buffer
198 var _buffer_pos
: Int = 0
201 protected fun fill_buffer
is abstract
203 # Is the last fill_buffer reach the end
204 protected fun end_reached
: Bool is abstract
206 # Allocate a `_buffer` for a given `capacity`.
207 protected fun prepare_buffer
(capacity
: Int)
209 _buffer
= new Buffer.with_capacity
(capacity
)
210 _buffer_pos
= 0 # need to read
219 ##############################################################"
221 abstract class FDStream
226 redef fun close
do native_close
(fd
)
228 private fun native_close
(i
: Int): Int is extern "stream_FDStream_FDStream_native_close_1"
229 private fun native_read_char
(i
: Int): Int is extern "stream_FDStream_FDStream_native_read_char_1"
230 private fun native_read
(i
: Int, buf
: NativeString, len
: Int): Int is extern "stream_FDStream_FDStream_native_read_3"
231 private fun native_write
(i
: Int, buf
: NativeString, len
: Int): Int is extern "stream_FDStream_FDStream_native_write_3"
232 private fun native_write_char
(i
: Int, c
: Char): Int is extern "stream_FDStream_FDStream_native_write_char_2"
234 init(fd
: Int) do self.fd
= fd
240 redef var eof
: Bool = false
244 var nb
= native_read_char
(fd
)
245 if nb
== -1 then eof
= true
255 redef var is_writable
: Bool
259 var nb
= native_write
(fd
, s
.to_cstring
, s
.length
)
260 if nb
< s
.length
then is_writable
= false
280 redef interface Object
281 # returns first available stream to read or write to
282 # return null on interruption (possibly a signal)
283 protected fun poll
( streams
: Sequence[FDStream] ) : nullable FDStream
285 var in_fds
= new Array[Int]
286 var out_fds
= new Array[Int]
287 var fd_to_stream
= new HashMap[Int,FDStream]
290 if s
isa FDIStream then in_fds
.add
( fd
)
291 if s
isa FDOStream then out_fds
.add
( fd
)
296 var polled_fd
= intern_poll
( in_fds
, out_fds
)
298 if polled_fd
== null then
301 return fd_to_stream
[polled_fd
]
305 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) `{
306 int in_len, out_len, total_len;
307 struct pollfd *c_fds;
310 int first_polled_fd = -1;
313 in_len = Array_of_Int_length( in_fds );
314 out_len = Array_of_Int_length( out_fds );
315 total_len = in_len + out_len;
316 c_fds = malloc( sizeof(struct pollfd) * total_len );
319 for ( i=0; i<in_len; i ++ ) {
321 fd = Array_of_Int__index( in_fds, i );
324 c_fds[i].events = POLLIN;
328 for ( i=0; i<out_len; i ++ ) {
330 fd = Array_of_Int__index( out_fds, i );
333 c_fds[i].events = POLLOUT;
336 /* poll all fds, unlimited timeout */
337 result = poll( c_fds, total_len, -1 );
340 /* analyse results */
341 for ( i=0; i<total_len; i++ )
342 if ( c_fds[i].revents & c_fds[i].events || /* awaited event */
343 c_fds[i].revents & POLLHUP ) /* closed */
345 first_polled_fd = c_fds[i].fd;
349 return Int_as_nullable( first_polled_fd );
351 else if ( result < 0 )
352 fprintf( stderr, "Error in Stream:poll: %s\n", strerror( errno ) );
358 # Stream to a String. Mainly used for compatibility with OStream type and tests.
362 private var content
= new Array[String]
363 redef fun to_s
do return content
.to_s
364 redef fun is_writable
do return true
365 redef fun write
(str
) do content
.add
(str
)