Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::filesystem #21137

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions framework/include/utils/MooseUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
#include <vector>
#include <map>
#include <list>
#include <iterator>
// #include <iterator>
#include <filesystem>
#include <deque>

// Forward Declarations
Expand All @@ -57,15 +58,13 @@ class MultiMooseEnum;
namespace MooseUtils
{

std::string pathjoin(const std::string & s);
std::filesystem::path pathjoin(const std::filesystem::path & p);

template <typename... Args>
std::string
pathjoin(const std::string & s, Args... args)
std::filesystem::path
pathjoin(const std::filesystem::path & p, Args... args)
{
if (s[s.size() - 1] == '/')
return s + pathjoin(args...);
return s + "/" + pathjoin(args...);
return p / pathjoin(args...);
}

/// Check if the input string can be parsed into a Real
Expand Down Expand Up @@ -230,7 +229,8 @@ std::string stripExtension(const std::string & s);
*
* If the supplied filename does not contain a path, it returns "." as the path
*/
std::pair<std::string, std::string> splitFileName(std::string full_file);
std::pair<std::filesystem::path, std::filesystem::path>
splitFileName(const std::filesystem::path & full_file);

/**
* Returns the current working directory as a string. If there's a problem
Expand Down
2 changes: 1 addition & 1 deletion framework/src/utils/ADFParser.C
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ADFParser::JITCompile()
else
{
// check if we can find an installed version of the monolithic include
const auto include_path =
const std::string include_path =
MooseUtils::pathjoin(Moose::getExecutablePath(), "../include/moose/ADRealMonolithic.h");
if (MooseUtils::checkFileReadable(include_path, false, false, false))
result = JITCompileHelper("ADReal", "", "#include \"" + include_path + "\"\n");
Expand Down
48 changes: 11 additions & 37 deletions framework/src/utils/MooseUtils.C
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <fstream>
#include <istream>
#include <iterator>
#include <filesystem>
#include <ctime>

// System includes
Expand All @@ -53,10 +54,10 @@ std::string getLatestCheckpointFileHelper(const std::list<std::string> & checkpo

namespace MooseUtils
{
std::string
pathjoin(const std::string & s)
std::filesystem::path
pathjoin(const std::filesystem::path & p)
{
return s;
return p;
}

std::string
Expand Down Expand Up @@ -245,11 +246,7 @@ pathExists(const std::string & path)
bool
pathIsDirectory(const std::string & path)
{
struct stat buffer;
// stat call fails?
if (stat(path.c_str(), &buffer))
return false;
return S_IFDIR & buffer.st_mode;
return std::filesystem::is_directory(path);
}

bool
Expand Down Expand Up @@ -418,34 +415,14 @@ stripExtension(const std::string & s)
return s;
}

std::pair<std::string, std::string>
splitFileName(std::string full_file)
std::pair<std::filesystem::path, std::filesystem::path>
splitFileName(const std::filesystem::path & p)
{
// Error if path ends with /
if (full_file.empty() || *full_file.rbegin() == '/')
mooseError("Invalid full file name: ", full_file);

// Define the variables to output
std::string path;
std::string file;

// Locate the / sepearting the file from path
std::size_t found = full_file.find_last_of("/");

// If no / is found used "." for the path, otherwise seperate the two
if (found == std::string::npos)
{
path = ".";
file = full_file;
}
else
{
path = full_file.substr(0, found);
file = full_file.substr(found + 1);
}
if (!p.has_filename())
mooseError("Invalid full file name: ", p);

// Return the path and file as a pair
return std::pair<std::string, std::string>(path, file);
return {p.parent_path(), p.filename()};
}

std::string
Expand Down Expand Up @@ -1149,10 +1126,7 @@ fileSize(const std::string & filename)
std::string
realpath(const std::string & path)
{
char dummy[PETSC_MAX_PATH_LEN];
if (PetscGetFullPath(path.c_str(), dummy, sizeof(dummy)))
mooseError("Failed to get real path for ", path);
return dummy;
return std::filesystem::absolute(path);
}

std::string
Expand Down
2 changes: 1 addition & 1 deletion modules/tensor_mechanics/test/tests/uexternaldb/tests
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
library_mode = 'DYNAMIC'
valgrind = 'NONE'
cli_args = 'UserObjects/uexternaldb/execute_on=INITIAL'
expect_out = 'getoutdir 1 \. '
expect_out = 'getoutdir 0 '
requirement = 'The Abaqus UEXTERNALDB interface shall provide the Abaqus API to obtain the '
'simulation output directory.'
[]
Expand Down
2 changes: 1 addition & 1 deletion unit/src/SplitFileNameTest.C
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ TEST(SplitFileName, validName)

full = "valid.txt";
split = MooseUtils::splitFileName(full);
EXPECT_EQ(split.first.compare("."), 0);
EXPECT_EQ(split.first.compare(""), 0);
EXPECT_EQ(split.second.compare("valid.txt"), 0);
}