diff --git a/CMakeLists.txt b/CMakeLists.txt index 70152bb6b..6530016fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -347,6 +347,7 @@ list(APPEND libopenmc_SOURCES src/endf.cpp src/error.cpp src/event.cpp + src/file_utils.cpp src/finalize.cpp src/geometry.cpp src/geometry_aux.cpp diff --git a/include/openmc/file_utils.h b/include/openmc/file_utils.h index 786d35955..11b20b4aa 100644 --- a/include/openmc/file_utils.h +++ b/include/openmc/file_utils.h @@ -1,11 +1,7 @@ #ifndef OPENMC_FILE_UTILS_H #define OPENMC_FILE_UTILS_H -#include // any_of -#include // for isalpha -#include // for ifstream #include -#include namespace openmc { @@ -13,48 +9,18 @@ namespace openmc { //! Determine if a path is a directory //! \param[in] path Path to check //! \return Whether the path is a directory -inline bool dir_exists(const std::string& path) -{ - struct stat s; - if (stat(path.c_str(), &s) != 0) - return false; - - return s.st_mode & S_IFDIR; -} +bool dir_exists(const std::string& path); //! 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 path to a directory - if (dir_exists(filename)) - return false; - - std::ifstream s {filename}; - return s.good(); -} +bool file_exists(const std::string& filename); // Gets the file extension of whatever string is passed in. This is defined as // a sequence of strictly alphanumeric characters which follow the last period, // i.e. at least one alphabet character is present, and zero or more numbers. // If such a sequence of characters is not found, an empty string is returned. -inline std::string get_file_extension(const std::string& filename) -{ - // check that at least one letter is present - const auto last_period_pos = filename.find_last_of('.'); - - // no file extension - if (last_period_pos == std::string::npos) - return ""; - - const std::string ending = filename.substr(last_period_pos + 1); - - // check that at least one character is present. - const bool has_alpha = std::any_of(ending.begin(), ending.end(), - [](char x) { return static_cast(std::isalpha(x)); }); - return has_alpha ? ending : ""; -} +std::string get_file_extension(const std::string& filename); } // namespace openmc diff --git a/src/file_utils.cpp b/src/file_utils.cpp new file mode 100644 index 000000000..15b30e3b6 --- /dev/null +++ b/src/file_utils.cpp @@ -0,0 +1,46 @@ +#include "openmc/file_utils.h" + +#include // any_of +#include // for isalpha +#include // for ifstream +#include + +namespace openmc { + +bool dir_exists(const std::string& path) +{ + struct stat s; + if (stat(path.c_str(), &s) != 0) + return false; + + return s.st_mode & S_IFDIR; +} + +bool file_exists(const std::string& filename) +{ + // rule out file being a path to a directory + if (dir_exists(filename)) + return false; + + std::ifstream s {filename}; + return s.good(); +} + +std::string get_file_extension(const std::string& filename) +{ + // check that at least one letter is present + const auto last_period_pos = filename.find_last_of('.'); + + // no file extension + if (last_period_pos == std::string::npos) + return ""; + + const std::string ending = filename.substr(last_period_pos + 1); + + // check that at least one character is present. + const bool has_alpha = std::any_of(ending.begin(), ending.end(), + [](char x) { return static_cast(std::isalpha(x)); }); + return has_alpha ? ending : ""; +} + +} // namespace openmc