home *** CD-ROM | disk | FTP | other *** search
- \ SEQREAD.SEQ Sequential read and load file by Tom Zimmer
-
- \ Link this file into the FILELIST chain.
-
- FILES DEFINITIONS
-
- VARIABLE SEQREAD.SEQ
-
- FORTH DEFINITIONS
-
- \ Read sequential lines from a file, delimited by CRLF.
-
- 1024 constant IBLEN \ input buffer length
- 256 constant OBLEN \ output buffer length
- variable INLEN \ input text length
- 0 CONSTANT INBSEG \ the input buffer
- CREATE OUTBUF OBLEN ALLOT \ the line output buffer
-
- 5 b/hcb * constant MAXNEST \ maximum of 5 hcb's
-
- create HNDLS maxnest allot
- HNDLS SHNDL !-T \ Preset pointer
- variable LOADOFF \ line offset of error
- variable FILEPOINTER 2 allot \ most recent read
- variable LOADING \ Are we in the proccess of loading a file?
- 0 LOADING !-T \ initialize to not loading.
-
- DEFER LOADSTAT ' NOOP IS LOADSTAT
-
- : SHNDL+ ( --- A1 )
- SHNDL @ B/HCB + ;
-
- : .FILE ( --- )
- shndl @ count type ;
-
- : GET_ALINE ( --- )
- INBSEG SSEG !
- 0 inlen @ 10 scan dup \ really looks for LF
- if 1 -1 d+ then inlen !
- dup oblen 1- min dup >R outbuf c!
- INBSEG 0 ?CS: outbuf 1+ R> cmoveL
- INBSEG SWAP OVER 0 inlen @ cmoveL
- errorline incr
- ?CS: SSEG ! ;
-
- : FILEPOINTER+ ( --- )
- outbuf c@ 0 filepointer 2@ d+ filepointer 2! ;
-
- HEX
-
- CODE CURPOINTER ( handle --- double-current )
- pop bx
- ADD bx, # HNDLOFFSET
- mov ax, 0 [bx]
- mov bx, ax
- mov cx, # 0
- mov dx, # 0
- mov ax, # 4201 \ from end of file
- int 21
- push ax
- push dx
- next
- end-code
- DECIMAL
-
- : SAVEPOINTER ( --- )
- shndl @ curpointer inlen @ 0 d- filepointer 2! ;
-
- : ?FILLBUFF ( --- ) \ refill INBUF
- inlen @ oblen u<
- if inlen @ iblen over - shndl @ INBSEG EXHREAD
- inlen +! savepointer
- else filepointer+
- then ;
-
- : <LINEREAD> ( --- a1 ) \ read a line delimited by CRLF
- ?fillbuff \ re-fill buffer if needed.
- get_aline \ returns line including CRLF
- outbuf ;
-
- DEFER LINEREAD ' <LINEREAD> IS LINEREAD
-
- : (?SERROR) ( ADDR N1 BOOL --- )
- LOADING @
- IF
- IF >R >R SP0 @ SP! PRINTING OFF
- LOADING OFF
- DECIMAL CR ." File = " .FILE
- ." at Line " errorline @ u.
- CR OUTBUF COUNT TYPE
- CR >IN @ 1- HERE C@ - 0 MAX
- DUP LOADOFF ! 0
- ?DO ASCII - EMIT LOOP
- ." ^-- " R> R> TYPE SPACE
- ['] <RUN> IS RUN QUIT \ Disk error
- ELSE 2DROP \ No error comes here
- THEN
- ELSE ['] <RUN> IS RUN (?ERROR) \ Command line error
- THEN ;
-
- : SEQUP ( --- )
- shndl @ >hndle @ -1 <>
- if shndl @ b/hcb + dup hndls maxnest + U>
- abort" Nested too deeply on SLOAD!"
- dup shndl ! clr-hcb
- then ;
-
- : SEQINIT ( --- )
- NOOP HNDLS SHNDL !
- HNDLS MAXNEST OVER + SWAP
- DO I CLR-HCB
- B/HCB +LOOP
- IBLEN 0 16 UM/MOD NIP 1+ ALLOC 8 = MEMCHK NIP
- [ ' INBSEG >BODY ] LITERAL ! ;
-
- : SEQDOWN ( --- )
- shndl @ hclose drop
- shndl @ b/hcb - hndls max shndl !
- shndl @ >hndle @ -1 <>
- if filepointer 2@ shndl @ movepointer
- >in off span off #tib off inlen off
- then ;
-
- : CLOSE ( --- )
- SEQDOWN ;
-
- : $HOPEN ( A1 --- F1 ) \ Returns a boolean for open successful
- shndl @ dup >r hclose drop
- r@ $>handle r> hopen ;
-
- : SEEK ( d1 --- ) \ Move the filepointer in SHNDL to the
- shndl @ movepointer ; \ specified by d1.
-
- variable LVR
-
- : SHOWLINES ( --- ) lvr on ;
-
- : HIDELINES ( --- ) lvr off ;
-
- : FILLTIB ( --- )
- span @ loadline +!
- lineread
- dup count + 2- 8224 swap !
- lvr @
- if CR loadline @ 5 u.r space
- DUP COUNT TYPE
- then dup 1+ 'tib ! c@ dup span ! #tib !
- >in off ;
-
- : <LOAD> ( --- )
- loadstat
- loading dup @ >r on
- 'tib @ >r >in @ >r span @ >r SPAN OFF errorline @ >r
- begin span @ loadline +!
- lineread inlen @ 0>
- while dup count + 2- 8224 swap !
- lvr @
- if CR errorline @ 5 u.r space
- DUP COUNT TYPE
- then dup 1+ 'tib ! c@ dup span ! #tib !
- >in off run
- INLEN @ oblen < if LOADSTAT then
- repeat drop r> errorline !
- r> dup span ! #tib ! r> >in ! r> 'tib !
- r> loading ! loadstat ;
-
- DEFER LOADER ' <LOAD> IS LOADER
-
- : >LINE ( n1 --- )
- 0.0 shndl @ movepointer
- loadline off inlen off errorline off
- 1- 0 max ?dup
- if cr ." Stepping to line " dup 1+ u. ." .."
- 0
- ?do errorline incr
- lineread c@ dup loadline +! 0= ?leave
- loop
- then ;
-
- : LOADED, ( --- )
- 'TIB @ >R >IN @ >R SPAN @ >R \ SAVE STATE
- SHNDL @ COUNT
- BEGIN 2DUP ASCII \ SCAN DUP \ Skip the leading PATH
- WHILE 2SWAP 2DROP
- 1 -1 D+
- REPEAT 2DROP DUP SPAN ! #TIB ! \ Set SPAN and #TIB
- 'TIB ! \ Set TIB to SHNDL
- >IN OFF \ Set >IN
- CONTEXT @ >R \ Save current context
- CURRENT @ >R \ and current vocab state
- FILES DEFINITIONS \ select FILES vocabulary
- VARIABLE \ Make the header
- R> CURRENT ! R> CONTEXT ! \ restore vocabulary state
- R> DUP SPAN ! #TIB ! \ Restore STATE
- R> >IN ! R> 'TIB ! ;
-
- : <FLOAD> ( --- )
- LOADED,
- 0.0 shndl @ movepointer
- loadline dup @ >r off
- errorline off
- LOADER
- r> loadline ! ;
-
- : FLOAD ( --- t1 )
- ['] ?error >body @ >r
- sequp inlen off
- ['] (?SERROR) IS ?ERROR
- >IN @ BL WORD C@ OVER >IN ! + >R
- outbuf off loadoff off shndl @ !hcb
- R> 0 filepointer 2@ d+ >R >R
- shndl @ hopen 0<>
- if cr ." Open Error in " .file
- abort
- then <fload>
- R> R> filepointer 2!
- shndl @ c@ 6 + loadline +! \ adj for filename length
- \ and "FLOAD ".
- seqdown r> is ?error ;
-
- : CHARREAD ( --- c1 ) \ Read a character from the current file.
- loading @
- if begin >in @ span @ = \ If nothing in line
- inlen @ 0> and \ and input buf not empty
- while ?fillbuff \ Optionally refill buffer
- filltib \ refill the TIB
- repeat
- then >in @ >in incr tib + c@ ;
-
- : OK ( --- ) \ Load currently open file
- inlen off <fload> ;
-
-
- : \S ( n1 --- ) \ Ignore the rest of the file.
- shndl @ endfile 2drop \ Move to end of file
- loadline off inlen off \ Reset input buffers
- span @ >in ! ; \ Ignore rest of line
-