Gazebo Common

API Reference

4.7.0
gz/common/Filesystem.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2017 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 
18 #ifndef IGNITION_COMMON_FILESYSTEM_HH_
19 #define IGNITION_COMMON_FILESYSTEM_HH_
20 
21 #include <memory>
22 #include <string>
23 
24 #include <gz/common/config.hh>
25 #include <gz/common/Export.hh>
27 
28 namespace ignition
29 {
30  namespace common
31  {
35  {
39 
44  };
45 
49  bool IGNITION_COMMON_VISIBLE exists(const std::string &_path);
50 
54  bool IGNITION_COMMON_VISIBLE isDirectory(const std::string &_path);
55 
59  bool IGNITION_COMMON_VISIBLE isFile(const std::string &_path);
60 
65  bool IGNITION_COMMON_VISIBLE createDirectory(const std::string &_path);
66 
71  bool IGNITION_COMMON_VISIBLE createDirectories(const std::string &_path, std::ostream &_errorOut);
72 
76  bool IGNITION_COMMON_VISIBLE createDirectories(const std::string &_path);
77 
82  std::string IGNITION_COMMON_VISIBLE const separator(
83  std::string const &_s);
84 
98  void IGNITION_COMMON_VISIBLE changeFromUnixPath(std::string &_path);
99 
106  std::string IGNITION_COMMON_VISIBLE copyFromUnixPath(
107  const std::string &_path);
108 
115  void IGNITION_COMMON_VISIBLE changeToUnixPath(std::string &_path);
116 
123  std::string IGNITION_COMMON_VISIBLE copyToUnixPath(
124  const std::string &_path);
125 
130  std::string IGNITION_COMMON_VISIBLE absPath(const std::string &_path);
131 
138  std::string IGNITION_COMMON_VISIBLE joinPaths(const std::string &_path1,
139  const std::string &_path2);
140 
142  inline std::string joinPaths(const std::string &_path)
143  {
144  return _path;
145  }
146 
147  // The below is C++ variadic template magic to allow a joinPaths
148  // method that takes 1-n number of arguments to append together.
149 
154  template<typename... Args>
155  inline std::string joinPaths(const std::string &_path1,
156  const std::string &_path2,
157  Args const &..._args)
158  {
159  return joinPaths(joinPaths(_path1, _path2),
160  joinPaths(_args...));
161  }
162 
165  std::string IGNITION_COMMON_VISIBLE cwd();
166 
170  bool IGNITION_COMMON_VISIBLE chdir(const std::string &_dir);
171 
175  std::string IGNITION_COMMON_VISIBLE basename(
176  const std::string &_path);
177 
182  std::string IGNITION_COMMON_VISIBLE parentPath(
183  const std::string &_path);
184 
190  bool IGNITION_COMMON_VISIBLE copyFile(
191  const std::string &_existingFilename,
192  const std::string &_newFilename,
193  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
194 
199  bool IGNITION_COMMON_VISIBLE copyDirectory(
200  const std::string &_existingDirname,
201  const std::string &_newDirname,
202  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
203 
209  bool IGNITION_COMMON_VISIBLE moveFile(
210  const std::string &_existingFilename,
211  const std::string &_newFilename,
212  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
213 
219  bool IGNITION_COMMON_VISIBLE removeDirectory(
220  const std::string &_path,
221  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
222 
227  bool IGNITION_COMMON_VISIBLE removeFile(
228  const std::string &_existingFilename,
229  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
230 
235  bool IGNITION_COMMON_VISIBLE removeDirectoryOrFile(
236  const std::string &_path,
237  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
238 
243  bool IGNITION_COMMON_VISIBLE removeAll(
244  const std::string &_path,
245  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
246 
254  std::string IGNITION_COMMON_VISIBLE uniqueFilePath(
255  const std::string &_pathAndName, const std::string &_extension);
256 
260  std::string IGNITION_COMMON_VISIBLE uniqueDirectoryPath(
261  const std::string &_dir);
262 
264  class DirIterPrivate;
265 
268  class IGNITION_COMMON_VISIBLE DirIter
269  {
272  public: explicit DirIter(const std::string &_in);
273 
275  public: DirIter();
276 
280  public: std::string operator*() const;
281 
284  public: const DirIter &operator++();
285 
290  public: bool operator!=(const DirIter &_other) const;
291 
293  public: ~DirIter();
294 
296  private: void Next();
297 
299  private: void SetInternalEmpty();
300 
302  private: void CloseHandle();
303 
306  private: std::unique_ptr<DirIterPrivate> dataPtr;
308  };
309  }
310 }
311 
312 #endif
@ FSWO_SUPPRESS_WARNINGS
Errors that occur during filesystem manipulation should not be logged. The user will be responsible f...
Definition: gz/common/Filesystem.hh:43
Forward declarations for the common classes.
STL class.
#define IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
Microsoft Visual Studio does not automatically export the interface information for member variables ...
Definition: gz/common/SuppressWarning.hh:65
std::string uniqueDirectoryPath(const std::string &_dir)
Unique directory path to not overwrite existing directory.
std::string joinPaths(const std::string &_path1, const std::string &_path2)
Join two strings together to form a path.
std::string cwd()
Get the current working directory.
bool moveFile(const std::string &_existingFilename, const std::string &_newFilename, const FilesystemWarningOp _warningOp=FSWO_LOG_WARNINGS)
Move a file.
std::string copyFromUnixPath(const std::string &_path)
Returns a copy of _path which has been passed through changeFromUnixPath.
const DirIter & operator++()
Pre-increment operator; moves to next directory record.
bool exists(const std::string &_path)
Determine whether the given path exists on the filesystem.
bool chdir(const std::string &_dir)
Change current working directory to _dir.
bool createDirectory(const std::string &_path)
Create a new directory on the filesystem. Intermediate directories must already exist.
std::string uniqueFilePath(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 isFile(const std::string &_path)
Check if the given path is a file.
@ FSWO_LOG_WARNINGS
Errors that occur during filesystem manipulation should be logged as warnings using ignwarn....
Definition: gz/common/Filesystem.hh:38
bool isDirectory(const std::string &_path)
Determine whether the given path is a directory.
bool removeFile(const std::string &_existingFilename, const FilesystemWarningOp _warningOp=FSWO_LOG_WARNINGS)
Remove a file.
DirIter()
Constructor for end element.
bool operator!=(const DirIter &_other) const
Comparison operator to see if this iterator is at the same point as another iterator.
FilesystemWarningOp
Options for how to handle errors that occur in functions that manipulate the filesystem.
Definition: gz/common/Filesystem.hh:34
std::string absPath(const std::string &_path)
Get the absolute path of a provided path. Relative paths are resolved relative to the current working...
bool removeAll(const std::string &_path, const FilesystemWarningOp _warningOp=FSWO_LOG_WARNINGS)
Remove a file or a directory and all its contents.
STL class.
std::string parentPath(const std::string &_path)
Given a path, get just its parent path portion, without separator at the end.
void changeToUnixPath(std::string &_path)
Replace the preferred directory separator of the current operating system with a forward-slash '/'....
bool removeDirectoryOrFile(const std::string &_path, const FilesystemWarningOp _warningOp=FSWO_LOG_WARNINGS)
Remove an empty directory or file.
void changeFromUnixPath(std::string &_path)
Replace forward-slashes '/' with the preferred directory separator of the current operating system....
std::string basename(const std::string &_path)
Given a path, get just the basename portion.
bool copyDirectory(const std::string &_existingDirname, const std::string &_newDirname, const FilesystemWarningOp _warningOp=FSWO_LOG_WARNINGS)
Copy a directory, overwrite the destination directory if exists.
std::string operator*() const
Dereference operator; returns current directory record.
bool removeDirectory(const std::string &_path, const FilesystemWarningOp _warningOp=FSWO_LOG_WARNINGS)
Remove an empty directory.
std::string copyToUnixPath(const std::string &_path)
Returns a copy of _path which has been passed through changeToUnixPath.
bool createDirectories(const std::string &_path, std::ostream &_errorOut)
Create directories for the given path errors are printed to the given stream.
A class for iterating over all items in a directory.
Definition: gz/common/Filesystem.hh:268
bool copyFile(const std::string &_existingFilename, const std::string &_newFilename, const FilesystemWarningOp _warningOp=FSWO_LOG_WARNINGS)
Copy a file.
#define IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
Definition: gz/common/SuppressWarning.hh:68
const std::string separator(std::string const &_s)
Append the preferred path separator character for this platform onto the passed-in string.