commit f240d161756b2c4f15f53e03bcd6089589e69c97
parent 92411058f1e845eec7a4b81cd76280125bbfd3b1
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Thu, 8 Sep 2022 14:46:05 -0400
lib/io: move getc and putc into the IO namespace
Diffstat:
4 files changed, 17 insertions(+), 24 deletions(-)
diff --git a/fs/doc/io.txt b/fs/doc/io.txt
@@ -50,23 +50,16 @@ noop).
In addition to those device-specific words, all IO structures share these
convenience words:
-: write ( a n hdl -- )
+:write ( a n hdl -- )
Repeatedly call :writebuf until all requested bytes have been written.
-## Generic API
-
-From the base API of IO handlers, lib/io has these generic words:
-
-getc ( hdl -- c )
+:getc ( hdl -- c )
Read 1 byte from hdl an return it. Advance position by 1 byte. Return -1 on
EOF.
-putc ( c hdl -- )
+:putc ( c hdl -- )
Write 1 byte to hdl. Advance position by 1 byte. Aborts if unable to write.
-It also has, for each IO handler word, a helper to directly call it. For
-example, "hdl flush" simply does "hdl 8 + @ execute".
-
## StdIn and StdOut
lib/io defines StdIn and StdOut values. These point to IO structures and, at
@@ -75,5 +68,5 @@ ConsoleOut (an IO structure that writes to emit).
You can change those values to redirect I/Os for code that uses these values.
-There is also stdin, which is a shortcut for "StdIn getc" and stdout, for
-"StdOut putc".
+There is also stdin, which is a shortcut for "StdIn IO :getc" and stdout, for
+"StdOut IO :putc".
diff --git a/fs/lib/io.fs b/fs/lib/io.fs
@@ -3,8 +3,14 @@
create _buf( $100 allot
here value _)buf
-: getc ( hdl -- c ) 1 swap IO :readbuf if c@ else -1 ( EOF ) then ;
-: putc ( c hdl -- ) swap _buf( c! _buf( 1 rot IO :writebuf ;
+struct+[ IO
+ : :write ( a n self -- ) >r begin ( a n ) ?dup while
+ 2dup r@ :writebuf ?dup not if abort" error during write" then
+ ( a n written-n ) tuck - ( a written-n new-n ) rot> + swap repeat ( a )
+ drop rdrop ;
+: :getc ( hdl -- c ) 1 swap IO :readbuf if c@ else -1 ( EOF ) then ;
+: :putc ( c hdl -- ) swap _buf( c! _buf( 1 rot IO :writebuf ;
+]struct
\ StdIO
: _err abort" Invalid I/O" ;
@@ -12,11 +18,11 @@ here value _)buf
drop in< dup 0< if drop 0 else _buf( c! _buf( 1 then ;
create ConsoleIn ' _readbuf , ' _err , ' _err ,
ConsoleIn value StdIn
-: stdin StdIn getc ;
+: stdin StdIn IO :getc ;
: _writebuf ( a n hdl -- written-n ) 2drop c@ emit 1 ;
create ConsoleOut ' _err , ' _writebuf , ' drop ,
ConsoleOut value StdOut
-: stdout StdOut putc ;
+: stdout StdOut IO :putc ;
: stdio$ ConsoleIn to StdIn ConsoleOut to StdOut ;
\ read stdin for a maximum of STR_MAXSZ-1 characters until LF is encountered,
@@ -28,9 +34,3 @@ ConsoleOut value StdOut
stdin dup LF = not while 8b to!+ V1 repeat drop
r> _buf( - 1- ( len ) _buf( c! _buf( ( str ) ;
-struct+[ IO
- : :write ( a n self -- ) >r begin ( a n ) ?dup while
- 2dup r@ :writebuf ?dup not if abort" error during write" then
- ( a n written-n ) tuck - ( a written-n new-n ) rot> + swap repeat ( a )
- drop rdrop ;
-]struct
diff --git a/fs/sys/file.fs b/fs/sys/file.fs
@@ -71,7 +71,7 @@ struct[ Path
dup id _curpath CELLSZ + ! fs _curpath ! ;
: :fload ( self -- ) dup fs swap id fload ;
: :spit ( self -- )
- :open begin ( file ) dup getc dup 0>= while emit repeat drop ;
+ :open begin ( file ) dup File :getc dup 0>= while emit repeat drop ;
\ TODO implement truncating for when dstfile is larger than self.
: _copyfile ( dst src -- ) \ arguments are opened File
diff --git a/fs/xcomp/pc/build.fs b/fs/xcomp/pc/build.fs
@@ -32,7 +32,7 @@ create _buf SECSZ allot0
: spitfile ( fpath drv -- ) >r
curpath :find# Path :open begin ( fc )
- begin dup getc keepc? until
+ begin dup IO :getc keepc? until
dup 0>= while r@ rsvdwrite repeat ( fc c )
drop File :close rdrop ;