*
*    TextFilter                                        Rev: 23 Jan 88
*
*    Program to filter out non-text bytes from a file
*
*    This program will filter out everything except printable ASCII or CR,
*     LF, FF, HT, or VT.
*
     NOPAGE

     INCLUDE "AsMac:CoreMacros.i"

     INCLUDE "AsMac:AllocMem.i"
     INCLUDE "AsMac:Close.i"
     INCLUDE "AsMac:DeleteFile.i"
     INCLUDE "AsMac:Exist.i"
     INCLUDE "AsMac:FreeMem.i"
     INCLUDE "AsMac:Open.i"
     INCLUDE "AsMac:Read.i"
     INCLUDE "AsMac:Write.i"


     DefineSections TextFilter

BufferSize     EQU  10240


TextFilter:
     OpenLib exec.library     ; Open the exec library.
     BEQ TextFilter_19
     OpenLib dos.library      ; Open the dos library.
     BEQ TextFilter_18
                              ; Open the intuition library.
     OpenLib intuition.library
     BEQ TextFilter_17
                              ; Allocate memory for the source and
                              ;    destination buffers.
     AllocMem SourceBuf,D,BufferSize
     BEQ TextFilter_16
     AllocMem DestBuf,D,BufferSize
     BEQ TextFilter_15
                              ; Open a window to ask for file names.
     Open I,AskFNSpec,AskFileName,new
     BEQ TextFilter_14
TextFilter_1:                 ; Ask for the filename for the SOURCE file.
     Write D,AskSourceName,AskFileName
     BEQ TextFilter_13
                              ; Get the response.
     Read AskFileName,D,Response
     BEQ TextFilter_13
     SUBQ.L #1,Response.Len   ; Adjust response length to get rid of the LF.

     MOVE.L Response.Len,D0   ; IF no file name was given,
     BEQ TextFilter_13        ; THEN pass to TextFilter_13 to abort.
                              ; ENDIF

     MOVEA.L #SourceName,A1   ; Put the source file name string at
     MOVEA.L #Response,A0     ;    SourceName with null termination.
TextFilter_2:
     MOVE.B (A0)+,(A1)+
     SUBQ.L #1,D0
     BNE.S TextFilter_2
     MOVE.B #0,(A1)+          ;    (Terminate it with an ASCII null.)

     Exist I,SourceName       ; Find out if the file exists.
     BNE.S TextFilter_3       ; IF it does not exist,
                              ; THEN tell the operator that the file does
                              ;    not exist.
     Write D,FileNotExist,AskFileName
     BRA TextFilter_1         ;    Loop back to TextFilter_1 to get the
                              ;    correct file name.
TextFilter_3:                 ; ENDIF

                              ; Ask for the filename for the DESTINATION
                              ;    file.
     Write D,AskDestName,AskFileName
     BEQ TextFilter_13
                              ; Get the response.
     Read AskFileName,D,Response
     BEQ TextFilter_13
     SUBQ.L #1,Response.Len   ; Adjust response length to get rid of the LF.

     MOVE.L Response.Len,D0   ; IF no file name was given,
     BEQ TextFilter_13        ; THEN pass to TextFilter_13 to abort.
                              ; ENDIF

     MOVEA.L #DestName,A1     ; Put the destination file name string at
     MOVEA.L #Response,A0     ;    DestName with null termination.
TextFilter_4:
     MOVE.B (A0)+,(A1)+
     SUBQ.L #1,D0
     BNE.S TextFilter_4
     MOVE.B #0,(A1)+          ;    (Terminate it with an ASCII null.)

     Exist I,DestName         ; Find out if the file exists.

     BEQ.S TextFilter_5       ; IF it does exist,
     DeleteFile I,DestName    ; THEN delete it.
     BEQ TextFilter_13
TextFilter_5:                 ; ENDIF

                              ; Open the SOURCE and DESTINATION files.
     Open I,SourceName,SourceFile,read
     BEQ TextFilter_13
     Open I,DestName,DestFile,new
     BEQ TextFilter_12
TextFilter_6:                 ; REPEAT (Start of REPEAT UNTIL)

                              ;> Read the SOURCE file into the SourceBuffer.
     Read SourceFile,I,SourceBuf
     BEQ TextFilter_11
     TST.L D0                 ;>> IF all bytes have been read from the file,
     BEQ.S TextFilter_10      ;>> THEN pass to TextFilter_10 to break out of
                              ;>>    the REPEAT UNTIL loop.
                              ;>> ENDIF
     MOVE.L SourceBuf.Adr,A0  ;> Copy from the source buffer to the
                              ;>    destination buffer. Filter out non-text
     MOVE.L DestBuf.Adr,A3    ;>    bytes as it is copied.
     CLR.L D3
TextFilter_7:                 ;>    REPEAT (Start of REPEAT UNTIL)
     MOVE.B (A0)+,D2          ;>>    Get the byte from the source buffer.
     BLE.S TextFilter_9       ;>>    IF the byte is printable ASCII or CR,
     CMPI.B #$7F,D2           ;>>       LF, FF, HT, or VT,
     BEQ.S TextFilter_9
     CMPI.B #$20,D2
     BGE.S TextFilter_8
     CMPI.B #$08,D2
     BLE.S TextFilter_9
     CMPI.B #$0E,D2
     BGT.S TextFilter_9
TextFilter_8:
     MOVE.B D2,(A3)+          ;>>    THEN copy it to the destination buffer.
     ADDQ.L #1,D3
TextFilter_9:                 ;>>    ENDIF
     SUBQ.L #1,D0             ;>    UNTIL all bytes have been read from the
     BNE TextFilter_7         ;>       source buffer.

                              ;> Write from the DestBuffer to the DESTINATION
                              ;>  file.
     Write I,DestBuf,DestFile,D3
     BEQ TextFilter_11
     BRA TextFilter_6         ;> Loop back to TextFilter_6 to get the rest
                              ;>  of the SOURCE file.

TextFilter_10:                ; UNTIL done copying data from the source to
                              ;    the destination file.

TextFilter_11:                ; Close the SOURCE and DESTINATION files.
     Close DestFile
TextFilter_12:
     Close SourceFile
TextFilter_13:                ; Close the AskFileName window.
     Close AskFileName
TextFilter_14:                ; De-allocate memory.
     FreeMem DestBuf
TextFilter_15:
     FreeMem SourceBuf
TextFilter_16:                ; Close the intuition library.
     CloseLib intuition.library
TextFilter_17:
     CloseLib dos.library     ; Close the dos library.
TextFilter_18:
     CloseLib exec.library    ; Close the exec library.
TextFilter_19:
     CLR.L D0                 ; Exit the program with return code of zero.
     RTS




*----------------------------------------------------------------------------

LF   SET $0A

     SECTION   DataSection,DATA

AskFNSpec:

  DC.B 'CON:40/15/600/52/Ask_File_Names',0

AskFNSpecEnd:

AskSourceName:

  DC.B LF,' Specify the source filename:      '

AskSourceNameEnd:

AskDestName:

  DC.B LF,' Specify the destination filename: '

AskDestNameEnd:

FileNotExist:

  DC.B LF,LF,'[33m ERROR: The specified file does not exist.[0m',LF,LF

FileNotExistEnd:


*----------------------------------------------------------------------------

     SECTION   BSS_Section,BSS

Response:
     DS.B 80
ResponseEnd:
     CNOP 0,2

SourceName:
     DS.B 80

DestName:
     DS.B 80


     END
