CreatePath - Full Path Creation (wstring version)

本文介绍了一个使用C++实现的递归函数,用于创建包含多个层级的路径。该函数能够处理尾随斜杠和网络位置,并通过调用SetLastError()来简化错误验证过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Environment: Visual C++

This is a simple function I wrote in order to deal with creating complete (multi-level) paths.

The function works recursively, and uses std::wstring , but can actaully work on any basic_string -based string. It can deal with trailing slashes (eg. "c:/temp/" vs. "c:/temp"), as well as network locations (eg. "//machine/shared").

You might notice the call to SetLastError() . The reason is to make it easy to verify what went wrong in case of an error - instead of handling exceptions and/or error strings, all you have to do is call GetLastError() if the function returns false .

You can use the function to make sure a directory exists before creating a file in it:

 

Here is the source code:

bool CreatePath(std::wstring &wsPath)
{
DWORD attr;
int pos;
bool result = true;

// Check for trailing slash:
pos = wsPath.find_last_of(SLASH);
if (wsPath.length() == pos + 1) // last character is "/"
{
wsPath.resize(pos);
}

// Look for existing object:
attr = GetFileAttributesW(wsPath.c_str());
if (0xFFFFFFFF == attr) // doesn't exist yet - create it!
{
pos = wsPath.find_last_of(SLASH);
if (0 < pos)
{
// Create parent dirs:
result = CreatePath(wsPath.substr(0, pos));
}
// Create node:
result = result && CreateDirectoryW(wsPath.c_str(), NULL);
}
else if (FILE_ATTRIBUTE_DIRECTORY != attr)
{ // object already exists, but is not a dir
SetLastError(ERROR_FILE_EXISTS);
result = false;
}

return result;
}
内容概要:本文档《团队协作避坑指南:用GitCode权限管理|10分钟配置精细化开发管控》主要介绍了如何利用GitCode进行权限管理,以实现团队协作中的高效、安全和精细化管控。首先,文档解释了GitCode权限管理的核心概念,包括不同级别的权限(如组织级、项目级、仓库级和分支级)及其作用范围和典型角色。接着,文档详细描述了10分钟快速配置权限的具体步骤,从创建组织到设置权限模板,再到创建项目组与成员。随后,文档深入探讨了精细权限控制方案,涵盖分支保护规则配置、文件级代码拥有者(CODEOWNERS)以及合并请求(MR)审批规则等内容。最后,文档提出了企业级管控策略,包括敏感操作限制、合规性审计方案和定期权限审查流程,并分享了某50人团队采用此方案后的显著成效,如权限配置时间减少85%,越权操作事故下降92%,代码审核效率提升60%。 适合人群:适用于有一定GitCode使用基础的技术负责人、项目经理和开发工程师等团队成员。 使用场景及目标:①帮助团队快速搭建和配置权限管理体系;②确保代码库的安全性和稳定性;③提高团队协作效率,降低越权操作风险;④为新入职员工提供标准化的权限配置流程。 阅读建议:本指南不仅提供了详细的配置步骤,还强调了权限管理的最佳实践和持续优化建议。读者在学习过程中应结合实际应用场景,灵活应用所学内容,并定期审查和调整权限设置,以适应团队发展的需要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值