Gazebo Common

API Reference

3.14.1
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 <ignition/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 
69  bool IGNITION_COMMON_VISIBLE createDirectories(const std::string &_path);
70 
75  std::string IGNITION_COMMON_VISIBLE const separator(
76  std::string const &_s);
77 
91  void IGNITION_COMMON_VISIBLE changeFromUnixPath(std::string &_path);
92 
99  std::string IGNITION_COMMON_VISIBLE copyFromUnixPath(
100  const std::string &_path);
101 
108  void IGNITION_COMMON_VISIBLE changeToUnixPath(std::string &_path);
109 
116  std::string IGNITION_COMMON_VISIBLE copyToUnixPath(
117  const std::string &_path);
118 
123  std::string IGNITION_COMMON_VISIBLE absPath(const std::string &_path);
124 
131  std::string IGNITION_COMMON_VISIBLE joinPaths(const std::string &_path1,
132  const std::string &_path2);
133 
135  inline std::string joinPaths(const std::string &_path)
136  {
137  return _path;
138  }
139 
140  // The below is C++ variadic template magic to allow a joinPaths
141  // method that takes 1-n number of arguments to append together.
142 
147  template<typename... Args>
148  inline std::string joinPaths(const std::string &_path1,
149  const std::string &_path2,
150  Args const &..._args)
151  {
152  return joinPaths(joinPaths(_path1, _path2),
153  joinPaths(_args...));
154  }
155 
158  std::string IGNITION_COMMON_VISIBLE cwd();
159 
163  bool IGNITION_COMMON_VISIBLE chdir(const std::string &_dir);
164 
168  std::string IGNITION_COMMON_VISIBLE basename(
169  const std::string &_path);
170 
175  std::string IGNITION_COMMON_VISIBLE parentPath(
176  const std::string &_path);
177 
183  bool IGNITION_COMMON_VISIBLE copyFile(
184  const std::string &_existingFilename,
185  const std::string &_newFilename,
186  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
187 
192  bool IGNITION_COMMON_VISIBLE copyDirectory(
193  const std::string &_existingDirname,
194  const std::string &_newDirname,
195  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
196 
202  bool IGNITION_COMMON_VISIBLE moveFile(
203  const std::string &_existingFilename,
204  const std::string &_newFilename,
205  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
206 
212  bool IGNITION_COMMON_VISIBLE removeDirectory(
213  const std::string &_path,
214  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
215 
220  bool IGNITION_COMMON_VISIBLE removeFile(
221  const std::string &_existingFilename,
222  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
223 
228  bool IGNITION_COMMON_VISIBLE removeDirectoryOrFile(
229  const std::string &_path,
230  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
231 
236  bool IGNITION_COMMON_VISIBLE removeAll(
237  const std::string &_path,
238  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
239 
247  std::string IGNITION_COMMON_VISIBLE uniqueFilePath(
248  const std::string &_pathAndName, const std::string &_extension);
249 
253  std::string IGNITION_COMMON_VISIBLE uniqueDirectoryPath(
254  const std::string &_dir);
255 
257  class DirIterPrivate;
258 
261  class IGNITION_COMMON_VISIBLE DirIter
262  {
265  public: explicit DirIter(const std::string &_in);
266 
268  public: DirIter();
269 
273  public: std::string operator*() const;
274 
277  public: const DirIter &operator++();
278 
283  public: bool operator!=(const DirIter &_other) const;
284 
286  public: ~DirIter();
287 
289  private: void Next();
290 
292  private: void SetInternalEmpty();
293 
295  private: void CloseHandle();
296 
299  private: std::unique_ptr<DirIterPrivate> dataPtr;
301  };
302  }
303 }
304 
305 #endif
@ FSWO_SUPPRESS_WARNINGS
Errors that occur during filesystem manipulation should not be logged. The user will be responsible f...
Definition: Filesystem.hh:42
Forward declarations for the common classes.
STL class.
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.
#define IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
Microsoft Visual Studio does not automatically export the interface information for member variables ...
Definition: SuppressWarning.hh:64
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: 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: 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.
#define IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
Definition: SuppressWarning.hh:67
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.
A class for iterating over all items in a directory.
Definition: Filesystem.hh:261
bool copyFile(const std::string &_existingFilename, const std::string &_newFilename, const FilesystemWarningOp _warningOp=FSWO_LOG_WARNINGS)
Copy a file.
const std::string separator(std::string const &_s)
Append the preferred path separator character for this platform onto the passed-in string.
bool createDirectories(const std::string &_path)
Create directories for the given path.