woaidongmao

文章均收录自他人博客,但不喜标题前加-[转贴],因其丑陋,见谅!~
随笔 - 1469, 文章 - 0, 评论 - 661, 引用 - 0
数据加载中……

内存搜索与解析

char*            MemFind(const void* src_buf, int src_len, const void* find_buf, int find_len);
BOOL            ParserMem(const void* src_buf, int src_len, const vector< vector<char> >& vec_par, vector< vector<char> >& vec_result);//---- 多重二进制分割
BOOL            ParserMem(const void* src_buf, int src_len, const vector<char>& vec_par, vector< vector<char> >& vec_result);//---- 单层二进制分割
BOOL            ParserMem(const void* src_buf, int src_len, const char* par_buf, vector< vector<char> >& vec_result);//---- 字符分割
BOOL            ParserMem(const void* src_buf, int src_len, const char* par_buf, vector<string>& vec_result);//---- 字符分割

 

char* Glob_Fun::MemFind(const void* src_buf, int src_len, const void* find_buf, int find_len)
{
    if ((NULL == src_buf) || (NULL == find_buf)
        || (src_len <= 0) || (find_len <= 0))
        return NULL;

    char*cp = (char*)src_buf;
    char*src_end = (char*)src_buf +  src_len;
    char*find_end = (char*)find_buf + find_len;
    char*s_src = NULL, *s_find = NULL;
    while (cp < src_end)
    {
        s_src = cp;
        s_find = (char*)find_buf;
        while (s_src < src_end && s_find < find_end && *s_src == *s_find)
            ++s_src, ++s_find;
        if (s_find == find_end)
            return(cp);
        ++cp;
    }
    return(NULL);
}

BOOL Glob_Fun::ParserMem(
                         const void*                    src_buf,
                         int                            src_len,
                         const vector< vector<char> >&    vec_par,
                         vector< vector<char> >&        vec_result)
{
    if ((NULL == src_buf) || (src_len <= 0) || vec_par.size() <= 0)
        return FALSE;
    for (size_t t = 0; t < vec_par.size(); ++t)
    {
        if(0 >= vec_par[t].size())
            return FALSE;
    }

    vec_result.clear();

    vector<char> vec_tmp;
    BOOL bMatched = FALSE;
    int nParCount = vec_par.size();
    int nParTick = 0;
    char*cp = (char*)src_buf;
    char*src_end = (char*)src_buf +  src_len;
    int  nParLen = (int)vec_par[0].size();
    char*find_buf = (char*)&(vec_par[0][0]);
    char*find_end = find_buf + nParLen;
    char*s_src = NULL, *s_find = NULL;

    while (cp < src_end)
    {
        s_src = cp;
        s_find = (char*)find_buf;
        while (s_src < src_end && s_find < find_end && *s_src == *s_find)
            ++s_src,++s_find;
        if (s_find == find_end)
        {   
            bMatched = TRUE;
            cp += nParLen;
            vec_result.push_back(vec_tmp);
            vec_tmp.clear();
            ++nParTick;
            if(nParCount == nParTick)
                nParTick = 0;
            nParLen = (int)(vec_par[nParTick].size());
            find_buf = (char*)&(vec_par[nParTick][0]);
            find_end = find_buf + nParLen;            
        }
        else
        {
            vec_tmp.push_back(*cp);
            ++cp;
        }
    }
    if(TRUE == bMatched)
        vec_result.push_back(vec_tmp);
    return(bMatched);
}

BOOL Glob_Fun::ParserMem(
                         const void*                    src_buf,
                         int                            src_len,
                         const vector<char>&            vec_par,
                         vector< vector<char> >&        vec_result)
{
    if ((NULL == src_buf) || (src_len <= 0) || vec_par.size() <= 0)
        return FALSE;
    vec_result.clear();

    vector<char> vec_tmp;
    BOOL bMatched = FALSE;
    char*cp = (char*)src_buf;
    char*src_end = (char*)src_buf +  src_len;
    int  nParLen = (int)vec_par.size();
    char*find_buf = (char*)&(vec_par[0]);
    char*find_end = find_buf + nParLen;
    char*s_src = NULL, *s_find = NULL;

    while (cp < src_end)
    {
        s_src = cp;
        s_find = (char*)find_buf;
        while (s_src < src_end && s_find < find_end && *s_src == *s_find)
            ++s_src, ++s_find;
        if (s_find == find_end)
        {   
            bMatched = TRUE;
            cp += nParLen;
            vec_result.push_back(vec_tmp);
            vec_tmp.clear();        
        }
        else
        {
            vec_tmp.push_back(*cp);
            ++cp;
        }
    }
    if(TRUE == bMatched)
        vec_result.push_back(vec_tmp);
    return(bMatched);
}

BOOL Glob_Fun::ParserMem(
                         const void*                    src_buf,
                         int                            src_len,
                         const char*                    par_buf,
                         vector< vector<char> >&        vec_result)
{
    if ((NULL == src_buf) || (src_len <= 0) || strlen(par_buf) <= 0)
        return FALSE;
    vec_result.clear();

    vector<char> vec_tmp;
    BOOL bMatched = FALSE;
    char*cp = (char*)src_buf;
    char*src_end = (char*)src_buf +  src_len;
    int  nParLen = strlen(par_buf);
    char*find_buf = (char*)par_buf;
    char*find_end = find_buf + nParLen;
    char*s_src = NULL, *s_find = NULL;

    while (cp < src_end)
    {
        s_src = cp;
        s_find = (char*)find_buf;
        while (s_src < src_end && s_find < find_end && *s_src == *s_find)
            ++s_src, ++s_find;
        if (s_find == find_end)
        {   
            bMatched = TRUE;
            cp += nParLen;
            vec_result.push_back(vec_tmp);
            vec_tmp.clear();        
        }
        else
        {
            vec_tmp.push_back(*cp);
            ++cp;
        }
    }
    if(TRUE == bMatched)
        vec_result.push_back(vec_tmp);
    return(bMatched);
}

BOOL Glob_Fun::ParserMem(
                         const void*                    src_buf,
                         int                            src_len,
                         const char*                    par_buf,
                         vector<string>&                vec_result)
{
    if ((NULL == src_buf) || (src_len <= 0) || strlen(par_buf) <= 0)
        return FALSE;
    vec_result.clear();

    string vec_tmp;
    BOOL bMatched = FALSE;
    char*cp = (char*)src_buf;
    char*src_end = (char*)src_buf +  src_len;
    int  nParLen = strlen(par_buf);
    char*find_buf = (char*)par_buf;
    char*find_end = find_buf + nParLen;
    char*s_src = NULL, *s_find = NULL;

    while (cp < src_end)
    {
        s_src = cp;
        s_find = (char*)find_buf;
        while (s_src < src_end && s_find < find_end && *s_src == *s_find)
            ++s_src, ++s_find;
        if (s_find == find_end)
        {   
            bMatched = TRUE;
            cp += nParLen;
            vec_result.push_back(vec_tmp);
            vec_tmp.clear();        
        }
        else
        {
            vec_tmp.push_back(*cp);
            ++cp;
        }
    }
    if(TRUE == bMatched)
        vec_result.push_back(vec_tmp);
    return(bMatched);
}

posted on 2008-06-23 15:43 肥仔 阅读(1512) 评论(1)  编辑 收藏 引用 所属分类: 库 & 代码段

评论

# re: 内存搜索与解析[未登录]  回复  更多评论   

good
2009-04-20 16:38 | a

只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理