/*
id=firewall time=1111 fw = dst = type dddd= Virus
如果值中有空格的话,就用双引号引起来。
每个键值对用空格隔开,但是空格不一定是一个,可能是几个。
每个键值对可能是只有键,而没有值。比如那个dst。
更加复杂的情况是有的键后面跟空格再根等号。
还有情况是等号后面加空格,在加其值
要分析的字符串,把它按照键值对的组合一对一对的插入到数据库中去
by wzt <[email protected]>
*/
#include <stdio.h>
#include <string.h>
#define DEBUG
#define MAX 100
char *str = "id=firewall time=\"1111\" fw =dst = \"type\" dddd= Virus";
void extract_str(char *str);
void extract_str(char *str)
{
char a[MAX],b[MAX];
char c[MAX];
char next_str[MAX];
int flag,flag_exit;
int i,j = 0;
printf("-----------------------------------------\n");
for( i = 0 ; str[i] != '\0' ; i++ ){
if( str[i] != ' ' && str[i] != '=' )
a[j++] = str[i];
else
a[j]= '\0';
if( str[i] == '=' ){
j = 0; i ++;
while( str[i] != '\0' ){
if( str[i] == '"' ){
b[j++] = str[i];
i++;
while( str[i] != '"' ){
flag = i;
b[j++] = str[i];
i++;
}
b[j++] = str[i];
break;
}
else{
if( str[i] != ' ' ){
flag = i;
b[j++] = str[i];
if( str[i+1] == ' ' ) break;
}
}
i++;
}
b[j] = '\0';
break;
}
}
#ifdef DEBUG
printf("%s\n",a);
printf("%s\n",b);
#endif
if( str[i] == '\0' )
flag_exit = 1;
i++;
for( ; str[i] != '\0' ; i++ ){
if( str[i] == ' ' ) continue;
else{
if( str[i] == '=' ){
strncpy(next_str,&str[flag - strlen(b) + 1 ],strlen(str) - flag + strlen(b) );
memset(b,'\0',MAX);
break;
}
else{
strncpy(next_str,&str[i],strlen(str) -i + 1 );
break;
}
}
}
sprintf(c,"%s=%s",a,b);
printf("!!%s\n",c);
if( flag_exit == 1 )
exit(0);
#ifdef DEBUG
printf("!!!NEXT : %s\n",next_str);
#endif
extract_str(next_str);
}
int main(void)
{
extract_str(str);
return 0;
}
|