Skip to content

Commit

Permalink
Implement Path::createDirectoryRecursive (shader-slang#4871)
Browse files Browse the repository at this point in the history
* Implement Path::createDirectoryRecursive

Implement Path::createDirectoryRecursive with existing Path::createDirectory
that uses system call instead of c++ standard lib.

* Change the use of 'while(1)' to 'for(;;)'
  • Loading branch information
kaizhangNV authored Aug 20, 2024
1 parent f77a5ac commit d286ff5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
54 changes: 51 additions & 3 deletions source/core/slang-io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,57 @@ namespace Slang

bool Path::createDirectoryRecursive(const String& path)
{
std::error_code ec;
std::filesystem::create_directories(path.getBuffer(), ec);
return !ec;
String finalPath = Path::simplify(path);
if (finalPath.getLength() == 0)
{
return false;
}

List<String> pathList;

// Check whether the parent directories exist, and add to the pathList if they are
// not, we will create all the directories from back of the list.
String parentDir = finalPath;
for(;;)
{
if (parentDir.getLength() == 0 || File::exists(parentDir))
{
break;
}
else
{
pathList.add(parentDir);
parentDir = Path::getParentDirectory(parentDir);
}
}

// If there are no directories to create, then we are done
if (pathList.getCount() == 0)
{
return true;
}

// Traverse from back of the list, because that is most outer directory.
Int i = 0;
for (i = pathList.getCount() - 1; i >= 0; i--)
{
if (!createDirectory(pathList[i]))
{
break;
}
}

// Something wrong when creating parent directories
if (i > 0)
{
// Remove the directories if we've created
if (i != pathList.getCount() - 1)
remove(pathList[i]);

return false;
}

return true;
}

/* static */SlangResult Path::getPathType(const String& path, SlangPathType* pathTypeOut)
Expand Down
2 changes: 1 addition & 1 deletion source/slang/slang-ir-specialize-function-call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ struct FunctionParameterSpecializationContext

IRInst* findNonuniformIndexInst(IRInst* inst)
{
while(1)
for(;;)
{
if (inst == nullptr)
return nullptr;
Expand Down

0 comments on commit d286ff5

Please sign in to comment.