'===========================================================================
' Subject: CONVERT ANY FILE TO BLOAD FILE     Date: 12-11-97 (00:34)       
'  Author: Andrew S. Gibson                   Code: QB, QBasic, PDS        
'  Origin: zapf_dingbat@juno.com            Packet: MISC.ABC
'===========================================================================
DEFINT A-Z
' I wrote this a long time ago... any body that does graphical stuff can
' appreciate this.. email me if you want to.  Zapf_DingBat@JUNO.COM
'
' Purpose: Converts any file to a BLOAD file.
'          header format:
'           FID     LDA    NB   FS-RD
'         /FD 00\ /XX XX\ /00\ /XX XX\
'   FID=BSAVE IDENTIFICATION
'   LDA=LOAD ADDRESS - STANDARD FORMAT (like DEF SEG sans '&H')
'    NB=NULL BYTE (FOR SPACING)
' FS-RD=File size - Least significant Byte first -- Most Significant Byte Last
'       (like ASM)
'
' ask for input file
LOCATE CSRLIN, 1, 1
LINE INPUT "Enter the filename to convert to a BLOAD file "; FileName$

' ask for fake bload address
LINE INPUT "Please enter the SEGMENT to load (in HEX)     "; BsaveSegment$

' open input file
OPEN FileName$ FOR BINARY AS #1
IF LOF(1) > 65535 THEN CLOSE : PRINT "Input file size too large !": END

' convert FileName$ to OutPutFileName$
NameLength = LEN(FileName$) - 3
OutPutFileName$ = UCASE$(LEFT$(FileName$, NameLength) + "BSV")

' open output file
OPEN OutPutFileName$ FOR BINARY AS #2

' Make a character string out of BsaveSegment$
BSaveSeg$ = CHR$(VAL("&H" + LEFT$(BsaveSegment$, 2))) + CHR$(VAL("&H" + RIGHT$(BsaveSegment$, 2)))

' Convert LOF information to File Size in HEX and make a character string out
' of FileSize$
FileSize$ = HEX$(LOF(1))
BackWord$ = CHR$(VAL("&H" + RIGHT$(FileSize$, 2))) + CHR$(VAL("&H" + LEFT$(FileSize$, 2)))

' summate other strings into BSaveHeader$
BSaveHeader$ = CHR$(253) + CHR$(0) + BSaveSeg$ + CHR$(0) + BackWord$

' Write faked BSAVE header to Output File ON ONE LINE !!!!!!!!!!!!!
PUT #2, , BSaveHeader$

' Show Bytes remaining
PRINT : PRINT "Converting "; UCASE$(FileName$); " to "; OutPutFileName$; "...": PRINT
' read 4k (or remaining) from input file and write them to output file !!!
  Remaining& = LOF(1)
  LOCATE CSRLIN, 1, 1: PRINT USING "Bytes Remaining #,###,###,###"; Remaining&;
  DO
    IF Remaining& > 4096 THEN
      ThisPass = 4096
    ELSE
      ThisPass = Remaining&
    END IF
    Buffer$ = SPACE$(ThisPass)
    GET #1, , Buffer$
    PUT #2, , Buffer$
    Remaining& = Remaining& - ThisPass
    LOCATE CSRLIN, 1, 1: PRINT USING "Bytes Remaining #,###,###,###"; Remaining&;
  LOOP WHILE Remaining&

  CLOSE #1, #2

END
