]> gitweb.factorcode.org Git - factor.git/blob - basis/io/streams/throwing/throwing.factor
factor: trim more using lists.
[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 math namespaces sequences ;
4 IN: io.streams.throwing
5
6 ERROR: stream-exhausted n stream word ;
7
8 TUPLE: throws-on-eof-stream stream ;
9 INSTANCE: throws-on-eof-stream input-stream
10
11 C: <throws-on-eof-stream> throws-on-eof-stream
12
13 M: throws-on-eof-stream stream-element-type stream>> stream-element-type ;
14
15 M: throws-on-eof-stream dispose stream>> dispose ;
16
17 M:: throws-on-eof-stream stream-read1 ( stream -- obj )
18     stream stream>> stream-read1
19     [ 1 stream \ read1 stream-exhausted ] unless* ;
20
21 M:: throws-on-eof-stream stream-read-unsafe ( n buf stream -- count )
22     n buf stream stream>> stream-read-unsafe
23     dup n = [ n stream \ stream-read-unsafe stream-exhausted ] unless ;
24
25 M:: throws-on-eof-stream stream-read-partial-unsafe ( n buf stream -- count )
26     n buf stream stream>> stream-read-partial-unsafe
27     [ n stream \ stream-read-partial-unsafe stream-exhausted ] when-zero ;
28
29 M: throws-on-eof-stream stream-tell
30     stream>> stream-tell ;
31
32 M: throws-on-eof-stream stream-seek
33     stream>> stream-seek ;
34
35 M: throws-on-eof-stream stream-seekable?
36     stream>> stream-seekable? ;
37
38 M: throws-on-eof-stream stream-length
39     stream>> stream-length ;
40
41 M: throws-on-eof-stream stream-read-until
42     [ stream>> stream-read-until ]
43     [ '[ length _ \ read-until stream-exhausted ] unless* ] bi ;
44
45 : stream-throw-on-eof ( ..a stream quot: ( ..a stream' -- ..b ) -- ..b )
46     [ <throws-on-eof-stream> ] dip with-input-stream* ; inline
47
48 : throw-on-eof ( ..a quot: ( ..a -- ..b ) -- ..b )
49     [ input-stream get <throws-on-eof-stream> ] dip with-input-stream* ; inline