Guest User

Quick Files

a guest
Jul 20th, 2025
19
0
358 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | Source Code | 0 0
  1. \ QFILES.F 2025-07-21
  2.  
  3. sys @ application
  4.  
  5. pad value cf ( -- a ) \ current file handle
  6.  
  7. -? : HANDLE ( "name" -- ; -- )
  8. create -1 , 0 c, max-path allot does> to cf ;
  9.  
  10. \ Store, fetch, print filename of the current handle
  11. : !FNAME ( a u -- ) max-path min cf cell+ pack count upper ;
  12. : @FNAME ( -- a u ) cf cell+ count ;
  13. : .FNAME ( -- ) @fname type space ;
  14.  
  15. \ File error handler
  16. : ?FERR ( ? n a u -- ? )
  17. rot if cr .fname type ." error" abort end 2drop ;
  18.  
  19. : FREAD ( a u -- a u2 ) over swap cf @ read-file s" read" ?ferr ;
  20. : FREADLN ( a u -- a u2 flag ) over swap cf @ read-line s" read" ?ferr ;
  21. : FWRITE ( a u -- ) cf @ write-file s" write" ?ferr ;
  22. : FWRITELN ( a u -- ) cf @ write-line s" write" ?ferr ;
  23. : FSEEK ( ud -- ) cf @ reposition-file s" seek" ?ferr ;
  24. : FPOS ( -- ud ) cf @ file-position s" position" ?ferr ;
  25. : FSIZE ( -- ud ) cf @ file-size s" size" ?ferr ;
  26. : FOPEN ( fam -- ) @fname rot open-file s" open" ?ferr cf ! ;
  27. : FCREAT ( fam -- ) @fname rot create-file s" create" ?ferr cf ! ;
  28. : FCLOSE ( -- ) cf @ cf on close-file drop ;
  29.  
  30. \ Parse the DOS command line for the n'th blank delimited
  31. \ parameter. Return address, length and true if found, or
  32. \ false otherwise.
  33. : ARG ( n -- adr len -1 | 0 )
  34. >r 0 0 cmdtail r> 0 ?do
  35. 2nip bl skip 2dup bl scan rot over - -rot
  36. loop 2drop dup if -1 end and ;
  37.  
  38. sys !
  39.  
  40. behead cf cf
  41.  
  42. 0 [if]
  43.  
  44. Quick Files
  45.  
  46. Suited to small turnkey applications with simple command-line
  47. requirements e.g. one or two filenames.
  48.  
  49. - File operations are performed on the 'current' handle.
  50.  
  51. - Executing a handle's name makes it 'current'.
  52.  
  53. - FOPEN and FCREAT take a filename from the current handle.
  54. The user is responsible for initializing the handle with !FNAME
  55. before calling FOPEN or FCREAT .
  56.  
  57. - FCLOSE closes the disk file and sets the handle file-id to -1.
  58. Errors are not reported. The filename field is left unchanged
  59. allowing easy re-opening or even overwriting the closed file.
  60.  
  61. - FREAD FREADLN return the buffer address in addition to the
  62. number of bytes read.
  63.  
  64. - ?FERR is not limited to 'ior's. Can be used to abort on any
  65. error condition related to the current handle.
  66.  
  67. Porting:
  68.  
  69. CMDTAIL ( -- a u ) return the DOS command-line string
  70. MAX-PATH ( -- u ) maximum length of path/filename
  71. UPPER ( a u -- ) convert string to upper case
  72. PACK ( a u a2 -- a2 ) as for PLACE but leave dest addr
  73. END macro for 'EXIT THEN'
  74.  
  75. Licence:
  76.  
  77. Public Domain. Use at your own risk.
  78.  
  79. [then]
  80.  
  81.  
Tags: Forth
Advertisement
Add Comment
Please, Sign In to add comment