pydoto-cli

Simple ToDo-like application, that just works. Add any tasks without closing your terminal.
git clone https://git.nfu.su/pydoto-cli.git
Log | Files | Refs | README

commit b1f401e97e50475ce8dc063916ec7232ade3f27b
parent 9b9b86fee2040bf6510aaec631c37fcede3de3a2
Author: Pr0fiTX <uventik999@gmail.com>
Date:   Thu, 31 Jul 2025 22:29:27 +0300

[NEW]: ID Gen Function for Tasks

Diffstat:
M.gitignore | 2++
Mcdoto.c | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
2 files changed, 102 insertions(+), 8 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1,3 @@ TEST_BUILD +Ref/ +cdoto.txt diff --git a/cdoto.c b/cdoto.c @@ -7,6 +7,7 @@ #define TASK_DESCRIPTION_LEN 1024 #define TASK_FILE_NAME "cdoto.txt" #define TASK_PARSE_TEMPLATE "%d:%s:%s:%d\n" +#define TASK_FIELDS_COUNT 4 /* * h - help menu * c NAME [DESCRIPTION] - create task @@ -18,7 +19,7 @@ typedef struct task { int id; char title[TASK_TITLE_LEN]; char description[TASK_DESCRIPTION_LEN]; - short status; // 0 - done; 1 - active ; 2 - other; + int status; // 0 - done; 1 - active ; 2 - other; } Task; typedef struct tasks_list { @@ -26,10 +27,34 @@ typedef struct tasks_list { struct tasks_list *next; } TList; -// FUNCTIONS +// DECLARATIONS + +// System +int file_touch(char *filename); // TODO: +int file_read_last_str(FILE *fp, char *str, int str_size); + +// Task shit +int task_gen_id(char *filename); +Task task_create(int argc, char *argv[]); +int task_file_append(Task *task); +int strtotask(char *str, Task *task); + +// DEFINITIONS + +int file_touch(char *filename) { + FILE *fp; + + if (!(fp = fopen(filename, "a"))) { + return 1; + } + + return 0; +} + 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 + Task new_task = {task_gen_id(TASK_FILE_NAME), "Task", "-", + 1}; // Status: active + int c_arg_cntr = 0; // 1 < c_arg_cntr < 3 strcpy(new_task.title, optarg); c_arg_cntr++; // 1 @@ -55,7 +80,76 @@ int task_file_append(Task *task) { return 0; } -// int task_file_read() +int file_read_last_str(FILE *fp, char *str, int str_size) { + long long fp_pos = ftell(fp); + int fp_nl_ctr = 0; + + fseek(fp, 0, SEEK_END); + + while (fp_pos > 0 && fseek(fp, --fp_pos, SEEK_SET) == 0) { + if (fgetc(fp) == '\n') { + if (fp_nl_ctr >= 1) { + if (!(fgets(str, str_size, fp))) { + return 1; + } + } + fp_nl_ctr++; + } + } + + if (fp_pos == 0 && fp_nl_ctr == 1) { + rewind(fp); + if (!(fgets(str, str_size, fp))) { + return 1; + } + } + return 0; +} + +int strtotask(char *str, Task *task) { + int things_readed = 0; + + things_readed = sscanf(str, TASK_PARSE_TEMPLATE, &task->id, task->title, + task->description, &task->status); + if (things_readed != TASK_FIELDS_COUNT) { + fprintf(stderr, + "!=> Can't properly parse str -> Task. Expected: %d; Readed: %d.\n", + TASK_FIELDS_COUNT, things_readed); + return 1; + } + return 0; +} + +int task_gen_id(char *filename) { + FILE *fp; + char buffer[1024]; + int id; + Task last_task; + + file_touch(filename); + + if (!(fp = fopen(TASK_FILE_NAME, "r"))) { + fprintf(stderr, "!=> Can't open task file\n"); + return -1; + } + + if (file_read_last_str(fp, buffer, 1024)) { + fprintf(stderr, "!=> Can't read task from file\n"); + return -1; + } + + if (buffer[0] == '\0') { + id = 1; + return id; + } + + if (strtotask(buffer, &last_task)) { + return -1; + } + + id = last_task.id + 1; + return id; +} // ENTRY POINT int main(int argc, char *argv[]) { @@ -70,12 +164,10 @@ int main(int argc, char *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]); + printf("=> Incorrect option. Try \"%s -h\" for help.\n", argv[0]); return 1; } }