home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / assembler / thesource / volume2 / source / system / asmmacros.lha / AsmMacDemo.src < prev    next >
Encoding:
Text File  |  1990-11-16  |  6.7 KB  |  219 lines

  1. *
  2. *    TextFilter                                        Rev: 23 Jan 88
  3. *
  4. *    Program to filter out non-text bytes from a file
  5. *
  6. *    This program will filter out everything except printable ASCII or CR,
  7. *     LF, FF, HT, or VT.
  8. *
  9.      NOPAGE
  10.  
  11.      INCLUDE "AsMac:CoreMacros.i"
  12.  
  13.      INCLUDE "AsMac:AllocMem.i"
  14.      INCLUDE "AsMac:Close.i"
  15.      INCLUDE "AsMac:DeleteFile.i"
  16.      INCLUDE "AsMac:Exist.i"
  17.      INCLUDE "AsMac:FreeMem.i"
  18.      INCLUDE "AsMac:Open.i"
  19.      INCLUDE "AsMac:Read.i"
  20.      INCLUDE "AsMac:Write.i"
  21.  
  22.  
  23.      DefineSections TextFilter
  24.  
  25. BufferSize     EQU  10240
  26.  
  27.  
  28. TextFilter:
  29.      OpenLib exec.library     ; Open the exec library.
  30.      BEQ TextFilter_19
  31.      OpenLib dos.library      ; Open the dos library.
  32.      BEQ TextFilter_18
  33.                               ; Open the intuition library.
  34.      OpenLib intuition.library
  35.      BEQ TextFilter_17
  36.                               ; Allocate memory for the source and
  37.                               ;    destination buffers.
  38.      AllocMem SourceBuf,D,BufferSize
  39.      BEQ TextFilter_16
  40.      AllocMem DestBuf,D,BufferSize
  41.      BEQ TextFilter_15
  42.                               ; Open a window to ask for file names.
  43.      Open I,AskFNSpec,AskFileName,new
  44.      BEQ TextFilter_14
  45. TextFilter_1:                 ; Ask for the filename for the SOURCE file.
  46.      Write D,AskSourceName,AskFileName
  47.      BEQ TextFilter_13
  48.                               ; Get the response.
  49.      Read AskFileName,D,Response
  50.      BEQ TextFilter_13
  51.      SUBQ.L #1,Response.Len   ; Adjust response length to get rid of the LF.
  52.  
  53.      MOVE.L Response.Len,D0   ; IF no file name was given,
  54.      BEQ TextFilter_13        ; THEN pass to TextFilter_13 to abort.
  55.                               ; ENDIF
  56.  
  57.      MOVEA.L #SourceName,A1   ; Put the source file name string at
  58.      MOVEA.L #Response,A0     ;    SourceName with null termination.
  59. TextFilter_2:
  60.      MOVE.B (A0)+,(A1)+
  61.      SUBQ.L #1,D0
  62.      BNE.S TextFilter_2
  63.      MOVE.B #0,(A1)+          ;    (Terminate it with an ASCII null.)
  64.  
  65.      Exist I,SourceName       ; Find out if the file exists.
  66.      BNE.S TextFilter_3       ; IF it does not exist,
  67.                               ; THEN tell the operator that the file does
  68.                               ;    not exist.
  69.      Write D,FileNotExist,AskFileName
  70.      BRA TextFilter_1         ;    Loop back to TextFilter_1 to get the
  71.                               ;    correct file name.
  72. TextFilter_3:                 ; ENDIF
  73.  
  74.                               ; Ask for the filename for the DESTINATION
  75.                               ;    file.
  76.      Write D,AskDestName,AskFileName
  77.      BEQ TextFilter_13
  78.                               ; Get the response.
  79.      Read AskFileName,D,Response
  80.      BEQ TextFilter_13
  81.      SUBQ.L #1,Response.Len   ; Adjust response length to get rid of the LF.
  82.  
  83.      MOVE.L Response.Len,D0   ; IF no file name was given,
  84.      BEQ TextFilter_13        ; THEN pass to TextFilter_13 to abort.
  85.                               ; ENDIF
  86.  
  87.      MOVEA.L #DestName,A1     ; Put the destination file name string at
  88.      MOVEA.L #Response,A0     ;    DestName with null termination.
  89. TextFilter_4:
  90.      MOVE.B (A0)+,(A1)+
  91.      SUBQ.L #1,D0
  92.      BNE.S TextFilter_4
  93.      MOVE.B #0,(A1)+          ;    (Terminate it with an ASCII null.)
  94.  
  95.      Exist I,DestName         ; Find out if the file exists.
  96.  
  97.      BEQ.S TextFilter_5       ; IF it does exist,
  98.      DeleteFile I,DestName    ; THEN delete it.
  99.      BEQ TextFilter_13
  100. TextFilter_5:                 ; ENDIF
  101.  
  102.                               ; Open the SOURCE and DESTINATION files.
  103.      Open I,SourceName,SourceFile,read
  104.      BEQ TextFilter_13
  105.      Open I,DestName,DestFile,new
  106.      BEQ TextFilter_12
  107. TextFilter_6:                 ; REPEAT (Start of REPEAT UNTIL)
  108.  
  109.                               ;> Read the SOURCE file into the SourceBuffer.
  110.      Read SourceFile,I,SourceBuf
  111.      BEQ TextFilter_11
  112.      TST.L D0                 ;>> IF all bytes have been read from the file,
  113.      BEQ.S TextFilter_10      ;>> THEN pass to TextFilter_10 to break out of
  114.                               ;>>    the REPEAT UNTIL loop.
  115.                               ;>> ENDIF
  116.      MOVE.L SourceBuf.Adr,A0  ;> Copy from the source buffer to the
  117.                               ;>    destination buffer. Filter out non-text
  118.      MOVE.L DestBuf.Adr,A3    ;>    bytes as it is copied.
  119.      CLR.L D3
  120. TextFilter_7:                 ;>    REPEAT (Start of REPEAT UNTIL)
  121.      MOVE.B (A0)+,D2          ;>>    Get the byte from the source buffer.
  122.      BLE.S TextFilter_9       ;>>    IF the byte is printable ASCII or CR,
  123.      CMPI.B #$7F,D2           ;>>       LF, FF, HT, or VT,
  124.      BEQ.S TextFilter_9
  125.      CMPI.B #$20,D2
  126.      BGE.S TextFilter_8
  127.      CMPI.B #$08,D2
  128.      BLE.S TextFilter_9
  129.      CMPI.B #$0E,D2
  130.      BGT.S TextFilter_9
  131. TextFilter_8:
  132.      MOVE.B D2,(A3)+          ;>>    THEN copy it to the destination buffer.
  133.      ADDQ.L #1,D3
  134. TextFilter_9:                 ;>>    ENDIF
  135.      SUBQ.L #1,D0             ;>    UNTIL all bytes have been read from the
  136.      BNE TextFilter_7         ;>       source buffer.
  137.  
  138.                               ;> Write from the DestBuffer to the DESTINATION
  139.                               ;>  file.
  140.      Write I,DestBuf,DestFile,D3
  141.      BEQ TextFilter_11
  142.      BRA TextFilter_6         ;> Loop back to TextFilter_6 to get the rest
  143.                               ;>  of the SOURCE file.
  144.  
  145. TextFilter_10:                ; UNTIL done copying data from the source to
  146.                               ;    the destination file.
  147.  
  148. TextFilter_11:                ; Close the SOURCE and DESTINATION files.
  149.      Close DestFile
  150. TextFilter_12:
  151.      Close SourceFile
  152. TextFilter_13:                ; Close the AskFileName window.
  153.      Close AskFileName
  154. TextFilter_14:                ; De-allocate memory.
  155.      FreeMem DestBuf
  156. TextFilter_15:
  157.      FreeMem SourceBuf
  158. TextFilter_16:                ; Close the intuition library.
  159.      CloseLib intuition.library
  160. TextFilter_17:
  161.      CloseLib dos.library     ; Close the dos library.
  162. TextFilter_18:
  163.      CloseLib exec.library    ; Close the exec library.
  164. TextFilter_19:
  165.      CLR.L D0                 ; Exit the program with return code of zero.
  166.      RTS
  167.  
  168.  
  169.  
  170.  
  171. *----------------------------------------------------------------------------
  172.  
  173. LF   SET $0A
  174.  
  175.      SECTION   DataSection,DATA
  176.  
  177. AskFNSpec:
  178.  
  179.   DC.B 'CON:40/15/600/52/Ask_File_Names',0
  180.  
  181. AskFNSpecEnd:
  182.  
  183. AskSourceName:
  184.  
  185.   DC.B LF,' Specify the source filename:      '
  186.  
  187. AskSourceNameEnd:
  188.  
  189. AskDestName:
  190.  
  191.   DC.B LF,' Specify the destination filename: '
  192.  
  193. AskDestNameEnd:
  194.  
  195. FileNotExist:
  196.  
  197.   DC.B LF,LF,' ERROR: The specified file does not exist.',LF,LF
  198.  
  199. FileNotExistEnd:
  200.  
  201.  
  202. *----------------------------------------------------------------------------
  203.  
  204.      SECTION   BSS_Section,BSS
  205.  
  206. Response:
  207.      DS.B 80
  208. ResponseEnd:
  209.      CNOP 0,2
  210.  
  211. SourceName:
  212.      DS.B 80
  213.  
  214. DestName:
  215.      DS.B 80
  216.  
  217.  
  218.      END
  219.