Sujet : Re: Quick file handling
De : dxforth (at) *nospam* gmail.com (dxf)
Groupes : comp.lang.forthDate : 06. Jun 2025, 12:39:25
Autres entêtes
Organisation : i2pn2 (i2pn.org)
Message-ID : <e31ed9c71e24247aa19b56ec90f4a2bf9b2f151c@i2pn2.org>
References : 1 2
User-Agent : Mozilla Thunderbird
On 6/06/2025 9:05 pm,
albert@spenarnc.xs4all.nl wrote:
In article <64c0aee9cd512ac490923f15b3cd2c0533b7bf3b@i2pn2.org>,
dxf <dxforth@gmail.com> wrote:
I needed to do a quick file hack and came up with this. It's a
cut down version of the library I use when writing applications.
It will require customizing for other forths.
>
>
\ FILEHACK.F
\
\ Simplified file handling (see example below)
\
\ N.B. This was written for DX-Forth and will require modification
\ for others. E.g. SYS SYSTEM APPLICATION BEHEAD '$FF AND' etc can
\ be omitted and the codes in ?DERR adjusted to match your system.
\
\ Public domain
>
sys @ base @
>
forth definitions decimal
>
system
\ Create file handle - holds file-id and filename
: HANDLE create -1 , max-path 1+ allot ;
application
>
: !FNAME ( a u handle -- ) >r max-path min r> cell+ place ;
: @FNAME ( handle -- a u ) cell+ count ;
>
: ?derr ( ior -- ) ?dup if
space $FF and case
2 of ." file not found" endof
3 of ." path not found" endof
4 of ." too many open files" endof
5 of ." access denied" endof
6 of ." invalid handle" endof
dup . ." DOS"
endcase ." error" abort
then ;
How can it make sense to have a special error handling
for file only?
For whom? This suited my circumstances. YMMV
>
\ General functions using file-id
: FREAD ( a u fid -- a u' ) 2>r dup 2r> read-file ?derr ;
: FREADLN ( a u fid -- a u' n ) 2>r dup 2r> read-line ?derr ;
: FWRITE ( a u fid -- ) write-file ?derr ;
: FWRITELN ( a u fid -- ) write-line ?derr ;
: FSEEK ( ud fid -- ) reposition-file ?derr ;
: FPOS ( fid -- ud ) file-position ?derr ;
: FCLOSE ( fid -- ) close-file drop ;
How can it make sense to write wrappers around open-file close-file
read-file etc.?
Certainly ior needs to be handled. THROW is no good to me as it will
just print a number. Other systems may not even have THROW.
<SNIP>
I embrace strings with full cell length.
GET-FILE PUT-FILE are sensible extensions.
"tetris.cfg" GET-FILE EVALUATE
(as are " type prefixes , "recognizers" )
I never claimed what I posted would suit you.