home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / filter.seq < prev    next >
Text File  |  1990-07-24  |  4KB  |  91 lines

  1. \\ FILTER.SEQ           A simple file filter program    by Tom Zimmer
  2.  
  3.   This file illustrates how to make a program that processes all of the
  4. characters in a file, and writes them back out to another file. The program
  5. FILTER.COM is used as follows:
  6.  
  7.                 C>FILTER <myin_file >myout_file [Enter]
  8.  
  9. "myin_file" contains the characters to be processed, and "myout_file" will
  10. hold the characters after processing.
  11.  
  12.   If you simply load this file, you will compile the simple and SLOW
  13. version of FILTER, it will proccess all characters of a file, performing
  14. a DOS file read and a DOS file write call for each character processed.
  15. This works just fine, but is quite slow. A second example which is
  16. commented out, uses buffered I/O, and processes text MANY time faster.
  17.  
  18.  
  19. {
  20.  
  21. : main          ( -- )
  22.                 raw_stdin
  23.                 begin   getchar dup  0<
  24.                                 over ^Z = or 0=
  25.                 while   putchar
  26.                 repeat  drop ;
  27.  
  28. }
  29.  
  30. ***************************************************************************
  31.  
  32.   The FAST version of FILTER, for use with FILES ONLY.
  33.  
  34.   Don't use this one with keyboard input, as you will have to type
  35. 2000 characters before it will process the first character entered.
  36.  
  37. : main          ( -- )
  38.                 raw_stdin
  39.                 2000 =: gch.max         \ 2k input buffer
  40.                 2000 =: pch.max         \ 2k output buffer
  41.                 bufio_init              \ initialize buffered I/O
  42.                 begin   getchar_b dup  0<
  43.                                   over ^Z = or 0=
  44.                 while   putchar_b
  45.                 repeat  drop
  46.                 flush_b ;               \ flush any un-written characters
  47.  
  48. ***************************************************************************
  49.  
  50.   Here is another version of FILTER, it is more complex, but also more
  51. flexible. This version uses a table to handle control characters, which
  52. makes it faster. Extra processing can easily be added since you need
  53. only plug the function into the table and recompile it.
  54.  
  55. : do_exit       ( -- )
  56.                 flush_b ABORT ;
  57.  
  58. : do_cr         ( -- )
  59.                 $0D putchar_b
  60.                 $0A putchar_b ;
  61.  
  62. : do_tab        ( -- )
  63.                 8 0 do $20 putchar_b loop ;
  64.  
  65. : do_ctrl       ( c1 --- )              \ handle control characters
  66.                 exec:
  67. \               -1 end of file
  68.                 do_exit
  69. \               0 null  1 a     2 b     3 c     4 d     5 e     6 f
  70.                 noop    noop    noop    noop    noop    noop    noop
  71. \               7 g     8 h     9 i     LF      11 k    12 l    Enter
  72.                 noop    noop    do_tab  noop    noop    noop    do_cr
  73. \               14 n    15 o    16 p    17 q    18 r    19 s    20 t
  74.                 noop    noop    noop    noop    noop    noop    noop
  75. \               21 u    22 v    23 w    24 x    25 y    26 z    Esc
  76.                 noop    noop    noop    noop    noop    do_exit noop
  77. \               28 \    29 ]    30 ^    31 _
  78.                 noop    noop    noop    noop ;
  79.  
  80. : main          ( -- )
  81.                 raw_stdin
  82.                 2000 =: gch.max         \ 2k input buffer
  83.                 2000 =: pch.max         \ 2k output buffer
  84.                 bufio_init              \ initialize buffered I/O
  85.                 begin   getchar_b dup $20 <
  86.                         if      1+ do_ctrl
  87.                         else    putchar_b
  88.                         then
  89.                 again   ;
  90.  
  91.