Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #133590 > unrolled thread

Quick file handling

Started bydxf <dxforth@gmail.com>
First post2025-06-06 20:53 +1000
Last post2025-07-21 13:38 +1000
Articles 5 — 2 participants

Back to article view | Back to comp.lang.forth


Contents

  Quick file handling dxf <dxforth@gmail.com> - 2025-06-06 20:53 +1000
    Re: Quick file handling albert@spenarnc.xs4all.nl - 2025-06-06 13:05 +0200
      Re: Quick file handling dxf <dxforth@gmail.com> - 2025-06-06 21:39 +1000
    Re: Quick file handling dxf <dxforth@gmail.com> - 2025-07-20 12:35 +1000
      Re: Quick file handling dxf <dxforth@gmail.com> - 2025-07-21 13:38 +1000

#133590 — Quick file handling

Fromdxf <dxforth@gmail.com>
Date2025-06-06 20:53 +1000
SubjectQuick file handling
Message-ID<64c0aee9cd512ac490923f15b3cd2c0533b7bf3b@i2pn2.org>
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 ;

\ Functions using handles

handle IFILE  \ Input handle
handle OFILE  \ Output handle

\ Get file-id from handle
aka @ >FID ( handle -- fid )

: IFID ( -- fid )  ifile >fid ;
: OFID ( -- fid )  ofile >fid ;

\ Open filename a/u for input
: OPENIN ( a u fam -- )
  >r 2dup r> open-file ?derr ifile tuck ! !fname ;

\ Create filename a/u for output
: MAKEOUT ( a u fam -- )
  >r 2dup r> create-file ?derr ofile tuck ! !fname ;

: CLOSEIN ( -- )  ifid fclose  -1 ifile ! ;
: CLOSEOUT ( -- )  ofid fclose  -1 ofile ! ;

\ Reposition IFILE
: SEEKIN ( ud -- )  ifid fseek ;

\ Reposition OFILE
: SEEKOUT ( ud -- )  ofid fseek ;

\ Get IFILE position
: INPOS ( -- ud )  ifid fpos ;

\ Get OFILE position
: OUTPOS ( -- ud )  ofid fpos ;

\ Read binary from IFILE
: READ ( a u -- a u2 )  ifid fread ;

\ Write binary to OFILE
: WRITE ( a u -- )  ofid fwrite ;

\ Read text from IFILE
: READLN ( a u -- a u2 flag )  ifid freadln ;

\ Write text to OFILE
: WRITELN ( a u -- )  ofid fwriteln ;

defer CLOSEFILES ( -- )
:noname  closein closeout ; is closefiles

base !  sys !

behead ?derr ?derr

0 [if]

  : fcopy ( -- )  \ file copy
    begin  pad 64 read  dup while  write  repeat 2drop ;

  s" infile.dat" r/o openin  s" outfile.dat" w/o makeout
  fcopy
  closefiles

[then]

\ End
 

[toc] | [next] | [standalone]


#133592

Fromalbert@spenarnc.xs4all.nl
Date2025-06-06 13:05 +0200
Message-ID<nnd$33be0d63$62d07b84@31279367604044df>
In reply to#133590
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?

>
>\ 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.?

<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" )

>
>\ End
>
-- 
Temu exploits Christians: (Disclaimer, only 10 apostles)
Last Supper Acrylic Suncatcher - 15Cm Round Stained Glass- Style Wall
Art For Home, Office And Garden Decor - Perfect For Windows, Bars,
And Gifts For Friends Family And Colleagues.

[toc] | [prev] | [next] | [standalone]


#133593

Fromdxf <dxforth@gmail.com>
Date2025-06-06 21:39 +1000
Message-ID<e31ed9c71e24247aa19b56ec90f4a2bf9b2f151c@i2pn2.org>
In reply to#133592
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.

[toc] | [prev] | [next] | [standalone]


#134075

Fromdxf <dxforth@gmail.com>
Date2025-07-20 12:35 +1000
Message-ID<6c861dfeed2f6aa79109fc6d53439d41da282c9a@i2pn2.org>
In reply to#133590
For anyone interested here's an updated version that corrects some
oversights.  Also included is a buffered version that allows char I/O.

Intended for DX-Forth, porting would require some changes.  For the
basic version that would be the ior error messages.  For the buffered
version I've used assembler (8080 and 8086).  It comes with a doc
which may help.  Good luck!

It's available at:

FILES01.ZIP

https://drive.google.com/drive/folders/1kh2WcPUc3hQpLcz7TQ-YQiowrozvxfGw

[toc] | [prev] | [next] | [standalone]


#134078

Fromdxf <dxforth@gmail.com>
Date2025-07-21 13:38 +1000
Message-ID<64405ba91031e8ec0e3a58288d0f7ec964361183@i2pn2.org>
In reply to#134075
File handling schemes are much like that for strings.  One is never enough.
Below is the shortest I've used that I'm not ashamed of and have dubbed it
'Quick Files'.

https://pastebin.com/zxBpxsg9

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web