diff options
author | FreeArtMan <dos21h@gmail.com> | 2015-11-07 22:13:25 +0000 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2015-11-07 22:13:25 +0000 |
commit | e7ecf0537902ff87965f4473bf316c17b90c39a5 (patch) | |
tree | c79ffb63938e0044b5a6e796a16c59387bddaf15 | |
parent | 04918e30ddd690ec0f1b5fd231eb616ad60aed65 (diff) | |
download | microbbs-master.tar.gz microbbs-master.zip |
-rw-r--r-- | buildinfo.h | 2 | ||||
-rw-r--r-- | microbbs.c | 1 | ||||
-rw-r--r-- | todo.c | 116 | ||||
-rw-r--r-- | todo.h | 4 |
4 files changed, 84 insertions, 39 deletions
diff --git a/buildinfo.h b/buildinfo.h index 9554d85..04c1b02 100644 --- a/buildinfo.h +++ b/buildinfo.h @@ -4,7 +4,7 @@ #include <stdio.h> #include <stdlib.h> -#define BUILD_VERSION "0.2.4" +#define BUILD_VERSION "0.2.6" #define BUILD_DATE (__DATE__) void print_build_info(); @@ -15,6 +15,7 @@ #include "todo.h" #include "door.h" #include "captcha.h" +#include "netbytes.h" #include "libterm/term.h" #include "libterm/term_io.h" @@ -24,30 +24,8 @@ int bbs_todo( term_screen *ts, const char *fname) 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 - + bbs_todo_load( todo_fname, todo_list ); //log that someone use todo bbs_log( NULL, "visited TODO" ); @@ -183,35 +161,97 @@ int bbs_todo_remove( term_screen *ts, List *todo, const char *fname ) term_cur_set_r( ts, 0 ); term_printf( ts, "Remove entry from todo:\n"); - - - return ret; } int bbs_todo_save( const char *fname, List *todo ) { - int ret=-1; - - f_file *f=NULL; + FILE *f; struct ListNode *iter=todo->first; + int count_max = 0; + + f = fopen( fname, "w" ); + + if ( f == NULL ) + { + return -1; + } - f = f_file_open( fname, F_FILE_WRITE ); while ( iter != NULL ) { - size_t str_size=strlen( iter->val )+2; + + netbyte_store nb; + nb_u8arr td; + int er=0; + uint8_t *res=NULL; + + size_t str_size = strlen( iter->val ); char *str = malloc( str_size ); - memcpy( str, iter->val, str_size-2); - str[str_size-1] = '\0'; - str[str_size-2] = '\n'; - f_file_write( f, strlen(str), str ); - printf( "%s\n", str ); fflush( stdout ); + + memcpy( str, iter->val, str_size ); + + nb_init( &nb ); + er = nb_u8arr_create( &td, str_size, (uint8_t *)str ); + if (er) + printf("er create u8arr: %d\n",er); + + er = nb_add_u8arr( &nb, &td ); + if (er) + printf("er add u8arr: %d\n",er); + + res = nb_create( &nb ); + + + + fwrite( res, 1, nb.size, f ); + free( str ); + free( res ); + iter = iter->next; + if ( count_max > 128 ) + break; + count_max += 1; } - f_file_close( f ); - return ret; + fclose( f ); + + return 0; +} + +int bbs_todo_load( const char *fname, List *todo ) +{ + FILE *f; + int i; + __nb_type t; + + netbyte_store nb; + + nb_init( &nb ); + + f = fopen( fname, "r" ); + if ( f == NULL ) + { + return -1; + } + + while (nb_fread( &nb, fileno(f) ) == 0) + { + for ( i=0; i<nb_count(&nb); i++ ) + { + t = nb.types[i]; + if (t.type == NBT_U8ARRAY) + { + nb_u8arr *l = (nb_u8arr *)t.nb_val; + char *u = malloc( l->len ); + memcpy( u, l->val, l->len ); + llist_push( todo, u ); + } + } + } + fclose( f ); + + return 0; } #endif
\ No newline at end of file @@ -7,12 +7,16 @@ #include "logs.h" #include "file_use.h" #include "list.h" +#include "netbytes.h" #include "kconfig.h" +#define TODO_MAX_NUMBER 40 + int bbs_todo( term_screen *, const char *); int bbs_todo_add( term_screen*, List*, const char*); int bbs_todo_remove( term_screen*, List*, const char* ); int bbs_todo_save( const char*, List* ); +int bbs_todo_load( const char*, List* ); #endif
\ No newline at end of file |