Browse Source

增加进度条

master
453530270@qq.com 2 years ago
parent
commit
d059c9e207
  1. 4
      ftree.h
  2. 28
      server.c

4
ftree.h

@ -28,6 +28,10 @@
#define PORT 30100 #define PORT 30100
#endif #endif
# 进度条
#define PROCBAR_LEN 101
#define PROC_STYLE '#'
struct request { struct request {
int type; // Request type is REGFILE, REGDIR, TRANSFILE int type; // Request type is REGFILE, REGDIR, TRANSFILE
char path[MAXPATH]; char path[MAXPATH];

28
server.c

@ -26,6 +26,8 @@ struct request *createrequest(void);
void complete_path(const char *child, char *s); void complete_path(const char *child, char *s);
int handleclient(struct client *p); int handleclient(struct client *p);
int checkfile(struct client *p); int checkfile(struct client *p);
// 百分比进度条
void procbar(double total,double current);
int bindandlisten(unsigned short port); int bindandlisten(unsigned short port);
@ -109,6 +111,7 @@ void rcopy_server(unsigned short port, char *dest) {
/* /*
* Based on the state of the client complete task. * Based on the state of the client complete task.
*/ */
int handleclient(struct client *p) { int handleclient(struct client *p) {
@ -211,6 +214,7 @@ int handleclient(struct client *p) {
status = ERROR; status = ERROR;
} }
} while((len == 1) && ++s < p->info->size); } while((len == 1) && ++s < p->info->size);
//close file handle
fclose(in); fclose(in);
if (len != 1) { if (len != 1) {
perror("server: reading file"); perror("server: reading file");
@ -378,3 +382,27 @@ int checkfile(struct client *p) {
} }
} }
} }
/**
*
*/
void procbar(double total,double current){
char bar[PROCBAR_LEN];
memset(bar,'\0',sizeof(bar));
//
int cnt=0;
int len= strlen(label);
// 占比
double rate = (100*current)/total;
int loop_count = (int) rate;
//loop print
whiel(cnt<=loop_count){
printf("[%-100s][%.11f%%][%c]\r",bar,rate,label[cnt%len]);
// 直接输出不,非缓冲区
fflush(stdout);
bar[cnt++] = PROC_STYLE;
usleep(2000);
}
// 输出换行,反正被覆盖
printf("\n");
}
Loading…
Cancel
Save