Skip to content

Commit e1102b6

Browse files
committed
move file_utils to its own source file
1 parent 489eb59 commit e1102b6

File tree

3 files changed

+50
-37
lines changed

3 files changed

+50
-37
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ list(APPEND libopenmc_SOURCES
347347
src/endf.cpp
348348
src/error.cpp
349349
src/event.cpp
350+
src/file_utils.cpp
350351
src/finalize.cpp
351352
src/geometry.cpp
352353
src/geometry_aux.cpp

include/openmc/file_utils.h

+3-37
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,26 @@
11
#ifndef OPENMC_FILE_UTILS_H
22
#define OPENMC_FILE_UTILS_H
33

4-
#include <algorithm> // any_of
5-
#include <cctype> // for isalpha
6-
#include <fstream> // for ifstream
74
#include <string>
8-
#include <sys/stat.h>
95

106
namespace openmc {
117

128
// TODO: replace with std::filesystem when switch to C++17 is made
139
//! Determine if a path is a directory
1410
//! \param[in] path Path to check
1511
//! \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);
2413

2514
//! Determine if a file exists
2615
//! \param[in] filename Path to file
2716
//! \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);
3718

3819
// Gets the file extension of whatever string is passed in. This is defined as
3920
// a sequence of strictly alphanumeric characters which follow the last period,
4021
// i.e. at least one alphabet character is present, and zero or more numbers.
4122
// 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);
5824

5925
} // namespace openmc
6026

src/file_utils.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "openmc/file_utils.h"
2+
3+
#include <algorithm> // any_of
4+
#include <cctype> // for isalpha
5+
#include <fstream> // for ifstream
6+
#include <sys/stat.h>
7+
8+
namespace openmc {
9+
10+
bool dir_exists(const std::string& path)
11+
{
12+
struct stat s;
13+
if (stat(path.c_str(), &s) != 0)
14+
return false;
15+
16+
return s.st_mode & S_IFDIR;
17+
}
18+
19+
bool file_exists(const std::string& filename)
20+
{
21+
// rule out file being a path to a directory
22+
if (dir_exists(filename))
23+
return false;
24+
25+
std::ifstream s {filename};
26+
return s.good();
27+
}
28+
29+
std::string get_file_extension(const std::string& filename)
30+
{
31+
// check that at least one letter is present
32+
const auto last_period_pos = filename.find_last_of('.');
33+
34+
// no file extension
35+
if (last_period_pos == std::string::npos)
36+
return "";
37+
38+
const std::string ending = filename.substr(last_period_pos + 1);
39+
40+
// check that at least one character is present.
41+
const bool has_alpha = std::any_of(ending.begin(), ending.end(),
42+
[](char x) { return static_cast<bool>(std::isalpha(x)); });
43+
return has_alpha ? ending : "";
44+
}
45+
46+
} // namespace openmc

0 commit comments

Comments
 (0)