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 #include <boost/uuid/sha1.hpp>
23 #include <iomanip>
24 #include <sstream>
25 
26 #include "gazebo/util/system.hh"
27 
28 namespace gazebo
29 {
30  namespace common
31  {
34 
36  GZ_COMMON_VISIBLE
37  void load();
38 
41  GZ_COMMON_VISIBLE
42  void add_search_path_suffix(const std::string &_suffix);
43 
47  GZ_COMMON_VISIBLE
48  std::string find_file(const std::string &_file);
49 
55  GZ_COMMON_VISIBLE
56  std::string find_file(const std::string &_file,
57  bool _searchLocalPath);
58 
62  GZ_COMMON_VISIBLE
63  std::string find_file_path(const std::string &_file);
64 
69  template<typename T>
70  std::string get_sha1(const T &_buffer);
71 
75  GZ_COMMON_VISIBLE
76  const char *getEnv(const char *_name);
77 
80  GZ_COMMON_VISIBLE
81  std::string cwd();
82 
86  GZ_COMMON_VISIBLE
87  bool exists(const std::string &_path);
88 
92  GZ_COMMON_VISIBLE
93  bool isDirectory(const std::string &_path);
94 
98  GZ_COMMON_VISIBLE
99  bool isFile(const std::string &_path);
100 
104  GZ_COMMON_VISIBLE
105  std::string absPath(const std::string &_path);
106 
111  GZ_COMMON_VISIBLE
112  bool copyFile(const std::string &_existingFilename,
113  const std::string &_newFilename);
114 
119  GZ_COMMON_VISIBLE
120  bool moveFile(const std::string &_existingFilename,
121  const std::string &_newFilename);
122 
131  GZ_COMMON_VISIBLE
132  void replaceAll(std::string &_result,
133  const std::string &_orig,
134  const std::string &_key,
135  const std::string &_replacement);
136 
145  GZ_COMMON_VISIBLE
146  std::string replaceAll(const std::string &_orig,
147  const std::string &_key,
148  const std::string &_replacement);
149 
154  GZ_COMMON_VISIBLE
155  std::vector<std::string> split(const std::string &_str,
156  const std::string &_delim);
157 
165  GZ_COMMON_VISIBLE
166  std::string unique_file_path(const std::string &_pathAndName,
167  const std::string &_extension);
169  }
170 
172  // Implementation of get_sha1
173  template<typename T>
174  std::string common::get_sha1(const T &_buffer)
175  {
176  boost::uuids::detail::sha1 sha1;
177  unsigned int hash[5];
178  std::stringstream stream;
179 
180  if (_buffer.size() == 0)
181  {
182  sha1.process_bytes(nullptr, 0);
183  }
184  else
185  {
186  sha1.process_bytes(&(_buffer[0]), _buffer.size() * sizeof(_buffer[0]));
187  }
188 
189  sha1.get_digest(hash);
190 
191  for (std::size_t i = 0; i < sizeof(hash) / sizeof(hash[0]); ++i)
192  {
193  stream << std::setfill('0')
194  << std::setw(sizeof(hash[0]) * 2)
195  << std::hex
196  << hash[i];
197  }
198 
199  return stream.str();
200  }
201 }
202 #endif
std::string get_sha1(const T &_buffer)
Compute the SHA1 hash of an array of bytes.
Definition: CommonIface.hh:174
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.
std::string find_file_path(const std::string &_file)
search for a file in common::SystemPaths
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'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.