39 template <
class Container>
40 static std::string
FindFile(
const std::string &fileName,
const Container &folders,
bool cwd =
false);
46 static std::string
GetCWD();
64 template <
template <
class,
class>
class Container = std::vector>
65 static Container<std::string, std::allocator<std::string>>
Glob(
const std::string &pattern);
68 template <
class Container>
73 if (fileName[0] ==
'/') {
75 if (stat(fileName.c_str(), &buffer) == 0) {
79 for (
auto &folder : folders) {
80 std::string fullPath = folder +
"/" + fileName;
81 if (stat(fullPath.c_str(), &buffer) == 0) {
86 std::string fullPath =
GetCWD() +
"/" + fileName;
87 if (stat(fullPath.c_str(), &buffer) == 0) {
93 return std::string(
"");
96 template <
template <
class,
class>
class Container>
97 Container<std::string, std::allocator<std::string>>
FileUtils::Glob(
const std::string &pattern) {
98 const std::string routineName(
"FileUtils::Glob");
102 memset(&globResult, 0,
sizeof(globResult));
105 int returnValue = glob(pattern.c_str(), GLOB_TILDE, NULL, &globResult);
106 if (returnValue != 0 && returnValue != GLOB_NOMATCH) {
107 globfree(&globResult);
108 std::stringstream ss;
109 ss << routineName <<
" failed with return value " << returnValue << std::endl;
110 throw std::runtime_error(ss.str());
114 Container<std::string, std::allocator<std::string>> fileNames;
115 for (
size_t i = 0; i < globResult.gl_pathc; ++i) {
116 fileNames.push_back(std::string(globResult.gl_pathv[i]));
120 globfree(&globResult);
static std::string FindFile(const std::string &fileName, const Container &folders, bool cwd=false)
Search for the given file inside the given folders.
Definition: FileUtils.h:69
A static class with file utility functions.
Definition: FileUtils.h:24
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
static Container< std::string, std::allocator< std::string > > Glob(const std::string &pattern)
Find all files matching a pattern.
Definition: FileUtils.h:97
static std::string GetCWD()
Get the current working directory.
Definition: FileUtils.cpp:14