mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-21 06:25:30 -04:00
move file_utils to its own source file
This commit is contained in:
parent
489eb59c99
commit
e1102b6408
3 changed files with 50 additions and 37 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,11 +1,7 @@
|
|||
#ifndef OPENMC_FILE_UTILS_H
|
||||
#define OPENMC_FILE_UTILS_H
|
||||
|
||||
#include <algorithm> // any_of
|
||||
#include <cctype> // for isalpha
|
||||
#include <fstream> // for ifstream
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
|
||||
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<bool>(std::isalpha(x)); });
|
||||
return has_alpha ? ending : "";
|
||||
}
|
||||
std::string get_file_extension(const std::string& filename);
|
||||
|
||||
} // namespace openmc
|
||||
|
||||
|
|
|
|||
46
src/file_utils.cpp
Normal file
46
src/file_utils.cpp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#include "openmc/file_utils.h"
|
||||
|
||||
#include <algorithm> // any_of
|
||||
#include <cctype> // for isalpha
|
||||
#include <fstream> // for ifstream
|
||||
#include <sys/stat.h>
|
||||
|
||||
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<bool>(std::isalpha(x)); });
|
||||
return has_alpha ? ending : "";
|
||||
}
|
||||
|
||||
} // namespace openmc
|
||||
Loading…
Add table
Add a link
Reference in a new issue