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 # 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 # Things that can be efficienlty writen to a OStream
103 # The point of this interface it to allow is instance to be efficenty
104 # writen into a OStream without having to allocate a big String object
106 # ready-to-save documents usually provide this interface.
108 # Write itself to a `stream`
109 # The specific logic it let to the concrete subclasses
110 fun write_to
(stream
: OStream) is abstract
112 # Like `write_to` but return a new String (may be quite large)
114 # This funtionnality is anectodical, since the point
115 # of streamable object to to be efficienlty written to a
116 # stream without having to allocate and concatenate strings
117 fun write_to_string
: String
119 var stream
= new StringOStream
127 redef fun write_to
(stream
) do stream
.write
(self)
130 # Input streams with a buffer
131 abstract class BufferedIStream
136 if _buffer_pos
>= _buffer
.length
then
139 if _buffer_pos
>= _buffer
.length
then
142 var c
= _buffer
.chars
[_buffer_pos
]
149 var s
= new FlatBuffer.with_capacity
(i
)
151 var k
= _buffer
.length
155 if eof
then return s
.to_s
159 while j
< k
and i
> 0 do
160 s
.add
(_buffer
.chars
[j
])
171 var s
= new FlatBuffer
174 var k
= _buffer
.length
176 s
.add
(_buffer
.chars
[j
])
185 redef fun append_line_to
(s
)
188 # First phase: look for a '\n'
190 while i
< _buffer
.length
and _buffer
.chars
[i
] != '\n' do i
+= 1
192 # if there is something to append
193 if i
> _buffer_pos
then
194 # Enlarge the string (if needed)
195 s
.enlarge
(s
.length
+ i
- _buffer_pos
)
197 # Copy from the buffer to the string
200 s
.add
(_buffer
.chars
[j
])
205 if i
< _buffer
.length
then
206 # so \n is in _buffer[i]
207 _buffer_pos
= i
+ 1 # skip \n
221 redef fun eof
do return _buffer_pos
>= _buffer
.length
and end_reached
224 var _buffer
: nullable FlatBuffer = null
226 # The current position in the buffer
227 var _buffer_pos
: Int = 0
230 protected fun fill_buffer
is abstract
232 # Is the last fill_buffer reach the end
233 protected fun end_reached
: Bool is abstract
235 # Allocate a `_buffer` for a given `capacity`.
236 protected fun prepare_buffer
(capacity
: Int)
238 _buffer
= new FlatBuffer.with_capacity
(capacity
)
239 _buffer_pos
= 0 # need to read
248 ##############################################################"
250 abstract class FDStream
255 redef fun close
do native_close
(fd
)
257 private fun native_close
(i
: Int): Int is extern "stream_FDStream_FDStream_native_close_1"
258 private fun native_read_char
(i
: Int): Int is extern "stream_FDStream_FDStream_native_read_char_1"
259 private fun native_read
(i
: Int, buf
: NativeString, len
: Int): Int is extern "stream_FDStream_FDStream_native_read_3"
260 private fun native_write
(i
: Int, buf
: NativeString, len
: Int): Int is extern "stream_FDStream_FDStream_native_write_3"
261 private fun native_write_char
(i
: Int, c
: Char): Int is extern "stream_FDStream_FDStream_native_write_char_2"
263 init(fd
: Int) do self.fd
= fd
269 redef var eof
: Bool = false
273 var nb
= native_read_char
(fd
)
274 if nb
== -1 then eof
= true
284 redef var is_writable
: Bool
288 var nb
= native_write
(fd
, s
.to_cstring
, s
.length
)
289 if nb
< s
.length
then is_writable
= false
309 redef interface Object
310 # returns first available stream to read or write to
311 # return null on interruption (possibly a signal)
312 protected fun poll
( streams
: Sequence[FDStream] ) : nullable FDStream
314 var in_fds
= new Array[Int]
315 var out_fds
= new Array[Int]
316 var fd_to_stream
= new HashMap[Int,FDStream]
319 if s
isa FDIStream then in_fds
.add
( fd
)
320 if s
isa FDOStream then out_fds
.add
( fd
)
325 var polled_fd
= intern_poll
( in_fds
, out_fds
)
327 if polled_fd
== null then
330 return fd_to_stream
[polled_fd
]
334 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) `{
335 int in_len, out_len, total_len;
336 struct pollfd *c_fds;
339 int first_polled_fd = -1;
342 in_len = Array_of_Int_length( in_fds );
343 out_len = Array_of_Int_length( out_fds );
344 total_len = in_len + out_len;
345 c_fds = malloc( sizeof(struct pollfd) * total_len );
348 for ( i=0; i<in_len; i ++ ) {
350 fd = Array_of_Int__index( in_fds, i );
353 c_fds[i].events = POLLIN;
357 for ( i=0; i<out_len; i ++ ) {
359 fd = Array_of_Int__index( out_fds, i );
362 c_fds[i].events = POLLOUT;
365 /* poll all fds, unlimited timeout */
366 result = poll( c_fds, total_len, -1 );
369 /* analyse results */
370 for ( i=0; i<total_len; i++ )
371 if ( c_fds[i].revents & c_fds[i].events || /* awaited event */
372 c_fds[i].revents & POLLHUP ) /* closed */
374 first_polled_fd = c_fds[i].fd;
378 return Int_as_nullable( first_polled_fd );
380 else if ( result < 0 )
381 fprintf( stderr, "Error in Stream:poll: %s\n", strerror( errno ) );
387 # Stream to a String. Mainly used for compatibility with OStream type and tests.
391 private var content
= new Array[String]
392 redef fun to_s
do return content
.to_s
393 redef fun is_writable
do return true
394 redef fun write
(str
) do content
.add
(str
)