diff --git a/include/openmc/file_utils.h b/include/openmc/file_utils.h index f9c23468d..ae3a26e94 100644 --- a/include/openmc/file_utils.h +++ b/include/openmc/file_utils.h @@ -3,15 +3,31 @@ #include // for ifstream #include +#include namespace openmc { +// TODO: replace with std::filesysem when switch to C++17 is made +//! Determine if a path is a directory +//! \param[in] path Path to check +//! \return Whether the path is a directory +inline bool is_dir(const std::string& path) { + struct stat s; + if (stat(path.c_str(), &s) != 0) return false; + + return s.st_mode & S_IFDIR; +} + //! Determine if a file exists //! \param[in] filename Path to file //! \return Whether file exists inline bool file_exists(const std::string& filename) { + // rule out file being a directory path + if (is_dir(filename)) return false; + std::ifstream s {filename}; + s.seekg(0, std::ios::beg); return s.good(); }