38 #include <rclcpp/rclcpp.hpp>
39 #include <boost/algorithm/string.hpp>
58 if (!std::filesystem::is_regular_file(template_path))
60 RCLCPP_ERROR_STREAM(
getLogger(),
"Unable to find template file " << template_path.string());
65 std::ifstream template_stream(template_path);
66 if (!template_stream.good())
68 RCLCPP_ERROR_STREAM(
getLogger(),
"Unable to load file " << template_path.string());
73 std::string template_string;
74 template_stream.seekg(0, std::ios::end);
75 template_string.reserve(template_stream.tellg());
76 template_stream.seekg(0, std::ios::beg);
77 template_string.assign((std::istreambuf_iterator<char>(template_stream)), std::istreambuf_iterator<char>());
78 template_stream.close();
83 std::string key_with_brackets =
"[" + variable.key +
"]";
84 boost::replace_all(template_string, key_with_brackets, variable.value);
88 std::filesystem::path file_path =
getPath();
91 std::ofstream output_stream(file_path, std::ios_base::trunc);
92 if (!output_stream.good())
94 RCLCPP_ERROR_STREAM(
getLogger(),
"Unable to open file for writing " << file_path.string());
98 output_stream << template_string.c_str();
99 output_stream.close();
std::filesystem::path getPath() const
Returns the fully qualified path to this file.
virtual std::filesystem::path getTemplatePath() const =0
Returns the full path to the template file.
static std::vector< TemplateVariable > variables
bool write() override
Writes the file to disk.
rclcpp::Logger getLogger()
bool createParentFolders(const std::filesystem::path &file_path)
Create parent folders (recursively)
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.