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

cdoto.c (10379B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <unistd.h>
      5 
      6 // CONSTANTS
      7 #define TASK_TITLE_LEN 256
      8 #define TASK_DESCRIPTION_LEN 1024
      9 #define TASK_FILE_NAME "cdoto.txt"
     10 #define TASK_FIELDS_COUNT 4
     11 // Task Templates
     12 #define TASK_WRITE_TEMPLATE "%d^%s^%s^%d\n"
     13 #define TASK_READ_TEMPLATE "%d^%255[^^]^%1023[^^]^%d\n"
     14 #define TASK_PRINT_TEMPLATE "%d: [%c] %s (%s)\n"
     15 #define TASK_DEFAULT_INIT {0, "Task", "-", ACTIVE}
     16 /*
     17  * h - help menu (./prog -h)
     18  * c TITLE DESCRIPTION - create task (./prog -c "Title" "Description")
     19  * d ID - mark task with this ID as done or use negative ID for undone (./prog
     20  * -d 24) & (./prog -d -24)
     21  * r ID - mark task with this ID as deleted or use
     22  * negative ID for restore (./prog -r 24) & (./prog -r -24)
     23  */
     24 #define ARGUMENTS "hc:d:"
     25 
     26 const char help_msg[] =
     27     "HELP MENU:\n\nFlags:\n\t-h -- show this message\n\t-c TITLE DESCRIPTION "
     28     "-- create new task\n\t-d ID -- mark task as DONE, or provide negative ID "
     29     "(ex. -3) "
     30     "to mark as ACTIVE\n";
     31 
     32 // STRUCTURES
     33 typedef enum {
     34   RET_NONVALID = -1, // For Numbers
     35   RET_SUCCESS = 0,
     36   RET_ERROR = 1, // For Statuses
     37   RET_UNDEFBEHAV = 77,
     38   RET_NOTIMPLEMENTED = 99
     39 } RetCode;
     40 
     41 typedef enum { DONE = 0, ACTIVE = 1, EXPIRED = 2, DELETED = 3 } TaskStatus;
     42 
     43 typedef struct task {
     44   int id;
     45   char title[TASK_TITLE_LEN];
     46   char description[TASK_DESCRIPTION_LEN];
     47   TaskStatus status;
     48 } Task;
     49 
     50 typedef struct tasks_list {
     51   Task *task;
     52   struct tasks_list *next;
     53 } TList;
     54 
     55 // DECLARATIONS
     56 
     57 // System
     58 int file_touch(char *filename);
     59 FILE *file_open_r(char *filename);
     60 FILE *file_open_a(char *filename); // TODO:
     61 FILE *file_open_w(char *filename);
     62 
     63 // Task&Files
     64 TList *task_read(char *filename, TList *head);
     65 int task_write(char *filename, TList *head);
     66 int task_file_append(Task *task);
     67 int file_read_last_str(FILE *fp, char *str, int str_size);
     68 
     69 // Tasks Stuff
     70 int task_gen_id(char *filename);
     71 int task_create(Task *task, char *title, char *description);
     72 int strtotask(char *str, Task *task);
     73 
     74 // TList things
     75 Task *task_search_id(int id, TList *head);
     76 int task_show(TList *head, char mode);
     77 void tlist_free(TList *head);
     78 
     79 // DEFINITIONS
     80 
     81 int task_write(char *filename, TList *head) {
     82   FILE *fp = file_open_w(filename);
     83   if (!fp) {
     84     return RET_ERROR;
     85   }
     86   TList *curr = head;
     87 
     88   while (curr != NULL) {
     89     fprintf(fp, TASK_WRITE_TEMPLATE, curr->task->id, curr->task->title,
     90             curr->task->description, curr->task->status);
     91     curr = curr->next;
     92   }
     93 
     94   return RET_SUCCESS;
     95 }
     96 
     97 // Ret: E:NULL S:Pointer To The File
     98 FILE *file_open_w(char *filename) {
     99   FILE *fp = fopen(filename, "w");
    100 
    101   if (fp == NULL) {
    102     fprintf(stderr, "!=> Can't open file \"%s\" in WRITE mode.\n", filename);
    103     return NULL;
    104   }
    105   return fp;
    106 }
    107 
    108 // Ret: E:NULL S:ptr to Task
    109 Task *task_search_id(int id, TList *head) {
    110   TList *curr = head;
    111   unsigned int s_id;
    112 
    113   if (id < 0) {
    114     s_id = -id;
    115   } else {
    116     s_id = id;
    117   }
    118 
    119   while (curr != NULL) {
    120     if (curr->task->id == s_id) {
    121       return curr->task;
    122     }
    123     curr = curr->next;
    124   }
    125   return NULL;
    126 }
    127 
    128 void tlist_free(TList *head) {
    129   TList *next_node;
    130   TList *node = head;
    131 
    132   while (node != NULL) {
    133     next_node = node->next;
    134     free(node->task);
    135     free(node);
    136     node = next_node;
    137   }
    138 }
    139 
    140 // Ret: E:RET_NONVALID S:count of printed tasks
    141 int task_show(TList *head, char mode) {
    142   TList *curr_node = head;
    143   int tasks_printed = 0;
    144   char status_char;
    145 
    146   if (head == NULL) {
    147     return RET_NONVALID;
    148   }
    149 
    150   while (curr_node != NULL) {
    151     switch (curr_node->task->status) {
    152     case ACTIVE:
    153       status_char = ' ';
    154       break;
    155     case DONE:
    156       status_char = 'x';
    157       break;
    158     case DELETED:
    159       status_char = 'D';
    160       break;
    161     case EXPIRED:
    162       status_char = '~';
    163       break;
    164     default:
    165       return RET_UNDEFBEHAV;
    166     }
    167 
    168     printf(TASK_PRINT_TEMPLATE, curr_node->task->id, status_char,
    169            curr_node->task->title, curr_node->task->description);
    170     curr_node = curr_node->next;
    171     tasks_printed++;
    172   }
    173   return tasks_printed;
    174 }
    175 
    176 // Ret: E:NULL S:non-NULL
    177 FILE *file_open_r(char *filename) {
    178   FILE *fp = fopen(filename, "r");
    179 
    180   if (fp == NULL) {
    181     fprintf(stderr, "!=> Can't open file \"%s\" in READ mode.\n", filename);
    182     return NULL;
    183   }
    184   return fp;
    185 }
    186 
    187 // Ret: E: NULL S: non-NULL
    188 TList *task_read(char *filename, TList *head) {
    189   FILE *fp = file_open_r(filename);
    190   if (fp == NULL) {
    191     return NULL;
    192   }
    193 
    194   TList *prev_node = NULL;
    195   TList *curr_node = NULL;
    196   int tasks_cntr = 0;
    197   char str_buff[TASK_TITLE_LEN + TASK_DESCRIPTION_LEN + 128];
    198   char *is_continue = "a"; // Init to prevent undefined behavior
    199 
    200   // Reading Strings & Creating Chained List
    201   while (is_continue != NULL) {
    202     is_continue =
    203         fgets(str_buff, TASK_TITLE_LEN + TASK_DESCRIPTION_LEN + 128, fp);
    204     if (is_continue == NULL || str_buff[0] == '\n') {
    205       break;
    206     }
    207 
    208     curr_node = (TList *)malloc(sizeof(struct tasks_list));
    209     curr_node->next = NULL;
    210     curr_node->task = (Task *)malloc(sizeof(struct task));
    211     if (head == NULL) {
    212       head = curr_node;
    213     }
    214     strtotask(str_buff, curr_node->task);
    215     if (prev_node != NULL) {
    216       prev_node->next = curr_node;
    217     }
    218     prev_node = curr_node;
    219 
    220     tasks_cntr++;
    221   }
    222   return head;
    223 }
    224 
    225 // Ret: E:RET_ERROR S:RET_SUCCESS
    226 int file_touch(char *filename) {
    227   FILE *fp;
    228 
    229   if (!(fp = fopen(filename, "a"))) {
    230     return RET_ERROR;
    231   }
    232 
    233   fclose(fp);
    234   return RET_SUCCESS;
    235 }
    236 
    237 // Ret: E:RET_ERROR S:RET_SUCCESS
    238 int task_create(Task *task, char *title, char *description) {
    239   int c_arg_cntr = 0; // 1 < c_arg_cntr < 3
    240 
    241   task->id = task_gen_id(TASK_FILE_NAME);
    242   if (task->id == RET_NONVALID) {
    243     fprintf(stderr, "!=> Error occured while generating ID.\n");
    244     return RET_ERROR;
    245   }
    246 
    247   strcpy(task->title, title ? title : "Task");
    248   task->title[TASK_TITLE_LEN - 1] = '\0';
    249   strcpy(task->description, description ? description : "-");
    250   task->description[TASK_DESCRIPTION_LEN - 1] = '\0';
    251 
    252   return RET_SUCCESS;
    253 }
    254 
    255 // Ret: E:RET_ERROR S:RET_SUCCESS
    256 int task_file_append(Task *task) {
    257   FILE *fp;
    258 
    259   fp = fopen(TASK_FILE_NAME, "a");
    260   if (!fp) {
    261     perror("!=> Can't open file to append task.\n");
    262     return RET_ERROR;
    263   }
    264   fprintf(fp, TASK_WRITE_TEMPLATE, task->id, task->title, task->description,
    265           task->status);
    266   fclose(fp);
    267   return RET_SUCCESS;
    268 }
    269 
    270 // Ret: E:RET_ERROR S:RET_SUCCESS
    271 int file_read_last_str(FILE *fp, char *str, int str_size) {
    272   long long fp_pos;
    273   int fp_nl_ctr = 0;
    274 
    275   fseek(fp, 0, SEEK_END);
    276   fp_pos = ftell(fp);
    277 
    278   while (fp_pos > 0 && fseek(fp, --fp_pos, SEEK_SET) == 0) {
    279     if (fgetc(fp) == '\n') {
    280       if (fp_nl_ctr >= 1) {
    281         if (!(fgets(str, str_size, fp))) {
    282           return RET_ERROR;
    283         }
    284         return RET_SUCCESS;
    285       }
    286       fp_nl_ctr++;
    287     }
    288   }
    289 
    290   if (fp_pos == 0 && fp_nl_ctr == 1) {
    291     rewind(fp);
    292     if (!(fgets(str, str_size, fp))) {
    293       return RET_ERROR;
    294     }
    295   }
    296   return RET_SUCCESS;
    297 }
    298 
    299 // Ret: E:RET_ERROR S:RET_SUCCESS
    300 int strtotask(char *str, Task *task) {
    301   int things_readed = 0;
    302 
    303   things_readed = sscanf(str, TASK_READ_TEMPLATE, &task->id, task->title,
    304                          task->description, &task->status);
    305   if (things_readed != TASK_FIELDS_COUNT) {
    306     fprintf(stderr,
    307             "!=> Can't properly parse str -> Task. Expected:%d; Readed:%d.\n",
    308             TASK_FIELDS_COUNT, things_readed);
    309     return RET_ERROR;
    310   }
    311   return RET_SUCCESS;
    312 }
    313 
    314 // Ret: E:RET_NONVALID S:id for new task [1;...)
    315 int task_gen_id(char *filename) {
    316   FILE *fp;
    317   char buffer[1024];
    318   int id;
    319   Task last_task;
    320 
    321   file_touch(filename);
    322 
    323   if (!(fp = file_open_r(filename))) {
    324     return RET_NONVALID;
    325   }
    326 
    327   // If the file are empty: id = 1
    328   if (fseek(fp, 0, SEEK_END) == 0 && ftell(fp) == 0) {
    329     id = 1;
    330     return id;
    331   }
    332 
    333   // Else getting last task id
    334   if (file_read_last_str(fp, buffer, 1024)) {
    335     fprintf(stderr, "!=> Can't read task from file\n");
    336     return RET_NONVALID;
    337   }
    338 
    339   if (strtotask(buffer, &last_task)) {
    340     return RET_NONVALID;
    341   }
    342 
    343   fclose(fp);
    344   id = last_task.id + 1;
    345   return id;
    346 }
    347 
    348 // ENTRY POINT
    349 int main(int argc, char *argv[]) {
    350   TList *head = NULL; // Init head of TList
    351   int opt;            // For optarg()
    352 
    353   // -c flag stuff
    354   // TODO: Alloc them in case of -c
    355   Task new_task = TASK_DEFAULT_INIT;
    356   char title[TASK_TITLE_LEN];
    357   char description[TASK_DESCRIPTION_LEN];
    358 
    359   // -d flag stuff
    360   Task *found_task;
    361   int searhing_id;
    362 
    363   if (argc == 1) {
    364     if (!(head = task_read(TASK_FILE_NAME, head))) {
    365       fprintf(stderr, "!=> You don't have any tasks yet.\n");
    366       return RET_ERROR;
    367     }
    368     task_show(head, 0);
    369     tlist_free(head);
    370     return RET_SUCCESS;
    371   }
    372 
    373   while ((opt = getopt(argc, argv, ARGUMENTS)) != -1) {
    374     switch (opt) {
    375     case 'c':
    376       // Parsing TITLE
    377       strncpy(title, optarg, TASK_TITLE_LEN - 1);
    378       title[TASK_TITLE_LEN - 1] = '\0';
    379       // Parsing DESCRIPTION
    380       if (optind < argc) {
    381         strncpy(description, argv[optind], TASK_DESCRIPTION_LEN);
    382         description[TASK_DESCRIPTION_LEN - 1] = '\0';
    383       } else {
    384         description[0] = '-';
    385         description[1] = '\0';
    386       }
    387 
    388       // Creating & Saving new task
    389       if (task_create(&new_task, title, description)) {
    390         fprintf(stderr, "!=> Can't create task.\n");
    391         return RET_ERROR;
    392       }
    393       if (task_file_append(&new_task)) {
    394         return RET_ERROR;
    395       }
    396       return RET_SUCCESS;
    397     case 'h':
    398       printf("%s", help_msg);
    399       return RET_SUCCESS;
    400     case 'd':
    401       searhing_id = atoi(optarg);
    402       // Loading tasks from file to the memory
    403       if (!(head = task_read(TASK_FILE_NAME, head))) {
    404         fprintf(stderr, "!=> Can't read tasks from file to the memory.\n");
    405         return RET_ERROR;
    406       }
    407       // Searching for needed task
    408       if (!(found_task = task_search_id(searhing_id, head))) {
    409         fprintf(stderr, "!=> Invalid ID.\n");
    410         return RET_ERROR;
    411       }
    412       // Changing status
    413       if (searhing_id >= 0) {
    414         found_task->status = DONE;
    415       } else {
    416         found_task->status = ACTIVE; // If Negative status
    417       }
    418       // Writing tasks into the file
    419       if (task_write(TASK_FILE_NAME, head)) {
    420         return RET_ERROR;
    421       }
    422       // Exit
    423       tlist_free(head);
    424       return RET_SUCCESS;
    425     default:
    426       printf("=> Incorrect option. Try \"%s -h\" for help.\n", argv[0]);
    427       return RET_ERROR;
    428     }
    429   }
    430   return RET_SUCCESS;
    431 }