Skip to content

Commit aafd8be

Browse files
authored
Merge pull request #628 from sebproell/yaml-emit-rel-paths
Let --to-yaml emit relative file paths
2 parents ee1ee77 + 1dc3115 commit aafd8be

File tree

8 files changed

+151
-133
lines changed

8 files changed

+151
-133
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_input_spec.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ void Core::IO::InputSpec::emit_metadata(YamlNodeRef yaml) const
8888
{
8989
FOUR_C_ASSERT(pimpl_, "InputSpec is empty.");
9090

91-
auto root = yaml.node;
92-
pimpl_->emit_metadata(root);
91+
pimpl_->emit_metadata(yaml);
9392
}
9493

9594
Core::IO::Internal::InputSpecImpl& Core::IO::InputSpec::impl()

src/core/io/src/4C_io_input_spec_builders.cpp

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "4C_utils_string.hpp"
1111

1212
#include <format>
13-
#include <iostream>
1413
#include <numeric>
1514
#include <set>
1615
#include <unordered_map>
@@ -751,25 +750,25 @@ void Core::IO::Internal::GroupSpec::print(std::ostream& stream, std::size_t inde
751750
}
752751
}
753752

754-
void Core::IO::Internal::GroupSpec::emit_metadata(ryml::NodeRef node) const
753+
void Core::IO::Internal::GroupSpec::emit_metadata(YamlNodeRef node) const
755754
{
756-
node |= ryml::MAP;
757-
node["name"] << name;
755+
node.node |= ryml::MAP;
756+
node.node["name"] << name;
758757

759-
node["type"] = "group";
758+
node.node["type"] = "group";
760759
if (!data.description.empty())
761760
{
762-
node["description"] << data.description;
761+
node.node["description"] << data.description;
763762
}
764-
emit_value_as_yaml(node["required"], data.required.value());
765-
emit_value_as_yaml(node["defaultable"], data.defaultable);
766-
node["specs"] |= ryml::SEQ;
763+
emit_value_as_yaml(node.wrap(node.node["required"]), data.required.value());
764+
emit_value_as_yaml(node.wrap(node.node["defaultable"]), data.defaultable);
765+
node.node["specs"] |= ryml::SEQ;
767766
{
768767
for (const auto& spec : specs)
769768
{
770-
auto child = node["specs"].append_child();
769+
auto child = node.node["specs"].append_child();
771770
child |= ryml::MAP;
772-
spec.impl().emit_metadata(child);
771+
spec.impl().emit_metadata(node.wrap(child));
773772
};
774773
}
775774
}
@@ -864,23 +863,23 @@ void Core::IO::Internal::AllOfSpec::print(std::ostream& stream, std::size_t inde
864863
}
865864
}
866865

867-
void Core::IO::Internal::AllOfSpec::emit_metadata(ryml::NodeRef node) const
866+
void Core::IO::Internal::AllOfSpec::emit_metadata(YamlNodeRef node) const
868867
{
869-
node |= ryml::MAP;
868+
node.node |= ryml::MAP;
870869

871-
node["type"] = "all_of";
870+
node.node["type"] = "all_of";
872871
if (!data.description.empty())
873872
{
874-
node["description"] << data.description;
873+
node.node["description"] << data.description;
875874
}
876-
emit_value_as_yaml(node["required"], data.required.value());
877-
node["specs"] |= ryml::SEQ;
875+
emit_value_as_yaml(node.wrap(node.node["required"]), data.required.value());
876+
node.node["specs"] |= ryml::SEQ;
878877
{
879878
for (const auto& spec : specs)
880879
{
881-
auto child = node["specs"].append_child();
880+
auto child = node.node["specs"].append_child();
882881
child |= ryml::MAP;
883-
spec.impl().emit_metadata(child);
882+
spec.impl().emit_metadata(node.wrap(child));
884883
}
885884
}
886885
}
@@ -1021,20 +1020,20 @@ void Core::IO::Internal::OneOfSpec::print(std::ostream& stream, std::size_t inde
10211020
}
10221021
}
10231022

1024-
void Core::IO::Internal::OneOfSpec::emit_metadata(ryml::NodeRef node) const
1023+
void Core::IO::Internal::OneOfSpec::emit_metadata(YamlNodeRef node) const
10251024
{
1026-
node |= ryml::MAP;
1025+
node.node |= ryml::MAP;
10271026

1028-
node["type"] << "one_of";
1027+
node.node["type"] << "one_of";
10291028
if (!data.description.empty())
10301029
{
1031-
node["description"] << data.description;
1030+
node.node["description"] << data.description;
10321031
}
1033-
node["specs"] |= ryml::SEQ;
1032+
node.node["specs"] |= ryml::SEQ;
10341033
for (const auto& spec : specs)
10351034
{
1036-
auto child = node["specs"].append_child();
1037-
spec.impl().emit_metadata(child);
1035+
auto child = node.node["specs"].append_child();
1036+
spec.impl().emit_metadata(node.wrap(child));
10381037
}
10391038
}
10401039

@@ -1153,21 +1152,21 @@ void Core::IO::Internal::ListSpec::print(std::ostream& stream, std::size_t inden
11531152
spec.impl().print(stream, indent + 2);
11541153
}
11551154

1156-
void Core::IO::Internal::ListSpec::emit_metadata(ryml::NodeRef node) const
1155+
void Core::IO::Internal::ListSpec::emit_metadata(YamlNodeRef node) const
11571156
{
1158-
node |= ryml::MAP;
1157+
node.node |= ryml::MAP;
11591158

1160-
node["name"] << name;
1159+
node.node["name"] << name;
11611160

1162-
node["type"] << "list";
1161+
node.node["type"] << "list";
11631162
if (!data.description.empty())
11641163
{
1165-
node["description"] << Core::Utils::trim(data.description);
1164+
node.node["description"] << Core::Utils::trim(data.description);
11661165
}
1167-
emit_value_as_yaml(node["required"], data.required);
1168-
if (data.size > 0) node["size"] << data.size;
1169-
node["spec"] |= ryml::MAP;
1170-
spec.impl().emit_metadata(node["spec"]);
1166+
emit_value_as_yaml(node.wrap(node.node["required"]), data.required);
1167+
if (data.size > 0) node.node["size"] << data.size;
1168+
node.node["spec"] |= ryml::MAP;
1169+
spec.impl().emit_metadata(node.wrap(node.node["spec"]));
11711170
}
11721171

11731172

0 commit comments

Comments
 (0)