From 5d66cc376fd5c96fe4357a8f9eca3b5727a6eb9f Mon Sep 17 00:00:00 2001 From: akiyamn Date: Thu, 10 Sep 2020 18:48:02 +1000 Subject: Refactored arg checking and fixed source missing bug --- fileutil.c | 49 +++++++++++++++++++++++++++---------------------- fileutil.h | 1 + 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/fileutil.c b/fileutil.c index 6906a24..92eb81f 100644 --- a/fileutil.c +++ b/fileutil.c @@ -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 diff --git a/fileutil.h b/fileutil.h index 64e7aa3..f5783a0 100644 --- a/fileutil.h +++ b/fileutil.h @@ -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[]); -- cgit v1.2.3