最近写一个程序来取出一个存在XML中的plugins6的报警信息
结果被恶心蒙了
现在终于明白内存对于C/C++选手来说是多么重要,一堆信息,大概有个几万条,每条记录有使几个字段,每个字段大约占几千个字节,刚开始自己选择结构体数组来实现,悲哀的发现:结构体数组是顺序存储,要想存放所有的记录,必须要一块很大且连续的内存空间来实现,可怜我的内存了
源代码:
没别的,就是墨迹加上恶心
#include
#include
#include
#include
#include
/*MAX_SIZE为字段类型的个数*/
#define MAX_SIZE 17
/*定义结构体来存放各个字段*/
typedef struct
{
/*存放该日志信息的各个字段*/
char field[17][4000] ;
}record_sql;
/*定义结构体数组,此处有些BUG*/
record_sql record[4500] ;
/*各个字段处理的函数*/
void deal_field(char *str, int flag) ;
void deal_id(char *str) ;
void deal_name(char *str) ;
void deal_cue_id(char *str) ;
void deal_bugtraq_id(char *str) ;
void deal_summary(char *str) ;
void deal_version(char *str) ;
void deal_category(char *str) ;
void deal_dependencies(char *str) ;
void deal_filename(char *str) ;
void deal_required_ports(char *str) ;
void deal_xrefs(char *str) ;
void deal_required_keys(char *str) ;
void deal_required_udp_ports(char *str) ;
void deal_excluded_keys(char *str) ;
void deal_timeout(char *str) ;
void deal_description(char *str) ;
void deal_copyright(char *str) ;
/*数据库操作的函数*/
int log_mysql_connection(void) ;
void display_header(void) ;
void display_row(void) ;
int log_mysql_insert(char *sqlstr) ;
int log_mysql_select(char *sqlstr) ;
int main(){
/*用来找各条记录的字段*/
FILE* fp_start = NULL ;
pcre *re ;
const char *error ;
int erroffset ;
int ovector[100] ;
int rc ;
char msg[4000] = {0} ;
/*'='的个数为57个,注意要固定格式,便于文件的读取*/
char *str = "=========================================================" ;
int n ;
int flag = 0;
/*用来控制记录的数目*/
int i = 0 ;
/*定义正则式*/
char *rex[MAX_SIZE];
rex[0] = "" ;
rex[1] = "" ;
rex[2] = "" ;
rex[3] = "" ;
rex[4] = "" ;
rex[5] = "" ;
rex[6] = "" ;
rex[7] = "" ;
rex[8] = "" ;
rex[9] = "" ;
rex[10] = "" ;
rex[11] = "" ;
rex[12] = "" ;
rex[13] = "" ;
rex[14] = "" ;
rex[15] = "" ;
rex[16] = "" ;
/*打开文件*/
fp_start = fopen("plugins6.xml", "r+") ;
if(fp_start == NULL){
perror("fopen return error\n") ;
exit(1);
}
/*读出“===”之间的字符串
*定位到两个“===”
*/
while(fgetc(fp_start) != '='){
;
}
fgets(msg,sizeof(msg),fp_start) ;
printf("The start msg : %s \n",msg) ;
memset(msg,0,sizeof(msg)) ;
int end ;
/*这层循环用来读一条记录的各个字段*/
while(end !=0){
end = strcmp(msg,str) ;
memset(msg,0,sizeof(msg)) ;
/*取出一行信息*/
fgets(msg,sizeof(msg),fp_start) ;
printf("%s \n",msg) ;
/*循环匹配*/
while(flag<17){
re = pcre_compile(rex[flag], 0, &error, &erroffset, NULL);
if (re == NULL) {
printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
}
rc = pcre_exec(re, NULL, msg, strlen(msg), 0, 0, ovector, 100);
if (rc < 0) {
flag++;
continue;
}
else{
/*如果为DESCRIPTION字段的话,需要将好几行插到一个字符串中*/
if(flag == 15){
char c ;
char temp[2] ;
while((c = fgetc(fp_start)) != '<'){
sprintf(temp,"%c",c);
strcat(msg,temp);
}
}
/*把取来的串进行相应的操作,把msg和flag当作参数传给函数*/
printf("%s \n",msg) ;
strcpy(record[i].field[flag],msg) ;
flag=0;
}
free(re);
break;
}
if (flag == 17)
{
i++ ;
fgets(msg,sizeof(msg),fp_start) ;
flag = 0 ;
}
}
/*关闭文件指针,这时候已经把所有的记录都存放到结构体中了*/
fclose(fp_start);
int j,k;
for(j = 0 ; j < i-1 ; j++){
for(k = 0 ; k < 17 ; k++)
printf("The %d , %d is %s\n",j,k,record[j].field[k]) ;
}
/*查看成功后,处理去掉那些恶心的玩意*/
for(j = 0 ; j < i-1 ; j++){
for(k = 0 ; k < 17 ; k++){
if(record[j].field[k][0]){
deal_field(record[j].field[k], k) ;
}
}
}
/*查看一下这个结构体中是不是已经去掉了那些恶心玩意*/
for(j = 0 ; j < i-1 ; j++){
printf("\n") ;
printf("\n") ;
/*定义SQL语句*/
char sqlstr[50000] ;
/*编写SQL语句*/
memset(sqlstr,0,sizeof(sqlstr));
sprintf(sqlstr,"insert into plugins6 values(\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\')",record[j].field[0],record[j].field[1],record[j].field[2],record[j].field[3],record[j].field[4],record[j].field[5],record[j].field[6],record[j].field[7],record[j].field[8],record[j].field[9],record[j].field[10],record[j].field[11],record[j].field[12],record[j].field[13],record[j].field[14],record[j].field[15],record[j].field[16]);
printf("**************%s****************\n",sqlstr) ;
/*往数据库中插*/
log_mysql_main(sqlstr) ;
for(k = 0 ; k < 17 ; k++)
printf("\nThe %d , %d is %s\n",j,k,record[j].field[k]) ;
}
}
void deal_field(char *str, int flag){
switch(flag){
case 0:
deal_id(str) ;
break ;
case 1:
deal_name(str) ;
break ;
case 2:
deal_cue_id(str) ;
break ;
case 3:
deal_bugtraq_id(str) ;
break ;
case 4:
deal_summary(str) ;
break ;
case 5:
deal_version(str) ;
break ;
case 6:
deal_category(str) ;
break ;
case 7:
deal_dependencies(str) ;
break ;
case 8:
deal_filename(str) ;
break ;
case 9:
deal_required_ports(str) ;
break ;
case 10:
deal_xrefs(str) ;
break ;
case 11:
deal_required_keys(str) ;
break ;
case 12:
deal_required_udp_ports(str) ;
break ;
case 13:
deal_excluded_keys(str) ;
break ;
case 14:
deal_timeout(str) ;
break ;
case 15:
deal_description(str) ;
break ;
case 16:
deal_copyright(str) ;
break ;
default:
break ;
}
}
void deal_id(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+4 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_name(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+6 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_cue_id(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+8 ;
q = strstr(p,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_bugtraq_id(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+12 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_summary(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+9 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_version(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+9 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_category(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+10 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_dependencies(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[4000]={0} ;
p = str+14 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_filename(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+10 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_required_ports(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+16 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_xrefs(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+7 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_required_keys(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+15 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_required_udp_ports(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+20 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_excluded_keys(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+15 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_timeout(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[1000]={0} ;
p = str+9 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_description(char *str){
char *flag ="";
int i = 0 ;
char *p;
char temp[5000]={0} ;
p = str+13 ;
while(*p!= '\0') { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
void deal_copyright(char *str){
char *flag ="";
int i = 0 ;
char *p;
char *q;
char temp[100]={0} ;
p = str+11 ;
q = strstr(str,flag) ;
while(p!= q) { temp[i] = *p; p++; i++ ;}
memset(str,0,strlen(str));
strcpy(str,temp);
}
恶心了两天了,我受够了