summaryrefslogtreecommitdiffstats
path: root/todo.c
blob: aa074b290ea76a5ae02a2463122953e72fe2c33e (plain) (blame)
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
#include "todo.h"

#ifdef CONFIG_TODO

int bbs_todo( term_screen *ts, const char *fname)
{
	int ret=0;

	int ret_len;
	size_t in_size=0;
	char *in_buf=NULL;
	int quit_loop=0;
	int menu_input = 0;
	char menu_cmd = 0;
	int row = 0;
	char *todo_fname = NULL;

	if ( ts == NULL )
		return ret;


	if ( fname == NULL )
	{
		todo_fname = CONFIG_TODO_DEFAULT_FILE;
	} else
	{
		todo_fname = fname;
	}

	//LOAD DATA FROM FILE TO LINKED LIST
	List *todo_list = llist_new();
	f_file *file = f_file_open( todo_fname, F_FILE_READ );
	if ( file != NULL )
	{
		const int l_s = 128;
		int r_v;
		{
		cycle0:;
			char *l = malloc( l_s );
			r_v = f_file_readl( file, l_s, l );
			if ( r_v < 0 ) goto exit_cycle0;
			l[r_v] = '\0';
			llist_push( todo_list, l );
			goto cycle0;
		exit_cycle0:;
			if ( l != NULL ) free( l );
		}

	}
	f_file_close( file );
	//LOAD DATA FROM FILE TO LINKED LIST
	//END


	//log that someone use todo
	bbs_log( NULL, "visited TODO" );

	while( (quit_loop == 0) )
	{
		term_clr_scr( ts );
		term_cur_set_c( ts, 0 );
		printf("(A)dd,(Q)uit"); fflush( stdout );

		//dispaly todo list
		row = term_get_maxrow( ts );
		term_cur_set_c( ts, 0 );
		{
			struct ListNode *iter=todo_list->first;
			int cnt = 1;
			while ( (iter != NULL) || ( cnt > row - 2))
			{
				term_cur_set_c( ts, 0 );
				term_cur_set_r( ts, 1+cnt );
				printf( "[%02d] -> %s\n", cnt, (char *)iter->val);
				cnt += 1;
				iter = iter->next;
			}
		}


		term_cur_set_c( ts, 0 );
		term_cur_set_r( ts, term_get_maxrow( ts ) );
		printf(":"); fflush( stdout );

		menu_input = term_getc( ts );
		//if something whent wrong dont know why, need to get some test case
		if ( menu_input == -1 )
			continue;
		menu_cmd = (char)menu_input;

		switch( menu_cmd )
		{
			case 'q':
			case 'Q':
				quit_loop = 1;
				break;

			case 'a':
			case 'A':
				bbs_todo_add( ts, todo_list, todo_fname );
				break;

			case 's':
			case 'S':
				{
					struct ListNode *iter=todo_list->first;
					int cnt = 1;
					while (iter != NULL)
					{
						printf( "[%02d] -> %s\n", cnt, (char *)iter->val);
						cnt += 1;
						iter = iter->next;
					}
				}
				break;

			default:
				printf("Try more\n");
		}
	}

	llist_free( todo_list );

	return ret;
}

int bbs_todo_add( term_screen *ts, List *todo, const char *fname)
{
	int ret = -1;
	int fret = -1;
	const int buf_size = 64;
	char buf[buf_size];

	if ( ts == NULL )
		return -1;

	if ( fname == NULL )
		return -1;

	term_clr_scr( ts );
	term_cur_set_c( ts, 0 );
	term_cur_set_r( ts, 0 );
	printf("Add new todo task:\n"); fflush( stdout );

	memset( buf, 0, buf_size );
	term_cur_set_c( ts, 0 );
	fret = term_readline( ts, buf, buf_size, READLINE_ALPHA );
	if ( fret > 0 )
	{
		char *l = malloc( fret );
		memcpy( l, buf, fret );
		llist_push( todo, l );
		bbs_todo_save( fname, todo );
		ret = 0;
	} else
	{
		return -1;
	}

	return ret;
}

int bbs_todo_save( const char *fname, List *todo )
{
	int ret=-1;

	f_file *f=NULL;
	struct ListNode *iter=todo->first;

	f = f_file_open( fname, F_FILE_WRITE );
	while ( iter->next != NULL )
	{
		f_file_write( f, strlen(iter->val), iter->val );
		printf("%s\n", iter->val );
		iter = iter->next;
	}
	f_file_close( f );

	return ret;
}

#endif