1 ! Copyright (C) 2010 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors destructors io kernel locals namespaces
5 IN: io.streams.throwing
7 ERROR: stream-exhausted n stream word ;
9 TUPLE: throws-on-eof-stream stream ;
10 INSTANCE: throws-on-eof-stream input-stream
12 C: <throws-on-eof-stream> throws-on-eof-stream
14 M: throws-on-eof-stream stream-element-type stream>> stream-element-type ;
16 M: throws-on-eof-stream dispose stream>> dispose ;
18 M:: throws-on-eof-stream stream-read1 ( stream -- obj )
19 stream stream>> stream-read1
20 [ 1 stream \ read1 throw-stream-exhausted ] unless* ;
22 M:: throws-on-eof-stream stream-read-unsafe ( n buf stream -- count )
23 n buf stream stream>> stream-read-unsafe
24 dup n = [ n stream \ stream-read-unsafe throw-stream-exhausted ] unless ;
26 M:: throws-on-eof-stream stream-read-partial-unsafe ( n buf stream -- count )
27 n buf stream stream>> stream-read-partial-unsafe
28 [ n stream \ stream-read-partial-unsafe throw-stream-exhausted ] when-zero ;
30 M: throws-on-eof-stream stream-tell
31 stream>> stream-tell ;
33 M: throws-on-eof-stream stream-seek
34 stream>> stream-seek ;
36 M: throws-on-eof-stream stream-seekable?
37 stream>> stream-seekable? ;
39 M: throws-on-eof-stream stream-length
40 stream>> stream-length ;
42 M: throws-on-eof-stream stream-read-until
43 [ stream>> stream-read-until ]
44 [ '[ length _ \ read-until throw-stream-exhausted ] unless* ] bi ;
46 : stream-throw-on-eof ( ..a stream quot: ( ..a stream' -- ..b ) -- ..b )
47 [ <throws-on-eof-stream> ] dip with-input-stream* ; inline
49 : throw-on-eof ( ..a quot: ( ..a -- ..b ) -- ..b )
50 [ input-stream get <throws-on-eof-stream> ] dip with-input-stream* ; inline