dune-vtk 2.12
Loading...
Searching...
No Matches
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
8namespace Dune::Vtk
9{
10 // A minimalistic filesystem class
11 class Path
12 : public std::vector<std::string>
13 {
15 using iterator = Super::iterator;
16 using const_iterator = Super::const_iterator;
17
18 public:
19#ifdef _WIN32
20 static constexpr char preferredSeparator = '\\';
21#else
22 static constexpr char preferredSeparator = '/';
23#endif
24
25 public:
26 Path() = default;
27
28 // NOTE: implicit conversion is allowed here
29 template <class String>
30 Path(String const& p)
31 : original(p)
32 {
33 split(p);
34 }
35
36 template <class InputIt>
37 Path(InputIt it, InputIt end_it)
38 : Super(it, end_it)
39 {
40 original = this->string();
41 }
42
43 template <class String>
45 : Path(list.begin(), list.end())
46 {}
47
50 {
51 this->pop_back();
52 return *this;
53 }
54
57 {
58 return empty() ? Path() : Path(begin(), --end());
59 }
60
62 Path filename() const
63 {
64 return empty() ? Path() : Path(back());
65 }
66
68 Path stem() const;
69
71 Path extension() const;
72
74 std::string string() const;
75
77
80 static bool isAbsolute(std::string p);
81
82 bool isAbsolute() const { return isAbsolute(original); }
83
84 bool isRelative() const { return !isAbsolute(); }
85
87 bool isFile() const;
88
90 bool isDirectory() const;
91
93 bool operator==(Path const& p)
94 {
95 return this->string() == p.string();
96 }
97
99 Path& operator/=(Path const& p);
100
102 template <class CharT, class Traits>
104 {
105 out << '"' << p.string() << '"';
106 return out;
107 }
108
109 protected:
110
111 // split the path string into names separated by a `/`, remove relative directories,
112 // like `.` or `..`, if possible.
113 void split(std::string p);
114
115 private:
116 std::string original = "";
117 };
118
120 bool exists(Path const&);
121
123 bool createDirectories(Path const&);
124
126 Path currentPath();
127
129 Path relative(Path const& a, Path const& b);
130
131 inline Path operator/ (Path lhs, Path const& rhs)
132 {
133 return lhs /= rhs;
134 }
135
136} // end namespace Dune::Vtk
Definition writer.hh:16
Path relative(Path const &a, Path const &b)
Find the path of a relative to directory of b
Definition filesystem.cc:172
Path operator/(Path lhs, Path const &rhs)
Definition filesystem.hh:131
bool createDirectories(Path const &p)
Create directory and non existing parent directories.
Definition filesystem.cc:139
Path currentPath()
Returns the current path.
Definition filesystem.cc:124
X & lhs()
Y & rhs()
iterator end()
bool empty() const
iterator begin()
Definition filesystem.hh:13
bool isDirectory() const
Check whether path is a regular file.
Definition filesystem.cc:116
static constexpr char preferredSeparator
Definition filesystem.hh:22
Path & operator/=(Path const &p)
Appends elements to the path.
Definition filesystem.cc:100
Path & removeFilename()
Removes filename path component.
Definition filesystem.hh:49
Path parentPath() const
Returns the path of the parent path.
Definition filesystem.hh:56
Path()=default
Path(std::initializer_list< String > const &list)
Definition filesystem.hh:44
Path stem() const
Returns the stem path component.
Definition filesystem.cc:65
Path extension() const
Returns the file extension path component.
Definition filesystem.cc:76
std::string string() const
Return the path as string.
Definition filesystem.cc:26
bool isFile() const
Check whether path is a regular file.
Definition filesystem.cc:108
Path filename() const
Returns filename path component.
Definition filesystem.hh:62
bool isRelative() const
Definition filesystem.hh:84
Path(InputIt it, InputIt end_it)
Definition filesystem.hh:37
bool operator==(Path const &p)
Lexicographically compares two paths.
Definition filesystem.hh:93
bool isAbsolute() const
Definition filesystem.hh:82
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &out, Path const &p)
output of the path
Definition filesystem.hh:103
Path(String const &p)
Definition filesystem.hh:30
void split(std::string p)
Definition filesystem.cc:39
std::string back(std::string ... args)
std::string pop_back(std::string ... args)