#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> //write()
#include <string.h> //strlen()
#define N 200
char buf[N];
char buf_backup[N];
int delete_char_from_file(char *file, char ch);
int insert_char_from_file(char *file, const char *before, const char *insert);
void ble_statistics(char *file);
int main(int argc, const char *argv[])
{
FILE *fp1 = NULL, *fp2 = NULL;
int i = 0;
int start=0,end=0;
int count = 0;
char file[100] = {0};
if((fp1 = fopen(argv[1], "r")) == NULL){
perror("fail to fopen");
return -1;
}
strcpy(file,argv[1]);
file[strlen(argv[1])-2] = '\0';
strcat(file, "_hand.c");
if((fp2 = fopen(file, "w+")) == NULL){
perror("fail to fopen");
return -1;
}
LIBLE1:
while( fgets(buf,N,fp1)!=NULL ) //不可以使用 != -1; 因为当读到文件末尾时会返回0, 产生死循环
{
i=0;
while(buf[i++]!='<'){// 1<3456>89
if(i==N){
goto LIBLE1;
}
}
if(strncmp(buf+i-11, "notified: ", 10)!=0){
continue;
}
start=i;
while(buf[i++]!='>');
end=i;
memset(buf_backup, '\0', N);
strncpy(buf_backup, buf+start,end-start-1);
fputs(buf_backup, fp2);
printf("%s\n", buf_backup);
}
if(fclose(fp1)!=0){
perror("fail to fclose");
return -1;
}
if(fclose(fp2)!=0){
perror("fail to fclose");
return -1;
}
if(delete_char_from_file(file,' ')==-1){
printf("fail to delete_char_from_file\n");
}
insert_char_from_file(file, "55aa", "\n");
ble_statistics(file);
return 0;
}
int to(char ch)
{
int k = 0;
if(ch >'9'){//a b c d e f
k = ch -'a'+ 10;
}else{
k = ch -'0';
}
return k ;
}
void ble_statistics(char *file)
{
FILE *fp = NULL;
char buf[1024];
int i = 0, k =333;
int sum = 0;
int backups[3];
struct {
long package_count;
int list[100][5];
}record;
if((fp = fopen(file, "r")) == NULL){
perror("fail to fopen");
return;
}
while(fgets(buf, 1024, fp)!=NULL){ // 12345
if(strncmp(buf,"55aa",4)!=0){
continue;
}else if(k==333 && strncmp(buf,"55aa",4)==0){
k = to(*(buf+12))*16+to(*(buf+13));
continue;
}
backups[2]=backups[1];
backups[1]=backups[0];
if(strlen(buf) != 15){
backups[0]=0x111;
record.package_count++;
continue;
}else{
backups[0]=to(*(buf+12))*16+to(*(buf+13));
}
if(backups[1]==0x111){
record.list[i][0] = record.package_count;
if(i==0){
record.list[i][1] = record.package_count-k;
}else{
record.list[i][1] = record.package_count-record.list[i-1][0]-1;
}
record.list[i][2] = backups[2]+2;
record.list[i][3] = backups[0]-1;
if(backups[0] <= backups[2]){
record.list[i][4] =backups[0]-backups[2] -1 + 256 ;
}else{
record.list[i][4] =backups[0]-backups[2]-1;
}
i++;
record.package_count++;
continue;
}else if(backups[0]!=(backups[1]+1)%256){
record.list[i][0] = record.package_count;
if(i==0){
record.list[i][1] = record.package_count-k;
}else{
record.list[i][1] = record.package_count-record.list[i-1][0]-1;
}
record.list[i][2] = backups[2]+2;
record.list[i][3] = backups[0]-1;
if(backups[0] <= backups[2]){
record.list[i][4] =backups[0]-backups[2] -2 + 256 ;
}else{
record.list[i][4] =backups[0]-backups[2]-2;
}
i++;
record.package_count++;
continue;
}
record.package_count++;
}
printf("--------------------------------------------\n");
printf(" 行数 发包数 丢包起 丢包止 丢包数\n");
for(k = 0; k<i;k++){
if(k !=0 ){
sum +=record.list[k][4];
}
printf("%7d;%7d;%7d;%7d;%7d\n",record.list[k][0],record.list[k][1],record.list[k][2],record.list[k][3],record.list[k][4]);
}
printf(" 丢包数=%ld 包\n",sum);
printf("有效检测包数=%ld 包\n",record.package_count-k-1);
printf(" 丢包率=%.3lf %%\n",(double)sum/(record.package_count-k-1)*100);
printf("--------------------------------------------\n");
if(fclose(fp)!=0){
perror("fail to fclose");
}
return;
}
int insert_char_from_file(char *file, const char *before, const char *insert)
{
int M = 5120000;//byte
char *temp;
char *buffer = NULL;
char cha;
FILE *fp = NULL;
int count = 0;
int i = 0;
int before_count,insert_count;
while(*(before+(i++))!='\0');
before_count = i-1;
i = 0;
while(*(insert+(i++))!='\0');
insert_count = i-1;
if((buffer = malloc(M))==NULL){
perror("fail to malloc");
return -1;
}
if((temp = malloc(before_count+1))==NULL){
perror("fail to malloc");
return -1;
}
memset(temp, '\0', before_count+1);
if((fp = fopen(file, "r+")) == NULL){
perror("fail to fopen");
return -1;
}
while(1){
if(fgets(temp, before_count+1, fp) == NULL){
break;
}
//printf("----------------%s\n",temp);
if(count+before_count > M){
printf("the file too long,please < %d byte\n",M);
return -1;
}
//printf("%s\n",temp);
if(strncmp(temp, before, before_count)!=0){ //1238uui55aa456
//printf("****\n");
*(buffer+count)=*temp;
count++;
fseek(fp, -strlen(temp)+1, SEEK_CUR);
}else{
strncpy((buffer+count), insert, insert_count);
count++;
strncpy((buffer+count), before, 1);
count = count + insert_count;
fseek(fp, -strlen(temp)+1, SEEK_CUR);
}
}
if(fclose(fp)!=0){
perror("fail to fclose");
return -1;
}
if((fp = fopen(file, "w+")) == NULL){
perror("fail to fopen");
return -1;
}
while(i<=count && fputc(*(buffer+i),fp)){
i++;
}
if(fclose(fp)!=0){
perror("fail to fclose");
return -1;
}
free(buffer);
free(temp);
return 0;
}
int delete_char_from_file(char *file, char ch)
{
int M = 5120000;//byte
char *buffer = NULL;
char cha;
FILE *fp = NULL;
int count = 0;
int i = 0;
if((buffer = malloc(M))==NULL){
perror("fail to malloc");
return -1;
}
if((fp = fopen(file, "r+")) == NULL){
perror("fail to fopen");
return -1;
}
while((cha = fgetc(fp)) != EOF){
if(count > M){
printf("the file too long,please < %d byte\n",M);
return -1;
}
if(cha == ch){
continue;
}
*(buffer+count)=cha;
count++;
}
if(fclose(fp)!=0){
perror("fail to fclose");
return -1;
}
if((fp = fopen(file, "w+")) == NULL){
perror("fail to fopen");
return -1;
}
while(i<=count && fputc(*(buffer+i),fp)){
i++;
}
if(fclose(fp)!=0){
perror("fail to fclose");
return -1;
}
free(buffer);
return count;
}