From 75f29c8bb6d9fd2cde8ebc6bc9a50c6af436794e Mon Sep 17 00:00:00 2001 From: dam Date: Sun, 6 Nov 2022 02:14:51 +0000 Subject: Added is_action_detected to simplify argument detection. --- main.c | 53 +++++++++++++++++------------------------------------ 1 file changed, 17 insertions(+), 36 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 214d61c..1c0356f 100644 --- a/main.c +++ b/main.c @@ -1116,6 +1116,16 @@ void exit_gracefully(int signal) { ungetch('q'); } +// Returns true if any of the action parameters is equal to arg, false otherwise. +bool is_action_detected(const char *arg, const char *action_name, const char *action_abbreviation) { + assert(arg != NULL); + assert(action_name != NULL); + assert(action_abbreviation != NULL); + + return strncmp(arg, action_name, strlen(action_name)+1) == 0 + || strncmp(arg, action_abbreviation, strlen(action_abbreviation)+1) == 0; +} + int main(int argc, char *argv[]) { if (initialize_app_folder() == false) { @@ -1127,26 +1137,17 @@ int main(int argc, char *argv[]) { reset_database(&archive); if (is_file_accessible(db_file_path) == false) { - store_database(&database, db_file_path); + store_database(&database, db_file_path); // TODO Check for error. } if (is_file_accessible(ar_file_path) == false) { - export_to_csv(&archive, ar_file_path); + export_to_csv(&archive, ar_file_path); // TODO Check for error. } if (argc > 1) { - - char *action; - bool is_action = false; bool is_exit_requested = false; for (unsigned idx = 1; idx < argc; idx++) { - - is_action = false; - action = "-h"; - is_action |= strncmp(argv[idx], action, strlen(action)+1) == 0; - action = "--help"; - is_action |= strncmp(argv[idx], action, strlen(action)+1) == 0; - if (is_action) { + if (is_action_detected(argv[idx], "--help", "-h")) { // TODO Maybe rearrange the order of the command. fprintf(stdout, "Usage: ttt [OPTION]... [FILE]...\n" @@ -1187,23 +1188,13 @@ int main(int argc, char *argv[]) { return EXIT_SUCCESS; } - is_action = false; - action = "-v"; - is_action |= strncmp(argv[idx], action, strlen(action)+1) == 0; - action = "--version"; - is_action |= strncmp(argv[idx], action, strlen(action)+1) == 0; - if (is_action) { + if (is_action_detected(argv[idx], "--version", "-v")) { fprintf(stdout, "Task Time Tracker version " VERSION "\n"); free_memory(); return EXIT_SUCCESS; } - is_action = false; - action = "-i"; - is_action |= strncmp(argv[idx], action, strlen(action)+1) == 0; - action = "--import-csv"; - is_action |= strncmp(argv[idx], action, strlen(action)+1) == 0; - if (is_action) { + if (is_action_detected(argv[idx], "--import-csv", "-i")) { idx++; if (idx >= argc) { fprintf(stdout, "Missing CSV file path to import.\n"); @@ -1219,12 +1210,7 @@ int main(int argc, char *argv[]) { continue; } - is_action = false; - action = "-e"; - is_action |= strncmp(argv[idx], action, strlen(action)+1) == 0; - action = "--export-csv"; - is_action |= strncmp(argv[idx], action, strlen(action)+1) == 0; - if (is_action) { + if (is_action_detected(argv[idx], "--export-csv", "-e")) { idx++; if (idx >= argc) { fprintf(stdout, "Missing CSV file path to export.\n"); @@ -1239,12 +1225,7 @@ int main(int argc, char *argv[]) { continue; } - is_action = false; - action = "-n"; - is_action |= strncmp(argv[idx], action, strlen(action)+1) == 0; - action = "--no-autosave"; - is_action |= strncmp(argv[idx], action, strlen(action)+1) == 0; - if (is_action) { + if (is_action_detected(argv[idx], "--no-autosave", "-n")) { is_autosave_enabled = false; continue; } -- cgit v1.2.3