diff --git a/dune/gmsh4/reader.hh b/dune/gmsh4/reader.hh index c87b16b91819566dff0b87e18f0c2a58d197b9d4..46e1817fa0bcc02bd70ab8ad021aacc9b0c9fb3f 100644 --- a/dune/gmsh4/reader.hh +++ b/dune/gmsh4/reader.hh @@ -134,6 +134,7 @@ namespace Dune void readString(std::istream& stream, std::string& name) { + DUNE_THROW(Dune::NotImplemented, "Method readString() not yet implemented"); name = "Hallo"; } diff --git a/dune/gmsh4/utility/filesystem.cc b/dune/gmsh4/utility/filesystem.cc index fd738cfb7169c51792766c51e041ab95c64bd972..7ba4f015d3c507d06fa34c51bd7eb049108185b3 100644 --- a/dune/gmsh4/utility/filesystem.cc +++ b/dune/gmsh4/utility/filesystem.cc @@ -21,174 +21,171 @@ template 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 - { - if (empty()) - return "."; + auto it = begin(); + auto result = *it; + for (++it; it != end(); ++it) + 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) - { - std::string separators = "/\\"; - 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 + trim(p); + Dune::Vtk::split(p.begin(), p.end(), separators.begin(), separators.end(), + [this,&relative](auto first, auto end) { - auto f = filename().string(); - auto pos = f.find_last_of('.'); - if (f == "." || f == ".." || pos == std::string::npos) - return {f}; - else - return {f.substr(0,pos)}; - } + 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::extension() const - { - auto f = filename().string(); - auto pos = f.find_last_of('.'); - if (f == "." || f == ".." || pos == std::string::npos) - return {}; - else - return {f.substr(pos)}; - } +Path Path::stem() const +{ + auto f = filename().string(); + auto pos = f.find_last_of('.'); + if (f == "." || f == ".." || pos == std::string::npos) + return {f}; + else + return {f.substr(0,pos)}; +} - bool Path::is_absolute(std::string p) - { - if (p[0] == '/') - return true; +Path Path::extension() const +{ + auto f = filename().string(); + 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) - { - insert(end(), p.begin(), p.end()); - original += preferred_separator + p.original; - return *this; - } + return false; +} - bool Path::is_file() const - { - std::string p = this->string(); - struct stat info; - return stat(p.c_str(), &info) == 0 && (info.st_mode & S_IFREG) != 0; - } +Path& Path::operator/=(Path const& p) +{ + insert(end(), p.begin(), p.end()); + original += preferredSeparator + p.original; + return *this; +} - bool Path::is_directory() const - { - std::string p = this->string(); - struct stat info; - return stat(p.c_str(), &info) == 0 && (info.st_mode & S_IFDIR) != 0; - } +bool Path::isFile() const +{ + std::string p = this->string(); + struct stat info; + return stat(p.c_str(), &info) == 0 && (info.st_mode & S_IFREG) != 0; +} - Path current_path() - { - char cwd_[FILENAME_MAX]; - _ignore_(GET_CURRENT_DIR(cwd_, sizeof(cwd_))); - std::string cwd(cwd_); - return { Gmsh4::trim(cwd) }; - } +bool Path::isDirectory() const +{ + std::string p = this->string(); + struct stat info; + return stat(p.c_str(), &info) == 0 && (info.st_mode & S_IFDIR) != 0; +} - bool exists(Path const& p) - { - return p.is_file() || p.is_directory(); - } +Path currentPath() +{ + char cwd_[FILENAME_MAX]; + _ignore_(GET_CURRENT_DIR(cwd_, sizeof(cwd_))); + std::string cwd(cwd_); + return { trim(cwd) }; +} - bool create_directories(Path const& p) - { - if (p.is_directory()) - 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; - } - } +bool exists(Path const& p) +{ + return p.isFile() || p.isDirectory(); +} - 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; +bool createDirectories(Path const& p) +{ + if (p.isDirectory()) + 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 -} // end namespace Dune + 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 + Path rel("."); + for (; a_it != a.end(); ++a_it) + rel /= *a_it; + + return rel; +} + +} } // end namespace Dune::Gmsh4 diff --git a/dune/gmsh4/utility/filesystem.hh b/dune/gmsh4/utility/filesystem.hh index d69a65b51782ad07f78023e209d4bd54563c93f1..ef15a261787d42cd289fbd3ed6530d6a444b8b09 100644 --- a/dune/gmsh4/utility/filesystem.hh +++ b/dune/gmsh4/utility/filesystem.hh @@ -9,7 +9,6 @@ namespace Dune { namespace Gmsh4 { - // A minimalistic filesystem class class Path : public std::vector @@ -20,9 +19,9 @@ namespace Dune public: #ifdef _WIN32 - static constexpr char preferred_separator = '\\'; + static constexpr char preferredSeparator = '\\'; #else - static constexpr char preferred_separator = '/'; + static constexpr char preferredSeparator = '/'; #endif public: @@ -49,14 +48,14 @@ namespace Dune {} /// Removes filename path component - Path& remove_filename() + Path& removeFilename() { this->pop_back(); return *this; } /// Returns the path of the parent path - Path parent_path() const + Path parentPath() const { return empty() ? Path() : Path(begin(), --end()); } @@ -80,17 +79,17 @@ namespace Dune /** In Linux, test whether the path starts with `/`, in Windows whether it starts * 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 - bool is_file() const; + bool isFile() const; /// Check whether path is a regular file - bool is_directory() const; + bool isDirectory() const; /// Lexicographically compares two paths bool operator==(Path const& p) @@ -123,10 +122,10 @@ namespace Dune bool exists(Path const&); /// Create directory and non existing parent directories. - bool create_directories(Path const&); + bool createDirectories(Path const&); /// Returns the current path - Path current_path(); + Path currentPath(); /// Find the path of `a` relative to directory of `b` Path relative(Path const& a, Path const& b);