#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; int quit_loop=0; int max_row=0; int menu_input=0; char menu_cmd=0; bbs_log_article( NULL ); if ( fname != NULL ) { f = fopen( fname, "r" ); if ( f == NULL ) { term_printf( ts, "Cannot open article file\n"); return -1; } term_clr_scr( ts ); max_row = term_get_maxrow( ts ); fret = 1; i = 0; while ( (fret > 0) && (quit_loop == 0) ) { fret = getline( &str, &str_size, f ); if ( fret > 0 ) { term_cur_set_c( ts, 0 ); printf("%s", str); i += 1; } if ( i >= max_row ) { term_cur_set_c( ts, 0 ); term_cur_set_r( ts, max_row ); term_printf( ts, "(N)ext,(D)ump,(Q)uit:"); 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 'n': case 'N': i = 0; break; //dump whole file to the screen case 'd': case 'D': { char *dump_line=NULL; size_t dump_line_size=0; size_t tmp_size=-1; fseek( f, 0, SEEK_SET ); while( (tmp_size = getline(&dump_line, &dump_line_size, f)) != -1 ) { term_cur_set_c( ts, 0 ); term_printf( ts, "%s", dump_line); } FREE( dump_line ); term_getc( ts ); } i = 0; break; default: term_printf( ts, "Try more\n"); } } if ( (fret == -1) ) term_getc( ts ); if ( (fret == -1) ) break; } FREE( str ); 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 quit_loop=0; int menu_input = 0; char menu_cmd = 0; //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(); 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; cntd_name ); sds pathname = sds_new( dir_name ); pathname = sds_cat( pathname, d_name ); sds_free( d_name ); //check if its file if ( stat( pathname, &path_node ) == 0 ) { if ( path_node.st_mode & S_IFREG ) { llist_push( dir_list, pathname ); } else { sds_free( pathname ); } } else { sds_free( pathname ); } free( eps[cnt] ); } free( eps ); } else { term_printf( ts, "Err\n"); ERROR("Cannot open article directory\n"); } ret = 0; } else { ERROR("No directory specified\n"); return ret; } quit_loop = 0; while ( (quit_loop == 0) ) { term_clr_scr( ts ); term_cur_set_c( ts, 0 ); term_printf( ts, "(L)ist articles,(R)read article,(Q)uit"); fflush( stdout ); term_cur_set_c( ts, 0 ); term_cur_set_r( ts, term_get_maxrow( ts ) ); term_printf( ts, ":"); 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 'l': case 'L': { term_cur_set_r( ts, 2 ); //print list of articles struct ListNode *iter=dir_list->first; int cnt = 1; while (iter != NULL) { struct stat art_stat; term_cur_set_c( ts, 0 ); //get stat structure and get filesize stat( iter->val, &art_stat );//no err check term_printf( ts, "[%02d] %s ( %ld bytes)\n", cnt, (char *)iter->val, art_stat.st_size ); cnt += 1; iter = iter->next; } term_getc( ts ); } break; case 'r': case 'R': { //while there is no more 10 articles use only 1 number //TODO fix that term_cur_set_c( ts, 0 ); term_printf( ts, "Input article number 1-9:"); int article_input = term_getc( ts ); //if something whent wrong dont know why, need to get some test case char article_number = (char)article_input-'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: term_printf( ts, "Try more\n"); } } //needed special care to free sds strings //dangerous stuff as list contains sds strings then we need to call sds_free //not just free, maybe try to use llist_manager it should handle that { struct ListNode *iter=dir_list->first; while (iter != NULL) { if ( iter->val != NULL ) { sds_free( iter->val ); iter->val = NULL; } iter = iter->next; } } llist_free( dir_list ); return ret; } #endif