summaryrefslogtreecommitdiffstats
path: root/tasklst.c
blob: 7e8b22feebe5538cb26c874fa2be151bc053d084 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* See LICENSE file for copyright and license details. */

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "tasklst.h"

static int task_lst_count_status(TaskLst list, int status);
static Task *task_lst_get_last_task(TaskLst list);

static int
task_lst_count_status(TaskLst list, int status)
{
	int total;
	Task *ptr;
	
	for (ptr = list.first, total = 0; ptr != NULL; ptr = ptr->next) {
		if (ptr->status == status)
			++total;
	}

	return total;
}

static Task *
task_lst_get_last_task(TaskLst list)
{
	Task *ptr, *next;

	ptr = list.first;
	while (ptr != NULL) {
		next = ptr->next;
		if (next == NULL)
			return ptr;

		ptr = next;
	}

	return ptr;
}

void
task_lst_init(TaskLst *list)
{
	list->expiry = 0;
	list->first = NULL;
}

void
task_lst_cleanup(TaskLst *list)
{
	Task *ptr, *next;
	
	ptr = list->first;
	while (ptr != NULL) {
		next = ptr->next;
		free(ptr);
		ptr = next;
	}
}

void
task_lst_set_expiration(TaskLst *list, int64_t delta)
{
	list->expiry = time(NULL) + delta;
}

int
task_lst_expired(TaskLst list)
{
	return (time(NULL) > list.expiry) ? 1 : 0;
}

int
task_lst_count_todo(TaskLst list)
{
	return task_lst_count_status(list, TASK_TODO);
}

int
task_lst_count_done(TaskLst list)
{
	return task_lst_count_status(list, TASK_DONE);
}

Task *
task_lst_get_task(TaskLst list, int i)
{
	Task *ptr;
	
	for (ptr = list.first; i > 0; ptr = ptr->next) {
		if (ptr == NULL) /* We're out of bounds */
			return NULL;

		--i;
	}

	return ptr;
}

int
task_lst_add_task(TaskLst *list, int status, const char *str)
{
	Task *last, *newtask;

	newtask = malloc(sizeof(Task));
	if (newtask == NULL)
		return -1;

	newtask->status = status;
	strncpy(newtask->tdesc, str, TASK_LST_DESC_MAX_SIZE);

	last = task_lst_get_last_task(*list);
	if (last == NULL)
		list->first = newtask;
	else
		last->next = newtask;

	newtask->prev = last; /* Also if last == NULL */
	newtask->next = NULL;

	return 0;
}

int
task_lst_edit_task(TaskLst *list, int i, const char *newstr)
{
	Task *edit;

	if ((edit = task_lst_get_task(*list, i)) == NULL)
		return -1;
	
	strncpy(edit->tdesc, newstr, TASK_LST_DESC_MAX_SIZE);

	return 0;
} 

int
task_lst_del_task(TaskLst *list, int i)
{
	Task *del, *prev, *next;

	if ((del = task_lst_get_task(*list, i)) == NULL)
		return -1;

	prev = del->prev;
	next = del->next;

	if (prev != NULL)
		prev->next = next;

	if (next != NULL)
		next->prev = prev;

	if (list->first == del)
		list->first = next;

	free(del);

	return 0;
}

int
task_lst_read_from_file(TaskLst *list, FILE *fp)
{
	int stat_buf;
	char *ptr, *endptr;
	char linebuf[TASK_LST_DESC_MAX_SIZE];
	
	task_lst_init(list);

	if (fscanf(fp, "%" SCNd64 "\n", &list->expiry) <= 0)
		return -1;

	while (feof(fp) == 0) {
		if (fgets(linebuf, sizeof(linebuf), fp) == NULL)
			break;

		ptr = strtok(linebuf, "\t");
		if (ptr == NULL)
			return -1;

		stat_buf = strtol(ptr, &endptr, 10);
		if (endptr[0] != '\0')
			return -1;

		ptr = strtok(NULL, "\n");
		if (ptr == NULL)
			return -1;

		task_lst_add_task(list, stat_buf, ptr);
	}

	return 0;
}

void
task_lst_write_to_file(FILE *fp, TaskLst list)
{
	Task *ptr;

	fprintf(fp, "%" PRId64 "\n", list.expiry);

	for (ptr = list.first; ptr != NULL; ptr = ptr->next)
		fprintf(fp, "%d\t%s\n", ptr->status, ptr->tdesc);
}