All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 _SDF_FILESYSTEM_HH_
19 #define _SDF_FILESYSTEM_HH_
20 
21 #include <memory>
22 #include <string>
23 
24 #include "sdf/system_util.hh"
25 
26 #ifdef _WIN32
27 // Disable warning C4251 which is triggered by
28 // std::unique_ptr
29 #pragma warning(push)
30 #pragma warning(disable: 4251)
31 #endif
32 
33 namespace sdf
34 {
35  namespace filesystem
36  {
41  bool exists(const std::string &_path);
42 
47  bool is_directory(const std::string &_path);
48 
54  bool create_directory(const std::string &_path);
55 
56  // The below is C++ variadic template magic to allow an append
57  // method that takes 1-n number of arguments to append together.
58 
64  std::string const separator(std::string const &_s);
65 
70  template<typename... Args>
71  std::string append(Args const &... args)
72  {
73  std::string result;
74  int unpack[] {
75  0, (result += separator(args), 0)...};
76  static_cast<void>(unpack);
77  return result.substr(0, result.length() - 1);
78  }
79 
83  std::string current_path();
84 
89  std::string basename(const std::string &_path);
90 
92  class DirIterPrivate;
93 
97  {
100  public: explicit DirIter(const std::string &_in);
101 
103  public: DirIter();
104 
107  public: std::string operator*() const;
108 
111  public: const DirIter& operator++();
112 
117  public: bool operator!=(const DirIter &_other) const;
118 
120  public: ~DirIter();
121 
123  private: void next();
124 
126  private: void set_internal_empty();
127 
129  private: void close_handle();
130 
132  private: std::unique_ptr<DirIterPrivate> dataPtr;
133  };
134  }
135 }
136 
137 #ifdef _WIN32
138 #pragma warning(pop)
139 #endif
140 
141 #endif
SDFORMAT_VISIBLE std::string current_path()
Get the current working path.
SDFORMAT_VISIBLE bool is_directory(const std::string &_path)
Determine whether the given path is a directory.
SDFORMAT_VISIBLE bool create_directory(const std::string &_path)
Create a new directory on the filesystem.
std::string append(Args const &...args)
Append one or more additional path elements to the first passed in argument.
Definition: Filesystem.hh:71
SDFORMAT_VISIBLE std::string const separator(std::string const &_s)
Append the preferred path separator character for this platform onto the passed-in string...
A class for iterating over all items in a directory.
Definition: Filesystem.hh:96
SDFORMAT_VISIBLE std::string basename(const std::string &_path)
Given a path, get just the basename portion.
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
SDFORMAT_VISIBLE bool exists(const std::string &_path)
Determine whether the given path exists on the filesystem.