'
' ADDCRLF v1.0 - J. Webb, DeltaVision Systems. No CopyRight, but please don't
'                distribute modified versions.
'
$B+,U-,I-,E#,P<                                              ! Compiler switches
'
PRINT "ADDCRLF v1.0 - J. Webb, DeltaVision Systems. No CopyRight."
PRINT
'
ON ERROR GOSUB trap                                            ! Trap any errors
'
IF BYTE{BASEPAGE+128}=0                               ! No command line present?
  '
  help                                                        ! Show help & exit
  '
ELSE                                                     ! Command line present!
  '
  f$=SPACE$(BYTE{BASEPAGE+128})                                ! Make some space
  BMOVE BASEPAGE+129,V:f$,LEN(f$)                ! Move command line into string
  '
  IF NOT EXIST(f$)                                    ! Does file doesn't exist?
    PRINT "File Not Found!"                                       ! Show message
    PRINT
    help                                                      ! Show help & exit
  ELSE                                                         ! File does exist
    '
    PRINT "Processing file "+UPPER$(f$)                  ! Show what we're doing
    OPEN "I",#1,f$                                         ! Open file for input
    IF LOF(#1)<2                                         ! Smaller than 2 bytes?
      CLOSE #1                                                     ! Close input
      OPEN "A",#1,f$                                   ! Open file for appending
      PRINT #1                                                     ! Append CRLF
    ELSE                                              ! Same/Longer than 2 bytes
      SEEK #1,LOF(#1)-2                                   ! Go to end of file -2
      IF INP(#1)=13 AND INP(#1)=10                      ! If last bytes are CRLF
        PRINT "File ends in CRLF"                                ! Nothing to do
      ELSE                                              ! Last bytes aren't CRLF
        CLOSE #1                                                   ! Close input
        PRINT "Adding CRLF"                                   ! Have to add CRLF
        OPEN "A",#1,f$                                 ! Open file for appending
        PRINT #1                                                   ! Append CRLF
      ENDIF
    ENDIF
    CLOSE                                                       ! Close any file
    '
  ENDIF
ENDIF
END
'
> PROCEDURE help                                        ! Show help, wait & exit
  '
  PRINT " Makes sure a text file ends with CRLF.";
  PRINT " Supply the path+name+extension of the"
  PRINT " file to check on the command line."
  PRINT
  PRINT "Any Key or wait 10 seconds..."
  '
  t%=TIMER
  REPEAT
  UNTIL TIMER-t%>2000 OR INP?(2)
  '
  IF INP?(2)
    ~INP(2)
  ENDIF
  '
  END
  '
RETURN
> PROCEDURE trap                                  ! Jump here if an error occurs
  '
  PRINT CHR$(7);"ERROR ";ERR                                 ! Show error number
  PAUSE 50                                                     ! Wait one second
  END                                                                     ! Exit
  '
RETURN
