stream: fix missing documentation warnings
[nit.git] / lib / standard / stream.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
9 # another product.
10
11 # Input and output streams of characters
12 module stream
13
14 intrude import ropes
15
16 in "C" `{
17 #include <unistd.h>
18 #include <poll.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <signal.h>
22 `}
23
24 # Abstract stream class
25 interface IOS
26 # close the stream
27 fun close is abstract
28 end
29
30 # Abstract input streams
31 interface IStream
32 super IOS
33 # Read a character. Return its ASCII value, -1 on EOF or timeout
34 fun read_char: Int is abstract
35
36 # Read at most i bytes
37 fun read(i: Int): String
38 do
39 var s = new FlatBuffer.with_capacity(i)
40 while i > 0 and not eof do
41 var c = read_char
42 if c >= 0 then
43 s.add(c.ascii)
44 i -= 1
45 end
46 end
47 return s.to_s
48 end
49
50 # Read a string until the end of the line.
51 fun read_line: String
52 do
53 assert not eof
54 var s = new FlatBuffer
55 append_line_to(s)
56 return s.to_s
57 end
58
59 # Read all the stream until the eof.
60 fun read_all: String
61 do
62 var s = new FlatBuffer
63 while not eof do
64 var c = read_char
65 if c >= 0 then s.add(c.ascii)
66 end
67 return s.to_s
68 end
69
70 # Read a string until the end of the line and append it to `s`.
71 fun append_line_to(s: Buffer)
72 do
73 loop
74 var x = read_char
75 if x == -1 then
76 if eof then return
77 else
78 var c = x.ascii
79 s.chars.push(c)
80 if c == '\n' then return
81 end
82 end
83 end
84
85 # Is there something to read.
86 # This function returns 'false' if there is something to read.
87 fun eof: Bool is abstract
88 end
89
90 # IStream capable of declaring if readable without blocking
91 interface PollableIStream
92 super IStream
93
94 # Is there something to read? (without blocking)
95 fun poll_in: Bool is abstract
96
97 end
98
99 # Abstract output stream
100 interface OStream
101 super IOS
102 # write a string
103 fun write(s: Text) is abstract
104
105 # Can the stream be used to write
106 fun is_writable: Bool is abstract
107 end
108
109 # Things that can be efficienlty writen to a OStream
110 #
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
113 #
114 # ready-to-save documents usually provide this interface.
115 interface Streamable
116 # Write itself to a `stream`
117 # The specific logic it let to the concrete subclasses
118 fun write_to(stream: OStream) is abstract
119
120 # Like `write_to` but return a new String (may be quite large)
121 #
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
126 do
127 var stream = new StringOStream
128 write_to(stream)
129 return stream.to_s
130 end
131 end
132
133 redef class Text
134 super Streamable
135 redef fun write_to(stream) do stream.write(self)
136 end
137
138 # Input streams with a buffer
139 abstract class BufferedIStream
140 super IStream
141 redef fun read_char
142 do
143 assert not eof
144 if _buffer_pos >= _buffer.length then
145 fill_buffer
146 end
147 if _buffer_pos >= _buffer.length then
148 return -1
149 end
150 var c = _buffer.chars[_buffer_pos]
151 _buffer_pos += 1
152 return c.ascii
153 end
154
155 redef fun read(i)
156 do
157 if _buffer.length == _buffer_pos then
158 if not eof then
159 fill_buffer
160 return read(i)
161 end
162 return ""
163 end
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
168 end
169 _buffer_pos += i
170 return _buffer.substring(_buffer_pos - i, i).to_s
171 end
172
173 redef fun read_all
174 do
175 var s = new FlatBuffer
176 while not eof do
177 var j = _buffer_pos
178 var k = _buffer.length
179 while j < k do
180 s.add(_buffer[j])
181 j += 1
182 end
183 _buffer_pos = j
184 fill_buffer
185 end
186 return s.to_s
187 end
188
189 redef fun append_line_to(s)
190 do
191 loop
192 # First phase: look for a '\n'
193 var i = _buffer_pos
194 while i < _buffer.length and _buffer.chars[i] != '\n' do i += 1
195
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)
200
201 # Copy from the buffer to the string
202 var j = _buffer_pos
203 while j < i do
204 s.add(_buffer.chars[j])
205 j += 1
206 end
207 end
208
209 if i < _buffer.length then
210 # so \n is in _buffer[i]
211 _buffer_pos = i + 1 # skip \n
212 return
213 else
214 # so \n is not found
215 _buffer_pos = i
216 if end_reached then
217 return
218 else
219 fill_buffer
220 end
221 end
222 end
223 end
224
225 redef fun eof do return _buffer_pos >= _buffer.length and end_reached
226
227 # The buffer
228 private var buffer: nullable FlatBuffer = null
229
230 # The current position in the buffer
231 private var buffer_pos: Int = 0
232
233 # Fill the buffer
234 protected fun fill_buffer is abstract
235
236 # Is the last fill_buffer reach the end
237 protected fun end_reached: Bool is abstract
238
239 # Allocate a `_buffer` for a given `capacity`.
240 protected fun prepare_buffer(capacity: Int)
241 do
242 _buffer = new FlatBuffer.with_capacity(capacity)
243 _buffer_pos = 0 # need to read
244 end
245 end
246
247 # An Input/Output Stream
248 interface IOStream
249 super IStream
250 super OStream
251 end
252
253 ##############################################################"
254
255 # A File Descriptor Stream.
256 abstract class FDStream
257 super IOS
258 # File description
259 var fd: Int
260
261 redef fun close do native_close(fd)
262
263 private fun native_close(i: Int): Int is extern "stream_FDStream_FDStream_native_close_1"
264 private fun native_read_char(i: Int): Int is extern "stream_FDStream_FDStream_native_read_char_1"
265 private fun native_read(i: Int, buf: NativeString, len: Int): Int is extern "stream_FDStream_FDStream_native_read_3"
266 private fun native_write(i: Int, buf: NativeString, len: Int): Int is extern "stream_FDStream_FDStream_native_write_3"
267 private fun native_write_char(i: Int, c: Char): Int is extern "stream_FDStream_FDStream_native_write_char_2"
268 end
269
270 # An Input File Descriptor Stream.
271 class FDIStream
272 super FDStream
273 super IStream
274 redef var eof: Bool = false
275
276 redef fun read_char
277 do
278 var nb = native_read_char(fd)
279 if nb == -1 then eof = true
280 return nb
281 end
282 end
283
284 # An Output File Descriptor Stream.
285 class FDOStream
286 super FDStream
287 super OStream
288 redef var is_writable = true
289
290 redef fun write(s)
291 do
292 var nb = native_write(fd, s.to_cstring, s.length)
293 if nb < s.length then is_writable = false
294 end
295 end
296
297 # An Input/Output File Descriptor Stream.
298 class FDIOStream
299 super FDIStream
300 super FDOStream
301 super IOStream
302 end
303
304 redef interface Object
305 # returns first available stream to read or write to
306 # return null on interruption (possibly a signal)
307 protected fun poll( streams : Sequence[FDStream] ) : nullable FDStream
308 do
309 var in_fds = new Array[Int]
310 var out_fds = new Array[Int]
311 var fd_to_stream = new HashMap[Int,FDStream]
312 for s in streams do
313 var fd = s.fd
314 if s isa FDIStream then in_fds.add( fd )
315 if s isa FDOStream then out_fds.add( fd )
316
317 fd_to_stream[fd] = s
318 end
319
320 var polled_fd = intern_poll( in_fds, out_fds )
321
322 if polled_fd == null then
323 return null
324 else
325 return fd_to_stream[polled_fd]
326 end
327 end
328
329 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) `{
330 int in_len, out_len, total_len;
331 struct pollfd *c_fds;
332 sigset_t sigmask;
333 int i;
334 int first_polled_fd = -1;
335 int result;
336
337 in_len = Array_of_Int_length( in_fds );
338 out_len = Array_of_Int_length( out_fds );
339 total_len = in_len + out_len;
340 c_fds = malloc( sizeof(struct pollfd) * total_len );
341
342 /* input streams */
343 for ( i=0; i<in_len; i ++ ) {
344 int fd;
345 fd = Array_of_Int__index( in_fds, i );
346
347 c_fds[i].fd = fd;
348 c_fds[i].events = POLLIN;
349 }
350
351 /* output streams */
352 for ( i=0; i<out_len; i ++ ) {
353 int fd;
354 fd = Array_of_Int__index( out_fds, i );
355
356 c_fds[i].fd = fd;
357 c_fds[i].events = POLLOUT;
358 }
359
360 /* poll all fds, unlimited timeout */
361 result = poll( c_fds, total_len, -1 );
362
363 if ( result > 0 ) {
364 /* analyse results */
365 for ( i=0; i<total_len; i++ )
366 if ( c_fds[i].revents & c_fds[i].events || /* awaited event */
367 c_fds[i].revents & POLLHUP ) /* closed */
368 {
369 first_polled_fd = c_fds[i].fd;
370 break;
371 }
372
373 return Int_as_nullable( first_polled_fd );
374 }
375 else if ( result < 0 )
376 fprintf( stderr, "Error in Stream:poll: %s\n", strerror( errno ) );
377
378 return null_Int();
379 `}
380 end
381
382 # Stream to a String.
383 #
384 # Mainly used for compatibility with OStream type and tests.
385 class StringOStream
386 super OStream
387
388 private var content = new Array[String]
389 redef fun to_s do return content.to_s
390 redef fun is_writable do return not closed
391 redef fun write(str)
392 do
393 assert not closed
394 content.add(str.to_s)
395 end
396
397 # Is the stream closed?
398 protected var closed = false
399
400 redef fun close do closed = true
401 end
402
403 # Stream from a String.
404 #
405 # Mainly used for compatibility with IStream type and tests.
406 class StringIStream
407 super IStream
408
409 # The string to read from.
410 var source: String
411
412 # The current position in the string.
413 private var cursor: Int = 0
414
415 redef fun read_char do
416 if cursor < source.length then
417 var c = source[cursor].ascii
418
419 cursor += 1
420 return c
421 else
422 return -1
423 end
424 end
425
426 redef fun close do
427 source = ""
428 end
429
430 redef fun eof do return cursor >= source.length
431 end