summaryrefslogtreecommitdiff
path: root/articles.c
blob: e7586dcd5fd3cf912dab7dd2a8017edeafb2e57d (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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "articles.h"

#ifdef CONFIG_ARTICLES

//TODO add checkout on size of art it will fix,
//     warn about cutted images
int bbs_article( term_screen *ts, const char *fname )
{
	int ret=-1;
	int i=0;

	FILE *f;
	size_t str_size=0, fret=1;
	char *str=NULL;
	size_t in_size=0;
	char *in_buf=NULL;
	int max_x, max_y, ret_len;
	int quit_loop=0;

	if ( ts->mode == SCREEN_MODE_80x24 )
	{
		max_x = 80;
		max_y = 24;
	} else
	{
		printf("Unknown mode\n");
		max_x = 80;
		max_y = 24;
	}

	bbs_log_article( NULL );
	
	if ( fname != NULL )
	{
		f = fopen( fname, "r" );
		if ( f == NULL )
		{
			printf("Cannot open article file\n");
			return -1;
		}
		
		fret = 1;
		i = 0;
		while ( (fret > 0) && (quit_loop == 0) )
		{
			fret = getline( &str, &str_size, f );
			if ( fret > 0 )
			{
				printf("%s", str);
				i += 1;
			}

			//if ( ((i > 0) && (fret == -1)) ||
			//	((fret > 0) && (i > max_x-1)) )
			if ( i >= max_y-1 )
			{
				printf("(N)ext,(D)ump,(Q)uit:");
				ret_len = getline( &in_buf, &in_size, stdin );
				if ( ret_len > 0 )
				{
					char ch = in_buf[0];
					switch( ch )
					{
						case 'q':
						case 'Q':
							quit_loop = 1;
							break;
						case 'n':
						case 'N':
							i = 0;
							break;
						//dump whole file to the screen
						case 'd':
						case 'D':
							{
								char tmp_buf[81];
								size_t tmp_size;
								fseek( f, 0, SEEK_SET );							
								while ( (tmp_size = fread( &tmp_buf, 1, 80, f )) > 0 )
								{
									tmp_buf[ tmp_size ] = 0;
									printf( "%s", tmp_buf );
								}
							}
							i = 0;
							break;
						default:
							printf("Try more\n");
					}
				}
			}

			if ( (fret == -1) )
				break;

		}
		
		fclose( f );
		ret = 0;
	}

	return ret;
}


//just for full fill libc example
static int dir_article_selector (const struct dirent *unused)
{
  return 1;
}

int bbs_article_list( term_screen *ts, const char *dir_name )
{
	int ret=-1;
	int max_x=-1, max_y=-1;
	size_t str_size=0,fret=-1;
	int ret_len;
	char *str=NULL;
	size_t in_size=0;
	char *in_buf=NULL;
	int quit_loop=0;
	int i;

	//list dir
	struct stat path_node;
	struct dirent **eps;
	int n;

	//get all articles names in list and use list every time needed
	List *dir_list = llist_new();

	if ( ts->mode == SCREEN_MODE_80x24 )
	{
		max_x = 80;
		max_y = 24;
	} else
	{
		printf("Unknown mode\n");
		max_x = 80;
		max_y = 24;
	}

	bbs_log_article_list( NULL );

	if ( dir_name != NULL )
	{
		//should have some bugs be carefull
		n = scandir( dir_name, &eps, dir_article_selector, alphasort );
		if ( n >= 0)
		{
			int cnt;
			for ( cnt=0; cnt<n; cnt++)
			{
				size_t tmp_size = strlen( eps[cnt]->d_name );
				char *cnct_path = malloc( tmp_size + strlen(dir_name)+1);
				memcpy( cnct_path, dir_name, strlen(dir_name));
				memcpy( cnct_path+sizeof(dir_name)+2, eps[cnt]->d_name, 
					tmp_size+1 );

				//check if its file
				if ( stat( cnct_path, &path_node ) == 0 )
				{
					if ( path_node.st_mode & S_IFREG )
					{
						llist_push( dir_list, cnct_path );
					}
				}
			}
		} else
		{
			printf("Err\n");
			ERROR("Cannot open article directory\n");
		}
		ret = 0;
	}


	i = 0;
	while ( (quit_loop == 0) )
	{
		printf("(L)ist articles,(R)read article,(Q)uit:");
		ret_len = getline( &in_buf, &in_size, stdin );
		if ( ret_len > 0 )
		{
			char ch = in_buf[0];
			switch( ch )
			{
				case 'q':
				case 'Q':
					quit_loop = 1;
					break;
				case 'l':
				case 'L':
					{
						//print list of articles
						struct ListNode *iter=dir_list->first;
						int cnt = 1;
						while (iter != NULL)
						{
							printf( "%d%s\n", cnt, (char *)iter->val);
							cnt += 1;
							iter = iter->next;
						}
					}
					break;
				case 'r':
				case 'R':
					{
						//while there is no more 10 articles use only 1 number
						//TODO fix that
						int article_number = in_buf[1]-'0';
						if ( article_number > 0 && article_number < 10)
						{
							struct ListNode *iter = dir_list->first;
							int cnt = 1;
							while ( iter != NULL )
							{
								if ( cnt == article_number )
								{
									bbs_article( ts, iter->val );
									break;
								}
								cnt += 1;
								iter = iter->next;
							}
						}
					}
					break;
				default:
					printf("Try more\n");
			}
		}
	}

	llist_free( dir_list );


	return ret;
}

#endif