aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordam <dam@gudinoff>2022-11-06 02:14:51 +0000
committerdam <dam@gudinoff>2022-11-06 02:14:51 +0000
commit75f29c8bb6d9fd2cde8ebc6bc9a50c6af436794e (patch)
treec7e891fff5642b2414b98157a2e91af72042fecf
parentfa00ffed8da48f720323aea1601898ed2abb76e6 (diff)
downloadtask-time-tracker-75f29c8bb6d9fd2cde8ebc6bc9a50c6af436794e.tar.zst
task-time-tracker-75f29c8bb6d9fd2cde8ebc6bc9a50c6af436794e.zip
Added is_action_detected to simplify argument detection.
-rw-r--r--main.c53
1 files changed, 17 insertions, 36 deletions
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;
}