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 #include <moveit/utils/logger.hpp>
41 
42 namespace moveit_setup
43 {
44 namespace
45 {
46 rclcpp::Logger getLogger()
47 {
48  return moveit::getLogger("moveit.setup_assistant.setup.templates");
49 }
50 } // namespace
51 std::vector<TemplateVariable> TemplatedGeneratedFile::variables;
52 
54 {
55  std::filesystem::path template_path = getTemplatePath();
56 
57  // Error check file
58  if (!std::filesystem::is_regular_file(template_path))
59  {
60  RCLCPP_ERROR_STREAM(getLogger(), "Unable to find template file " << template_path.string());
61  return false;
62  }
63 
64  // Load file
65  std::ifstream template_stream(template_path);
66  if (!template_stream.good()) // File not found
67  {
68  RCLCPP_ERROR_STREAM(getLogger(), "Unable to load file " << template_path.string());
69  return false;
70  }
71 
72  // Load the file to a string using an efficient memory allocation technique
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();
79 
80  // Replace keywords in string ------------------------------------------------------------
81  for (const auto& variable : variables)
82  {
83  std::string key_with_brackets = "[" + variable.key + "]";
84  boost::replace_all(template_string, key_with_brackets, variable.value);
85  }
86 
87  // Save string to new location -----------------------------------------------------------
88  std::filesystem::path file_path = getPath();
89  createParentFolders(file_path);
90 
91  std::ofstream output_stream(file_path, std::ios_base::trunc);
92  if (!output_stream.good())
93  {
94  RCLCPP_ERROR_STREAM(getLogger(), "Unable to open file for writing " << file_path.string());
95  return false;
96  }
97 
98  output_stream << template_string.c_str();
99  output_stream.close();
100 
101  return true; // file created successfully
102 }
103 
104 } // namespace moveit_setup
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
Definition: templates.hpp:72
bool write() override
Writes the file to disk.
Definition: templates.cpp:53
bool createParentFolders(const std::filesystem::path &file_path)
Create parent folders (recursively)
Definition: utilities.hpp:76
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.
Definition: logger.cpp:79