Implemented a program that reads command line arguments and performs find/replace on files.
This program would perform the find and replace function on the specified input file. Unless the same file is entered as both the input and output files, the input file will never be modified. The results of the find/replace would be printed in the specified output file (Note: output file would be overwritten if the file is not empty). To use this program, users must enter a command line using the required flags (see below) to indicate the search replacement texts. The command should also state the input file and output file. Despite that, there are optional flags that individuals can use to modify the replacement process. For example, users can use the l flag to indicate which lines the find/replace function should be performed and the w flag to perform prefix/suffix replacement (replace all words with the specified prefix/suffix).Flags:
- -s [string: search text] (required) (ex. -s world)
- -r [string: replacement test] (required) (ex. -r Hello)
- -l [int: start, int: end] (optional) (ex. -l 1,2 => only perform the find/replace function between lines 1 and 2 inclusive)
- -w (optional) (no argument, but search text must be entered as *world for suffix find/replace or world* for prefix find/replace)
Note/Warning:
- The input order doesn't matter for the flags listed above.
- The input and output files must be entered as the last two command line arguments, with input coming before output
- Due to certain reasons, the prefix/suffix find/replace function doesn't support any words that start with a space or punctuation
Error Codes - When the input command line is invalid, the program returns an error code in the following order of precedence:
- MISSING_ARGUMENT (code 1 - when the input command line is less than seven arguments)
- DUPLICATE_ARGUMENT (code 8 - when a flag is entered twice)
- INPUT_FILE_MISSING (code 2 - when the indicated input file couldn't be opened or doesn't exist)
- OUTPUT_FILE_UNWRITABLE (code 3 - when the indicated output file is not writable)
- S_ARGUMENT_MISSING (code 4 - when the s flag or s flag argument is missing)
- R_ARGUMENT_MISSING (code 5 - when the r flag or r flag argument is missing)
- L_ARGUMENT_INVALID (code 6 - when the l flag argument is invalid)
- WILDCARD_INVALID (code 7 - when the w flag is entered but the search text indicated by the s flag is invalid)
- Windows or Linux operating system
- GNU Compiler Collection (GCC)
- Text Editor or IDEs
- Notepad (Worst Case)
- Code::Blocks (Please download the one with GCC compiler)
- Eclipse
- Visual Studio Code
- Fork this repository by clicking the Fork button located on the top of the page
- Navigate to "Your Repositories" on GitHub and open the forked repo
- In the terminal, git clone the repo using the URL or SSH key
make
and make test
to run the test cases written in unit_tests.c. Individuals may use the existing code as an example and write their test cases.
Note: If including your own test cases, you must include your input file in tests.in.orig and expected output file in tests.out.exp. In the terminal, type
gcc [source file]
. By default, this will produce an executable file named a.out (Linux) or a.exe (Windows). If you want the executable file to have a custom name, type gcc -o [name of executable] [source file]
. After compiling the program, run the executable file by entering ./[executable file] [command line arguments for running the program]
.
Zhen Wei Liao - @ZhenBuilt the main program and parts of FindReplace.h/unit_tests.c This project is under the MIT License.
Please check the LICENSE.md file for more information.