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

📄 rsc.c

📁 新一代基于事件的嵌入式操作系统dyos在三星的s3c44b0的arm芯片上的完整移植代码
💻 C
📖 第 1 页 / 共 2 页
字号:
//返回: 无
//------------------------------------------------------------------------------
bool_t rsc_movto_elder(struct  rsc_node *lesser,struct  rsc_node *node)
{
    if((lesser==NULL)||(node==NULL))
        return false;
    if(semp_pend(&tg_semp_rsc_root,cn_timeout_forever) ==false)
        return false;
    if(node->parent == pg_rsc_root)    //根结点不分前后
        ;
    else
    {
        if(node->parent->child == node)
            node->parent->child = node->next;
        //以下从链表中取出结点
        node->next->previous = node->previous;
        node->previous->next = node->next;
        //以下把node插入小弟位置,由于是循环链表,长兄结点的前面就是小弟结点.
        node->next = lesser;
        node->previous = lesser->previous;
        lesser->previous->next = node;
        lesser->previous = node;
        if(lesser->parent->child == lesser)
            lesser->parent->child = node;
    }
    semp_post(&tg_semp_rsc_root);
    return false;
}

//----长子位置后移--------------------------------------------------------------
//功能: 同辈结点的相对位置不变,但是长子位置后移一格,即把满子变成长子
//参数: parent,父结点指针
//返回: 无
//------------------------------------------------------------------------------
bool_t rsc_round_back(struct  rsc_node *parent)
{
    if(parent == NULL)
        return false;
    if(semp_pend(&tg_semp_rsc_root,cn_timeout_forever) ==false)
        return false;
    if(parent->child == NULL)
        ;
    else
        parent->child = parent->child->previous;
    semp_post(&tg_semp_rsc_root);
    return false;
}

//----长子位置前移--------------------------------------------------------------
//功能: 同辈结点的相对位置不变,但是长子位置前移一格,即把次子变成长子
//参数: parent,父结点指针
//返回: 无
//------------------------------------------------------------------------------
bool_t rsc_round_forward(struct  rsc_node *parent)
{
    if(parent == NULL)
        return false;
    if(semp_pend(&tg_semp_rsc_root,cn_timeout_forever) ==false)
        return false;
    if(parent->child == NULL)
        ;
    else
        parent->child = parent->child->next;
    semp_post(&tg_semp_rsc_root);
    return false;
}

//----资源更名-----------------------------------------------------------------
//功能: 修改资源名
//参数: node,被修改的资源结点指针
//      new_name,新名字
//返回: 无
//-----------------------------------------------------------------------------
bool_t rsc_rename_node(struct rsc_node *node,char *new_name)
{
    if((node == NULL) || (new_name == NULL))
        return false;
    node->name = new_name;  //无需判断新名字字符串的长度,资源允许名字是空串
    return false;
}
//----返回根结点---------------------------------------------------------------
//功能: 返回指定结点的根结点指针
//参数: node,指定结点
//返回: 根结点指针
//----------------------------------------------------------------------------
struct  rsc_node *rsc_get_root(struct  rsc_node *scion_node)
{
    struct  rsc_node *node = scion_node;
    if(semp_pend(&tg_semp_rsc_root,cn_timeout_forever) ==false)
        return NULL;
    while(node->parent != pg_rsc_root)
        node = node->parent;
    semp_post(&tg_semp_rsc_root);
    return node;
}

//----返回父结点--------------------------------------------------------------
//功能: 返回某结点的父结点,
//参数: son_node,需要查找的树枝的任意一个子结点
//返回: 父结点.
//-----------------------------------------------------------------------------
struct  rsc_node *rsc_get_parent(struct  rsc_node *son_node)
{
    struct  rsc_node *node;
    if(semp_pend(&tg_semp_rsc_root,cn_timeout_forever) ==false)
        return NULL;
    node = son_node->parent;
    semp_post(&tg_semp_rsc_root);
    return node;
}

//----返回长子结点--------------------------------------------------------------
//功能: 返回某结点的长子结点,
//参数: parent_node,需要查找的树枝的父结点
//返回: 长子结点.
//-----------------------------------------------------------------------------
struct  rsc_node *rsc_get_son(struct  rsc_node *parent_node)
{
    struct  rsc_node *node;
    if(semp_pend(&tg_semp_rsc_root,cn_timeout_forever) ==false)
        return NULL;
    node = parent_node->child;
    semp_post(&tg_semp_rsc_root);
    return node;
}

//----返回弟结点--------------------------------------------------------------
//功能: 返回某结点的弟结点,
//参数: elder_node,需要查找的树枝的兄结点
//返回: 弟结点.
//-----------------------------------------------------------------------------
struct  rsc_node *rsc_get_next(struct  rsc_node *elder_node)
{
    struct  rsc_node *node;
    if(semp_pend(&tg_semp_rsc_root,cn_timeout_forever) == false)
        return NULL;
    node = elder_node->next;
    semp_post(&tg_semp_rsc_root);
    return node;
}
//----返回树枝一个末梢结点-----------------------------------------------------
//功能: 返回某树枝的一个末梢结点,末梢结点是指没有子结点的结点.
//参数: parent_node,需要搜索的树枝的父结点
//返回: 树枝的一个末梢结点.当没有子结点时,返回NULL
//备注: 当需要删除整个树枝时,本函数很有用,结合rsc_del_node函数,反复调用本函数,
//      并把返回的结点删除,直到本函数返回NULL.需要删除一个文件夹或者删除一个存
//      在子窗口的gui窗口时,就需要用到删除整个树枝的操作.
//-----------------------------------------------------------------------------
struct  rsc_node *rsc_get_twig(struct  rsc_node *ancestor_node)
{
    struct  rsc_node *result=NULL,*current;
    if(ancestor_node == NULL)
        return NULL;
    if(semp_pend(&tg_semp_rsc_root,cn_timeout_forever) ==false)
        return NULL;
    current = ancestor_node;
    while(current->child != NULL)
    {
        current = current->child;
    }
    if(current == ancestor_node)
        result = NULL;
    else
        result = current;
    semp_post(&tg_semp_rsc_root);
    return result;
}

//----取结点的级别-----------------------------------------------------
//功能: 查看某结点是根节点的第几级子节点,
//参数: node,被查的结点
//返回: 子节点级数,0代表根节点。cn_limit_uint32代表出错。
//-----------------------------------------------------------------------------
uint32_t rsc_get_class(struct  rsc_node *node)
{
    uint32_t n=0;
    struct rsc_node *pl_node = node;
    if(semp_pend(&tg_semp_rsc_root,cn_timeout_forever) ==false)
        return cn_limit_uint32;
    while(pl_node->parent != pg_rsc_root)
    {
        pl_node = pl_node->parent;
        n++;
    }
    semp_post(&tg_semp_rsc_root);
    return n;
}
//----遍历一个树枝--------------------------------------------------------------
//功能: 从当前结点开始,获取下一个结点的指针,沿着搜索路线,直到搜索完整个树枝.搜索
//      路线为:当前结点的子结点,如果子结点为空则搜索弟结点,弟结点又为空则搜索父
//      结点的弟结点,直到搜索完成.
//参数: parent_node,需要搜索的树枝的祖先结点,NULL表示遍历整个资源队列
//      current_node,当前搜索位置,
//返回: 当前搜索位置的下一个结点指针,如果已经搜索完成,则返回NULL.
//备注: 当需要对资源链表中某一个树枝或者整个链表中的结点逐一进行某种操作时,可
//      反复调用本函数,第一次调用current_node = parent_node,其后current_node
//      = 上次返回值,直到返回空.
//------------------------------------------------------------------------------
struct  rsc_node *rsc_trave_scion(struct  rsc_node *ancestor_node,
                                  struct  rsc_node *current_node)
{
    struct  rsc_node *result=NULL,*current_copy;
    bool_t up = false;
    if(ancestor_node==NULL)
    {
        ancestor_node = pg_rsc_root;
        if(current_node==NULL)
            current_node = pg_rsc_root;
    }else
    {
        if(current_node==NULL)
            return NULL;
    }
    if(semp_pend(&tg_semp_rsc_root,cn_timeout_forever) ==false)
        return NULL;
    if((current_node == ancestor_node) && (ancestor_node->child == NULL))
        ;        //父结点没有子结点
    else
    {
        current_copy = current_node;
        do
        {
            if((up == false) && (current_copy->child != NULL))
            {   //子结点非空,返回子结点
                result = current_copy->child;
                break;
            }else if(current_copy->next != current_copy->parent->child)
            {   //子结点空,但本结点不是满子结点,返回弟结点
                result = current_copy->next;
                break;
            }else
            {   //无子结点,且本结点已经是满子结点,需要判断当前父结点的弟结点
                current_copy = current_copy->parent;
                up = true;
            }
        }while(current_copy != ancestor_node);
    }
    semp_post(&tg_semp_rsc_root);
    return result;
}

//----在兄弟结点中搜索资源-----------------------------------------------------
//功能: 在某一个资源结点的所有兄弟结点中搜索给定名字的资源
//参数: brother,兄弟结点中任意一个资源指针.
//      name,需要搜索的资源名
//返回: 如果搜索到资源返回资源结点指针,否则返回NULL
//-----------------------------------------------------------------------------
struct  rsc_node *rsc_search_sibling(struct  rsc_node *brother,char *name)
{
    struct  rsc_node *temp,*result = NULL;
    if((brother == NULL)||(name == NULL))
        return NULL;
    temp = brother;
    if(semp_pend(&tg_semp_rsc_root,cn_timeout_forever) ==false)
        return NULL;
    do
    {
        if(strcmp(temp->name,name)==0)
        {
            result = temp;
            break;
        }
        temp = temp->next;
    }while (temp != brother);
    semp_post(&tg_semp_rsc_root);
    return result;
}

//----在子结点中搜索资源-----------------------------------------------------
//功能: 在某一个资源结点的所有兄弟结点中搜索给定名字的资源
//参数: parent,父结点资源指针.
//      name,需要搜索的资源名,纯资源名,不包含路径.
//返回: 如果搜索到资源返回资源结点指针,否则返回NULL
//-----------------------------------------------------------------------------
struct  rsc_node *rsc_search_son(struct  rsc_node *parent,char *name)
{
    struct  rsc_node *temp,*result = NULL;
    if((parent == NULL)||(name == NULL))
        return NULL;
    if(semp_pend(&tg_semp_rsc_root,cn_timeout_forever) ==false)
        return NULL;
    temp = parent->child;
    if(temp == NULL)
        result = NULL;
    else
    {
        do
        {
            if(strcmp(temp->name,name)==0)
            {
                result = temp;
                break;
            }
            temp = temp->next;
        }while (temp != parent->child);
    }
    semp_post(&tg_semp_rsc_root);
    return result;
}

//----在后代中搜索资源名-------------------------------------------------------
//功能: 与rsc_search_layer类似,搜索ancestor_node的所有后代结点,直到找到一个名称
//      匹配资源。
//参数: ancestor_node,树枝的父结点
//      name,需要搜索的资源名,纯资源名,不包含路径.
//返回: 如果搜索到资源返回资源结点指针,否则返回NULL
//------------------------------------------------------------------------------
struct rsc_node *rsc_search_scion(struct rsc_node *ancestor_node,char *name)
{
    struct  rsc_node  *current,*temp,*result = NULL;
    if((ancestor_node == NULL)||(name == NULL))
        return NULL;
    current = ancestor_node;
    //在rsc_trave_scion中已经有信号量保护,此处无须保护
    while((temp = rsc_trave_scion(ancestor_node,current)) != NULL)
    {
        if(strcmp(temp->name,name) == 0)
        {
            result = temp;
            break;
        }
        current = temp;
    }
    return result;
}

//----沿路径搜索资源名---------------------------------------------------------
//功能: 与rsc_search_scion类似,不同的是,path是包含路径名的结点,执行精确搜索。
//参数: ancestor_node,树枝的父结点
//      path,包含路径名的资源名
//返回: 如果搜索到资源返回资源结点指针,否则返回NULL
//-----------------------------------------------------------------------------
struct  rsc_node *rsc_search(struct  rsc_node *ancestor_node,char *path)
{
    char *path_name,*dest_name;
    bool_t   match;
    uint32_t i;
    
    struct  rsc_node *current,*result = NULL;
    if(path == NULL)
        return NULL;
    if(semp_pend(&tg_semp_rsc_root,cn_timeout_forever) ==false)
        return NULL;
    if(ancestor_node == NULL)
        current = pg_rsc_root->child;
    else
        current = ancestor_node->child;
    if(current == NULL)
    {
        semp_post(&tg_semp_rsc_root);
        return NULL;
    }
    path_name = path;
    while(1)
    {
        dest_name = current->name;
        match=false;
        for(i=0;i<cn_rsc_name_limit;i++)   //资源名长度限制
        {
            if((path_name[i] == 0)||((path_name[i]=='\\')&&(path_name[i+1]==0)))
            {   //已经达到字符串结束,且相等
                result = current;
                break;
            }else if(path_name[i] == '\\')
            {   //字符串相等,但path_name未结束
                match = true;
                break;
            }
            if(path_name[i] != dest_name[i])
            {   //字符串不相等
                match = false;
                break;
            }
        }
        if(result != NULL)  //名字匹配且到达path字符串末,查找结束
            break;

        if(match != true)
        {   //本结点名字不匹配,查找下一个结点
            current = current->next;
            if(current == current->parent->child)
            //本级的所有结点已经搜索完,没有找到匹配的.
                break;
        }else
        {   //本结点名字匹配,但是还没到path串结束,继续查找子结点
            path_name += i+1;
            current = current->child;
            if(current == NULL)
            //或者资源链已经搜索结束,但path_name并未结束
                break;
        }
    }
    semp_post(&tg_semp_rsc_root);
    return result;
}

⌨️ 快捷键说明

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