dune-vtk  0.2
filesystem.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 #include "string.hh"
7 
8 namespace Dune
9 {
10  namespace Vtk
11  {
12  // A minimalistic filesystem class
13  class Path
14  : public std::vector<std::string>
15  {
16  using Super = std::vector<std::string>;
17  using iterator = Super::iterator;
18  using const_iterator = Super::const_iterator;
19 
20  public:
21 #ifdef _WIN32
22  static constexpr char preferredSeparator = '\\';
23 #else
24  static constexpr char preferredSeparator = '/';
25 #endif
26 
27  public:
28  Path() = default;
29 
30  // NOTE: implicit conversion is allowed here
31  template <class String>
32  Path(String const& p)
33  : original(p)
34  {
35  split(p);
36  }
37 
38  template <class InputIt>
39  Path(InputIt it, InputIt end_it)
40  : Super(it, end_it)
41  {
42  original = this->string();
43  }
44 
45  template <class String>
46  Path(std::initializer_list<String> const& list)
47  : Path(list.begin(), list.end())
48  {}
49 
52  {
53  this->pop_back();
54  return *this;
55  }
56 
58  Path parentPath() const
59  {
60  return empty() ? Path() : Path(begin(), --end());
61  }
62 
64  Path filename() const
65  {
66  return empty() ? Path() : Path(back());
67  }
68 
70  Path stem() const;
71 
73  Path extension() const;
74 
76  std::string string() const;
77 
79 
82  static bool isAbsolute(std::string p);
83 
84  bool isAbsolute() const { return isAbsolute(original); }
85 
86  bool isRelative() const { return !isAbsolute(); }
87 
89  bool isFile() const;
90 
92  bool isDirectory() const;
93 
95  bool operator==(Path const& p)
96  {
97  return this->string() == p.string();
98  }
99 
101  Path& operator/=(Path const& p);
102 
104  template <class CharT, class Traits>
105  friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& out, Path const& p)
106  {
107  out << '"' << p.string() << '"';
108  return out;
109  }
110 
111  protected:
112 
113  // split the path string into names separated by a `/`, remove relative directories,
114  // like `.` or `..`, if possible.
115  void split(std::string p);
116 
117  private:
118  std::string original = "";
119  };
120 
122  bool exists(Path const&);
123 
125  bool createDirectories(Path const&);
126 
128  Path currentPath();
129 
131  Path relative(Path const& a, Path const& b);
132 
133  } // end namespace Vtk
134 } // end namespace Dune
Definition: writer.hh:13
Path relative(Path const &a, Path const &b)
Find the path of a relative to directory of b
Definition: filesystem.cc:173
bool createDirectories(Path const &p)
Create directory and non existing parent directories.
Definition: filesystem.cc:140
Path currentPath()
Returns the current path.
Definition: filesystem.cc:125
bool exists(Path const &p)
Test whether the path is a valid (existing and accessible) file / directory.
Definition: filesystem.cc:134
Definition: filesystem.hh:15
bool isDirectory() const
Check whether path is a regular file.
Definition: filesystem.cc:117
static constexpr char preferredSeparator
Definition: filesystem.hh:24
Path & operator/=(Path const &p)
Appends elements to the path.
Definition: filesystem.cc:101
Path parentPath() const
Returns the path of the parent path.
Definition: filesystem.hh:58
Path()=default
Path(std::initializer_list< String > const &list)
Definition: filesystem.hh:46
Path stem() const
Returns the stem path component.
Definition: filesystem.cc:66
Path extension() const
Returns the file extension path component.
Definition: filesystem.cc:77
std::string string() const
Return the path as string.
Definition: filesystem.cc:27
bool isFile() const
Check whether path is a regular file.
Definition: filesystem.cc:109
Path filename() const
Returns filename path component.
Definition: filesystem.hh:64
bool isRelative() const
Definition: filesystem.hh:86
Path(InputIt it, InputIt end_it)
Definition: filesystem.hh:39
bool operator==(Path const &p)
Lexicographically compares two paths.
Definition: filesystem.hh:95
bool isAbsolute() const
Definition: filesystem.hh:84
Path & removeFilename()
Removes filename path component.
Definition: filesystem.hh:51
Path(String const &p)
Definition: filesystem.hh:32
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &out, Path const &p)
output of the path
Definition: filesystem.hh:105
void split(std::string p)
Definition: filesystem.cc:40