commit dba33857e210995ee8fd66820aa3690154eefa78
parent 13e3306d211b6346576a36bf72d93e37acea46ba
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 28 Nov 2022 20:38:39 -0500
sys/io: move :readall from File to IO
it's useful on non-file IOs too!
Diffstat:
4 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/fs/doc/file.txt b/fs/doc/file.txt
@@ -81,9 +81,6 @@ These words have the following meaning:
:seek ( pos hdl -- )
Place the handle at offset "pos" (in bytes).
-:readall ( a hdl -- )
- Read the whole file, from current position, at memory address "a".
-
## FSInfo API
The structure returned by ":info" above goes as follows:
diff --git a/fs/doc/io.txt b/fs/doc/io.txt
@@ -57,6 +57,9 @@ convenience words:
Repeatedly call :readbuf until 'n' bytes have been read (into buffer at 'a',
which of course must be big enough). Aborts if we can't read all bytes.
+:readall ( a hdl -- )
+ Read until EOF into memory address "a".
+
:getc ( hdl -- c )
Read 1 byte from hdl an return it. Advance position by 1 byte. Return -1 on
EOF. If putback is nonzero, return this value instead, and reset putback to 0.
diff --git a/fs/sys/file.fs b/fs/sys/file.fs
@@ -2,10 +2,6 @@
0 S" sys" bootfs Filesystem :child S" io.fs" bootfs Filesystem :child
bootfs swap fload
-struct+[ File
- : :readall ( a hdl -- ) dup size swap :read ;
-]struct
-
struct[ FSInfo
sfield name
sfield size
diff --git a/fs/sys/io.fs b/fs/sys/io.fs
@@ -10,6 +10,10 @@ struct+[ IO
>r swap r@ move r> ( a n read-n )
tuck - ( a read-n new-n ) rot> + swap repeat ( a )
drop rdrop ;
+ : :readall ( a hdl -- ) >r begin ( a )
+ dup -1 r@ :readbuf ?dup while ( a dst src read-n )
+ >r swap r@ move r> ( a read-n ) + repeat ( a a )
+ 2drop rdrop ;
: :putback ( c hdl ) to putback ;
create _buf 1 allot
: :putc ( c hdl -- ) swap _buf c! _buf 1 rot :writebuf not if _ioerr then ;