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

Better structure some code in the gmsh wrappers. #16230

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions include/deal.II/gmsh/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,15 @@ namespace Gmsh
double characteristic_length = 1.0;

/**
* Basename for the output files.
* Base name for the output files. The base name may contain a directory
* (followed by a slash), and must contain the base of the names of files
* to be created.
*
* If this is left empty, then temporary files are used, and removed when
* not needed any more.
* If this variable is left empty, then a temporary directory will be used,
* and both the files written into it as well as the temporary directory
* will be removed when not needed any more.
*/
std::string output_base_name = "";
std::string output_base_name = {};
};

# ifdef DEAL_II_WITH_OPENCASCADE
Expand Down
22 changes: 15 additions & 7 deletions source/gmsh/utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ namespace Gmsh
Triangulation<2, spacedim> &tria,
const AdditionalParameters &prm)
{
std::string base_name = prm.output_base_name;
char dir_template[] = "ctfbc-XXXXXX";
if (base_name.empty())
std::string base_name = prm.output_base_name;

// If necessary, create a temp directory to put files into. The
// following variable will hold the name of the tmp dir; it is
// initialized with a template, and 'mkdtemp' then overwrites it
// with the name of the directory it creates.
char tmp_dir_name[] = "ctfbc-XXXXXX";
if (prm.output_base_name.empty())
{
const char *temp = mkdtemp(dir_template);
const char *temp = mkdtemp(tmp_dir_name);
AssertThrow(temp != nullptr,
ExcMessage("Creating temporary directory failed!"));
base_name = temp;
Expand Down Expand Up @@ -105,7 +110,8 @@ namespace Gmsh
gridin.attach_triangulation(tria);
gridin.read_msh(grid_file);

if (base_name != prm.output_base_name)
// Clean up files if a tmp directory was used:
if (prm.output_base_name.empty())
{
// declaring the list without a type, i.e.,
//
Expand All @@ -125,10 +131,12 @@ namespace Gmsh
AssertThrow(ret_value == 0,
ExcMessage("Failed to remove " + *filename));
}
const auto ret_value = std::remove(dir_template);

// Finally also remove the tmp directory:
const auto ret_value = std::remove(tmp_dir_name);
AssertThrow(ret_value == 0,
ExcMessage("Failed to remove " +
std::string(dir_template)));
std::string(tmp_dir_name)));
}
}
# endif
Expand Down