commit 6a9fb1ade62dbddc16e1d551cc18773a9ea69494
parent 9bbea012c717ac4049c8c41e8b547fe963732c0d
Author: Pr0fiTX <uventik999@gmail.com>
Date: Fri, 25 Jul 2025 13:58:49 +0300
[ADD]: Ability to save ( append ) tasks into file
Diffstat:
4 files changed, 102 insertions(+), 13 deletions(-)
diff --git a/Ref/doto_create_flag_parser.c b/Ref/doto_create_flag_parser.c
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <unistd.h>
+
+/*
+ * h - help menu
+ * c NAME [DESCRIPTION]
+ */
+#define ARGUMENTS "hc:"
+
+struct c_arg {
+ char *title;
+ char *description;
+};
+
+int main(int argc, char *argv[]) {
+ int opt;
+
+ while ((opt = getopt(argc, argv, ARGUMENTS)) != -1) {
+ switch (opt) {
+ case 'c': {
+ struct c_arg demo;
+ int c_arg_cntr = 0; // 1 < c_arg_cntr < 3
+
+ demo.title = optarg;
+ c_arg_cntr++; // 1
+
+ if (optind < argc) {
+ demo.description = argv[optind];
+ c_arg_cntr++; // 2
+ }
+ printf("=> %s : %s\n", demo.title, demo.description);
+ break;
+ }
+ default: {
+ printf("=> Use %s -h for options.\n", argv[0]);
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
diff --git a/Ref/task_save_ref.txt b/Ref/task_save_ref.txt
@@ -0,0 +1,8 @@
+1:Task:Description:1/n
+2:Task:Description:1
+3:Task:Description:0
+4:Task:Description:1
+5:Task:Description:2
+6:Task:Description:1
+7:Task:Description:1
+8:Task:Description:0
diff --git a/cdoto.c b/cdoto.c
@@ -1,9 +1,12 @@
#include <stdio.h>
+#include <string.h>
#include <unistd.h>
// CONSTANTS (PP)
#define TASK_TITLE_LEN 256
#define TASK_DESCRIPTION_LEN 1024
+#define TASK_FILE_NAME "cdoto.txt"
+#define TASK_PARSE_TEMPLATE "%d:%s:%s:%d\n"
/*
* h - help menu
* c NAME [DESCRIPTION] - create task
@@ -12,6 +15,7 @@
// STRUCTURES
typedef struct task {
+ int id;
char title[TASK_TITLE_LEN];
char description[TASK_DESCRIPTION_LEN];
short status; // 0 - done; 1 - active ; 2 - other;
@@ -23,27 +27,58 @@ typedef struct tasks_list {
} TList;
// FUNCTIONS
-int task_fill_fields(Task *task) {
- // Iteractions with user
- printf("?> Task Title: ");
- fgets(task->title, TASK_TITLE_LEN, stdin);
- printf("?> Task Description: ");
- fgets(task->description, TASK_DESCRIPTION_LEN, stdin);
+Task task_create(int argc, char *argv[]) {
+ Task new_task = {0, "Task", "-", 1}; // Status: active
+ int c_arg_cntr = 0; // 1 < c_arg_cntr < 3
+ strcpy(new_task.title, optarg);
+ c_arg_cntr++; // 1
+
+ if (optind < argc && argv[optind][0] != '-') {
+ strcpy(new_task.description, argv[optind]);
+ c_arg_cntr++; // 2
+ }
+ return new_task;
+}
+
+int task_file_append(Task *task) {
+ FILE *fp;
+
+ fp = fopen(TASK_FILE_NAME, "a");
+ if (!fp) {
+ perror("!=> Can't open file to append task.\n");
+ return 1;
+ }
+ fprintf(fp, TASK_PARSE_TEMPLATE, task->id, task->title, task->description,
+ task->status);
+ fclose(fp);
return 0;
}
+// int task_file_read()
+
// ENTRY POINT
int main(int argc, char *argv[]) {
- // Task List stuff
TList *head = NULL; // Init head of TList
- TList *tlist; // Chained list of created tasks
+ int opt;
+ Task new_task;
- // Task stuff
- static Task new_task = {"Task", "-", 1}; // Status: active
- task_fill_fields(&new_task);
- printf("=> Title: %s=> Description: %s", new_task.title,
- new_task.description);
+ while ((opt = getopt(argc, argv, ARGUMENTS)) != -1) {
+ switch (opt) {
+ case 'c': {
+ new_task = task_create(argc, argv);
+ if (task_file_append(&new_task)) {
+ return 1;
+ }
+ printf("=> %s : %s\n", new_task.title, new_task.description); // WARN:DBG
+ break;
+ }
+ default: {
+ printf("=> Use %s -h for options.\n", argv[0]);
+ return 1;
+ }
+ }
+ }
return 0;
}
diff --git a/cdoto.txt b/cdoto.txt
@@ -0,0 +1,5 @@
+0:Hi:-:1
+0:HFr:DEE:1
+0:FH:FJKHK:1
+0:Task:Just a normal DAMNED task:1
+0:Task:Just another normal DAMNED task:1