#include "odot.h"
-void add(struct task t, FILE *fp){
- printf("\talloc\n");
- char *line = malloc(MAXLINE*sizeof(char));
- printf("\tfopen\n");
- FILE *buf = fopen("/home/huck/.local/state/odot/odot.txt", "w+");
+void add(struct task t){
+ char *line = malloc(MAXLINE*sizeof(char)); printf("\talloc\n");
+ FILE *fp = fopen("/home/huck/.local/state/odot/todo.txt", "r");
+ FILE *buf = fopen("/home/huck/.local/state/odot/odot.txt", "w+"); printf("\tfopen\n");
int i = 0;
- printf("\tNULLcheck\n");
- if (fgets(line,MAXLINE,fp) == NULL){
+ if (fgets(line,MAXLINE,fp) == NULL){ printf("\tNULLcheck\n");
puttask(t,buf);
} else {
printf("\tloopstart\n");
if (check() == 0){
break;
} else {
- puttask(gettask(line), buf);
+ puttask(*gettask(line), buf);
break;
}
case 1:
- dialogue("Already on list in a different group", gettask(line).group, YELLOW);
+ dialogue("Already on list in a different group", gettask(line)->group, YELLOW);
if (check() == 0){
puttask(t,buf);
i++;
}
- puttask(gettask(line), buf);
+ puttask(*gettask(line), buf);
break;
default:
/* first time t.task is lexigraphically greater than a previous task */
puttask(t,buf);
++i;
}
- puttask(gettask(line), buf);
+ puttask(*gettask(line), buf);
break;
}
}
fclose(fp);
fclose(buf);
- remove("/home/huck/.local/state/odot/todo.txt");
rename("/home/huck/.local/state/odot/odot.txt", "/home/huck/.local/state/odot/todo.txt");
- free(line);
fp = fopen("/home/huck/.local/state/odot/todo.txt", "w+");
if (!fp)
error(1);
}
-void rem(struct task t, FILE *fp){
+void rem(struct task t){
char *s = malloc(MAXLINE * sizeof(char));
+ FILE *fp = fopen("/home/huck/.local/state/odot/todo.txt", "r");
FILE *buf = fopen("/home/huck/.local/state/odot/odot.txt", "w+");
int i = 0;
fclose(fp);
fclose(buf);
-
remove ("/home/huck/.local/state/odot/todo.txt");
rename("/home/huck/.local/state/odot/odot.txt", "/home/huck/.local/state/odot/todo.txt");
- fp = fopen("/home/huck/.local/state/odot/todo.txt", "w+");
-
if (i == 0){
dialogue("Task not found", t.task, YELLOW);
printf("Add to list?");
if(check() == 0){
- add(t,fp);
+ add(t);
}
}
- free(s);
-
}
-void show(char *group, FILE *fp){
+void show(char *group){
char *task = malloc(MAXLINE * sizeof(char));
+ FILE *f = fopen("/home/huck/.local/state/odot/todo.txt", "r");
struct task tmp;
printf("\twhileloop\n");
int j = 0;
-
- while ((fgets(task, MAXLINE, fp)) != NULL ){
- printf("\t%i-nloop\n", ++j);
- tmp = gettask(task);
+ while ((fgets(task, MAXLINE, f)) != NULL ){
+ tmp = *gettask(task); printf("\t%i-nloop\n", ++j);
if (strcmp(group, "all") == 0 || strcmp(tmp.group, group) == 0){
formattask(tmp);
}
}
- free(task);
+ fclose(f);
}
struct task getnote(int n, char *arg[], char *group){
char *note = malloc(MAXLINE * sizeof(char));
- struct task tmp;
+ struct task *tmp;
if (n == 1){
printf("\t\treturn from note\n");
- return tmp;
+ return *tmp;
}
}
printf("\t%s %s getnote\n",note,group);
tmp = maketask(note,group);
- free(note);
- return tmp;
+ return *tmp;
}
/* uses a 3 bit number to represent options
}
-struct task maketask(char *task, char *group){
+struct task *maketask(char *task, char *group){
printf("\tmaketask\n");
- struct task tmp;
+ struct task *tmp;
- tmp.task = malloc(strlen(task) * sizeof(char));
- tmp.group = malloc(strlen(group) * sizeof(char));
+ tmp->task = malloc(strlen(task) * sizeof(char));
+ tmp->group = malloc(strlen(group) * sizeof(char));
- strcpy(tmp.task,task);
- strcpy(tmp.group,(strcmp(tmp.group,"") == 0) ? "all" : group);
+ strcpy(tmp->task,task);
+ strcpy(tmp->group,(strcmp(tmp->group,"") == 0) ? "all" : group);
- printf("\t%s-task %s-group\n",tmp.task,tmp.group);
+ printf("\t%s-task %s-group\n",tmp->task,tmp->group);
return tmp;
}
-struct task gettask(char *c){
- printf("\tgettask\n");
- struct task tmp;
-
- sscanf(c,"%s\t%s",tmp.task,tmp.group);
+struct task *gettask(char *c){
+ char *task, *group;
+ task = malloc(strlen(c) * sizeof(char));
+ group = malloc(strlen(c) * sizeof(char));
+
+ sscanf(c,"%s\t%s",task,group);
+
+ struct task *tmp = maketask(task,group);
return tmp;
}
-#include "odot.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
-int main(int argc, char *argv[]){
- printf("\tstart\n");
- struct task note;
- short opt;
- FILE *fp;
- char *group = malloc(MAXLINE*sizeof(char));
-
- printf("\tfopen\n");
- fp = fopen("/home/huck/.local/state/odot/todo.txt", "w+");
- if (!fp){
- error(1);
- }
+#define MAXLINE 500
+#define LIST "/home/huck/.local/state/odot/todo.txt"
+#define DONE "/home/huck/.local/state/odot/done.txt"
+#define TMP "/home/huck/.local/state/odot/odot.txt"
- printf("\tgetopt\n");
- opt = getopt(argc, argv);
+int getopt(int, char **);
+char *getnote(int, char **);
- printf("\tgetnote\n");
- note = getnote(argc, argv, group);
+void addnote(char *);
+void show();
- if (opt >= 4){
- printf("\trem\n");
- rem(note, fp);
- } else if (opt >= 2){
- printf("\tadd\n");
- add(note, fp);
- }
+int main(int argc, char *argv[]){
- if (opt % 2 != 0){
- printf("\tshow\n");
- show(group, fp);
+ if (argc == 1){
+ show();
+ } else {
+ addnote(getnote(argc,argv));
}
- printf("\texiting\n");
- fclose(fp);
- free(group);
return 0;
}
-void error(int err){
- switch (err) {
- case 1:
- fprintf(stderr,"ERROR: COULD NOT ACCESS FILE: %s\n", "/home/huck/.local/state/odot/todo.txt");
- break;
- case 2:
- fprintf(stderr,"ERROR: CANT ADD AND REMOVE SAME TASK\n");
- break;
+char *getnote(int n, char *arg[]){
+ char *s = malloc(MAXLINE * sizeof(char));
+ while (--n > 0){
+ strcat(s,*++arg);
+ strcat(s,(n > 1) ? " " : "\n");
}
- exit(err);
+ return s;
}
-void dialogue(char *m1, char *m2, int c){
- printf("\033[1;3%im%s\033[0m: %s\n", c, m1, m2);
+void addnote(char *note){
+ int i = 0;
+ char *buf = malloc(MAXLINE * sizeof(char));
+
+ FILE *fp = fopen(LIST,"r");
+ FILE *fdone = fopen(DONE,"a");
+ FILE *tmp = fopen(TMP,"w");
+ if(!fp || !tmp || !fdone)
+ exit(1);
+
+ while(fgets(buf, MAXLINE, fp)){
+ if (strcmp(note,buf) > 0){
+ fputs(buf,tmp);
+ } else if (strcmp(note,buf) == 0){
+ fputs(note,fdone);
+ printf("Removed from list: %s", note);
+ i++;
+ } else if (strcmp(note,buf) < 0){
+ if (i == 0){
+ fputs(note,tmp);
+ printf("Added to list: %s", note);
+ i++;
+ }
+ fputs(buf,tmp);
+ }
+ }
+ if (i == 0){
+ fputs(note,tmp);
+ printf("Added to list: %s", note);
+ }
+
+ fclose(tmp);
+ fclose(fp);
+ fclose(fdone);
+ free(buf);
+ remove(LIST);
+ rename(TMP,LIST);
return;
}
-void formattask(struct task t){
- printf("* %s\t%s\n", t.task, t.group);
-}
+void show(void){
+ FILE *fp = fopen(LIST,"r");
+ char *buf = malloc(MAXLINE * sizeof(char));
- /* Returns:
- 0 - Nothing matches
- 1 - Task matches
- 2 - Group matches
- 3 - Both match
- */
-int listcheck(struct task t, char *c){
- int i;
- i += (strcmp(t.task,gettask(c).task) == 0) ? 1 : 0;
- i += (strcmp(t.group,gettask(c).group) == 0) ? 2 : 0;
- return i;
-}
+ printf("\033[36;1mTODO\033[0m:\n");
+ while(fgets(buf,MAXLINE,fp)){
+ printf(" \033[35;1m*\033[0m %s", buf);
+ }
-/* returns 0 for yes and 1 for no */
-int check(void){
- printf("Continue? [y/n] (y):");
- if (((char) getchar()) == 'n')
- return 1;
- return 0;
+ free(buf);
+ fclose(fp);
+ return;
}
+
struct task getnote(int, char *[], char *);
short getopt(int, char *[]);
-void add(struct task, FILE *);
-void rem(struct task, FILE *);
-void show(char *group, FILE *);
+void add(struct task);
+void rem(struct task);
+void show(char *group);
int listcheck(struct task, char *);
int geturgency(int);
void error(int);
int check(void);
-struct task maketask(char *, char *);
+struct task *maketask(char *, char *);
void puttask(struct task n, FILE*);
-struct task gettask(char *);
+struct task *gettask(char *);
enum color {BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE};
+++ /dev/null
-SHELL = /bin/zsh
-PROG = odot
-PREFIX ?= /usr/local/bin
-ODOT = /home/huck/.local/share/odot
-TODOLIST = $(ODOT)/todo
-
-install : main.c input.c file.c
- [[ ! -d $(TODOLIST) ]] && mkdir -p $(ODOT)
- gcc *.c -o '$(PREFIX)/$(PROG)'
-
-header : odot.h
- gcc *.h
-
-clean :
- rm *.gch
- rm "$(PREFIX)/$(PROG)"
-
+++ /dev/null
-#include "odot.h"
-
-
+++ /dev/null
-#include "odot.h"
-
-void add(struct task t, FILE *fp){
- printf("\talloc\n");
- char *line = malloc(MAXLINE*sizeof(char));
- printf("\tfopen\n");
- FILE *buf = fopen("/home/huck/.local/state/odot/odot.txt", "w+");
- int i = 0;
-
- printf("\tNULLcheck\n");
- if (fgets(line,MAXLINE,fp) == NULL){
- puttask(t,buf);
- } else {
- printf("\tloopstart\n");
- int j = 0;
- while (fgets(line,MAXLINE,fp) != NULL){
- printf("\t%i-nloop\n", ++j);
- switch (listcheck(t,line)){
- case 3:
- dialogue("Already on todo list", t.task, CYAN);
- printf("Remove from list?");
- if (check() == 0){
- break;
- } else {
- puttask(gettask(line), buf);
- break;
- }
- case 1:
- dialogue("Already on list in a different group", gettask(line).group, YELLOW);
- if (check() == 0){
- puttask(t,buf);
- i++;
- }
- puttask(gettask(line), buf);
- break;
- default:
- /* first time t.task is lexigraphically greater than a previous task */
- if (i == 0 && strcmp(line,t.task) > 0){
- puttask(t,buf);
- ++i;
- }
- puttask(gettask(line), buf);
- break;
- }
- }
- }
-
- fclose(fp);
- fclose(buf);
- remove("/home/huck/.local/state/odot/todo.txt");
- rename("/home/huck/.local/state/odot/odot.txt", "/home/huck/.local/state/odot/todo.txt");
- free(line);
- fp = fopen("/home/huck/.local/state/odot/todo.txt", "w+");
- if (!fp)
- error(1);
- return;
-}
-
-
-void rem(struct task t, FILE *fp){
- char *s = malloc(MAXLINE * sizeof(char));
- FILE *buf = fopen("/home/huck/.local/state/odot/odot.txt", "w+");
- int i = 0;
-
- while (fgets(s, MAXLINE, fp) != NULL){
- s[strlen(s) - 1] = 0;
- if (strcmp(t.task, s) != 0){
- fputs(strcat(s,"\t"),buf);
- } else {
- i++;
- }
- }
-
- fclose(fp);
- fclose(buf);
-
- remove ("/home/huck/.local/state/odot/todo.txt");
- rename("/home/huck/.local/state/odot/odot.txt", "/home/huck/.local/state/odot/todo.txt");
-
- fp = fopen("/home/huck/.local/state/odot/todo.txt", "w+");
-
- if (i == 0){
- dialogue("Task not found", t.task, YELLOW);
- printf("Add to list?");
- if(check() == 0){
- add(t,fp);
- }
- }
- free(s);
-
-}
-
-void show(char *group, FILE *fp){
- char *task = malloc(MAXLINE * sizeof(char));
- struct task tmp;
-
- printf("\twhileloop\n");
- int j = 0;
-
- while ((fgets(task, MAXLINE, fp)) != NULL ){
- printf("\t%i-nloop\n", ++j);
- tmp = gettask(task);
- if (strcmp(group, "all") == 0 || strcmp(tmp.group, group) == 0){
- formattask(tmp);
- }
- }
- free(task);
-}
-
+++ /dev/null
-#include "odot.h"
-
-struct task getnote(int n, char *arg[], char *group){
- char *note = malloc(MAXLINE * sizeof(char));
- struct task tmp;
-
- if (n == 1){
- printf("\t\treturn from note\n");
- return tmp;
- }
-
-
- while(--n > 0){
- printf("\t%i-nloop\n",n);
- if ((*++arg)[0] != '-'){
- strcat(note, *arg);
- strcat(note, " ");
- } else if (strcmp(*arg,"-g") == 0){
- strcpy(group, (*++arg));
- n--;
- }
- }
- printf("\t%s %s getnote\n",note,group);
- tmp = maketask(note,group);
- free(note);
- return tmp;
-}
-
-/* uses a 3 bit number to represent options
- 1 - show list
- 2 - add to list
- 4 - remove from list
-*/
-short getopt(int n, char *arg[]){
- short options;
-
- printf("\treturn1\n");
- /* show list if no arguments given */
- if (n == 1)
- return 1;
-
- printf("\tcheckopt\n");
- while (--n > 0 && (*++arg)[0] == '-'){
- /* if theres a d delete, otherwise add */
- options += strchr(*arg, 's') ? 1 : 0;
- options += strchr(*arg, 'n') ? 2 : 0;
- options += strchr(*arg, 'd') ? 4 : 0;
- }
- printf("\t%i-optcode\n", options);
- if ((options - 5) > 0)
- error(2);
- return options;
-}
-
-
-struct task maketask(char *task, char *group){
- printf("\tmaketask\n");
- struct task tmp;
-
- tmp.task = malloc(strlen(task) * sizeof(char));
- tmp.group = malloc(strlen(group) * sizeof(char));
-
- strcpy(tmp.task,task);
- strcpy(tmp.group,(strcmp(tmp.group,"") == 0) ? "all" : group);
-
- printf("\t%s-task %s-group\n",tmp.task,tmp.group);
-
- return tmp;
-}
-
-
-struct task gettask(char *c){
- printf("\tgettask\n");
- struct task tmp;
-
- sscanf(c,"%s\t%s",tmp.task,tmp.group);
-
- return tmp;
-}
-
-void puttask(struct task t, FILE *fp){
- printf("\t%s %s puttask\n", t.task, t.group);
- fprintf(fp,"%s\t%s\n",t.task,t.group);
-}
-#include "odot.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
-int main(int argc, char *argv[]){
- printf("\tstart\n");
- struct task note;
- short opt;
- FILE *fp;
- char *group = malloc(MAXLINE*sizeof(char));
-
- printf("\tfopen\n");
- fp = fopen("/home/huck/.local/state/odot/todo.txt", "w+");
- if (!fp){
- error(1);
- }
+#define MAXLINE 500
+#define LIST "/home/huck/.local/state/odot/todo.txt"
+#define DONE "/home/huck/.local/state/odot/done.txt"
+#define TMP "/home/huck/.local/state/odot/odot.txt"
- printf("\tgetopt\n");
- opt = getopt(argc, argv);
+int getopt(int, char **);
+char *getnote(int, char **);
- printf("\tgetnote\n");
- note = getnote(argc, argv, group);
+void addnote(char *);
+void show();
- if (opt >= 4){
- printf("\trem\n");
- rem(note, fp);
- } else if (opt >= 2){
- printf("\tadd\n");
- add(note, fp);
- }
+int main(int argc, char *argv[]){
- if (opt % 2 != 0){
- printf("\tshow\n");
- show(group, fp);
+ if (argc == 1){
+ show();
+ } else {
+ addnote(getnote(argc,argv));
}
- printf("\texiting\n");
- fclose(fp);
- free(group);
return 0;
}
-void error(int err){
- switch (err) {
- case 1:
- fprintf(stderr,"ERROR: COULD NOT ACCESS FILE: %s\n", "/home/huck/.local/state/odot/todo.txt");
- break;
- case 2:
- fprintf(stderr,"ERROR: CANT ADD AND REMOVE SAME TASK\n");
- break;
+char *getnote(int n, char *arg[]){
+ char *s = malloc(MAXLINE * sizeof(char));
+ while (--n > 0){
+ strcat(s,*++arg);
+ strcat(s,(n > 1) ? " " : "\n");
}
- exit(err);
+ return s;
}
-void dialogue(char *m1, char *m2, int c){
- printf("\033[1;3%im%s\033[0m: %s\n", c, m1, m2);
+void addnote(char *note){
+ int i = 0;
+ char *buf = malloc(MAXLINE * sizeof(char));
+
+ FILE *fp = fopen(LIST,"r");
+ FILE *fdone = fopen(DONE,"a");
+ FILE *tmp = fopen(TMP,"w");
+ if(!fp || !tmp || !fdone)
+ exit(1);
+
+ while(fgets(buf, MAXLINE, fp)){
+ if (strcmp(note,buf) > 0){
+ fputs(buf,tmp);
+ } else if (strcmp(note,buf) == 0){
+ fputs(note,fdone);
+ printf("Removed from list: %s", note);
+ i++;
+ } else if (strcmp(note,buf) < 0){
+ if (i == 0){
+ fputs(note,tmp);
+ printf("Added to list: %s", note);
+ i++;
+ }
+ fputs(buf,tmp);
+ }
+ }
+ if (i == 0){
+ fputs(note,tmp);
+ printf("Added to list: %s", note);
+ }
+
+ fclose(tmp);
+ fclose(fp);
+ fclose(fdone);
+ free(buf);
+ remove(LIST);
+ rename(TMP,LIST);
return;
}
-void formattask(struct task t){
- printf("* %s\t%s\n", t.task, t.group);
-}
+void show(void){
+ FILE *fp = fopen(LIST,"r");
+ char *buf = malloc(MAXLINE * sizeof(char));
- /* Returns:
- 0 - Nothing matches
- 1 - Task matches
- 2 - Group matches
- 3 - Both match
- */
-int listcheck(struct task t, char *c){
- int i;
- i += (strcmp(t.task,gettask(c).task) == 0) ? 1 : 0;
- i += (strcmp(t.group,gettask(c).group) == 0) ? 2 : 0;
- return i;
-}
+ printf("\033[36;1mTODO\033[0m:\n");
+ while(fgets(buf,MAXLINE,fp)){
+ printf(" \033[35;1m*\033[0m %s", buf);
+ }
-/* returns 0 for yes and 1 for no */
-int check(void){
- printf("Continue? [y/n] (y):");
- if (((char) getchar()) == 'n')
- return 1;
- return 0;
+ free(buf);
+ fclose(fp);
+ return;
}
+
+++ /dev/null
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-#include <time.h>
-#define TODOLIST "/home/huck/.local/state/odot/todo"
-
-#define MAXLINE 1000
-#define TIME "%H:%M %m-%d-%y"
-
-struct task {
- char *task;
- char *group;
-};
-
-struct task getnote(int, char *[], char *);
-short getopt(int, char *[]);
-
-void add(struct task, FILE *);
-void rem(struct task, FILE *);
-void show(char *group, FILE *);
-
-int listcheck(struct task, char *);
-int geturgency(int);
-char *gettime(void);
-
-void dialogue(char *, char *, int);
-void formattask(struct task t);
-void error(int);
-int check(void);
-
-struct task maketask(char *, char *);
-void puttask(struct task n, FILE*);
-struct task gettask(char *);
-
-enum color {BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE};