AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
Filesystem.hpp
1#pragma once
2
3#include <string>
4#include <vector>
5
6namespace AMDiS
7{
8 namespace filesystem
9 {
10 // A minimalistic filesystem class
11 class path
12 : public std::vector<std::string>
13 {
14 using Super = std::vector<std::string>;
15 using iterator = Super::iterator;
16 using const_iterator = Super::const_iterator;
17
18 public:
19#ifdef _WIN32
20 static constexpr char preferred_separator = '\\';
21#else
22 static constexpr char preferred_separator = '/';
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>
44 path(std::initializer_list<String> const& list)
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 is_absolute(std::string p);
81
82 bool is_absolute() const { return is_absolute(original); }
83
84 bool is_relative() const { return !is_absolute(); }
85
87 bool is_file() const;
88
90 bool is_directory() 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>
103 friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& out, path const& p)
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 create_directories(path const&);
124
126 path current_path();
127
128 } // end namespace filesystem
129} // end namespace AMDiS
Definition: Filesystem.hpp:13
bool is_file() const
Check whether path is a regular file.
Definition: Filesystem.cpp:115
path filename() const
Returns filename path component.
Definition: Filesystem.hpp:62
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &out, path const &p)
output of the path
Definition: Filesystem.hpp:103
path & operator/=(path const &p)
Appends elements to the path.
Definition: Filesystem.cpp:107
bool is_directory() const
Check whether path is a regular file.
Definition: Filesystem.cpp:123
path parent_path() const
Returns the path of the parent path.
Definition: Filesystem.hpp:56
bool operator==(path const &p)
Lexicographically compares two paths.
Definition: Filesystem.hpp:93
path extension() const
Returns the file extension path component.
Definition: Filesystem.cpp:83
path & remove_filename()
Removes filename path component.
Definition: Filesystem.hpp:49
std::string string() const
Return the path as string.
Definition: Filesystem.cpp:33
path stem() const
Returns the stem path component.
Definition: Filesystem.cpp:72