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/sdf_config.h>
25 #include "sdf/system_util.hh"
26 
27 #ifdef _WIN32
28 // Disable warning C4251 which is triggered by
29 // std::unique_ptr
30 #pragma warning(push)
31 #pragma warning(disable: 4251)
32 #endif
33 
34 namespace sdf
35 {
36  namespace filesystem
37  {
38  // Inline bracke to help doxygen filtering.
39  inline namespace SDF_VERSION_NAMESPACE {
40  //
41 
46  bool exists(const std::string &_path);
47 
52  bool is_directory(const std::string &_path);
53 
59  bool create_directory(const std::string &_path);
60 
61  // The below is C++ variadic template magic to allow an append
62  // method that takes 1-n number of arguments to append together.
63 
69  std::string const separator(std::string const &_s);
70 
75  template<typename... Args>
76  std::string append(Args const &... args)
77  {
78  std::string result;
79  int unpack[] {
80  0, (result += separator(args), 0)...};
81  static_cast<void>(unpack);
82  return result.substr(0, result.length() - 1);
83  }
84 
88  std::string current_path();
89 
94  std::string basename(const std::string &_path);
95 
97  class DirIterPrivate;
98 
102  {
105  public: explicit DirIter(const std::string &_in);
106 
108  public: DirIter();
109 
112  public: std::string operator*() const;
113 
116  public: const DirIter& operator++();
117 
122  public: bool operator!=(const DirIter &_other) const;
123 
125  public: ~DirIter();
126 
128  private: void next();
129 
131  private: void set_internal_empty();
132 
134  private: void close_handle();
135 
137  private: std::unique_ptr<DirIterPrivate> dataPtr;
138  };
139  }
140  }
141 }
142 
143 #ifdef _WIN32
144 #pragma warning(pop)
145 #endif
146 
147 #endif
SDFORMAT_VISIBLE std::string const separator(std::string const &_s)
Append the preferred path separator character for this platform onto the passed-in string...
SDFORMAT_VISIBLE bool exists(const std::string &_path)
Determine whether the given path exists on the filesystem.
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
A class for iterating over all items in a directory.
Definition: Filesystem.hh:101
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:76
namespace for Simulation Description Format parser
Definition: AirPressure.hh:25
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.