This program will accept a byte pattern to look for, a second byte pattern to replace the first one with, and a series of file names upon which to perform the search and replace operations. Invoking the program with no parameters, or with an invalid parameter, will give a screen showing the proper calling syntax and the error.
The program is NOT user interactive, all its parameters are passed to it in the command line tail. The program may be invoked from .bat files and it returns the following values in ERRORLEVEL for use in IF tests after return (command-line syntax is described under USE):
ERRORLEVEL = 0 -> no error, normal program termination ERRORLEVEL = 1 -> program halted by user with ^X ERRORLEVEL = 2 -> improper command-line syntax ERRORLEVEL = 3 -> invalid/missing input pattern ERRORLEVEL = 4 -> invalid/missing output pattern ERRORLEVEL = 5 -> input file open/read/close error(s) ERRORLEVEL = 6 -> output file creation/write/close error(s) ERRORLEVEL = 7 -> both input and output file error(s)
The screen output of the program IS redirectable with the DOS redirection operators, i.e., > and >>. Thus, if you want no visible output you can redirect it to nul, e.g., filter (parameters) > nul will do its work with no screen output and you could then check the ERRORLEVEL returned by the program to generate your own error messages. The screen output could also be redirected to a file, e.g., filter (parameters) test > tmp, or to a device, e.g., filter (parameters) test > PRN:
Example .bat file
echo off filter (parameters) %1 > nul if not errorlevel 7 goto :6 echo Input and output file error(s) goto :quit :6 if not errorlevel 6 goto :5 echo Output file error(s) goto :quit :5 if not errorlevel 5 goto :4 echo Input file error(s) goto :quit :4 if not errorlevel 4 goto :3 echo Bad output pattern goto :quit :3 if not errorlevel 3 goto :2 echo Bad input pattern goto :quit :2 if not errorlevel 2 goto :1 echo Bad syntax goto :quit :1 if not errorlevel 1 goto :0 echo Program aborted by user goto :quit :0 echo No error :quit
The program employs the Boyer-Moore pattern matching algorithm. For more details on this and other useful algorithms see: "Algorithms", R. Sedgewick, 1984, Addison-Wesley Publishing Company, Inc., Reading, MA. See also: "Algorithms & Data Structures", N. Wirth, 1986, Prentice-Hall, Inc., Englewood Cliffs, NJ. I highly recommend both of these texts, and the Wirth text in particular for all you Modula-2 programmers.