Skip to content

Commit

Permalink
Merge pull request idaholab#15289 from dylanjm/fix-file_base-bug
Browse files Browse the repository at this point in the history
Fix bug that creates empty directories when using abs path.
  • Loading branch information
dschwen authored May 15, 2020
2 parents 10eaddb + de1d89a commit 7a584ba
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions framework/include/utils/MooseUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,11 @@ linearPartitionChunk(dof_id_type num_items, dof_id_type num_chunks, dof_id_type
*/
std::string realpath(const std::string & path);

/**
* Like python's os.path.relpath
*/
std::string relativepath(const std::string & path, const std::string & start = ".");

/**
* Custom type trait that has a ::value of true for types that cam be use interchangably
* with Real. Most notably it is false for complex numbers, which do not have a
Expand Down
7 changes: 6 additions & 1 deletion framework/src/outputs/FileOutput.C
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ FileOutput::FileOutput(const InputParameters & parameters)
}

// Check the file directory of file_base and create if needed
std::string base = "./" + _file_base;
// Check if _file_base is an absolute path
std::string base;
if (_file_base.find_first_of("/", 0) == 0)
base = "./" + MooseUtils::relativepath(_file_base);
else
base = "./" + _file_base;
base = base.substr(0, base.find_last_of('/'));

if (_app.processor_id() == 0 && access(base.c_str(), W_OK) == -1)
Expand Down
38 changes: 38 additions & 0 deletions framework/src/utils/MooseUtils.C
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,44 @@ realpath(const std::string & path)
#endif
}

std::string
relativepath(const std::string & path, const std::string & start)
{
std::vector<std::string> vecpath;
std::vector<std::string> vecstart;
size_t index_size;
unsigned int same_size(0);

vecpath = split(path, "/");
vecstart = split(realpath(start), "/");
if (vecstart.size() < vecpath.size())
index_size = vecstart.size();
else
index_size = vecpath.size();

for (unsigned int i = 0; i < index_size; ++i)
{
if (vecstart[i] != vecpath[i])
{
same_size = i;
break;
}
}

std::string relative_path("");
for (unsigned int i = 0; i < (vecstart.size() - same_size); ++i)
relative_path += "../";

for (unsigned int i = same_size; i < vecpath.size(); ++i)
{
relative_path += vecpath[i];
if (i < (vecpath.size() - 1))
relative_path += "/";
}

return relative_path;
}

BoundingBox
buildBoundingBox(const Point & p1, const Point & p2)
{
Expand Down

0 comments on commit 7a584ba

Please sign in to comment.