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 <gz/utils/ImplPtr.hh>
25 #include <sdf/sdf_config.h>
26 #include "sdf/system_util.hh"
27 
28 namespace sdf
29 {
30  // Inline bracke to help doxygen filtering.
31  inline namespace SDF_VERSION_NAMESPACE {
32  //
33 
34  namespace filesystem
35  {
40  bool exists(const std::string &_path);
41 
46  bool is_directory(const std::string &_path);
47 
53  bool create_directory(const std::string &_path);
54 
55  // The below is C++ variadic template magic to allow an append
56  // method that takes 1-n number of arguments to append together.
57 
63  std::string const separator(std::string const &_s);
64 
69  template<typename... Args>
70  std::string append(Args const &... args)
71  {
72  std::string result;
73  int unpack[] {
74  0, (result += separator(args), 0)...};
75  static_cast<void>(unpack);
76  return result.substr(0, result.length() - 1);
77  }
78 
82  std::string current_path();
83 
88  std::string basename(const std::string &_path);
89 
93  {
96  public: explicit DirIter(const std::string &_in);
97 
99  public: DirIter();
100 
103  public: std::string operator*() const;
104 
107  public: const DirIter& operator++();
108 
113  public: bool operator!=(const DirIter &_other) const;
114 
116  public: ~DirIter();
117 
119  private: void next();
120 
122  private: void set_internal_empty();
123 
125  private: void close_handle();
126 
128  GZ_UTILS_UNIQUE_IMPL_PTR(dataPtr)
129  };
130  }
131  }
132 }
133 
134 #endif
sdf
namespace for Simulation Description Format parser
Definition: Actor.hh:34
sdf::SDF_VERSION_NAMESPACE::filesystem::exists
GZ_SDFORMAT_VISIBLE bool exists(const std::string &_path)
Determine whether the given path exists on the filesystem.
sdf_config.h
SDFORMAT_VISIBLE
#define SDFORMAT_VISIBLE
Definition: system_util.hh:25
sdf::SDF_VERSION_NAMESPACE::filesystem::create_directory
GZ_SDFORMAT_VISIBLE bool create_directory(const std::string &_path)
Create a new directory on the filesystem.
sdf::SDF_VERSION_NAMESPACE::filesystem::append
std::string append(Args const &... args)
Append one or more additional path elements to the first passed in argument.
Definition: Filesystem.hh:70
sdf::SDF_VERSION_NAMESPACE::filesystem::separator
GZ_SDFORMAT_VISIBLE const std::string separator(std::string const &_s)
Append the preferred path separator character for this platform onto the passed-in string.
sdf::SDF_VERSION_NAMESPACE::filesystem::current_path
GZ_SDFORMAT_VISIBLE std::string current_path()
Get the current working path.
sdf::SDF_VERSION_NAMESPACE::filesystem::basename
GZ_SDFORMAT_VISIBLE std::string basename(const std::string &_path)
Given a path, get just the basename portion.
system_util.hh
sdf::SDF_VERSION_NAMESPACE::filesystem::is_directory
GZ_SDFORMAT_VISIBLE bool is_directory(const std::string &_path)
Determine whether the given path is a directory.
sdf::SDF_VERSION_NAMESPACE::filesystem::DirIter
A class for iterating over all items in a directory.
Definition: Filesystem.hh:92