Jpp  master_rocky-43-ge265d140c
the software that should make you happy
JGlob.hh
Go to the documentation of this file.
1 #ifndef __JSYSTEM_JGLOB__
2 #define __JSYSTEM_JGLOB__
3 
4 #include <glob.h>
5 #include <string.h>
6 #include <string>
7 #include <vector>
8 
9 
10 /**
11  * \file
12  * File list.
13  * \author mdejong
14  */
15 namespace JSYSTEM {}
16 namespace JPP { using namespace JSYSTEM; }
17 
18 namespace JSYSTEM {
19 
20  /**
21  * Auxiliary class to list files.
22  */
23  struct JGlob :
24  public std::vector<std::string>
25  {
26  /**
27  * Default constructor.
28  */
30  {}
31 
32 
33  /**
34  * Constructor.
35  *
36  * \param pattern pattern
37  */
38  JGlob(const std::string& pattern)
39  {
40  (*this)(pattern);
41  }
42 
43 
44  /**
45  * Constructor.
46  *
47  * \param buffer list of patterns
48  */
50  {
51  (*this)(buffer);
52  }
53 
54 
55  /**
56  * Get list of files.
57  *
58  * \param pattern pattern
59  * \return list of files
60  */
61  const JGlob& operator()(const std::string& pattern)
62  {
63  this->clear();
64 
65  evaluate(pattern);
66 
67  return *this;
68  }
69 
70 
71  /**
72  * Get list of files.
73  *
74  *
75  * \param buffer list of patterns
76  * \return list of files
77  */
79  {
80  this->clear();
81 
82  for (const std::string& pattern : buffer) {
83  evaluate(pattern);
84  }
85 
86  return *this;
87  }
88 
89 
90  private:
91  /**
92  * Process pattern.
93  *
94  * \param pattern pattern
95  */
96  void evaluate(const std::string& pattern)
97  {
98  memset(&buffer, 0, sizeof(buffer));
99 
100  const int value = glob(pattern.c_str(), GLOB_TILDE | GLOB_BRACE, NULL, &buffer);
101 
102  if (value == 0) {
103 
104  for(size_t i = 0; i < buffer.gl_pathc; ++i) {
105  this->push_back(buffer.gl_pathv[i]);
106  }
107 
108  } else {
109 
110  this->push_back(pattern);
111  }
112 
113  globfree(&buffer);
114  }
115 
116  glob_t buffer;
117  };
118 
119 
120  /**
121  * Function object to get list of files for given pattern.
122  */
124 }
125 
126 #endif
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for operating system calls.
Definition: JDateAndTime.hh:21
static JGlob getFilenames
Function object to get list of files for given pattern.
Definition: JGlob.hh:123
Auxiliary class to list files.
Definition: JGlob.hh:25
const JGlob & operator()(const std::string &pattern)
Get list of files.
Definition: JGlob.hh:61
glob_t buffer
Definition: JGlob.hh:116
JGlob(const std::string &pattern)
Constructor.
Definition: JGlob.hh:38
void evaluate(const std::string &pattern)
Process pattern.
Definition: JGlob.hh:96
const JGlob & operator()(const std::vector< std::string > &buffer)
Get list of files.
Definition: JGlob.hh:78
JGlob()
Default constructor.
Definition: JGlob.hh:29
JGlob(const std::vector< std::string > &buffer)
Constructor.
Definition: JGlob.hh:49