commit ba9d67b74f468e0c9a66fb00670e13ae9eb5e8ef
parent 0cc6db52d64bf6f3a9fdc7cbd3db3beb302d8668
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 10 Oct 2022 09:17:29 -0400
sys/file: add p" f" and File :readall
Diffstat:
3 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/fs/doc/file.txt b/fs/doc/file.txt
@@ -81,6 +81,9 @@ 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 retured by ":info" above goes as follow:
@@ -174,3 +177,10 @@ This would mean that the File API would be broken on a subset of filesystems.
Does it mean that Dusk couldn't read them? No, only that file/dir enumeration
would have to go through FS-specific tools. I think that this inconvenience is
worth it if it means an overall simpler API.
+
+## Path literals
+
+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.
diff --git a/fs/sys/file.fs b/fs/sys/file.fs
@@ -2,6 +2,10 @@
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
@@ -104,6 +108,8 @@ struct[ Path
]struct
Path _curpath structbind Path curpath
+: p" [compile] S" curpath :find# ; immediate
+: f" [compile] p" Path :open ; immediate
: f<< word curpath :find# Path :fload ;
: ?f<< word curpath :find# dup Path :floaded? if drop else Path :fload then ;
diff --git a/fs/tests/sys/file.fs b/fs/tests/sys/file.fs
@@ -5,11 +5,11 @@
testbegin
\ Tests for sys/file
\ test chdir and relative find
-S" lib" curpath :find# Path :chdir
+p" lib" Path :chdir
S" str.fs" curpath :find # \ found!
S" /lib/str.fs" curpath :find # \ found!
\ can we come back one level?
-S" .." curpath :find# Path :chdir
+p" .." Path :chdir
S" lib/str.fs" curpath :find # \ found!
S" /lib/str.fs" curpath :find # \ found!
\ test paths with drive letters
@@ -20,12 +20,12 @@ S" b:lib/str.fs" curpath :find not #
S" lib/nope.fs" curpath :find not # \ not found!
S" /nope.fs" curpath :find not # \ not found!
\ test :info
-S" /lib/str.fs" curpath :find Path :info value myinfo
+p" /lib/str.fs" Path :info value myinfo
\ TODO: we can't test for name because it breaks on case-insensitive platforms.
\ try to find elegant ways to work around this.
myinfo FSInfo size # \ let's just test for nonzero size...
myinfo FSInfo dir? not #
-S" /lib" curpath :find Path :info value myinfo
+p" /lib" Path :info value myinfo
myinfo FSInfo dir? #
\ For the writeable part of the tests, we use a RAMDrive with a FAT12 mounted
\ in it so that write tests can be made without affecting the host.