Skip to content

Commit

Permalink
Added tCreateDirs for creating multiple directories in one go.
Browse files Browse the repository at this point in the history
  • Loading branch information
bluescan committed Aug 18, 2024
1 parent 2778f85 commit e29b6fc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
11 changes: 8 additions & 3 deletions Modules/System/Inc/System/tFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -644,12 +644,17 @@ bool tFindFilesRec(tList<tFileInfo>& files, const tString& dir, bool hidden =
bool tFindFilesRec(tList<tFileInfo>& files, const tString& dir, const tString& ext, bool hidden = true, Backend = Backend::Native);
bool tFindFilesRec(tList<tFileInfo>& files, const tString& dir, const tExtensions&, bool hidden = true, Backend = Backend::Native);

// Creates a directory. It can also handle creating all the directories in a path. Calling with a string like
// "C:/DirA/DirB/" will ensure that DirA and DirB exist. Returns true if successful.
// Creates a directory. The parent directory must already exist. For example, if you pass "C:/DirA/DirB/", DirB will
// only be created if C:/DirA/ already existed.
bool tCreateDir(const tString& dir);

// Creates multiple directories in one go. Calling with a string like "C:/DirA/DirB/" will ensure that DirA and DirB
// both exist. Returns true if successful.
bool tCreateDirs(const tString& dirs);

// A relentless delete. Doesn't care about read-only unless deleteReadOnly is false. This call does a recursive delete.
// If a file has an open handle, however, this fn will fail. If the directory didn't exist before the call then this function silently returns. Returns true if dir existed and was deleted.
// If a file has an open handle, however, this fn will fail. If the directory didn't exist before the call then this
// function silently returns. Returns true if dir existed and was deleted.
bool tDeleteDir(const tString& directory, bool deleteReadOnly = true);

// @todo Implement the tFile class. Right now we're basically just reserving the class name.
Expand Down
19 changes: 16 additions & 3 deletions Modules/System/Src/tFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3125,16 +3125,29 @@ bool tSystem::tCreateDir(const tString& dir)

#else
tPathStdFile(dirPath);
bool ok = std::filesystem::create_directory(dirPath.Chr());
if (!ok)
bool success = std::filesystem::create_directory(dirPath.Chr());
if (!success)
return tDirExists(dirPath);

return ok;
return success;

#endif
}


bool tSystem::tCreateDirs(const tString& dirs)
{
tString dirsPath = dirs;
tPathStdFile(dirsPath);

bool success = std::filesystem::create_directories(dirsPath.Chr());
if (!success)
return tDirExists(dirsPath);

return success;
}


bool tSystem::tDeleteDir(const tString& dir, bool deleteReadOnly)
{
#ifdef PLATFORM_WINDOWS
Expand Down
11 changes: 9 additions & 2 deletions UnitTests/Src/TestSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// System module tests.
//
// Copyright (c) 2017, 2019-2023 Tristan Grimmer.
// Copyright (c) 2017, 2019-2024 Tristan Grimmer.
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby
// granted, provided that the above copyright notice and this permission notice appear in all copies.
//
Expand Down Expand Up @@ -1173,7 +1173,7 @@ tTestUnit(File)
for (tStringItem* subd = subDirs.Head(); subd; subd = subd->Next())
tPrintf("SubDir: %s\n", subd->Text());

// Create a directory. Create a file in it. Then delete them all.
// Create a directory. Create a file in it. Then delete the directory with the file in it.
tCreateDir("TestData/CreatedDirectory/");
tRequire(tDirExists("TestData/CreatedDirectory/"));
tRequire(!tIsReadOnly("TestData/CreatedDirectory/"));
Expand All @@ -1184,6 +1184,13 @@ tTestUnit(File)
tDeleteDir("TestData/CreatedDirectory/");
tRequire(!tDirExists("TestData/CreatedDirectory/"));

// Create multiple directories in one go.
tCreateDirs("TestData/CreatedA/CreatedB/CreatedC/");
tRequire(tDirExists("TestData/CreatedA/CreatedB/CreatedC/"));

tDeleteDir("TestData/CreatedA/");
tRequire(!tDirExists("TestData/CreatedA/"));

tString normalPath = "Q:/Projects/Calamity/Crypto/../../Reign/./Squiggle/";
tPrintf("Testing GetSimplifiedPath on '%s'\n", normalPath.Pod());
tString simpPath = tGetSimplifiedPath(normalPath);
Expand Down
4 changes: 2 additions & 2 deletions UnitTests/Src/UnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ int main(int argc, char** argv)
// tTest(Interval);
// tTest(FileTypes);
// tTest(Directories);
// tTest(File);
tTest(File);
// tTest(FindRec);
// tTest(Network);
// tTest(Time);
tTest(Machine);
// tTest(Machine);
// tTest(CmdLine);
// tTest(String);
// tTest(List);
Expand Down

0 comments on commit e29b6fc

Please sign in to comment.