⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ache.c

📁 Linux 上的socket嗅探器
💻 C
📖 第 1 页 / 共 3 页
字号:
    lptree->datalen=0;
    lptree->ident=0;
    return 0;
}

/*
 * reset tree type
 */
int ResetTreeType(ACHETREE *lptree)
{
    if(lptree==0) /* 0 point to tree */
        return -1;
    if(lptree->lpParent==0)
    {
        if(lptree->lpChild==0||lptree->curChildNum<=0)
        {
            lptree->lpChild=0;
            lptree->curChildNum=0;
            lptree->type=ACHETRTEE_SINGLE;
        }
        else
        {
            lptree->type=ACHETRTEE_ROOT;
        }

    }
    else
    {
        if(lptree->lpChild==0||lptree->curChildNum<=0)
        {
            lptree->lpChild=0;
            lptree->curChildNum=0;
            lptree->type=ACHETRTEE_LEAFE;
        }
        else
        {
            lptree->type=ACHETRTEE_NORMAL;
        }
    }

    return 0;
}

/*
 * Set and get Tree data
 */

int SetAcheTreeData(ACHETREE *lptree, unsigned long data)
{
    if(lptree==0) /* 0 point to tree */
        return -1;
    lptree->usrdata=data;
    return 0;
}

int GetAcheTreeData(ACHETREE *lptree, unsigned long *lpdata)
{
    if(lptree==0||lpdata==0) /* 0 point to tree */
        return -1;
    (*lpdata)=lptree->usrdata;
    return 0;
}

/*
 * Set and get Tree text
 */

int SetAcheTreeText(ACHETREE *lptree, char *lptxt)
{
    if(lptree==0) /* 0 point to tree */
        return -1;
    if(lptree->lpData!=0)
        free(lptree->lpData);
    lptree->datalen=strlen(lptxt)+1;
    lptree->lpData=(void*)malloc(lptree->datalen);
    memcpy(lptree->lpData,lptxt,lptree->datalen);
    return lptree->datalen;
}

int GetAcheTreeText(ACHETREE *lptree, char *lptxt)
{
    if(lptree==0||lptree->lpData==0) /* 0 point to tree */
        return -1;
    memcpy(lptxt,lptree->lpData,lptree->datalen);
    return lptree->datalen;
}

/*
 * get child from parent tree acording to the index inform
 */

ACHETREE *GetChildTree(ACHETREE *lparent, int index)
{
    int res=0;
    ACHETREE *lpchild=0;
    if(lparent==0) /* 0 point to tree */
        return 0;
    if(lparent->ifinit!=ACHE_REGSIGNE)
        InitAcheTree(lparent);

    res=0;
    lpchild=lparent->lpChild;
    for(res=0;res<lparent->curChildNum;res++)
    {
        if(lpchild!=0)
        {
            if(lpchild->index==index)
                return lpchild ;
            lpchild=lpchild->lpnext;
        }
        else
            return 0;
    };
    return 0;
}

/*
 * get the root of a normal tree or leaf
 */
ACHETREE *GetRootOfTree(ACHETREE *lptree)
{
    ACHETREE *lparent=lptree;
    while(lparent->lpParent!=0)
        lparent=lparent->lpParent;
    return lparent;
}

/*
 * cut the relation between tree and it's parent
 */

int CutTreeRelation(ACHETREE *lptree)
{
    int res=0;
    ACHETREE *lparent, *lpchild ;
    if(lptree->ifinit!=ACHE_REGSIGNE)
    {
        InitAcheTree(lptree);
        return -1;
    }
    if(lptree->lpParent==0) /* it has no parent */
        return 0;
    lparent=lptree->lpParent;
    if(lparent->curChildNum>1) /* its parent has more than one child */
    {
        if(lptree->lppre!=0) /* it was not the first child before */
        {
            if(lptree->lpnext!=0) /* it was not the last child before */
            {
                lptree->lppre->lpnext=lptree->lpnext;
                lptree->lpnext->lppre=lptree->lppre;
            }
            else /* it was the last child before */
            {
                lptree->lppre->lpnext=0;
            }

        }
        else /* it was the first child before */
        {
            lptree->lpParent->lpChild=lptree->lpnext;
            lptree->lpnext->lppre=0;
        }
        lptree->lpParent->curChildNum--;
    }
    else if(lparent->lpChild==lptree)/* its parent has only one child */
    {
        lparent->lpChild=0;
        lparent->curChildNum=0;

    }
    else /* why can't we find it in its parent's children */
    {
        return -5;
    }

    /* reset the index of the parent tree */
    res=0;
    lparent=lptree->lpParent;
    lpchild=lparent->lpChild;
    for(res=0;res<lparent->curChildNum;res++)
    {
        if(lpchild!=0)
        {
            lpchild->index=res;
            lpchild=lpchild->lpnext;
        }
        else
            break;
    }
    ResetTreeType(lparent);

    /* reset the data of the tree */
    lptree->lppre=0;
    lptree->lpnext=0;
    lptree->lpParent=0;
    ResetTreeType(lptree);

    return 0;
}

/*
 * insert an existent tree to another as a child
 * this tree must has no neighbours, otherwise we cut
 * the relations between this tree and its parent
 */
int LinkToTree(ACHETREE *lparent, ACHETREE *lptree, int index)
{
    int ret=0,res=0;
    ACHETREE *lpchild=0, *lpinsert=0;
    if(lptree==0||lparent==0) /* 0 point to tree */
        return -1;
    else if(lptree==lparent) /* the same tree */
        return -2;
    if(lptree->ifinit!=ACHE_REGSIGNE)
        InitAcheTree(lptree);
    if(lparent->ifinit!=ACHE_REGSIGNE)
        InitAcheTree(lparent);
    /* find the location for insert */
    lpinsert=0;
    ret=0;
    lpchild=lparent->lpChild;
    if(lpchild!=0) /* make sure that the parent tree has children */
        for(ret=0;ret<lparent->curChildNum;ret++)
    {
        if(lptree==lpchild) /* already linked */
            return -3;
        if(lpinsert==0/* if has not found then get insert location */
            &&(lpchild->lpnext==0||lpchild->index>=index))
            lpinsert=lpchild;
        lpchild=lpchild->lpnext; /* point to it's neightbour */
        if(lpchild==0&&lpinsert==0)
            return -4;
    }
    /* here, cut the current relation of the tree 'lptree' */
    res=CutTreeRelation(lptree);
    if(res<0)
        return res-5;
    /* here we start linking */
    if(lparent->lpChild==0||lparent->curChildNum<=0)
    { /* The parent tree has no children, link tree as first child */
        lptree->lppre=0;
        lptree->lpnext=0;
        lptree->index=0;
        lptree->lpParent=lparent;
        lparent->lpChild=lptree;
        lparent->curChildNum=1;
    }
    else if(lpinsert->index>=index)
    { /* normal link tree, may be as head, nerver be the last child */
        if(lpinsert->lppre==0) /* as head */
        {
            lptree->lppre=0;
            lptree->lpnext=lpinsert;
            lpinsert->lppre=lptree;
            lptree->index=0;
            lptree->lpParent=lparent;
            lparent->lpChild=lptree;
            lparent->curChildNum++;
        }
        else /* normal */
        {
            lpinsert->lppre->lpnext=lptree;
            lptree->lppre=lpinsert->lppre;
            lptree->lpnext=lpinsert;
            lpinsert->lppre=lptree;
            lptree->index=lptree->lppre->index+1;
            lptree->lpParent=lparent;
            lparent->curChildNum++;
        }
    }
    else if(lpinsert->lpnext==0)
    { /* link tree as last child*/
        lptree->lppre=lpinsert;
        lptree->lpnext=0;
        lpinsert->lpnext=lptree;
        lptree->index=lparent->curChildNum;
        lptree->lpParent=lparent;
        lparent->curChildNum++;
    }
    /* reset the index of the parent tree */
    res=0;
    lpchild=lparent->lpChild;
    while(lpchild!=0)
    {
        lpchild->index=res;
        lpchild=lpchild->lpnext;
        res++;
        if(res>=lparent->curChildNum)
            break;
    }
    /* if the tree linked, its parent has at least one child
     * and must not be the leaf
     */
    ResetTreeType(lparent);
    /* reset the data of the tree */
    ResetTreeType(lptree);
    return 0;
}



/*
 * deep query the tree
 */
int DeepQueryAcheTree(ACHETREE *lptree, int (*useFunction)(ACHETREE *), int ifqueryself)
{
    int ret=0;
    if(lptree==0||useFunction==0) /* 0 point to tree or useFunction*/
        return -1;

    if(ifqueryself)
    {
        ret=(*useFunction)(lptree);
        if(ret<0)
            return ret;
    }

    if(lptree->curChildNum>0&&lptree->lpChild!=0)
    {
        int freei=0;
        ACHETREE *lpchild=lptree->lpChild;
        for(freei=0;freei<lptree->curChildNum;freei++)
        {
            if(lpchild!=0)
            {
                ret=(*useFunction)(lpchild);
                if(ret<0)
                    return ret ;

                ret=DeepQueryAcheTree(lpchild,useFunction,0);
                if(ret<0)
                    return ret;
                lpchild=lpchild->lpnext;
            }
            else
                break;
        }

        return 1;
    }

    return 0;
}

/*
 * wide query the tree
 */
int WideQueryAcheTree(ACHETREE *lptree, int (*useFunction)(ACHETREE *), int ifqueryself)
{
    int ret=0;
    if(lptree==0||useFunction==0) /* 0 point to tree or useFunction*/
        return -1;

    if(ifqueryself)
    {
        ret=(*useFunction)(lptree);
        if(ret<0)
            return ret;
    }

    if(lptree->curChildNum>0&&lptree->lpChild!=0)
    {
        int freei=0,res=0;
        ACHETREE *lpchild=lptree->lpChild;
        for(freei=0;freei<lptree->curChildNum;freei++)
        {
            if(lpchild!=0)
            {
                ret=(*useFunction)(lpchild);
                if(ret<0)
                    return ret;
                lpchild=lpchild->lpnext;
            }
            else
                break;
        }

        res=0;
        lpchild=lptree->lpChild;
        for(freei=0;freei<lptree->curChildNum;freei++)
        {
            if(lpchild!=0)
            {
                if(lpchild->curChildNum>0&&lpchild->lpChild!=0)
                {
                    ret=DeepQueryAcheTree(lpchild,useFunction,0);
                    if(ret<0)
                        return ret;
                }

                lpchild=lpchild->lpnext;
            }
            else
                break;
        }
    }

    return 0;
}


ACHETREE * AcheTreeInsertItem(
            ACHETREE *lparent,
            char *lptext,
            int index)
{
    ACHETREE *ret=0;
    if(lparent==0||lptext==0)
        return 0;
    ret=(ACHETREE *)malloc(sizeof(ACHETREE));
    if(index<0)
        index=lparent->curChildNum;
    InitAcheTree(ret);
    SetAcheTreeText(ret,lptext);
    LinkToTree(lparent,ret,index);
    return ret ;
}

/*
 * delete all items of tree
 */

int AcheTreeDelItems(ACHETREE *lparent)
{
    ACHETREE *lpchild=0, *tmp;
    if(lparent==0)
        return -1;

    lpchild=lparent->lpChild;
    while(lpchild!=0)
    {
        tmp=lpchild->lpnext;
        CutTreeRelation(lpchild);
        AcheTreeDelItems(lpchild);
        free(lpchild);
        lpchild=lparent->lpChild;
    }
    if(lparent->lpData!=0)
    {
        free(lparent->lpData);
        lparent->lpData=0;
    }
    InitAcheTree(lparent);
    return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -