commit be04c6454c44ff94370a0f5b4e7d0f24b91780dd
parent d25e210b0136a6cfe6588223ae8d522695aa9d54
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Sun, 16 Oct 2022 08:08:20 -0400
sys/file: Make MemIO into MakeFile
That buffer will be more useful with seek capabilities.
Diffstat:
6 files changed, 44 insertions(+), 50 deletions(-)
diff --git a/fs/cc/lib.fs b/fs/cc/lib.fs
@@ -9,7 +9,7 @@
(S a c n -- )
: memset swap fill ;
-$100 MemIO :new const _sio
+$100 MemFile :new const _sfile
(S hdl -- c )
: fgetc IO :getc ;
@@ -75,8 +75,8 @@ $100 MemIO :new const _sio
(S ... fmt -- str )
: sprintf
- 1 to _sio MemIO ptr _sio fprintf
- _sio MemIO ptr 1- ( len ) _sio MemIO :buf( tuck c! ;
+ 1 to _sfile MemFile pos _sfile fprintf
+ _sfile MemFile pos 1- ( len ) _sfile MemFile :buf( tuck c! ;
(S c -- f )
: isdigit 0-9? ;
@@ -128,7 +128,8 @@ $100 MemIO :new const _sio
: scanf StdOut fscanf ;
(S fmt str -- f )
: sscanf
- _sio MemIO :rewind c@+ _sio IO :write _sio MemIO :rewind _sio fscanf ;
+ 0 _sfile MemFile :seek c@+ _sfile IO :write
+ 0 _sfile MemFile :seek _sfile fscanf ;
:c struct File {
unsigned int putback;
diff --git a/fs/doc/file.txt b/fs/doc/file.txt
@@ -184,3 +184,20 @@ There are 2 words for Path/File shortcuts: p" and f".
p" foo" is a shortcut for S" foo" curpath :find# and f" foo" is a shortcut for
p" foo" Path :open.
+
+## MemFile
+
+MemFile is a structure that extends File and provides read/write/seek
+capabilities to a memory buffer. It extends File with those words:
+
+:new ( sz -- hdl )
+ Allocate a new buffer of size "sz" and return it.
+
+:buf( ( hdl -- a )
+ Address of the buffer.
+
+:)buf ( hdl -- a )
+ Address following the last byte of the buffer.
+
+:ptr ( hdl -- a )
+ Address corresponding to current position.
diff --git a/fs/doc/io.txt b/fs/doc/io.txt
@@ -76,29 +76,6 @@ convenience words:
:spit ( dst hdl -- )
Until EOF is reached, read "hdl" and write its contents to other IO "dst".
-## MemIO
-
-MemIO is a structure that extends IO and provides read/write capabilities to a
-memory buffer. It extends IO with those words:
-
-:new ( sz -- hdl )
- Allocate a new buffer of size "sz" and return it.
-
-bufsz ( hdl -- sz )
- Size of the buffer.
-
-ptr ( hdl -- idx )
- Index of current position, relative to :buf(.
-
-:buf( ( hdl -- a )
- Address of the buffer.
-
-:)buf ( hdl -- a )
- Address following the last byte of the buffer.
-
-:ptr ( hdl -- a )
- Address corresponding to current position.
-
## SumIO
The SumIO struct allows you to "spit" another IO into it and extract a result.
diff --git a/fs/sys/file.fs b/fs/sys/file.fs
@@ -119,3 +119,21 @@ Path _curpath structbind Path curpath
StdIn >r curpath :find# Path :open dup >r ( w hdl )
to StdIn execute
r> File :close r> to StdIn ;
+
+extends File struct[ MemFile
+ SZ &+ :buf(
+ : :)buf dup :buf( swap size + ;
+ : :ptr dup :buf( swap pos + ;
+ : _maxn ( n hdl -- real-n ) >r V1 pos + V1 size min r> pos - ;
+ : _readbuf ( n hdl -- a? read-n )
+ >r V1 _maxn ( read-n ) dup if V1 :ptr swap dup V1 to+ pos then rdrop ;
+ : _writebuf ( a n hdl -- written-n )
+ >r V1 _maxn ( a write-n ) dup if ( a write-n )
+ tuck V1 :ptr swap ( write-n a dst n ) move ( write-n ) dup V1 to+ pos
+ else nip then rdrop ;
+ : _seek ( pos hdl -- ) to pos ;
+ : :new ( sz -- hdl ) here swap ( hdl sz )
+ 0 ( putback ) , ['] _readbuf , ['] _writebuf , ['] drop , ['] drop ,
+ 0 ( pos ) , dup ( size ) , ['] _seek , ( sz ) allot ;
+]struct
+
diff --git a/fs/sys/io.fs b/fs/sys/io.fs
@@ -29,25 +29,6 @@ struct+[ IO
-1 V1 :readbuf ?dup while ( a n ) V2 :write repeat rdrop rdrop ;
]struct
-extends IO struct[ MemIO
- sfield bufsz \ size of buffer
- sfield ptr \ starts at 0, stops at bufsz
- SZ &+ :buf(
- : :)buf dup :buf( swap bufsz + ;
- : :ptr dup :buf( swap ptr + ;
- : :rewind 0 swap to ptr ;
- : _maxn ( n hdl -- real-n ) >r V1 ptr + V1 bufsz min r> ptr - ;
- : _readbuf ( n hdl -- a? read-n )
- >r V1 _maxn ( read-n ) dup if V1 :ptr swap dup V1 to+ ptr then rdrop ;
- : _writebuf ( a n hdl -- written-n )
- >r V1 _maxn ( a write-n ) dup if ( a write-n )
- tuck V1 :ptr swap ( write-n a dst n ) move ( write-n ) dup V1 to+ ptr
- else nip then rdrop ;
- : :new ( sz -- hdl ) here swap ( hdl sz )
- 0 ( putback ) , ['] _readbuf , ['] _writebuf , ['] drop , ['] drop ,
- dup ( bufsz ) , 0 ( ptr ) , ( sz ) allot ;
-]struct
-
: stdin StdIn IO :getc ;
: stdout StdOut IO :putc ;
: stdio$ ConsoleIn to StdIn ConsoleOut to StdOut ;
diff --git a/fs/tests/cc/lib.fs b/fs/tests/cc/lib.fs
@@ -21,12 +21,12 @@ S" bar" 42 43 44 45 capture foo S" foo 45 2c 002b 0000002a bar" #s=
:c void foo() { puts("Hello World!"); }
capture foo S" Hello World!" #s=
-$20 MemIO :new const memio
+$20 MemFile :new const memfile
S" What about this?" const s
-:c void foo() { fputs("What about this?", memio()); }
+:c void foo() { fputs("What about this?", memfile()); }
foo
-memio MemIO ptr s strlen #eq
-memio MemIO :buf( s c@+ []= #
+memfile MemFile pos s strlen #eq
+memfile MemFile :buf( s c@+ []= #
:c int foo(char *s) {
if (sscanf("foo %d bar", s)) {