CommonIface.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GAZEBO_COMMON_COMMONIFACE_HH_
18 #define GAZEBO_COMMON_COMMONIFACE_HH_
19 
20 #include <string>
21 #include <vector>
22 
23 #include <boost/version.hpp>
24 #if BOOST_VERSION < 106600
25 #include <boost/uuid/sha1.hpp>
26 #else
27 #include <boost/uuid/detail/sha1.hpp>
28 #endif
29 
30 #include <boost/filesystem.hpp>
31 #include <iomanip>
32 #include <sstream>
33 
34 #include <sdf/Element.hh>
35 
36 #include "gazebo/util/system.hh"
37 
38 namespace gazebo
39 {
40  namespace common
41  {
44 
46  GZ_COMMON_VISIBLE
47  void load();
48 
51  GZ_COMMON_VISIBLE
52  void add_search_path_suffix(const std::string &_suffix);
53 
57  GZ_COMMON_VISIBLE
58  std::string find_file(const std::string &_file);
59 
65  GZ_COMMON_VISIBLE
66  std::string find_file(const std::string &_file,
67  bool _searchLocalPath);
68 
72  GZ_COMMON_VISIBLE
73  std::string find_file_path(const std::string &_file);
74 
79  template<typename T>
80  std::string get_sha1(const T &_buffer);
81 
85  GZ_COMMON_VISIBLE
86  const char *getEnv(const char *_name);
87 
90  GZ_COMMON_VISIBLE
91  std::string cwd();
92 
96  GZ_COMMON_VISIBLE
97  bool exists(const std::string &_path);
98 
102  GZ_COMMON_VISIBLE
103  bool isDirectory(const std::string &_path);
104 
108  GZ_COMMON_VISIBLE
109  bool isFile(const std::string &_path);
110 
114  GZ_COMMON_VISIBLE
115  std::string absPath(const std::string &_path);
116 
121  GZ_COMMON_VISIBLE
122  bool copyFile(const std::string &_existingFilename,
123  const std::string &_newFilename);
124 
129  GZ_COMMON_VISIBLE
130  bool copyDir(const boost::filesystem::path &_source,
131  const boost::filesystem::path &_destination);
132 
137  GZ_COMMON_VISIBLE
138  bool moveFile(const std::string &_existingFilename,
139  const std::string &_newFilename);
140 
149  GZ_COMMON_VISIBLE
150  void replaceAll(std::string &_result,
151  const std::string &_orig,
152  const std::string &_key,
153  const std::string &_replacement);
154 
163  GZ_COMMON_VISIBLE
164  std::string replaceAll(const std::string &_orig,
165  const std::string &_key,
166  const std::string &_replacement);
167 
172  GZ_COMMON_VISIBLE
173  std::vector<std::string> split(const std::string &_str,
174  const std::string &_delim);
175 
183  GZ_COMMON_VISIBLE
184  std::string unique_file_path(const std::string &_pathAndName,
185  const std::string &_extension);
186 
196  GZ_COMMON_VISIBLE
197  std::string asFullPath(const std::string &_uri,
198  const std::string &_filePath);
199 
205  GZ_COMMON_VISIBLE
206  void convertToFullPaths(const sdf::ElementPtr &_elem,
207  const std::string &_filePath = {});
208 
214  GZ_COMMON_VISIBLE
215  void convertToFullPaths(std::string &_sdfString,
216  const std::string &_filePath);
218  }
219 
221  // Implementation of get_sha1
222  template<typename T>
223  std::string common::get_sha1(const T &_buffer)
224  {
225  boost::uuids::detail::sha1 sha1;
226  unsigned int hash[5];
227  std::stringstream stream;
228 
229  if (_buffer.size() == 0)
230  {
231  sha1.process_bytes(nullptr, 0);
232  }
233  else
234  {
235  sha1.process_bytes(&(_buffer[0]), _buffer.size() * sizeof(_buffer[0]));
236  }
237 
238  sha1.get_digest(hash);
239 
240  for (std::size_t i = 0; i < sizeof(hash) / sizeof(hash[0]); ++i)
241  {
242  stream << std::setfill('0')
243  << std::setw(sizeof(hash[0]) * 2)
244  << std::hex
245  << hash[i];
246  }
247 
248  return stream.str();
249  }
250 }
251 #endif
common
Definition: FuelModelDatabase.hh:42
std::vector< std::string > split(const std::string &_str, const std::string &_delim)
Splits a string into tokens.
bool isFile(const std::string &_path)
Check if the given path is a file.
std::string unique_file_path(const std::string &_pathAndName, const std::string &_extension)
Generates a path for a file which doesn't collide with existing files, by appending numbers to it (i....
bool copyDir(const boost::filesystem::path &_source, const boost::filesystem::path &_destination)
Copy a directory, overwrite the destination directory if exists.
std::string asFullPath(const std::string &_uri, const std::string &_filePath)
Combine a URI and a file path into a full path.
void replaceAll(std::string &_result, const std::string &_orig, const std::string &_key, const std::string &_replacement)
Replace all occurances of _key with _replacement.
std::string cwd()
Get the current working directory.
const char * getEnv(const char *_name)
Cross platform retrieval of an environment variable.
void convertToFullPaths(const sdf::ElementPtr &_elem, const std::string &_filePath={})
Convert all the URIs nested inside the given element to full paths based on the SDF element's file pa...
bool copyFile(const std::string &_existingFilename, const std::string &_newFilename)
Copy a file.
bool moveFile(const std::string &_existingFilename, const std::string &_newFilename)
Move a file.
std::string get_sha1(const T &_buffer)
Compute the SHA1 hash of an array of bytes.
Definition: CommonIface.hh:223
bool exists(const std::string &_path)
Returns true if _path is a file or directory.
bool isDirectory(const std::string &_path)
Check if the given path is a directory.
std::string absPath(const std::string &_path)
Get the absolute path of a provided path.
void add_search_path_suffix(const std::string &_suffix)
add path sufix to common::SystemPaths
std::string find_file(const std::string &_file)
search for file in common::SystemPaths
void load()
Load the common library.
std::string find_file_path(const std::string &_file)
search for a file in common::SystemPaths
Forward declarations for the common classes.
Definition: Animation.hh:27