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 ;
\ 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 ;
\ End
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 504 |
Nodes: | 16 (2 / 14) |
Uptime: | 239:04:32 |
Calls: | 9,888 |
Calls today: | 10 |
Files: | 13,794 |
Messages: | 6,207,349 |
Posted today: | 3 |