moveit2
The MoveIt Motion Planning Framework for ROS 2.
templates.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2021, PickNik Robotics, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of PickNik Robotics nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: David V. Lu!! */
36 
38 #include <rclcpp/rclcpp.hpp>
39 #include <boost/algorithm/string.hpp> // for string find and replace in templates
40 
41 namespace moveit_setup
42 {
43 std::vector<TemplateVariable> TemplatedGeneratedFile::variables_;
44 static const rclcpp::Logger LOGGER = rclcpp::get_logger("moveit_setup.templates");
45 
47 {
48  std::filesystem::path template_path = getTemplatePath();
49 
50  // Error check file
51  if (!std::filesystem::is_regular_file(template_path))
52  {
53  RCLCPP_ERROR_STREAM(LOGGER, "Unable to find template file " << template_path.string());
54  return false;
55  }
56 
57  // Load file
58  std::ifstream template_stream(template_path);
59  if (!template_stream.good()) // File not found
60  {
61  RCLCPP_ERROR_STREAM(LOGGER, "Unable to load file " << template_path.string());
62  return false;
63  }
64 
65  // Load the file to a string using an efficient memory allocation technique
66  std::string template_string;
67  template_stream.seekg(0, std::ios::end);
68  template_string.reserve(template_stream.tellg());
69  template_stream.seekg(0, std::ios::beg);
70  template_string.assign((std::istreambuf_iterator<char>(template_stream)), std::istreambuf_iterator<char>());
71  template_stream.close();
72 
73  // Replace keywords in string ------------------------------------------------------------
74  for (const auto& variable : variables_)
75  {
76  std::string key_with_brackets = "[" + variable.key + "]";
77  boost::replace_all(template_string, key_with_brackets, variable.value);
78  }
79 
80  // Save string to new location -----------------------------------------------------------
81  std::filesystem::path file_path = getPath();
82  createParentFolders(file_path);
83 
84  std::ofstream output_stream(file_path, std::ios_base::trunc);
85  if (!output_stream.good())
86  {
87  RCLCPP_ERROR_STREAM(LOGGER, "Unable to open file for writing " << file_path.string());
88  return false;
89  }
90 
91  output_stream << template_string.c_str();
92  output_stream.close();
93 
94  return true; // file created successfully
95 }
96 
97 } // namespace moveit_setup
std::filesystem::path getPath() const
Returns the fully qualified path to this file.
static std::vector< TemplateVariable > variables_
Definition: templates.hpp:72
virtual std::filesystem::path getTemplatePath() const =0
Returns the full path to the template file.
bool write() override
Writes the file to disk.
Definition: templates.cpp:46
bool createParentFolders(const std::filesystem::path &file_path)
Create parent folders (recursively)
Definition: utilities.hpp:68
const rclcpp::Logger LOGGER
Definition: async_test.h:31