dune-vtk 2.12
Loading...
Searching...
No Matches
filesystem.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3#pragma once
4
5#include <string>
6#include <vector>
7
8#include "string.hh"
9
10namespace Dune::Vtk
11{
12 // A minimalistic filesystem class
13 class Path
14 : public std::vector<std::string>
15 {
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>
40 : Super(it, end_it)
41 {
42 original = this->string();
43 }
44
45 template <class String>
47 : Path(list.begin(), list.end())
48 {}
49
52 {
53 this->pop_back();
54 return *this;
55 }
56
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>
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 inline Path operator/ (Path lhs, Path const& rhs)
134 {
135 return lhs /= rhs;
136 }
137
138} // end namespace Dune::Vtk
Definition datacollector.hh:19
Path relative(Path const &a, Path const &b)
Find the path of a relative to directory of b
Definition filesystem.cc:174
Path operator/(Path lhs, Path const &rhs)
Definition filesystem.hh:133
bool createDirectories(Path const &p)
Create directory and non existing parent directories.
Definition filesystem.cc:141
Path currentPath()
Returns the current path.
Definition filesystem.cc:126
X & lhs()
Y & rhs()
iterator end()
bool empty() const
iterator begin()
Implementation of DataCollector for cells, with continuous data.
Definition celltypedatacollector.hh:20
Definition filesystem.hh:15
bool isDirectory() const
Check whether path is a regular file.
Definition filesystem.cc:118
static constexpr char preferredSeparator
Definition filesystem.hh:24
Path & operator/=(Path const &p)
Appends elements to the path.
Definition filesystem.cc:102
Path & removeFilename()
Removes filename path component.
Definition filesystem.hh:51
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:67
Path extension() const
Returns the file extension path component.
Definition filesystem.cc:78
std::string string() const
Return the path as string.
Definition filesystem.cc:28
bool isFile() const
Check whether path is a regular file.
Definition filesystem.cc:110
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
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &out, Path const &p)
output of the path
Definition filesystem.hh:105
Path(String const &p)
Definition filesystem.hh:32
void split(std::string p)
Definition filesystem.cc:41
std::string back(std::string ... args)
std::string pop_back(std::string ... args)