Gazebo Common

API Reference

3.17.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 GZ_COMMON_FILESYSTEM_HH_
19 #define GZ_COMMON_FILESYSTEM_HH_
20 
21 #include <memory>
22 #include <string>
23 
24 #include <gz/common/Export.hh>
26 
27 namespace ignition
28 {
29  namespace common
30  {
34  {
38 
43  };
44 
48  bool IGNITION_COMMON_VISIBLE exists(const std::string &_path);
49 
53  bool IGNITION_COMMON_VISIBLE isDirectory(const std::string &_path);
54 
58  bool IGNITION_COMMON_VISIBLE isFile(const std::string &_path);
59 
64  bool IGNITION_COMMON_VISIBLE createDirectory(const std::string &_path);
65 
70  bool IGNITION_COMMON_VISIBLE createDirectories(const std::string &_path, std::ostream &_errorOut);
71 
75  bool IGNITION_COMMON_VISIBLE createDirectories(const std::string &_path);
76 
81  std::string IGNITION_COMMON_VISIBLE const separator(
82  std::string const &_s);
83 
97  void IGNITION_COMMON_VISIBLE changeFromUnixPath(std::string &_path);
98 
105  std::string IGNITION_COMMON_VISIBLE copyFromUnixPath(
106  const std::string &_path);
107 
114  void IGNITION_COMMON_VISIBLE changeToUnixPath(std::string &_path);
115 
122  std::string IGNITION_COMMON_VISIBLE copyToUnixPath(
123  const std::string &_path);
124 
129  std::string IGNITION_COMMON_VISIBLE absPath(const std::string &_path);
130 
137  std::string IGNITION_COMMON_VISIBLE joinPaths(const std::string &_path1,
138  const std::string &_path2);
139 
141  inline std::string joinPaths(const std::string &_path)
142  {
143  return _path;
144  }
145 
146  // The below is C++ variadic template magic to allow a joinPaths
147  // method that takes 1-n number of arguments to append together.
148 
153  template<typename... Args>
154  inline std::string joinPaths(const std::string &_path1,
155  const std::string &_path2,
156  Args const &..._args)
157  {
158  return joinPaths(joinPaths(_path1, _path2),
159  joinPaths(_args...));
160  }
161 
164  std::string IGNITION_COMMON_VISIBLE cwd();
165 
169  bool IGNITION_COMMON_VISIBLE chdir(const std::string &_dir);
170 
174  std::string IGNITION_COMMON_VISIBLE basename(
175  const std::string &_path);
176 
181  std::string IGNITION_COMMON_VISIBLE parentPath(
182  const std::string &_path);
183 
189  bool IGNITION_COMMON_VISIBLE copyFile(
190  const std::string &_existingFilename,
191  const std::string &_newFilename,
192  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
193 
198  bool IGNITION_COMMON_VISIBLE copyDirectory(
199  const std::string &_existingDirname,
200  const std::string &_newDirname,
201  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
202 
208  bool IGNITION_COMMON_VISIBLE moveFile(
209  const std::string &_existingFilename,
210  const std::string &_newFilename,
211  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
212 
218  bool IGNITION_COMMON_VISIBLE removeDirectory(
219  const std::string &_path,
220  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
221 
226  bool IGNITION_COMMON_VISIBLE removeFile(
227  const std::string &_existingFilename,
228  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
229 
234  bool IGNITION_COMMON_VISIBLE removeDirectoryOrFile(
235  const std::string &_path,
236  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
237 
242  bool IGNITION_COMMON_VISIBLE removeAll(
243  const std::string &_path,
244  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
245 
253  std::string IGNITION_COMMON_VISIBLE uniqueFilePath(
254  const std::string &_pathAndName, const std::string &_extension);
255 
259  std::string IGNITION_COMMON_VISIBLE uniqueDirectoryPath(
260  const std::string &_dir);
261 
263  class DirIterPrivate;
264 
267  class IGNITION_COMMON_VISIBLE DirIter
268  {
271  public: explicit DirIter(const std::string &_in);
272 
274  public: DirIter();
275 
279  public: std::string operator*() const;
280 
283  public: const DirIter &operator++();
284 
289  public: bool operator!=(const DirIter &_other) const;
290 
292  public: ~DirIter();
293 
295  private: void Next();
296 
298  private: void SetInternalEmpty();
299 
301  private: void CloseHandle();
302 
305  private: std::unique_ptr<DirIterPrivate> dataPtr;
307  };
308  }
309 }
310 
311 #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:42
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:37
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:33
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:267
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.