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 "gazebo/util/system.hh"
35 
36 namespace gazebo
37 {
38  namespace common
39  {
42 
44  GZ_COMMON_VISIBLE
45  void load();
46 
49  GZ_COMMON_VISIBLE
50  void add_search_path_suffix(const std::string &_suffix);
51 
55  GZ_COMMON_VISIBLE
56  std::string find_file(const std::string &_file);
57 
63  GZ_COMMON_VISIBLE
64  std::string find_file(const std::string &_file,
65  bool _searchLocalPath);
66 
70  GZ_COMMON_VISIBLE
71  std::string find_file_path(const std::string &_file);
72 
77  template<typename T>
78  std::string get_sha1(const T &_buffer);
79 
83  GZ_COMMON_VISIBLE
84  const char *getEnv(const char *_name);
85 
88  GZ_COMMON_VISIBLE
89  std::string cwd();
90 
94  GZ_COMMON_VISIBLE
95  bool exists(const std::string &_path);
96 
100  GZ_COMMON_VISIBLE
101  bool isDirectory(const std::string &_path);
102 
106  GZ_COMMON_VISIBLE
107  bool isFile(const std::string &_path);
108 
112  GZ_COMMON_VISIBLE
113  std::string absPath(const std::string &_path);
114 
119  GZ_COMMON_VISIBLE
120  bool copyFile(const std::string &_existingFilename,
121  const std::string &_newFilename);
122 
127  GZ_COMMON_VISIBLE
128  bool copyDir(const boost::filesystem::path &_source,
129  const boost::filesystem::path &_destination);
130 
135  GZ_COMMON_VISIBLE
136  bool moveFile(const std::string &_existingFilename,
137  const std::string &_newFilename);
138 
147  GZ_COMMON_VISIBLE
148  void replaceAll(std::string &_result,
149  const std::string &_orig,
150  const std::string &_key,
151  const std::string &_replacement);
152 
161  GZ_COMMON_VISIBLE
162  std::string replaceAll(const std::string &_orig,
163  const std::string &_key,
164  const std::string &_replacement);
165 
170  GZ_COMMON_VISIBLE
171  std::vector<std::string> split(const std::string &_str,
172  const std::string &_delim);
173 
181  GZ_COMMON_VISIBLE
182  std::string unique_file_path(const std::string &_pathAndName,
183  const std::string &_extension);
185  }
186 
188  // Implementation of get_sha1
189  template<typename T>
190  std::string common::get_sha1(const T &_buffer)
191  {
192  boost::uuids::detail::sha1 sha1;
193  unsigned int hash[5];
194  std::stringstream stream;
195 
196  if (_buffer.size() == 0)
197  {
198  sha1.process_bytes(nullptr, 0);
199  }
200  else
201  {
202  sha1.process_bytes(&(_buffer[0]), _buffer.size() * sizeof(_buffer[0]));
203  }
204 
205  sha1.get_digest(hash);
206 
207  for (std::size_t i = 0; i < sizeof(hash) / sizeof(hash[0]); ++i)
208  {
209  stream << std::setfill('0')
210  << std::setw(sizeof(hash[0]) * 2)
211  << std::hex
212  << hash[i];
213  }
214 
215  return stream.str();
216  }
217 }
218 #endif
std::string get_sha1(const T &_buffer)
Compute the SHA1 hash of an array of bytes.
Definition: CommonIface.hh:190
const char * getEnv(const char *_name)
Cross platform retrieval of an environment variable.
std::vector< std::string > split(const std::string &_str, const std::string &_delim)
Splits a string into tokens.
Forward declarations for the common classes.
Definition: Animation.hh:26
std::string find_file_path(const std::string &_file)
search for a file in common::SystemPaths
bool copyDir(const boost::filesystem::path &_source, const boost::filesystem::path &_destination)
Copy a directory, overwrite the destination directory if exists.
void load()
Load the common library.
bool isFile(const std::string &_path)
Check if the given path is a file.
bool copyFile(const std::string &_existingFilename, const std::string &_newFilename)
Copy a file.
std::string cwd()
Get the current working directory.
std::string absPath(const std::string &_path)
Get the absolute path of a provided path.
std::string unique_file_path(const std::string &_pathAndName, const std::string &_extension)
Generates a path for a file which doesn&#39;t collide with existing files, by appending numbers to it (i...
std::string find_file(const std::string &_file)
search for file in common::SystemPaths
bool isDirectory(const std::string &_path)
Check if the given path is a directory.
bool moveFile(const std::string &_existingFilename, const std::string &_newFilename)
Move a file.
void add_search_path_suffix(const std::string &_suffix)
add path sufix to common::SystemPaths
void replaceAll(std::string &_result, const std::string &_orig, const std::string &_key, const std::string &_replacement)
Replace all occurances of _key with _replacement.
bool exists(const std::string &_path)
Returns true if _path is a file or directory.