diff options
| author | akiyamn | 2020-09-10 18:48:02 +1000 |
|---|---|---|
| committer | akiyamn | 2020-09-10 18:52:13 +1000 |
| commit | 5d66cc376fd5c96fe4357a8f9eca3b5727a6eb9f (patch) | |
| tree | e75c68d48057214f5a290825c8276df20ecaa5bd | |
| parent | 03e396142909f633ddc201e7c948bfc71319a0d2 (diff) | |
| download | fit2100_ass1-1.0.0-rc2.tar.gz fit2100_ass1-1.0.0-rc2.zip | |
Refactored arg checking and fixed source missing bug1.0.0-rc2
| -rw-r--r-- | fileutil.c | 49 | ||||
| -rw-r--r-- | fileutil.h | 1 |
2 files changed, 28 insertions, 22 deletions
@@ -73,7 +73,7 @@ bool is_a_path(char* string) { } /* - * Copies a file given the path `file_path`, into the directory of path string `dir_path` + * Copies or moves a file given the path `file_path`, into the directory of path string `dir_path` * Moves the file rather than copies it (i.e. remove the original file) if `move` is true * Overwrites the target file if it exists if `overwrite` is true */ @@ -98,6 +98,29 @@ void copy_into_dir(char *file_path, char *dir_path, bool move, bool overwrite){ free(new_path); } +void validate_arguments(arguments_t *args, int argc, char **argv) { + // Determine if the source path is either not provided or the first argument + bool valid_source = is_a_path(argv[args->src_index]); + + // Is a valid combination of flags provided? + bool correct_flags = (args->move_flag || args->force_flag) ? args->dir_flag : true; + + // Is the -d tag followed by a path? + bool correct_directory = false; + if (args->dir_index < argc) { + correct_directory = (args->dir_flag) ? is_a_path(argv[args->dir_index]) : true; + } + + // Combine all conditions for a valid set of arguments + args->valid = + valid_source && + correct_flags && + correct_directory && + (argc >= MIN_NUM_OF_ARGS) && + (argc <= MAX_NUM_OF_ARGS); +} + + /* * Given argc and argv (number of program arguments and the program arguments) provided from main, * create an `arguments_t` type struct containing all of the information about the arguments. @@ -125,28 +148,13 @@ arguments_t parse_arguments(int argc, char **argv){ args.force_flag = true; } } + args.src_index = (argc > 1) ? 1 : -1; - // Determine if the source path is either not provided or the first argument - if (argc > 1 && is_a_path(argv[1])) args.src_index = 1; - - // Is a valid combination of flags provided? - bool correct_flags = (args.move_flag || args.force_flag) ? args.dir_flag : true; - - // Is the -d tag followed by a path? - bool correct_directory = false; - if (args.dir_index < argc) { - correct_directory = (args.dir_flag) ? is_a_path(argv[args.dir_index]) : true; - } - - // Combine all conditions for a valid set of arguments - args.valid = - correct_flags && - correct_directory && - (argc >= MIN_NUM_OF_ARGS) && - (argc <= MAX_NUM_OF_ARGS); + validate_arguments(&args, argc, argv); return args; } + /* * Given an arguments_t struct of the program's arguments, execute the correct suprogram * (i.e. move, copy or print to stdout) @@ -169,9 +177,6 @@ void execute_subprogram(arguments_t args, char *argv[]) { } else { print_file(source_path); // Print a file } - - - } // Main function of the whole program @@ -25,6 +25,7 @@ char* get_filename(char *full_path); bool is_a_path(char* string); void copy_into_dir(char *file_path, char *dir_path, bool move, bool overwrite); arguments_t parse_arguments(int argc, char **argv); +void validate_arguments(arguments_t *args, int argc, char **argv); void execute_subprogram(arguments_t args, char *argv[]); int main(int argc, char *argv[]); |
