Ignition Common

API Reference

3.5.0
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 
122  std::string IGNITION_COMMON_VISIBLE absPath(const std::string &_path);
123 
128  std::string IGNITION_COMMON_VISIBLE joinPaths(const std::string &_path1,
129  const std::string &_path2);
130 
132  inline std::string joinPaths(const std::string &_path)
133  {
134  return _path;
135  }
136 
137  // The below is C++ variadic template magic to allow a joinPaths
138  // method that takes 1-n number of arguments to append together.
139 
144  template<typename... Args>
145  inline std::string joinPaths(const std::string &_path1,
146  const std::string &_path2,
147  Args const &..._args)
148  {
149  return joinPaths(joinPaths(_path1, _path2),
150  joinPaths(_args...));
151  }
152 
155  std::string IGNITION_COMMON_VISIBLE cwd();
156 
160  std::string IGNITION_COMMON_VISIBLE basename(
161  const std::string &_path);
162 
167  std::string IGNITION_COMMON_VISIBLE parentPath(
168  const std::string &_path);
169 
175  bool IGNITION_COMMON_VISIBLE copyFile(
176  const std::string &_existingFilename,
177  const std::string &_newFilename,
178  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
179 
184  bool IGNITION_COMMON_VISIBLE copyDirectory(
185  const std::string &_existingDirname,
186  const std::string &_newDirname,
187  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
188 
194  bool IGNITION_COMMON_VISIBLE moveFile(
195  const std::string &_existingFilename,
196  const std::string &_newFilename,
197  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
198 
204  bool IGNITION_COMMON_VISIBLE removeDirectory(
205  const std::string &_path,
206  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
207 
212  bool IGNITION_COMMON_VISIBLE removeFile(
213  const std::string &_existingFilename,
214  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
215 
220  bool IGNITION_COMMON_VISIBLE removeDirectoryOrFile(
221  const std::string &_path,
222  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
223 
228  bool IGNITION_COMMON_VISIBLE removeAll(
229  const std::string &_path,
230  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
231 
239  std::string IGNITION_COMMON_VISIBLE uniqueFilePath(
240  const std::string &_pathAndName, const std::string &_extension);
241 
245  std::string IGNITION_COMMON_VISIBLE uniqueDirectoryPath(
246  const std::string &_dir);
247 
249  class DirIterPrivate;
250 
253  class IGNITION_COMMON_VISIBLE DirIter
254  {
257  public: explicit DirIter(const std::string &_in);
258 
260  public: DirIter();
261 
265  public: std::string operator*() const;
266 
269  public: const DirIter &operator++();
270 
275  public: bool operator!=(const DirIter &_other) const;
276 
278  public: ~DirIter();
279 
281  private: void Next();
282 
284  private: void SetInternalEmpty();
285 
287  private: void CloseHandle();
288 
291  private: std::unique_ptr<DirIterPrivate> dataPtr;
293  };
294  }
295 }
296 
297 #endif
bool removeAll(const std::string &_path, const FilesystemWarningOp _warningOp=FSWO_LOG_WARNINGS)
Remove a file or a directory and all its contents.
bool removeDirectoryOrFile(const std::string &_path, const FilesystemWarningOp _warningOp=FSWO_LOG_WARNINGS)
Remove an empty directory or file.
void changeToUnixPath(std::string &_path)
Replace the preferred directory separator of the current operating system with a forward-slash &#39;/&#39;...
std::string parentPath(const std::string &_path)
Given a path, get just its parent path portion, without separator at the end.
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.
void changeFromUnixPath(std::string &_path)
Replace forward-slashes &#39;/&#39; with the preferred directory separator of the current operating system...
A class for iterating over all items in a directory.
Definition: Filesystem.hh:253
std::string copyToUnixPath(const std::string &_path)
Returns a copy of _path which has been passed through changeToUnixPath.
bool removeDirectory(const std::string &_path, const FilesystemWarningOp _warningOp=FSWO_LOG_WARNINGS)
Remove an empty directory.
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: SuppressWarning.hh:67
std::string const separator(std::string const &_s)
Append the preferred path separator character for this platform onto the passed-in string...
STL class.
bool createDirectories(const std::string &_path)
Create directories for the given path.
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.
Errors that occur during filesystem manipulation should be logged as warnings using ignwarn...
Definition: Filesystem.hh:37
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.
std::string uniqueFilePath(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...
bool exists(const std::string &_path)
Determine whether the given path exists on the filesystem.
bool isFile(const std::string &_path)
Check if the given path is a file.
bool isDirectory(const std::string &_path)
Determine whether the given path is a directory.
Errors that occur during filesystem manipulation should not be logged. The user will be responsible f...
Definition: Filesystem.hh:42
bool removeFile(const std::string &_existingFilename, const FilesystemWarningOp _warningOp=FSWO_LOG_WARNINGS)
Remove a file.
FilesystemWarningOp
Options for how to handle errors that occur in functions that manipulate the filesystem.
Definition: Filesystem.hh:33
bool createDirectory(const std::string &_path)
Create a new directory on the filesystem. Intermediate directories must already exist.
std::string absPath(const std::string &_path)
Get the absolute path of a provided path.
Forward declarations for the common classes.
#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