]> gitweb.factorcode.org Git - factor.git/blob - basis/io/streams/throwing/throwing.factor
1d53d14d3ae415c721a174a43e0f0c192ba3c414
[factor.git] / basis / io / streams / throwing / throwing.factor
1 ! Copyright (C) 2010 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors destructors io kernel locals namespaces
4 sequences fry math ;
5 IN: io.streams.throwing
6
7 ERROR: stream-exhausted n stream word ;
8
9 TUPLE: throws-on-eof-stream stream ;
10 INSTANCE: throws-on-eof-stream input-stream
11
12 C: <throws-on-eof-stream> throws-on-eof-stream
13
14 M: throws-on-eof-stream stream-element-type stream>> stream-element-type ;
15
16 M: throws-on-eof-stream dispose stream>> dispose ;
17
18 M:: throws-on-eof-stream stream-read1 ( stream -- obj )
19     stream stream>> stream-read1
20     [ 1 stream \ read1 stream-exhausted ] unless* ;
21
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 stream-exhausted ] unless ;
25
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 stream-exhausted ] when-zero ;
29
30 M: throws-on-eof-stream stream-tell
31     stream>> stream-tell ;
32
33 M: throws-on-eof-stream stream-seek
34     stream>> stream-seek ;
35
36 M: throws-on-eof-stream stream-seekable?
37     stream>> stream-seekable? ;
38
39 M: throws-on-eof-stream stream-length
40     stream>> stream-length ;
41
42 M: throws-on-eof-stream stream-read-until
43     [ stream>> stream-read-until ]
44     [ '[ length _ \ read-until stream-exhausted ] unless* ] bi ;
45
46 : stream-throw-on-eof ( ..a stream quot: ( ..a stream' -- ..b ) -- ..b )
47     [ <throws-on-eof-stream> ] dip with-input-stream* ; inline
48
49 : throw-on-eof ( ..a quot: ( ..a -- ..b ) -- ..b )
50     [ input-stream get <throws-on-eof-stream> ] dip with-input-stream* ; inline