|
1 | 1 | #ifndef OPENMC_FILE_UTILS_H
|
2 | 2 | #define OPENMC_FILE_UTILS_H
|
3 | 3 |
|
4 |
| -#include <algorithm> // any_of |
5 |
| -#include <cctype> // for isalpha |
6 |
| -#include <fstream> // for ifstream |
7 | 4 | #include <string>
|
8 |
| -#include <sys/stat.h> |
9 | 5 |
|
10 | 6 | namespace openmc {
|
11 | 7 |
|
12 | 8 | // TODO: replace with std::filesystem when switch to C++17 is made
|
13 | 9 | //! Determine if a path is a directory
|
14 | 10 | //! \param[in] path Path to check
|
15 | 11 | //! \return Whether the path is a directory
|
16 |
| -inline bool dir_exists(const std::string& path) |
17 |
| -{ |
18 |
| - struct stat s; |
19 |
| - if (stat(path.c_str(), &s) != 0) |
20 |
| - return false; |
21 |
| - |
22 |
| - return s.st_mode & S_IFDIR; |
23 |
| -} |
| 12 | +bool dir_exists(const std::string& path); |
24 | 13 |
|
25 | 14 | //! Determine if a file exists
|
26 | 15 | //! \param[in] filename Path to file
|
27 | 16 | //! \return Whether file exists
|
28 |
| -inline bool file_exists(const std::string& filename) |
29 |
| -{ |
30 |
| - // rule out file being a path to a directory |
31 |
| - if (dir_exists(filename)) |
32 |
| - return false; |
33 |
| - |
34 |
| - std::ifstream s {filename}; |
35 |
| - return s.good(); |
36 |
| -} |
| 17 | +bool file_exists(const std::string& filename); |
37 | 18 |
|
38 | 19 | // Gets the file extension of whatever string is passed in. This is defined as
|
39 | 20 | // a sequence of strictly alphanumeric characters which follow the last period,
|
40 | 21 | // i.e. at least one alphabet character is present, and zero or more numbers.
|
41 | 22 | // If such a sequence of characters is not found, an empty string is returned.
|
42 |
| -inline std::string get_file_extension(const std::string& filename) |
43 |
| -{ |
44 |
| - // check that at least one letter is present |
45 |
| - const auto last_period_pos = filename.find_last_of('.'); |
46 |
| - |
47 |
| - // no file extension |
48 |
| - if (last_period_pos == std::string::npos) |
49 |
| - return ""; |
50 |
| - |
51 |
| - const std::string ending = filename.substr(last_period_pos + 1); |
52 |
| - |
53 |
| - // check that at least one character is present. |
54 |
| - const bool has_alpha = std::any_of(ending.begin(), ending.end(), |
55 |
| - [](char x) { return static_cast<bool>(std::isalpha(x)); }); |
56 |
| - return has_alpha ? ending : ""; |
57 |
| -} |
| 23 | +std::string get_file_extension(const std::string& filename); |
58 | 24 |
|
59 | 25 | } // namespace openmc
|
60 | 26 |
|
|
0 commit comments