Skip to content

Commit

Permalink
split filename using filesystem lib (#21134)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed May 25, 2022
1 parent ab0b6ec commit 4533bbf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 42 deletions.
16 changes: 8 additions & 8 deletions framework/include/utils/MooseUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
#include <vector>
#include <map>
#include <list>
#include <iterator>
// #include <iterator>
#include <filesystem>
#include <deque>

// Forward Declarations
Expand Down Expand Up @@ -77,15 +78,13 @@ MetaPhysicL::DualNumber<T, D, asd> abs(MetaPhysicL::DualNumber<T, D, asd> && in)
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...);
}

/// Returns the location of either a local repo run_tests script - or an
Expand Down Expand Up @@ -240,7 +239,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
42 changes: 9 additions & 33 deletions framework/src/utils/MooseUtils.C
Original file line number Diff line number Diff line change
Expand Up @@ -54,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 @@ -236,11 +236,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 @@ -409,34 +405,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

0 comments on commit 4533bbf

Please sign in to comment.