Guest User

Faster alternative to READ-LINE

a guest
Jul 29th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | Source Code | 0 0
  1. \ Faster alternative to READ-LINE. Reads text file into as
  2. \ much memory as is available and parses out complete lines.
  3. \ Buffer is automatically refilled should the input file be
  4. \ larger. It may add an extra empty line at the end compared
  5. \ to the source file however this is usually harmless.
  6.  
  7. \ READDATA ( a u -- a u2 ) infile READ-FILE
  8. \ SIZEINFILE ( -- ud ) infile FILE-SIZE
  9. \ SEEKINFILE ( ud -- ) infile REPOSITION-FILE
  10. \ END aka EXIT THEN
  11.  
  12. variable CPMEOF cpmeof off \ CP/M EOF char received
  13.  
  14. : /EOL ( a u -- u' offs ) \ scan for CR LF CRLF
  15. over swap begin dup while over c@ $0D of drop
  16. tuck swap - swap 1+ c@ $0A <> over + 2+ end $0A
  17. of drop swap - dup 1+ end $1A - while 1 /string
  18. repeat CPMEOF on then drop swap - dup ;
  19.  
  20. 2variable CHUNK 2variable SRC
  21.  
  22. : GETLINE2 ( -- a u EOL -1 | 0 ) \ buffered
  23. src 2@ 0 of drop chunk 2@ tuck readdata 2dup src 2!
  24. rot 1- umin then dup if 2dup /eol src 2@ rot /string
  25. CPMEOF @ if sizeinfile seekinfile drop 0 then src 2!
  26. tuck <> -1 end nip ;
  27.  
  28. \\ sample use
  29.  
  30. \ WRITETEXT ( a u -- ) outfile WRITE-LINE
  31. \ PAD UNUSED ( -- a u ) max free memory chunk
  32.  
  33. : +STRING ( a u a2 u2 -- a2 u+u2 ) \ append a u to a2 u2
  34. 2swap swap 2over + 2 pick move + ;
  35.  
  36. : ?OUT ( a u flag -- a' u' )
  37. if writetext pad 0 then ;
  38.  
  39. : INIT ( -- a 0 ) \ assume max line length 255
  40. pad 0 over unused 255 /string chunk 2!
  41. src off cpmeof off ;
  42.  
  43. : COPY ( -- ) init
  44. 0 begin ?out getline2 while \ not done
  45. >r 2swap +string r>
  46. repeat 2drop ;
  47.  
Advertisement
Add Comment
Please, Sign In to add comment