printf("* %s\t%s\t%i", t.task, t.group, t.date);
}
-gT
#include "odot.h"
-extern char *note;
extern FILE *fp;
enum color {BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE};
-
-void add(struct task t, int size){
- int l,m,h,i;
- FILE *fcopy = fopen("/tmp/fcopy", "w");
- char *s = malloc(MAXLINE * sizeof(char));
- h = size;
- m = h / 2;
- l = 0;
-
- while (!(h == m && m == l)){
- i = strcmp(t.task,gettask(fp).task);
-
- if (i < 0) {
- h = m;
- } else if (i > 0) {
- l = m;
- } else {
- dialogue("Task already on list",t.task, BLUE);
- return;
+void add(struct task t, FILE *fp){
+ char *c = malloc(strlen(t.task)*sizeof(char));
+ FILE *fc = fopen("/tmp/odot", "w");
+ int i = 0;
+
+ while (fgets(c,strlen(t.task),fp) != NULL){
+ switch (listcheck(t,fp)){
+ case 3:
+ dialogue("Already on todo list", t.task, CYAN);
+ break;
+ case 1:
+ dialogue("Already on list in a different group", gettask(fp).group, YELLOW);
+ /* confirm(); */
+ break;
+ default:
+ if (i == 0 && strcmp(c,t.task) > 0){
+ puttask(t,fc);
+ ++i;
+ }
}
-
- m = (h + l)/2;
- fseek(fp, m, SEEK_SET);
+ puttask(gettask(fp), fc);
}
- fseek(fp, 0, SEEK_SET);
-
- for (i = 0; i <= (size + 1); i++){
- if (i == m){
- fprintf(fp, "%s\t%i\t%s\n",t.task,t.date,t.group);
- } else {
- fgets(s, MAXLINE, fp);
- fputs(s, fcopy);
- }
- }
+ free(c);
fclose(fp);
- fclose(fcopy);
+ fclose(fc);
remove(TODOLIST);
- rename("/tmp/fcopy", TODOLIST);
- free(s);
- return;
+ rename("/tmp/odot", TODOLIST);
+ return;
}
-void rem(void){
- char *s = malloc(MAXLINE * sizeof(char));
- FILE *tmp = fopen("temp", "w");
-
- fp = fopen(TODOLIST, "r");
+void rem(FILE *fp){
+ char *s = malloc(MAXLINE * sizeof(char));
+ FILE *fc = fopen("/tmp/odot", "w");
while (fgets(s, MAXLINE, fp) != NULL){
s[strlen(s) - 1] = 0;
if (strcmp(note, s) != 0){
- fputs(strcat(s,"\n"),tmp);
+ fputs(strcat(s,"\n"),fc);
}
}
fclose(fp);
- fclose(tmp);
+ fclose(fc);
remove (TODOLIST);
- rename("temp", TODOLIST);
+ rename("/tmp/odot", TODOLIST);
free(s);
}
-void show(void){
- char *c;
-
- c = (char *) malloc(MAXLINE * sizeof(int));
+void show(FILE *fp){
+ char *c = malloc(sizeof(char));
- fp = fopen(TODOLIST,"r");
- while (fgets(c, MAXLINE, fp) != NULL )
+ while (fgets(c, 1, fp) != NULL )
formattask(gettask(fp));
free(c);
}
#include "odot.h"
enum color {BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE};
-extern FILE *fp;
-
-int listcheck(struct task t){
- char *s = malloc(strlen(t.task) * sizeof(char));
-
- fp = fopen(TODOLIST, "r");
- if (fp == NULL)
- error('f');
-
- while (fgets(s, strlen(t.task), fp) != NULL){
- /* strip newline for comparison, return 1 if string matches */
- s[strlen(s) - 1] = 0;
- if (strcmp(t.task, s) == 0){
- free(s);
- return 1;
- }
- }
- free(s);
- return 0;
+ /* Returns:
+ 0 - Nothing matches
+ 1 - Task matches
+ 2 - Group matches
+ 3 - Both match
+ */
+int listcheck(struct task t, FILE *fp){
+ int i;
+ i += (strcmp(t.task,gettask(fp).task) == 0) ? 1 : 0;
+ i += (strcmp(t.group,gettask(fp).group) == 0) ? 2 : 0;
+ return i;
}
return 1;
while (--n > 0 && (*++arg)[0] == '-'){
- c = malloc(strlen(*arg) * sizeof(char));
- c = *arg;
-
- options += strchr(c, 'd') ? 4 : 2;
- options += strchr(c, 's') ? 1 : 0;
-
+ /* if theres a d delete, otherwise add */
+ options += strchr(*arg, 'd') ? 4 : 2;
+ options += strchr(*arg, 's') ? 1 : 0;
/* if theres a g then add the next argument as a group */
- if (strchr(c,'g') != NULL){
- group = *++arg;
+ if (strchr(*arg,'g') != NULL){
+ strcpy(group,*++arg);
}
-
- free(c);
}
return options;
return tmp;
}
+
+void puttask(struct task t, FILE *fp){
+ fprintf(fp,"%s\t%s\t%i\n",t.task,t.group,t.date);
+}
char *getnote(int, char *[]);
short getopt(int, char *[]);
-void add(struct task, int);
-void rem(void);
-void show(void);
+void add(struct task, FILE *);
+void rem(FILE *);
+void show(FILE *);
-int listcheck(struct task);
+int listcheck(struct task, FILE *);
int geturgency(int);
char *gettime(void);
void formattask(struct task t);
void error(int);
-int linecount(FILE);
+int linecount(FILE *);
struct task maketask(char *, char *);
-void puttask(struct task n);
+void puttask(struct task n, FILE*);
struct task gettask(FILE *);
char *note, *o;
PREFIX ?= /usr/local/bin
TODOLIST = $(HOME)/.local/share/odot/todo
-install : main.c input.c file.c
+install : main.c input.c file.c dialogue.c func.c
gcc *.c -o '$(PREFIX)/$(PROG)'
+ [[ ! -d $(TODOLIST) ]] && mkdir -p $(TODOLIST)
header : todo.h
gcc *.h
rm *.gch
rm "$(PREFIX)/$(PROG)"
-test :
- todo
- todo -n something
- todo -d stuff to do now
- todo -ns stuff to do
- todo -ds stuff to do
#include "odot.h"
-extern char *note;
extern FILE *fp;
enum color {BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE};
-
-void add(struct task t, int size){
- int l,m,h,i;
- FILE *fcopy = fopen("/tmp/fcopy", "w");
- char *s = malloc(MAXLINE * sizeof(char));
- h = size;
- m = h / 2;
- l = 0;
-
- while (!(h == m && m == l)){
- i = strcmp(t.task,gettask(fp).task);
-
- if (i < 0) {
- h = m;
- } else if (i > 0) {
- l = m;
- } else {
- dialogue("Task already on list",t.task, BLUE);
- return;
+void add(struct task t, FILE *fp){
+ char *c = malloc(strlen(t.task)*sizeof(char));
+ FILE *fc = fopen("/tmp/odot", "w");
+ int i = 0;
+
+ while (fgets(c,strlen(t.task),fp) != NULL){
+ switch (listcheck(t,fp)){
+ case 3:
+ dialogue("Already on todo list", t.task, CYAN);
+ break;
+ case 1:
+ dialogue("Already on list in a different group", gettask(fp).group, YELLOW);
+ /* confirm(); */
+ break;
+ default:
+ if (i == 0 && strcmp(c,t.task) > 0){
+ puttask(t,fc);
+ ++i;
+ }
}
-
- m = (h + l)/2;
- fseek(fp, m, SEEK_SET);
+ puttask(gettask(fp), fc);
}
- fseek(fp, 0, SEEK_SET);
-
- for (i = 0; i <= (size + 1); i++){
- if (i == m){
- fprintf(fp, "%s\t%i\t%s\n",t.task,t.date,t.group);
- } else {
- fgets(s, MAXLINE, fp);
- fputs(s, fcopy);
- }
- }
+ free(c);
fclose(fp);
- fclose(fcopy);
+ fclose(fc);
remove(TODOLIST);
- rename("/tmp/fcopy", TODOLIST);
- free(s);
- return;
+ rename("/tmp/odot", TODOLIST);
+ return;
}
-void rem(void){
- char *s = malloc(MAXLINE * sizeof(char));
- FILE *tmp = fopen("temp", "w");
-
- fp = fopen(TODOLIST, "r");
+void rem(FILE *fp){
+ char *s = malloc(MAXLINE * sizeof(char));
+ FILE *fc = fopen("/tmp/odot", "w");
while (fgets(s, MAXLINE, fp) != NULL){
s[strlen(s) - 1] = 0;
if (strcmp(note, s) != 0){
- fputs(strcat(s,"\n"),tmp);
+ fputs(strcat(s,"\n"),fc);
}
}
fclose(fp);
- fclose(tmp);
+ fclose(fc);
remove (TODOLIST);
- rename("temp", TODOLIST);
+ rename("/tmp/odot", TODOLIST);
free(s);
}
-void show(void){
- char *c;
-
- c = (char *) malloc(MAXLINE * sizeof(int));
+void show(FILE *fp){
+ char *c = malloc(sizeof(char));
- fp = fopen(TODOLIST,"r");
- while (fgets(c, MAXLINE, fp) != NULL )
+ while (fgets(c, 1, fp) != NULL )
formattask(gettask(fp));
free(c);
}
#include "odot.h"
enum color {BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE};
-extern FILE *fp;
-
-int listcheck(struct task t){
- char *s = malloc(strlen(t.task) * sizeof(char));
-
- fp = fopen(TODOLIST, "r");
- if (fp == NULL)
- error('f');
-
- while (fgets(s, strlen(t.task), fp) != NULL){
- /* strip newline for comparison, return 1 if string matches */
- s[strlen(s) - 1] = 0;
- if (strcmp(t.task, s) == 0){
- free(s);
- return 1;
- }
- }
- free(s);
- return 0;
+ /* Returns:
+ 0 - Nothing matches
+ 1 - Task matches
+ 2 - Group matches
+ 3 - Both match
+ */
+int listcheck(struct task t, FILE *fp){
+ int i;
+ i += (strcmp(t.task,gettask(fp).task) == 0) ? 1 : 0;
+ i += (strcmp(t.group,gettask(fp).group) == 0) ? 2 : 0;
+ return i;
}
return 1;
while (--n > 0 && (*++arg)[0] == '-'){
- c = malloc(strlen(*arg) * sizeof(char));
- c = *arg;
-
- options += strchr(c, 'd') ? 4 : 2;
- options += strchr(c, 's') ? 1 : 0;
-
+ /* if theres a d delete, otherwise add */
+ options += strchr(*arg, 'd') ? 4 : 2;
+ options += strchr(*arg, 's') ? 1 : 0;
/* if theres a g then add the next argument as a group */
- if (strchr(c,'g') != NULL){
- group = *++arg;
+ if (strchr(*arg,'g') != NULL){
+ strcpy(group,*++arg);
}
-
- free(c);
}
return options;
return tmp;
}
+
+void puttask(struct task t, FILE *fp){
+ fprintf(fp,"%s\t%s\t%i\n",t.task,t.group,t.date);
+}
char *getnote(int, char *[]);
short getopt(int, char *[]);
-void add(struct task, int);
-void rem(void);
-void show(void);
+void add(struct task, FILE *);
+void rem(FILE *);
+void show(FILE *);
-int listcheck(struct task);
+int listcheck(struct task, FILE *);
int geturgency(int);
char *gettime(void);
void formattask(struct task t);
void error(int);
-int linecount(FILE);
+int linecount(FILE *);
struct task maketask(char *, char *);
-void puttask(struct task n);
+void puttask(struct task n, FILE*);
struct task gettask(FILE *);
char *note, *o;