2018-11-22 06:22:53 +01:00
|
|
|
# File I/O
|
|
|
|
|
2020-09-16 16:37:30 +02:00
|
|
|
The file device (device type 4) adds support for generic file I/O
|
|
|
|
that is similar to the C standard library on Unix. On a Unix host
|
|
|
|
these are very thin wrappers over fopen(), fclose(), etc. For non
|
|
|
|
Unix hosts, implementing these may take much more work, and it may
|
|
|
|
be preferable to design a file I/O device that models the host
|
|
|
|
expectations.
|
|
|
|
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
2024-09-09 20:18:25 +02:00
|
|
|
:file:operation (:-n)
|
2023-12-09 19:49:47 +01:00
|
|
|
DEVICE:FILES io:scan-for
|
|
|
|
dup n:negative? [ drop 'Error:_files_device_not_found s:put nl ] if;
|
2021-04-19 19:38:47 +02:00
|
|
|
io:invoke ;
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
First up, constants for the file modes.
|
|
|
|
|
2020-02-03 16:46:52 +01:00
|
|
|
| # | Used For |
|
2021-04-12 16:43:27 +02:00
|
|
|
| -- | ---------------------------- |
|
2020-02-03 16:46:52 +01:00
|
|
|
| R | Mode for READING |
|
|
|
|
| W | Mode for WRITING |
|
|
|
|
| A | Mode for APPENDING |
|
|
|
|
| R+ | Mode for READING and WRITING |
|
2018-11-22 06:22:53 +01:00
|
|
|
|
|
|
|
~~~
|
2024-09-09 17:49:02 +02:00
|
|
|
#0 'file:R const (:-n)
|
|
|
|
#1 'file:W const (:-n)
|
|
|
|
#2 'file:A const (:-n)
|
|
|
|
#3 'file:R+ const (:-n)
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
|
|
|
|
2020-09-16 16:37:30 +02:00
|
|
|
For opening a file, provide the file name and mode. This will return
|
|
|
|
a number identifying the file handle.
|
2018-11-22 06:22:53 +01:00
|
|
|
|
|
|
|
~~~
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:open (:sm-h) #0 file:operation ;
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
Given a file handle, close the file.
|
|
|
|
|
|
|
|
~~~
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:close (:h-) #1 file:operation ;
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
Given a file handle, read a character.
|
|
|
|
|
|
|
|
~~~
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:read (:h-c) #2 file:operation ;
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
Write a character to an open file.
|
|
|
|
|
|
|
|
~~~
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:write (:ch-) #3 file:operation ;
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
Return the current pointer within a file.
|
|
|
|
|
|
|
|
~~~
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:tell (:h-n) #4 file:operation ;
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
Move the file pointer to the specified location.
|
|
|
|
|
|
|
|
~~~
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:seek (:nh-) #5 file:operation ;
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
Return the size of the opened file.
|
|
|
|
|
|
|
|
~~~
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:size (:h-n) #6 file:operation ;
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
Given a file name, delete the file.
|
|
|
|
|
|
|
|
~~~
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:delete (:s-) #7 file:operation ;
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
Flush pending writes to disk.
|
|
|
|
|
|
|
|
~~~
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:flush (:f-) #8 file:operation ;
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
|
|
|
|
2023-07-06 03:09:39 +02:00
|
|
|
~~~
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:read/bytes (:pnf-) #9 file:operation ;
|
|
|
|
:file:write/bytes (:pnf-) #10 file:operation ;
|
|
|
|
:file:read/c (:h-c) #11 file:operation ;
|
|
|
|
:file:write/c (:ch-c) #12 file:operation ;
|
2023-07-06 03:09:39 +02:00
|
|
|
~~~
|
|
|
|
|
2018-11-22 06:22:53 +01:00
|
|
|
Given a file name, return `TRUE` if it exists or `FALSE` otherwise.
|
|
|
|
|
|
|
|
~~~
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:exists? (:s-f)
|
2018-11-22 06:22:53 +01:00
|
|
|
file:R file:open dup n:-zero?
|
|
|
|
[ file:close TRUE ]
|
2019-12-09 20:37:26 +01:00
|
|
|
[ drop FALSE ] choose ;
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
|
|
|
|
2020-09-16 16:37:30 +02:00
|
|
|
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:open-for-reading (:s-nn)
|
2020-06-29 21:07:57 +02:00
|
|
|
file:R file:open dup file:size swap ;
|
|
|
|
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:open-for-append (:s-nn)
|
2020-06-29 21:07:57 +02:00
|
|
|
file:A file:open dup file:size swap ;
|
|
|
|
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:open-for-writing (:s-n)
|
2020-06-29 21:07:57 +02:00
|
|
|
file:W file:open ;
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
With that out of the way, we can begin building higher level functionality.
|
|
|
|
|
|
|
|
The first of these reads a line from the file. This is read to `here`; move
|
|
|
|
it somewhere safe if you need to keep it around.
|
|
|
|
|
|
|
|
The second goes with it. The `for-each-line` word will invoke a combinator
|
|
|
|
once for each line in a file. This makes some things trivial. E.g., a simple
|
|
|
|
'cat' implementation could be as simple as:
|
|
|
|
|
|
|
|
'filename [ s:put nl ] file:for-each-line
|
|
|
|
|
|
|
|
~~~
|
|
|
|
{{
|
|
|
|
'FID var
|
|
|
|
'Size var
|
|
|
|
'Action var
|
|
|
|
:-eof? (-f) @FID file:tell @Size lt? ;
|
2019-12-09 20:37:26 +01:00
|
|
|
:preserve (q-) &FID [ &Size &call v:preserve ] v:preserve ;
|
2018-11-22 06:22:53 +01:00
|
|
|
---reveal---
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:read-line (:f-s)
|
2023-11-03 17:51:12 +01:00
|
|
|
here swap #13 file:operation here ;
|
2018-11-22 06:22:53 +01:00
|
|
|
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:for-each-line (:sq-)
|
2018-11-22 06:22:53 +01:00
|
|
|
[ !Action
|
2020-06-29 21:07:57 +02:00
|
|
|
file:open-for-reading !FID !Size
|
2018-11-22 06:22:53 +01:00
|
|
|
[ @FID file:read-line @Action call -eof? ] while
|
|
|
|
@FID file:close
|
|
|
|
] preserve ;
|
|
|
|
}}
|
|
|
|
~~~
|
|
|
|
|
|
|
|
`file:slurp` reads a file into a buffer.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
{{
|
|
|
|
'FID var
|
|
|
|
---reveal---
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:slurp (:as-)
|
2020-06-29 21:07:57 +02:00
|
|
|
[ swap buffer:set file:open-for-reading !FID
|
2020-02-03 16:46:52 +01:00
|
|
|
[ @FID file:read buffer:add ] times
|
2018-11-22 06:22:53 +01:00
|
|
|
@FID file:close
|
|
|
|
] buffer:preserve ;
|
|
|
|
}}
|
|
|
|
~~~
|
|
|
|
|
2020-02-03 16:46:52 +01:00
|
|
|
`file:spew` writes a string to a file.
|
|
|
|
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
2024-09-09 16:03:18 +02:00
|
|
|
:file:spew (:ss-)
|
2020-06-29 21:07:57 +02:00
|
|
|
file:open-for-writing swap [ over file:write ] s:for-each file:close ;
|
2018-11-22 06:22:53 +01:00
|
|
|
~~~
|
2021-08-23 15:23:10 +02:00
|
|
|
|
|
|
|
## d:source
|
|
|
|
|
|
|
|
~~~
|
2023-10-23 20:23:23 +02:00
|
|
|
'interface/filesystem.retro s:dedup
|
2023-04-12 02:34:17 +02:00
|
|
|
dup 'file:spew d:set-source
|
|
|
|
dup 'file:slurp d:set-source
|
|
|
|
dup 'file:for-each-line d:set-source
|
|
|
|
dup 'file:read-line d:set-source
|
|
|
|
dup 'file:open-for-writing d:set-source
|
|
|
|
dup 'file:open-for-append d:set-source
|
|
|
|
dup 'file:open-for-reading d:set-source
|
|
|
|
dup 'file:exists? d:set-source
|
|
|
|
dup 'file:flush d:set-source
|
|
|
|
dup 'file:delete d:set-source
|
|
|
|
dup 'file:size d:set-source
|
|
|
|
dup 'file:seek d:set-source
|
|
|
|
dup 'file:tell d:set-source
|
|
|
|
dup 'file:write d:set-source
|
|
|
|
dup 'file:read d:set-source
|
|
|
|
dup 'file:close d:set-source
|
|
|
|
dup 'file:open d:set-source
|
|
|
|
dup 'file:R+ d:set-source
|
|
|
|
dup 'file:A d:set-source
|
|
|
|
dup 'file:W d:set-source
|
|
|
|
dup 'file:R d:set-source
|
|
|
|
dup 'file:operation d:set-source
|
2023-10-22 18:11:59 +02:00
|
|
|
dup 'file:write/bytes d:set-source
|
|
|
|
dup 'file:read/bytes d:set-source
|
|
|
|
dup 'file:write/c d:set-source
|
|
|
|
dup 'file:read/c d:set-source
|
2021-08-23 15:23:10 +02:00
|
|
|
drop
|
|
|
|
~~~
|