Ignition Common

API Reference

3.0.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 
168  bool IGNITION_COMMON_VISIBLE copyFile(
169  const std::string &_existingFilename,
170  const std::string &_newFilename,
171  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
172 
178  bool IGNITION_COMMON_VISIBLE moveFile(
179  const std::string &_existingFilename,
180  const std::string &_newFilename,
181  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
182 
188  bool IGNITION_COMMON_VISIBLE removeDirectory(
189  const std::string &_path,
190  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
191 
196  bool IGNITION_COMMON_VISIBLE removeFile(
197  const std::string &_existingFilename,
198  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
199 
204  bool IGNITION_COMMON_VISIBLE removeDirectoryOrFile(
205  const std::string &_path,
206  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
207 
212  bool IGNITION_COMMON_VISIBLE removeAll(
213  const std::string &_path,
214  const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS);
215 
217  class DirIterPrivate;
218 
221  class IGNITION_COMMON_VISIBLE DirIter
222  {
225  public: explicit DirIter(const std::string &_in);
226 
228  public: DirIter();
229 
233  public: std::string operator*() const;
234 
237  public: const DirIter &operator++();
238 
243  public: bool operator!=(const DirIter &_other) const;
244 
246  public: ~DirIter();
247 
249  private: void Next();
250 
252  private: void SetInternalEmpty();
253 
255  private: void CloseHandle();
256 
259  private: std::unique_ptr<DirIterPrivate> dataPtr;
261  };
262  }
263 }
264 
265 #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 basename(const std::string &_path)
Given a path, get just the basename portion.
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:221
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 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 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