;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
;                                                        ;
; Lf-CrLf  read a text file & change LF's to CRLF's      ;             
;                                                        ;
; By : Randy Quick                                       ;
;      P.O Box 3                                         ;
;      Henley Beach                                      ;
;      Sth Australia     5022                            ;
;                                                        ;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;                                       


ExecBase     equ 4

;-----  EXEC  -----

OpenLib   equ -408
CloseLib  equ -414


;-----  DOS  -----


Open            equ -30
Close           equ -36
Lock            equ -84
UnLock          equ -90
Read            equ -42
Write           equ -48
Mode_Old        equ  1005
Mode_New        equ  1006


Main:

        movem.l d0-d7/a0-a6,-(sp)       ;save all registers

        movea.l ExecBase,a6             ;load exec base address
        lea     DosName(pc),a1
        moveq   #0,d0
        jsr     OpenLib(a6)             ;open dos.library
        move.l  d0,DosBase
        beq     DosErr
        
        move.l  #Window,d1              ;open CLI
        move.l  #Mode_Old,d2
        bsr     OpenFile                        
        move.l  d0,CliHandle
        beq     CliErr

        move.l  #Welcome,d2             ;print Welcome message
        move.l  #Welcomeend-Welcome,d3
        bsr     WriteCli                ;write it


First   move.l  #Text1,d2               ;first file request message
        move.l  #Text1end-Text1,d3
        bsr     WriteCli                ;and write it

        move.l  #160,d3
        move.l  #OutBuff,d2
        bsr     ReadCli                 ;get first file name
        move.l  d0,FFLen                ;length of first file
        cmp     #1,d0
        beq     Exit                    ;if zero, then exit
   
        lea     OutBuff,a0
        clr.b   -1(a0,d0)               ;change last byte to zero
        move.l  #OutBuff,d1             ;open first file
        move.l  #Mode_Old,d2
        bsr     OpenFile                        
        move.l  d0,FirstHand
        beq     NoFile1

Second  move.l  #Text2,d2               ;second file request message
        move.l  #Text2end-Text2,d3
        bsr     WriteCli                ;and write it

        move.l  #160,d3
        move.l  #OutBuff,d2
        bsr     ReadCli                 ;get second file name
        cmp     #1,d0
        beq     NoFile2                 ;if no file, then exit
        lea     OutBuff,a0
        clr.b   -1(a0,d0)               ;change last byte to zero

        move.l  #OutBuff,d1             ;try to open second file as old
        move.l  #Mode_Old,d2            
        bsr     OpenFile                ;if can, then it exists    
        move.l  d0,SecHand              
        bne     IsFile2                 ;just as well we checked!
        cmp     #1,d0
        beq     NoFile2                 ;no filename selected, so exit
DoIt
        move.l  #OutBuff,d1             ;open second file
        move.l  #Mode_New,d2
        bsr     OpenFile                        
        move.l  d0,SecHand
        beq     IllegalFile2            ;must be illegal file name        




;++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;+++++++++++++        WORK ROUTINE      +++++++++++++++++
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Work
        move.l  DosBase,a6                
        move.l  #InBuff,d2              ;where to read the data into
        move.l  FirstHand,d1            ;file to read from
        move.l  #256,d3                 ;size of buffer
        jsr     Read(a6)                ;and read them in
        move.l  d0,InBuffLen            ;save # of bytes read
        beq     Exit2                   ;0 bytes read
        
        lea     InBuff,a1               ;a1 will point to input buffer
        clr.b   0(a1,d0)                ;add zero        
        lea     OutBuff,a2              ;point a2 to output
        move.l  d0,d6                   ;# of BYTES read into a6 counter
        roxr    #1,d6                   ;halve it for # of WORDS
        addq    #1,d6                   ;and add 1
        move.l  #0,a5                   ;and a5 for output
Process
        move    (a1)+,d1                ;get word from buffer
        move.b  d1,d2                   ;save lower (second) byte
        roxr    #8,d1                   ;get first byte in d1
        cmp.b   #0,d1                   ;is it zero?
        beq     Done
        cmp.b   #10,d1                  ;is it LF?
        bne     OK1                     ;no
        move.b  #13,(a2)+               ;else put CR in
        adda    #1,a5                   ;and increment counter
OK1
        move.b  d1,(a2)+                ;and put in buffer
        adda    #1,a5                   ;keep a5 up to date
        cmp.b   #0,d2                   ;as is ASCII, only get 0 if done
        beq     Done
        cmp.b   #10,d2                  ;is it LF?
        bne     OK                      ;no
        move.b  #13,(a2)+               ;else put CR in
        adda    #1,a5
OK
        move.b  d2,(a2)+                ;and put in buffer
        adda    #1,a5
        dbra    d6,Process              ;check if finished
Done 
        move.l  DosBase,a6                
        move.l  #OutBuff,d2             ;where to read the data from
        move.l  SecHand,d1              ;file to write to
        move.l  a5,d3                   ;size of output buffer
        jsr     Write(a6)               ;and write them out
        cmp.l   #256,InBuffLen          ;see if complete file read yet
        beq     Work
        
 
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;+++++++++++++++   END OF MAIN ROUTINE   ++++++++++++++++
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Exit2                                   ;normal exit point
        move.l  SecHand,d1              ;close the second file  
        move.l  DosBase,a6
        jsr     Close(a6)
NoFile2
        move.l  FirstHand,d1            ;close the first file  
        move.l  DosBase,a6
        jsr     Close(a6)
Exit
        move.l  CliHandle,d1            ;normal exit point  
        move.l  DosBase,a6
        jsr     Close(a6)
CliErr
        move.l  DosBase,a1              ;can't open CLI
        move.l  ExecBase,a6
        jsr     CloseLib(a6)
DosErr
        movem.l (sp)+,d0-d7/a0-a6
        rts                             ;no dos.library!!  


;******************************************************************
;*******************    SUBROUTINES    ****************************
;******************************************************************


WriteCli
        move.l  CliHandle,d1
        move.l  DosBase,a6
        jsr     Write(a6)
        rts

ReadCli
        move.l  DosBase,a6
        move.l  CliHandle,d1
        jsr     Read(a6)
        rts

OpenFile
        move.l  DosBase,a6
        jsr     Open(a6)
        rts

IsFile2
        move.l  SecHand,d1              ;close the second file  
        move.l  DosBase,a6
        jsr     Close(a6)
        move.l  #Text4,d2
        move.l  #Text4end-Text4,d3      ;output warning
        bsr     WriteCli
        move.l  #3,d3
        move.l  #InBuff,d2
        bsr     ReadCli                 ;get answer
        cmp.b   #"Y",InBuff             ;check uppercase
        beq     DoIt                     
        cmp.b   #"y",InBuff             ;and lower case
        beq     DoIt
        bra     Second                  ;otherwise, get new name
                                        
NoFile1
        move.l  #Text3,d2               ;first file request message
        move.l  #Text3end-Text3,d3
        bsr     WriteCli                ;and write it
        bra     First

IllegalFile2
        move.l  #Text5,d2
        move.l  #Text5end-Text5,d3      ;output warning
        bsr     WriteCli
        bra     Second                  ;and resume

;******************************************************************
;*************************   DATA    ******************************
;******************************************************************
        even   

FFLen      ds.l 1               ;length of first file

DosBase    ds.l 1               ;pointer to dos.lib base

CliHandle  ds.l 1               ;pointer to console

FirstHand  ds.l 1               ;first file handle

SecHand    ds.l 1               ;second file handle

InBuffLen  ds.l 1               ;actual length read into input buffer

DosName    dc.b 'dos.library',0

Window     dc.b 'CON:0/0/639/192/Lf-CrLf',0
           even

InBuff     ds.w 129             ;text buffer - max size = 128 bytes + spare

OutBuff                         ;a/a, but twice the size -  reuse this
                                ;data area as output buffer

Welcome    dc.b $9b,"31",$43              ;move over 31 chr's
           dc.b $9b,"4;33;40m"            ;set mode red & underline
           dc.b "Lf-CrLf",10,10,0         ;message
           dc.b $9b,"22",$43              ;move over 22 chr's
           dc.b $9b,"0;33;40m"            ;set mode red with no underline
           dc.b "Version 1.0   19 May 1991",10,10
           dc.b $9b,"0;32;40m"            ;set mode black
           dc.b $9b,"28",$43              ;move over 28 chr's
           dc.b "By Randy Quick",10,10
           dc.b "This programme converts the standard Amiga LF (end of "
           dc.b "line) characters to",10
           dc.b "CR LF which is used on IBM machines.  Files using only " 
           dc.b "LF characters cannot",10
           dc.b "be successfully printed on IBM machines / printers.",10
           dc.b "To quit, press "
           dc.b $9b,"4;33;40m"            ;set mode red 
           dc.b "<RETURN>"
           dc.b $9b,"0;32;40m"            ;set mode black
           dc.b " without a filename.",10
           dc.b $9b,"0;31;40m"            ;restore normal
Welcomeend

Text1      dc.b 10,'What is the Input file name ? ',0
Text1end

Text2      dc.b 10,'What is the Output file name ? ',0
Text2end

Text3      dc.b $9b,"0;33;40m"    ;set mode red with no underline
           dc.b 10,"Sorry, can't find that file  ...  try again ( <RETURN>"
           dc.b " exits )",10,0
           dc.b $9b,"0;31;40m"    ;restore normal
Text3end

Text4      dc.b $9b,"0;33;40m"    ;set mode red with no underline
           dc.b 10,"That filename already exists!!   Do you wish to "
           dc.b "clobber it  (Y/N) ? ",0
           dc.b $9b,"0;31;40m"    ;restore normal
Text4end

Text5      dc.b $9b,"0;33;40m"    ;set mode red with no underline
           dc.b 10,'I cannot open that file!  Please select another '
           dc.b 'name.',10,0 
           dc.b $9b,"0;31;40m"    ;restore normal
Text5end

           end

