commit 7d3d6050d090bc5bb759b188deec9a787348de00 parent 1be0dad4a1c343191f769b9341170508a187977b Author: Virgil Dupras <hsoft@hardcoded.net> Date: Tue, 28 Jun 2022 17:13:26 -0400 Add str>zstr to sys/scratch and make lib/file use it Diffstat:
M | fs/lib/core.fs | | | 6 | +++--- |
M | fs/lib/file.fs | | | 6 | ++---- |
M | fs/sys/scratch.fs | | | 5 | ++++- |
3 files changed, 9 insertions(+), 8 deletions(-)
diff --git a/fs/lib/core.fs b/fs/lib/core.fs @@ -14,11 +14,11 @@ : 2dup over over ; \ Memory -: c@+ dup 1+ swap c@ ; -: c!+ tuck c! 1+ ; +: c@+ ( a -- a+1 c ) dup 1+ swap c@ ; +: c!+ ( c a -- a+1 ) tuck c! 1+ ; : Ac@+ Ac@ A+ ; : Ac!+ Ac! A+ ; -: fill ( a u b -- *A* ) rot> >r >A begin dup Ac!+ next drop ; +: fill ( a u b -- ) A>r rot> >r >A begin dup Ac!+ next drop r>A ; : allot0 ( n -- ) here over 0 fill allot ; \ Arithmetic diff --git a/fs/lib/file.fs b/fs/lib/file.fs @@ -1,8 +1,6 @@ \ File I/O -\ transform a fstring into a null-terminated string. -create _ $100 allot -: tocstr ( str -- a ) c@+ >r _ r@ move 0 _ r> + c! _ ; -: fopen ( fname -- fd ) tocstr zfopen ; +\ requires sys/scratch +: fopen ( fname -- fd ) str>zstr zfopen ; \ Autoloading \ entries in the floaded list have both a length byte and diff --git a/fs/sys/scratch.fs b/fs/sys/scratch.fs @@ -15,7 +15,10 @@ scratch( value scratch> : scratchallot ( n -- a ) scratch> over + scratch) >= if scratch( to scratch> then scratch> swap to+ scratch> ( a ) ; +: scratchallot0 ( n -- a ) dup scratchallot dup >r swap 0 fill r> ; \ push a range to the scratchpad as a string : []>str ( a u -- str ) dup 1+ scratchallot ( src u dst-1 ) >r dup r@ c!+ swap ( src dst u ) move r> ; - +\ transforms str into a null-terminated string +: str>zstr ( str -- zstr ) + c@+ dup 1+ scratchallot0 ( str+1 u a ) dup >r swap move r> ;