Skip to content

Commit 1dc3115

Browse files
committed
Let --to-yaml correctly resolve relative paths
1 parent d99840f commit 1dc3115

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

apps/global_full/4C_global_full_main.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,13 @@ int main(int argc, char* argv[])
349349
if (Core::Communication::my_mpi_rank(lcomm) == 0)
350350
{
351351
std::filesystem::path inputfile_name = argv[2];
352+
// Always make the input file path relative to the working directory. This ensures that any
353+
// absolute path encountered in the input file stays that way.
354+
if (inputfile_name.is_absolute())
355+
{
356+
inputfile_name = std::filesystem::relative(inputfile_name);
357+
}
358+
352359
std::filesystem::path outputfile_name;
353360
if (argc == 3)
354361
{
@@ -370,7 +377,7 @@ int main(int argc, char* argv[])
370377
Core::IO::InputFile input_file = Global::set_up_input_file(lcomm);
371378
input_file.read(inputfile_name);
372379
std::ofstream output_file(outputfile_name);
373-
input_file.write_as_yaml(output_file);
380+
input_file.write_as_yaml(output_file, outputfile_name);
374381
printf("The input file has been converted to yaml format");
375382
if (argc == 3) printf(", saved as %s", outputfile_name.c_str());
376383
printf("\n");

src/core/io/src/4C_io_input_file.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ namespace Core::IO
10321032
out << tree;
10331033
}
10341034

1035-
void InputFile::write_as_yaml(std::ostream& out) const
1035+
void InputFile::write_as_yaml(std::ostream& out, const std::filesystem::path& file_name) const
10361036
{
10371037
auto tree = init_yaml_tree_with_exceptions();
10381038
tree.rootref() |= ryml::MAP;
@@ -1048,7 +1048,8 @@ namespace Core::IO
10481048
{
10491049
auto& spec = pimpl_->valid_sections_.at(section_name);
10501050
match_section(section_name, container);
1051-
YamlNodeRef section_node{tree.rootref(), ""};
1051+
YamlNodeRef section_node{
1052+
tree.rootref(), file_name.empty() ? std::filesystem::path{} : file_name};
10521053
spec.emit(section_node, container);
10531054
}
10541055
else

src/core/io/src/4C_io_input_file.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,12 @@ namespace Core::IO
251251

252252
/**
253253
* Write the content of the input file to the given output stream @p out. The content is
254-
* written in .yaml format.
254+
* written in .yaml format. The optional @p file_name is used to construct correct relative
255+
* paths for all path parameters.
255256
*
256257
* @note You probably only want to call this function on rank 0.
257258
*/
258-
void write_as_yaml(std::ostream& out) const;
259+
void write_as_yaml(std::ostream& out, const std::filesystem::path& file_name = {}) const;
259260

260261
private:
261262
std::unique_ptr<Internal::InputFileImpl> pimpl_;

src/core/io/src/4C_io_yaml.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
#include "4C_io_yaml.hpp"
99

10-
#include <iostream>
11-
1210
FOUR_C_NAMESPACE_OPEN
1311

1412
namespace
@@ -53,11 +51,11 @@ void Core::IO::emit_value_as_yaml(YamlNodeRef node, const bool& value)
5351
void Core::IO::emit_value_as_yaml(YamlNodeRef node, const std::filesystem::path& value)
5452
{
5553
node.node |= ryml::VAL_DQUO;
56-
if (node.associated_file.empty())
54+
if (node.associated_file.empty() || value.is_absolute())
5755
{
5856
node.node << value.string();
5957
}
60-
// Make the path relative to the associated file.
58+
// Make the relative path relative to the associated file.
6159
else
6260
{
6361
auto relative_path = std::filesystem::relative(value, node.associated_file.parent_path());

0 commit comments

Comments
 (0)