]> gitweb.factorcode.org Git - factor.git/commitdiff
more stream-seekable? and stream-length methods
authorJoe Groff <arcata@gmail.com>
Sat, 15 Oct 2011 04:35:25 +0000 (21:35 -0700)
committerJoe Groff <arcata@gmail.com>
Tue, 18 Oct 2011 04:23:09 +0000 (21:23 -0700)
- limited-streams and throw-on-eof-streams
- string-readers and byte-readers

basis/io/streams/limited/limited-tests.factor
basis/io/streams/limited/limited.factor
basis/io/streams/string/string.factor
basis/io/streams/throwing/throwing-tests.factor
basis/io/streams/throwing/throwing.factor
core/io/streams/byte-array/byte-array.factor

index 916af4c29ae69944eb83600e7db0dce0085e497d..e5ef78a61ecef3160d5feed784f11bb57217c2ef 100644 (file)
@@ -122,3 +122,7 @@ IN: io.streams.limited.tests
         [ contents ] with-unlimited-input
     ] with-input-stream
 ] unit-test
+
+{ 4 } [ B{ 0 1 2 3 4 5 } binary <byte-reader> 4 <limited-stream> stream-length ] unit-test
+{ 6 } [ B{ 0 1 2 3 4 5 } binary <byte-reader> 8 <limited-stream> stream-length ] unit-test
+
index 175c53663b6a2ceb03ce4b8c2702f29331055bca..b387d640c4d50fca32ef5d9a105e3d055f8ae402 100644 (file)
@@ -3,7 +3,7 @@
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors byte-vectors combinators destructors fry io
 io.encodings io.files io.files.info kernel locals math
-namespaces sequences ;
+namespaces sequences math.order ;
 IN: io.streams.limited
 
 TUPLE: limited-stream stream count limit current start stop ;
@@ -22,7 +22,8 @@ TUPLE: limited-stream stream count limit current start stop ;
 GENERIC# limit-stream 1 ( stream limit -- stream' )
 
 M: decoder limit-stream ( stream limit -- stream' )
-    [ clone ] dip '[ _ limit-stream ] change-stream ;
+    '[ stream>> _ limit-stream ] [ code>> ] [ cr>> ] tri
+    decoder boa ; inline
 
 M: object limit-stream ( stream limit -- stream' )
     <limited-stream> ;
@@ -78,7 +79,8 @@ ERROR: limit-exceeded n stream ;
     pick 0 <= [ 3drop f ] [ [ stream>> ] dip call ] if ; inline
 
 :: maybe-read-unsafe ( n buf limited-stream quot: ( n buf stream -- count ) -- count )
-    n limited-stream [| n' stream | n' buf stream quot call ] maybe-read ; inline
+    n limited-stream adjust-limited-read :> ( n' lstream' )
+    n' 0 <= [ 0 ] [ n' buf lstream' stream>> quot call ] if ; inline
 
 PRIVATE>
 
@@ -130,6 +132,13 @@ M: limited-stream stream-seek
     [ stream>> stream-seek ]
     [ limited-stream-seek ] 3bi ;
 
+M: limited-stream stream-seekable?
+    stream>> stream-seekable? ; inline
+
+M: limited-stream stream-length
+    dup stream>> stream-length
+    [ swap limit>> min ] [ drop f ] if* ; inline
+
 M: limited-stream dispose stream>> dispose ;
 
 M: limited-stream stream-element-type
@@ -138,7 +147,7 @@ M: limited-stream stream-element-type
 GENERIC: unlimit-stream ( stream -- stream' )
 
 M: decoder unlimit-stream ( stream -- stream' )
-    [ stream>> ] change-stream ;
+    [ stream>> stream>> ] [ code>> ] [ cr>> ] tri decoder boa ;
 
 M: limited-stream unlimit-stream ( stream -- stream' ) stream>> ;
 
index 442a799ff44775be2da0c3077cee218962a4db67..a1fad6ba681a223773eff28dc36590276c3a6421 100644 (file)
@@ -15,6 +15,8 @@ M: string-reader stream-read1 sequence-read1 ;
 M: string-reader stream-read-until sequence-read-until ;
 M: string-reader stream-tell i>> ;
 M: string-reader stream-seek (stream-seek) ;
+M: string-reader stream-seekable? drop t ; inline
+M: string-reader stream-length underlying>> length ;
 M: string-reader dispose drop ;
 
 <PRIVATE
index f1567be84227414c9f13d3d856a169cb0aa944c5..ba0eac680d72cafb043ca695cebd16d0ec617f2f 100644 (file)
@@ -1,7 +1,8 @@
 ! Copyright (C) 2010 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: io io.encodings.utf8 io.files io.streams.string
-io.streams.throwing kernel tools.test destructors ;
+USING: destructors io io.encodings.binary io.encodings.utf8
+io.files io.streams.byte-array io.streams.string
+io.streams.throwing kernel namespaces tools.test ;
 IN: io.streams.throwing.tests
 
 [ "asdf" ]
@@ -39,14 +40,16 @@ IN: io.streams.throwing.tests
     ] with-file-reader
 ] unit-test
 
-[ "asdf" "asdf" ] [
-    "asdf" [
+[ B{ 0 1 2 3 } B{ 0 1 2 3 } ] [
+    B{ 0 1 2 3 } binary [
         [ 4 read 0 seek-absolute seek-input 4 read ] throw-on-eof
-    ] with-string-reader
+    ] with-byte-reader
 ] unit-test
 
 [
-    "asdf" [ [ 1 seek-absolute seek-input 4 read drop ] throw-on-eof ] with-string-reader
+    B{ 0 1 2 3 } binary [
+        [ 1 seek-absolute seek-input 4 read drop ] throw-on-eof
+    ] with-byte-reader
 ] [ stream-exhausted? ] must-fail-with
 
 [ "asd" CHAR: f ] [
@@ -57,6 +60,14 @@ IN: io.streams.throwing.tests
     "asdf" [ [ "g" read-until ] throw-on-eof ] with-string-reader
 ] [ stream-exhausted? ] must-fail-with
 
-[ 1 ] [
-    "asdf" [ [ 1 seek-absolute seek-input tell-input ] throw-on-eof ] with-string-reader
+{ 1 } [
+    B{ 0 1 2 3 } binary [
+        [ 1 seek-absolute seek-input tell-input ] throw-on-eof
+    ] with-byte-reader
+] unit-test
+
+{ t 4 } [
+    B{ 0 1 2 3 } binary [ [
+        input-stream get [ stream-seekable? ] [ stream-length ] bi
+    ] throw-on-eof ] with-byte-reader
 ] unit-test
index 4c8e5b74c7612b16e23a3ab3db88c58e5e1123a5..bc80bb08d7121b7b5924d3eeccfe801ff55f5674 100644 (file)
@@ -10,8 +10,6 @@ TUPLE: throws-on-eof-stream stream ;
 
 C: <throws-on-eof-stream> throws-on-eof-stream
 
-<PRIVATE
-
 M: throws-on-eof-stream stream-element-type stream>> stream-element-type ;
 
 M: throws-on-eof-stream dispose stream>> dispose ;
@@ -34,12 +32,16 @@ M: throws-on-eof-stream stream-tell
 M: throws-on-eof-stream stream-seek
     stream>> stream-seek ;
 
+M: throws-on-eof-stream stream-seekable?
+    stream>> stream-seekable? ;
+
+M: throws-on-eof-stream stream-length
+    stream>> stream-length ;
+
 M: throws-on-eof-stream stream-read-until
     [ stream>> stream-read-until ]
     [ '[ length _ \ read-until stream-exhausted ] unless* ] bi ;
 
-PRIVATE>
-
 : stream-throw-on-eof ( ..a stream quot: ( ..a stream' -- ..b ) -- ..b )
     [ <throws-on-eof-stream> ] dip with-input-stream* ; inline
 
index 3c880a8e38de9fe1abb77bd770693c13298cd79a..b8668e78b959a8c62732c8a5107a22a0775e4dd3 100644 (file)
@@ -25,6 +25,8 @@ M: byte-reader dispose drop ;
 
 M: byte-reader stream-tell i>> ;
 M: byte-reader stream-seek (stream-seek) ;
+M: byte-reader stream-seekable? drop t ; inline
+M: byte-reader stream-length underlying>> length ; inline
 
 : <byte-reader> ( byte-array encoding -- stream )
     [ B{ } like 0 byte-reader boa ] dip <decoder> ;