文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>分析一个算法

分析一个算法

时间:2006-12-24  来源:tthacker

在CU上看到的一个算法,今天重新修改了下,比较满足他的要求了.   id=firewall time="2006-12-12 15:19:14"  fw =FW dst=    type= virus pri=1   service=http src=192.168.1.158 如果值中有空格的话,就用双引号引起来。
每个键值对用空格隔开,但是空格不一定是一个,可能是几个。
每个键值对可能是只有键,而没有值。比如那个dst。
更加复杂的情况是有的键后面跟空格再根等号。
还有情况是等号后面加空格,在加其值
要分析的字符串,把它按照键值对的组合一对一对的插入到数据库中去    

/*
    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;
}

相关阅读 更多 +
排行榜 更多 +
蓝星战士

蓝星战士

飞行射击 下载
书桌看吧

书桌看吧

浏览阅读 下载
铠甲勇士之英雄传说

铠甲勇士之英雄传说

冒险解谜 下载