;
;       NAME:  SVLIN.M68
;
       OBJNAM  .SBR
;
;       FUNCTION:  This subroutine is used to read data from a SuperVue file
;       one line at a time. Since SuperVue does not terminate its data line
;       with a CR+LF pair, SVLIN reads the data until it finds a CR then
;       returns the data in the BUFFER string
;       If LENGTH returned = 0 then and end of file is reached
;       If your Supervue file contains the header information used in SuperVUE
;       it can be found by checking for CHR$(1) which is the begining
;       of the header information and CHR$(1) which is the terminating
;       character for the header information on the first line input
;
;       CALLING SEQUENCE:  XCALL SVLIN,OPTION,AREA{,BUFFER|NAME{,LENGTH}}
;       where
;       Name    Type    Use
;       OPTION  Float   1=open
;                       2=read
;                       3=close
;       AREA    X,616   DDB and buffer
;       BUFFER  Any     Returned data (READ call) make sure it is large
;                       enough to accept a whole line of data
;       NAME    String  Filename (OPEN call)
;       LENGTH  Float   Length of returned data (READ call)
;
;       AUTHOR: James A. Jarboe of this version GR/AM
;       VERY VERY LARGELY taken from FILEIN.M68 by Tom Dahlquist
;       donated to AMUS
;
;       EDIT HISTORY:
;         When   Who What
;                 Tom dahlquist wrote it as FILEIN.M68
;       11/15/85 JAJ modified for Supervue file input
;
;MAP1   OPTION,F,6      ! 1=open  2=read 3=close
;MAP1   AREA,X,616      ! DDB area do not change
;MAP1   BUFFER,S,200    ! make sure size is compatable with line length of
;                       ! file read
;MAP1   FILE$,S,24,"TEST.T"     ! filename
;MAP1   LENGTH,F                ! length of line returned here
;
;! open file
;       OPTION=1
;       ? "Opening ";FILE$
;       XCALL SVLIN,OPTION,AREA,FILE$
;       OPTION=2
;ONE:
;
;        ? "Reading ";FILE$
;       CALL IN                 ! get sv line
;        IF BUFFER[1,1]#CHR$(1) &
;               THEN GOTO FIRST'LINE   ! check for sv header info
;
;HEADTST:
;       HEAD=INSTR(2,BUFFER,CHR$(1))    ! check for end of header info
;       BUFFER=BUFFER[HEAD+1,LENGTH]    ! take out header info
;       LENGTH=LENGTH-HEAD              ! make LENGTH =  -header data
;FIRST'LINE:
;       GOTO PRINTIT                    ! print it out to screen or file
;
;PROCESS:
;       CALL IN                         ! get next line
;       IF LENGTH=0 THEN GOTO LOSE      ! if end of file then close it
;PRINTIT:
;       ? BUFFER;               !print buffer
;       ? CHR$(10);             !print a line feed
;       GOTO PROCESS
;
;!!!!!!!!!!!!
;! SUBROUTINES
;!!!!!!!!!!!!
;
;IN:
;       BUFFER=""               ! clear buffer
;       OPTION=2                ! read option
;       XCALL SVLIN,OPTION,AREA,BUFFER,LENGTH
;       RETURN
;LOSE:
;       FILE$="TEST.T"
;       OPTION=3                ! close option
;       ? "CLOSING ";FILE$
;       XCALL SVLIN,OPTION,AREA,FILE$
;       END

       SEARCH  SYS
       SEARCH  SYSSYM

       ASECT
       .=0
PRMCNT: BLKW    1
TYPE1:  BLKW    1                       ; type of arg 1
ADDR1:  BLKL    1                       ; address of arg1
SIZE1:  BLKL    1                       ; size arg1
TYPE2:  BLKW    1
ADDR2:  BLKL    1
SIZE2:  BLKL    1
TYPE3:  BLKW    1
ADDR3:  BLKL    1
SIZE3:  BLKL    1
TYPE4:  BLKW    1
ADDR4:  BLKL    1
SIZE4:  BLKL    1

       PSECT
       .=0
       VMAJOR=1
       VMINOR=1
       VEDIT=0

SVLIN:  PHDR    -1,0,PH$REU!PH$REE
       CMPW    @A3,#3                  ; must be at least 3 args...
       BLO     RETURN                  ; back if not...
       MOVW    TYPE1(A3),D6            ; get type of first...
       ANDW    #7,D6                   ; must be float...
       CMPW    D6,#4                   ; back if not...
       BNE     RETURN
       CMP     SIZE2(A3),#616.         ; right size?
       BNE     RETURN                  ; back if not.
       MOV     ADDR1(A3),A0            ; get option...
       FFTOL   @A0,D0                  ; D1=option...
       MOV     ADDR2(A3),A0            ; A0->DDB...
       CMPW    D0,#1                   ; OPEN...
       BEQ     OPEN
       CMPW    D0,#2                   ; READ...
       BEQ     READ
       CMPW    D0,#3                   ; CLOSE
       JEQ     CLOSE
RETURN: RTN
;
;       OPEN--clear the DDB, initialize it, open file.
;
OPEN:   CLEAR   @A0,D.DDB               ; clear it...
       LEA     A6,D.DDB(A0)            ; A6->I/O buffer...
       MOV     A6,D.BUF(A0)            ; put into DDB...
       MOVB    #D$INI,D.FLG(A0)        ; set INITed...
       MOV     ADDR3(A3),A2            ; A2->filename...
       FSPEC   @A0,T                   ; put filename into DDB, and
       OPENI   @A0                     ; open it.
       RTN
;
;       READ--move bytes to buffer until a CR, or file is empty.
;
READ:   CMPW    @A3,#4                  ; must have fourth arg...
       BLO     RETURN
       MOVW    TYPE4(A3),D6            ; check type of it...
       ANDW    #7,D6
       CMPW    D6,#4
       BNE     RETURN
       MOV     ADDR3(A3),A1            ; A1->buffer...
       MOV     SIZE3(A3),D0            ; D0=length of buffer...
       DEC     D0                      ; minus 1 for DBF...
       CLR     D2                      ; D2 used as counter...
LOOP:   FILINB  @A0                     ; ask for next byte...
       TST     D.SIZ(A0)               ; anything left?
       BEQ     EOF                     ; br if not...
       MOVB    D1,(A1)+                ; move it in...
       ADDW    #1,D2                   ; increment counter...
       CMPB    D1,#13.                 ; is it end of a SuperVUE line?
       BEQ     EOF                     ; well then lets get ready to go
       DBF     D0,LOOP                 ; and loop.
       BR      SETLTH
EOF:    CLRB    (A1)+                   ; clear rest of buffer...
       DBF     D0,EOF                  ; was out
SETLTH: MOV     ADDR4(A3),A1            ; A1->length variable...
       FLTOF   D2,@A1                  ; convert length...
       RTN
;
;       CLOSE--close the file.
;
CLOSE:  CLOSE   @A0
       RTN

       END