Commit eaed36c2 authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

renamed path to Path and use camelCase names

parent 0bf4fa59
...@@ -134,6 +134,7 @@ namespace Dune ...@@ -134,6 +134,7 @@ namespace Dune
void readString(std::istream& stream, std::string& name) void readString(std::istream& stream, std::string& name)
{ {
DUNE_THROW(Dune::NotImplemented, "Method readString() not yet implemented");
name = "Hallo"; name = "Hallo";
} }
......
...@@ -21,174 +21,171 @@ ...@@ -21,174 +21,171 @@
template <class... Args> template <class... Args>
void inline _ignore_(Args&&...) {} void inline _ignore_(Args&&...) {}
namespace Dune namespace Dune {
namespace Gmsh4 {
std::string Path::string() const
{ {
namespace Gmsh4 if (empty())
{ return ".";
std::string Path::string() const auto it = begin();
{ auto result = *it;
if (empty()) for (++it; it != end(); ++it)
return "."; result += preferredSeparator + *it;
return result;
}
auto it = begin();
auto result = *it;
for (++it; it != end(); ++it)
result += preferred_separator + *it;
return result;
}
void Path::split(std::string p)
{
std::string separators = "/\\";
bool relative = true;
void Path::split(std::string p) trim(p);
{ Dune::Vtk::split(p.begin(), p.end(), separators.begin(), separators.end(),
std::string separators = "/\\"; [this,&relative](auto first, auto end)
bool relative = true;
Gmsh4::trim(p);
Gmsh4::split(p.begin(), p.end(), separators.begin(), separators.end(),
[this,&relative](auto first, auto end)
{
auto token = std::string(first, end);
if ((!token.empty() && token != "." && token != "..") || (token.empty() && this->empty())) {
this->push_back(token);
relative = false;
} else if (token == "..") {
if (relative || this->empty()) {
this->push_back(token);
}
else {
this->pop_back();
}
}
});
}
Path Path::stem() const
{ {
auto f = filename().string(); auto token = std::string(first, end);
auto pos = f.find_last_of('.');
if (f == "." || f == ".." || pos == std::string::npos) if ((!token.empty() && token != "." && token != "..") || (token.empty() && this->empty())) {
return {f}; this->push_back(token);
else relative = false;
return {f.substr(0,pos)}; } else if (token == "..") {
} if (relative || this->empty()) {
this->push_back(token);
}
else {
this->pop_back();
}
}
});
}
Path Path::extension() const Path Path::stem() const
{ {
auto f = filename().string(); auto f = filename().string();
auto pos = f.find_last_of('.'); auto pos = f.find_last_of('.');
if (f == "." || f == ".." || pos == std::string::npos) if (f == "." || f == ".." || pos == std::string::npos)
return {}; return {f};
else else
return {f.substr(pos)}; return {f.substr(0,pos)};
} }
bool Path::is_absolute(std::string p) Path Path::extension() const
{ {
if (p[0] == '/') auto f = filename().string();
return true; auto pos = f.find_last_of('.');
if (f == "." || f == ".." || pos == std::string::npos)
return {};
else
return {f.substr(pos)};
}
// c:\ or z:/
if (std::isalpha(p[0]) && p[1] == ':' && (p[2] == '/' || p[2] == '\\'))
return true;
return false; bool Path::isAbsolute(std::string p)
} {
if (p[0] == '/')
return true;
// c:\ or z:/
if (std::isalpha(p[0]) && p[1] == ':' && (p[2] == '/' || p[2] == '\\'))
return true;
Path& Path::operator/=(Path const& p) return false;
{ }
insert(end(), p.begin(), p.end());
original += preferred_separator + p.original;
return *this;
}
bool Path::is_file() const Path& Path::operator/=(Path const& p)
{ {
std::string p = this->string(); insert(end(), p.begin(), p.end());
struct stat info; original += preferredSeparator + p.original;
return stat(p.c_str(), &info) == 0 && (info.st_mode & S_IFREG) != 0; return *this;
} }
bool Path::is_directory() const bool Path::isFile() const
{ {
std::string p = this->string(); std::string p = this->string();
struct stat info; struct stat info;
return stat(p.c_str(), &info) == 0 && (info.st_mode & S_IFDIR) != 0; return stat(p.c_str(), &info) == 0 && (info.st_mode & S_IFREG) != 0;
} }
Path current_path() bool Path::isDirectory() const
{ {
char cwd_[FILENAME_MAX]; std::string p = this->string();
_ignore_(GET_CURRENT_DIR(cwd_, sizeof(cwd_))); struct stat info;
std::string cwd(cwd_); return stat(p.c_str(), &info) == 0 && (info.st_mode & S_IFDIR) != 0;
return { Gmsh4::trim(cwd) }; }
}
bool exists(Path const& p) Path currentPath()
{ {
return p.is_file() || p.is_directory(); char cwd_[FILENAME_MAX];
} _ignore_(GET_CURRENT_DIR(cwd_, sizeof(cwd_)));
std::string cwd(cwd_);
return { trim(cwd) };
}
bool create_directories(Path const& p) bool exists(Path const& p)
{ {
if (p.is_directory()) return p.isFile() || p.isDirectory();
return true; }
auto parent = p.parent_path();
if (!parent.empty() && !parent.is_directory())
create_directories(parent);
#ifdef _WIN32
int ret = _mkdir(p.string().c_str());
#else
mode_t mode = 0755;
int ret = mkdir(p.string().c_str(), mode);
#endif
if (ret == 0)
return true;
switch (errno)
{
case ENOENT:
std::cerr << "parent didn't exist. Should not happen, since parent directory created before!\n";
std::abort();
return false;
break;
case EEXIST:
return true;
break;
default:
return false;
}
}
Path relative(Path const& a, Path const& b)
{
// find common base path
auto a_it = a.begin();
auto b_it = b.begin();
for (; a_it != a.end() && b_it != b.end(); ++a_it, ++b_it) {
if (*a_it != *b_it)
break;
}
// combine remaining parts of a to result path bool createDirectories(Path const& p)
Path rel("."); {
for (; a_it != a.end(); ++a_it) if (p.isDirectory())
rel /= *a_it; return true;
auto parent = p.parentPath();
if (!parent.empty() && !parent.isDirectory())
createDirectories(parent);
return rel; #ifdef _WIN32
} int ret = _mkdir(p.string().c_str());
#else
mode_t mode = 0755;
int ret = mkdir(p.string().c_str(), mode);
#endif
if (ret == 0)
return true;
} // end namespace Gmsh4 switch (errno)
} // end namespace Dune {
case ENOENT:
std::cerr << "parent didn't exist. Should not happen, since parent directory created before!\n";
std::abort();
return false;
break;
case EEXIST:
return true;
break;
default:
return false;
}
}
Path relative(Path const& a, Path const& b)
{
// find common base path
auto a_it = a.begin();
auto b_it = b.begin();
for (; a_it != a.end() && b_it != b.end(); ++a_it, ++b_it) {
if (*a_it != *b_it)
break;
}
// combine remaining parts of a to result path
Path rel(".");
for (; a_it != a.end(); ++a_it)
rel /= *a_it;
return rel;
}
} } // end namespace Dune::Gmsh4
...@@ -9,7 +9,6 @@ namespace Dune ...@@ -9,7 +9,6 @@ namespace Dune
{ {
namespace Gmsh4 namespace Gmsh4
{ {
// A minimalistic filesystem class // A minimalistic filesystem class
class Path class Path
: public std::vector<std::string> : public std::vector<std::string>
...@@ -20,9 +19,9 @@ namespace Dune ...@@ -20,9 +19,9 @@ namespace Dune
public: public:
#ifdef _WIN32 #ifdef _WIN32
static constexpr char preferred_separator = '\\'; static constexpr char preferredSeparator = '\\';
#else #else
static constexpr char preferred_separator = '/'; static constexpr char preferredSeparator = '/';
#endif #endif
public: public:
...@@ -49,14 +48,14 @@ namespace Dune ...@@ -49,14 +48,14 @@ namespace Dune
{} {}
/// Removes filename path component /// Removes filename path component
Path& remove_filename() Path& removeFilename()
{ {
this->pop_back(); this->pop_back();
return *this; return *this;
} }
/// Returns the path of the parent path /// Returns the path of the parent path
Path parent_path() const Path parentPath() const
{ {
return empty() ? Path() : Path(begin(), --end()); return empty() ? Path() : Path(begin(), --end());
} }
...@@ -80,17 +79,17 @@ namespace Dune ...@@ -80,17 +79,17 @@ namespace Dune
/** In Linux, test whether the path starts with `/`, in Windows whether it starts /** In Linux, test whether the path starts with `/`, in Windows whether it starts
* with `[a-z]:\\`. * with `[a-z]:\\`.
**/ **/
static bool is_absolute(std::string p); static bool isAbsolute(std::string p);
bool is_absolute() const { return is_absolute(original); } bool isAbsolute() const { return isAbsolute(original); }
bool is_relative() const { return !is_absolute(); } bool isRelative() const { return !isAbsolute(); }
/// Check whether path is a regular file /// Check whether path is a regular file
bool is_file() const; bool isFile() const;
/// Check whether path is a regular file /// Check whether path is a regular file
bool is_directory() const; bool isDirectory() const;
/// Lexicographically compares two paths /// Lexicographically compares two paths
bool operator==(Path const& p) bool operator==(Path const& p)
...@@ -123,10 +122,10 @@ namespace Dune ...@@ -123,10 +122,10 @@ namespace Dune
bool exists(Path const&); bool exists(Path const&);
/// Create directory and non existing parent directories. /// Create directory and non existing parent directories.
bool create_directories(Path const&); bool createDirectories(Path const&);
/// Returns the current path /// Returns the current path
Path current_path(); Path currentPath();
/// Find the path of `a` relative to directory of `b` /// Find the path of `a` relative to directory of `b`
Path relative(Path const& a, Path const& b); Path relative(Path const& a, Path const& b);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment