Adding function to check if a path is a directory

This commit is contained in:
Patrick Shriwise 2022-12-06 14:39:44 -06:00
parent 574845b18b
commit 270331aff9

View file

@ -3,15 +3,31 @@
#include <fstream> // for ifstream
#include <string>
#include <sys/stat.h>
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();
}