EventAnalysis  1.0.0
FileUtils.h
Go to the documentation of this file.
1 /*
2  * FileUtils.h
3  *
4  * Created on: 16 Nov 2017
5  * Author: Nicola Mori
6  */
7 
10 #ifndef FILEUTILS_H_
11 #define FILEUTILS_H_
12 
13 #include <glob.h> // glob(), globfree()
14 #include <sstream>
15 #include <stdexcept>
16 #include <string.h> // memset()
17 #include <string>
18 #include <sys/stat.h>
19 #include <vector>
20 
21 namespace EA {
22 
24 class FileUtils {
25 public:
39  template <class Container>
40  static std::string FindFile(const std::string &fileName, const Container &folders, bool cwd = false);
41 
46  static std::string GetCWD();
47 
64  template <template <class, class> class Container = std::vector>
65  static Container<std::string, std::allocator<std::string>> Glob(const std::string &pattern);
66 };
67 
68 template <class Container>
69 std::string FileUtils::FindFile(const std::string &fileName, const Container &folders, bool cwd) {
70 
71  struct stat buffer;
72  bool found = false;
73  if (fileName[0] == '/') {
74  // libName is an absolute path
75  if (stat(fileName.c_str(), &buffer) == 0) {
76  return fileName;
77  }
78  } else {
79  for (auto &folder : folders) {
80  std::string fullPath = folder + "/" + fileName;
81  if (stat(fullPath.c_str(), &buffer) == 0) {
82  return fullPath;
83  }
84  }
85  if (cwd) {
86  std::string fullPath = GetCWD() + "/" + fileName;
87  if (stat(fullPath.c_str(), &buffer) == 0) {
88  return fullPath;
89  }
90  }
91  }
92 
93  return std::string("");
94 }
95 
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");
99 
100  // glob struct resides on the stack
101  glob_t globResult;
102  memset(&globResult, 0, sizeof(globResult));
103 
104  // do the glob operation
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());
111  }
112 
113  // collect all the fileNames into a std::list<std::string>
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]));
117  }
118 
119  // cleanup
120  globfree(&globResult);
121 
122  // done
123  return fileNames;
124 }
125 
126 } // namespace EA
127 
128 #endif /* FILEUTILS_H_ */
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