unix support for stream seeking

db4
Doug Coleman 2009-02-07 01:03:12 -06:00
parent 5f39a714be
commit f6f716c4e3
5 changed files with 19 additions and 2 deletions

View File

@ -46,6 +46,9 @@ M: fd cancel-operation ( fd -- )
2bi
] if ;
M: unix (stream-seek)
handle>> fd>> swap SEEK_SET lseek io-error ;
SYMBOL: +retry+ ! just try the operation again without blocking
SYMBOL: +input+
SYMBOL: +output+

View File

@ -27,6 +27,9 @@ M: buffer dispose* ptr>> free ;
: buffer-empty? ( buffer -- ? )
fill>> zero? ; inline
: buffer-seek ( n buffer -- )
(>>pos) ; inline
: buffer-consume ( n buffer -- )
[ + ] change-pos
dup [ pos>> ] [ fill>> ] bi <

View File

@ -4,7 +4,7 @@ USING: math kernel io sequences io.buffers io.timeouts generic
byte-vectors system io.encodings math.order io.backend
continuations classes byte-arrays namespaces splitting
grouping dlists assocs io.encodings.binary summary accessors
destructors combinators ;
destructors combinators unix ;
IN: io.ports
SYMBOL: default-buffer-size
@ -93,6 +93,12 @@ M: input-port stream-read-until ( seps port -- str/f sep/f )
] [ [ 2drop ] 2dip ] if
] if ;
HOOK: (stream-seek) os ( n stream -- )
M: input-port stream-seek ( n stream -- )
dup check-disposed
2dup buffer>> buffer-seek (stream-seek) ;
TUPLE: output-port < buffered-port ;
: <output-port> ( handle -- output-port )

View File

@ -50,6 +50,8 @@ M: object <decoder> f decoder boa ;
M: decoder stream-read1
dup >decoder< decode-char fix-read1 ;
M: decoder stream-seek stream>> stream-seek ;
: fix-read ( stream string -- string )
over cr>> [
over cr-

View File

@ -15,6 +15,8 @@ GENERIC: stream-write ( seq stream -- )
GENERIC: stream-flush ( stream -- )
GENERIC: stream-nl ( stream -- )
GENERIC: stream-seek ( n stream -- )
: stream-print ( str stream -- ) [ stream-write ] keep stream-nl ;
! Default streams
@ -27,6 +29,7 @@ SYMBOL: error-stream
: read ( n -- seq ) input-stream get stream-read ;
: read-until ( seps -- seq sep/f ) input-stream get stream-read-until ;
: read-partial ( n -- seq ) input-stream get stream-read-partial ;
: seek ( n -- ) input-stream get stream-seek ;
: write1 ( elt -- ) output-stream get stream-write1 ;
: write ( seq -- ) output-stream get stream-write ;
@ -82,4 +85,4 @@ PRIVATE>
: stream-copy ( in out -- )
[ [ [ write ] each-block ] with-output-stream ]
curry with-input-stream ;
curry with-input-stream ;