Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- \ Faster alternative to READ-LINE. Reads text file into as
- \ much memory as is available and parses out complete lines.
- \ Buffer is automatically refilled should the input file be
- \ larger. It may add an extra empty line at the end compared
- \ to the source file however this is usually harmless.
- \ READDATA ( a u -- a u2 ) infile READ-FILE
- \ SIZEINFILE ( -- ud ) infile FILE-SIZE
- \ SEEKINFILE ( ud -- ) infile REPOSITION-FILE
- \ END aka EXIT THEN
- variable CPMEOF cpmeof off \ CP/M EOF char received
- : /EOL ( a u -- u' offs ) \ scan for CR LF CRLF
- over swap begin dup while over c@ $0D of drop
- tuck swap - swap 1+ c@ $0A <> over + 2+ end $0A
- of drop swap - dup 1+ end $1A - while 1 /string
- repeat CPMEOF on then drop swap - dup ;
- 2variable CHUNK 2variable SRC
- : GETLINE2 ( -- a u EOL -1 | 0 ) \ buffered
- src 2@ 0 of drop chunk 2@ tuck readdata 2dup src 2!
- rot 1- umin then dup if 2dup /eol src 2@ rot /string
- CPMEOF @ if sizeinfile seekinfile drop 0 then src 2!
- tuck <> -1 end nip ;
- \\ sample use
- \ WRITETEXT ( a u -- ) outfile WRITE-LINE
- \ PAD UNUSED ( -- a u ) max free memory chunk
- : +STRING ( a u a2 u2 -- a2 u+u2 ) \ append a u to a2 u2
- 2swap swap 2over + 2 pick move + ;
- : ?OUT ( a u flag -- a' u' )
- if writetext pad 0 then ;
- : INIT ( -- a 0 ) \ assume max line length 255
- pad 0 over unused 255 /string chunk 2!
- src off cpmeof off ;
- : COPY ( -- ) init
- 0 begin ?out getline2 while \ not done
- >r 2swap +string r>
- repeat 2drop ;
Advertisement
Add Comment
Please, Sign In to add comment