linux c 子路径剔除

将一个路径集传入,剔除子路径

/*
* brief: 删除一个路径集合中的子路径,进入这个方法后,如果是路径末尾不能有/,否则判断失败。
* param: ppath为路径集合
*/
GPtrArray_autoptr remove_child_path(GPtrArray_autoptr ppath)
{
    for (int i = 0; i < ppath->len; i++)
	{
        for (int j = 0; j < ppath->len; j++)
		{
			pchar pfile_path_i = (pchar)g_ptr_array_index(ppath, i); 
			pchar pfile_path_j = (pchar)g_ptr_array_index(ppath, j); 
            if (i != j && strstr(pfile_path_i, pfile_path_j) == pfile_path_i)
			{
				//如果两个目录层级一致,不存在父子关系。
				int nslash_counti = count_char_in_string(pfile_path_i, "/");
				int nslash_countj = count_char_in_string(pfile_path_j, "/");
				if (nslash_counti == nslash_countj)
				{
					log_info(__FILE__, __LINE__, "nslash_counti == nslash_countj ,i :%s  j:%s", pfile_path_i, pfile_path_j);
					continue;
				}
                // 发现子路径去除
                g_ptr_array_remove_index(ppath, i);
				free(pfile_path_i);
                break;
            }
        }
    }

	//帮ppath重新分配
	GPtrArray_autoptr ppath_new = g_ptr_array_new();

	for (int i = 0; i < ppath->len; i++)
	{
		pchar pfile_path = (pchar)g_ptr_array_index(ppath, i);
		if (pfile_path)
		{
			char *sz_temp = str_notify_str_dup(pfile_path);

			log_debug(__FILE__, __LINE__, "sz_temp : %s",sz_temp);

			g_ptr_array_add(ppath_new, (gpointer) sz_temp);
		}	
	}

	g_ptr_array_free(ppath, true);

	return ppath_new;
}

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值